A platform for community discussion. Free, open, simple.
FIX: Allow all caps within CJK text (#28018)
Régis Hanol23aa88d20382
diff --git a/lib/text_sentinel.rb b/lib/text_sentinel.rbindex 1a400712f79..7e8faff8820 100644--- a/lib/text_sentinel.rb+++ b/lib/text_sentinel.rb@@ -1,12 +1,5 @@# frozen_string_literal: true-# We use ActiveSupport mb_chars from here to properly support non ascii downcase-# TODO remove when ruby 2.4 lands-require "active_support/core_ext/string/multibyte"--#-# Given a string, tell us whether or not is acceptable.-#class TextSentinelattr_accessor :text@@ -43,11 +36,9 @@ class TextSentinelTextSentinel.new(text, min_entropy: entropy, max_word_length: SiteSetting.title_max_word_length)end- # Entropy is a number of how many unique characters the string needs.- # Non-ASCII characters are weighted heavier since they contain more "information"+ # Number of unique bytesdef entropy- chars = @text.to_s.strip.split("")- @entropy ||= chars.pack("M*" * chars.size).gsub("\n", "").split("=").uniq.size+ @entropy ||= @text.strip.bytes.uniq.sizeenddef valid?@@ -55,39 +46,32 @@ class TextSentinelseems_quiet?end+ # Ensure minumum entropydef seems_meaningful?- # Minimum entropy if entropy check required- @opts[:min_entropy].blank? || (entropy >= @opts[:min_entropy])+ @opts[:min_entropy].nil? || entropy >= @opts[:min_entropy]end+ # At least one non-symbol characterdef seems_pronounceable?- # At least some non-symbol characters- # (We don't have a comprehensive list of symbols, but this will eliminate some noise)- @text.gsub(symbols_regex, "").size > 0+ @text.match?(/\p{Alnum}/)end+ # Ensure maximum word lengthdef seems_unpretentious?- return true if skipped_locale.include?(SiteSetting.default_locale)- # Don't allow super long words if there is a word length maximum-- @opts[:max_word_length].blank? ||- (@text.split(%r{\s|/|-|\.|:}).map(&:size).max || 0) <= @opts[:max_word_length]+ skipped_locales.include?(SiteSetting.default_locale) || @opts[:max_word_length].nil? ||+ @text.scan(/\p{Alnum}+/).map(&:size).max.to_i <= @opts[:max_word_length]end+ # Ensure at least one lowercase letterdef seems_quiet?- return true if skipped_locale.include?(SiteSetting.default_locale)- # We don't allow all upper case content- SiteSetting.allow_uppercase_posts || @text == @text.mb_chars.downcase.to_s ||- @text != @text.mb_chars.upcase.to_s+ SiteSetting.allow_uppercase_posts || @text.match?(/\p{Lowercase_Letter}|\p{Other_Letter}/) ||+ [email protected]?(/\p{Letter}/)endprivate- def symbols_regex- /[\ -\/\[-\`\:-\@\{-\~]/m- end-- def skipped_locale- %w[zh_CN zh_TW ko ja].freeze+ # Hard to tell "word length" for CJK languages+ def skipped_locales+ @skipped_locales ||= %w[ja ko zh_CN zh_TW].freezeendenddiff --git a/spec/lib/text_sentinel_spec.rb b/spec/lib/text_sentinel_spec.rbindex 3b555391d37..b3864068c56 100644--- a/spec/lib/text_sentinel_spec.rb+++ b/spec/lib/text_sentinel_spec.rb@@ -30,15 +30,15 @@ RSpec.describe TextSentinel doendit "Works on foreign characters" do- expect(TextSentinel.new("去年十社會警告").entropy).to eq(19)+ expect(TextSentinel.new("去年十社會警告").entropy).to eq(18)endit "generates enough entropy for short foreign strings" do- expect(TextSentinel.new("又一个测").entropy).to eq(11)+ expect(TextSentinel.new("又一个测").entropy).to eq(10)endit "handles repeated foreign characters" do- expect(TextSentinel.new("又一个测试话题" * 3).entropy).to eq(18)+ expect(TextSentinel.new("又一个测试话题" * 3).entropy).to eq(17)endend@@ -108,8 +108,7 @@ RSpec.describe TextSentinel doexpect(TextSentinel.new("去年十二月,北韓不顧國際社會警告")).to be_validend- it "skips uppercase text for CJK locale" do- SiteSetting.default_locale = "zh_CN"+ it "allows all caps for CJK characters" doexpect(TextSentinel.new("去年SHIER月,北韓不顧國際社會警告")).to be_validend