]> cat aescling's git repositories - mastodon.git/commitdiff
Fix RTL detection on Ruby side (#3867)
authorunarist <m.unarist@gmail.com>
Tue, 20 Jun 2017 16:45:09 +0000 (01:45 +0900)
committerEugen Rochko <eugen@zeonfederated.com>
Tue, 20 Jun 2017 16:45:09 +0000 (18:45 +0200)
This fixes below bugs:

* pipe characters being counted as RTL character
* only first word being checked

app/helpers/stream_entries_helper.rb
spec/helpers/stream_entries_helper_spec.rb

index a17b021281f1c071a096511e7c2a223ded2b322e..4ef7cffb07f40e897fdac6099b84c97425066710 100644 (file)
@@ -53,11 +53,11 @@ module StreamEntriesHelper
 
   def rtl?(text)
     text = simplified_text(text)
-    rtl_characters = /[\p{Hebrew}|\p{Arabic}|\p{Syriac}|\p{Thaana}|\p{Nko}]+/m.match(text)
+    rtl_words = text.scan(/[\p{Hebrew}\p{Arabic}\p{Syriac}\p{Thaana}\p{Nko}]+/m)
 
-    if rtl_characters.present?
+    if rtl_words.present?
       total_size = text.size.to_f
-      rtl_size(rtl_characters.to_a) / total_size > 0.3
+      rtl_size(rtl_words) / total_size > 0.3
     else
       false
     end
@@ -77,8 +77,8 @@ module StreamEntriesHelper
     end
   end
 
-  def rtl_size(characters)
-    characters.reduce(0) { |acc, elem| acc + elem.size }.to_f
+  def rtl_size(words)
+    words.reduce(0) { |acc, elem| acc + elem.size }.to_f
   end
 
   def embedded_view?
index 1ef49a3ab5b330b3512c5dbf8638ee53ad43e249..2c0d7b2394c68f8f3a51a546022a83235fdc8702 100644 (file)
@@ -217,7 +217,7 @@ RSpec.describe StreamEntriesHelper, type: :helper do
     end
 
     it 'is true if right to left characters are greater than 1/3 of total text' do
-      expect(helper).to be_rtl 'aaݟ'
+      expect(helper).to be_rtl 'aaݟaaݟ'
     end
   end
 end