about summary refs log tree commit diff
diff options
context:
space:
mode:
authorunarist <m.unarist@gmail.com>2017-06-21 01:45:09 +0900
committerEugen Rochko <eugen@zeonfederated.com>2017-06-20 18:45:09 +0200
commitb16b69350eb4ded2e1011931433b51dac5e34b53 (patch)
tree7fdb2a90283e33be19fb000c787da9567c403452
parentda6fa029f66d50f38f2b6c85687994793f7766aa (diff)
Fix RTL detection on Ruby side (#3867)
This fixes below bugs:

* pipe characters being counted as RTL character
* only first word being checked
-rw-r--r--app/helpers/stream_entries_helper.rb10
-rw-r--r--spec/helpers/stream_entries_helper_spec.rb2
2 files changed, 6 insertions, 6 deletions
diff --git a/app/helpers/stream_entries_helper.rb b/app/helpers/stream_entries_helper.rb
index a17b02128..4ef7cffb0 100644
--- a/app/helpers/stream_entries_helper.rb
+++ b/app/helpers/stream_entries_helper.rb
@@ -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?
diff --git a/spec/helpers/stream_entries_helper_spec.rb b/spec/helpers/stream_entries_helper_spec.rb
index 1ef49a3ab..2c0d7b239 100644
--- a/spec/helpers/stream_entries_helper_spec.rb
+++ b/spec/helpers/stream_entries_helper_spec.rb
@@ -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