A platform for community discussion. Free, open, simple.
FIX: support CJK and spaceless scripts in watched word boundaries (#37844)
Régis Hanold7a53ada1633
diff --git a/app/services/word_watcher.rb b/app/services/word_watcher.rbindex 119c8e71da2..c92be317236 100644--- a/app/services/word_watcher.rb+++ b/app/services/word_watcher.rb@@ -61,7 +61,6 @@ class WordWatcher# This regexp is run in miniracer, and the client JS app# Make sure it is compatible with major browsers when changing- # hint: non-chrome browsers do not support 'lookbehind'def self.compiled_regexps_for_action(action, engine: :ruby, raise_errors: false)words = cached_words_for_action(action)return [] if words.blank?@@ -283,13 +282,28 @@ class WordWatcherprivate_class_method :censor_text_with_regexp- # Returns a regexp that transforms a regular expression into a regular- # expression that matches a whole word.+ SPACELESS_SCRIPTS = {+ "Han" => "\\u4E00-\\u9FFF\\u3400-\\u4DBF",+ "Hiragana" => "\\u3040-\\u309F",+ "Katakana" => "\\u30A0-\\u30FF",+ "Hangul" => "\\uAC00-\\uD7AF",+ "Thai" => "\\u0E00-\\u0E7F",+ "Lao" => "\\u0E80-\\u0EFF",+ "Myanmar" => "\\u1000-\\u109F",+ "Khmer" => "\\u1780-\\u17FF",+ "Tibetan" => "\\u0F00-\\u0FFF",+ }.values.join+def self.match_word_regexp(regexp, engine: :ruby)+ s = SPACELESS_SCRIPTSif engine == :js- "(?:\\P{L}|^)(#{regexp})(?=\\P{L}|$)"+ leading = "(?:[\\P{L}#{s}]|^|(?=[#{s}]))"+ trailing = "(?:(?=[\\P{L}#{s}]|$)|(?<=[#{s}]))"+ "#{leading}(#{regexp})#{trailing}"elsif engine == :ruby- "(?:[^[:word:]]|^)(#{regexp})(?=[^[:word:]]|$)"+ leading = "(?:(?<![[:word:]&&[^#{s}]])|(?=[#{s}]))"+ trailing = "(?:(?![[:word:]&&[^#{s}]])|(?<=[#{s}]))"+ "#{leading}(#{regexp})#{trailing}"elseraise "unknown regexp engine: #{engine}"enddiff --git a/frontend/discourse/admin/components/modal/watched-word-testing.gjs b/frontend/discourse/admin/components/modal/watched-word-testing.gjsindex 2d7f3190f15..13777adee47 100644--- a/frontend/discourse/admin/components/modal/watched-word-testing.gjs+++ b/frontend/discourse/admin/components/modal/watched-word-testing.gjs@@ -106,7 +106,10 @@ export default class WatchedWordTesting extends Component {options.case_sensitive ? "gu" : "gui");- matches.push(...(this.value.match(wordRegexp) || []));+ let match;+ while ((match = wordRegexp.exec(this.value)) !== null) {+ matches.push(match[1] || match[0]);+ }} catch {hasCompiledExpressionError = true;}diff --git a/spec/services/word_watcher_spec.rb b/spec/services/word_watcher_spec.rbindex 0867d2d2b4f..7ee982e8be2 100644--- a/spec/services/word_watcher_spec.rb+++ b/spec/services/word_watcher_spec.rb@@ -80,10 +80,14 @@ RSpec.describe WordWatcher doSiteSetting.watched_words_regular_expressions = falseregexps = described_class.compiled_regexps_for_action(:block)+ s = WordWatcher::SPACELESS_SCRIPTS+ leading = "(?:(?<![[:word:]&&[^#{s}]])|(?=[#{s}]))"+ trailing = "(?:(?![[:word:]&&[^#{s}]])|(?<=[#{s}]))"+expect(regexps).to be_an(Array)expect(regexps.map(&:inspect)).to contain_exactly(- "/(?:[^[:word:]]|^)(#{word1}|#{word2})(?=[^[:word:]]|$)/i",- "/(?:[^[:word:]]|^)(#{word3}|#{word4})(?=[^[:word:]]|$)/",+ "/#{leading}(#{word1}|#{word2})#{trailing}/i",+ "/#{leading}(#{word3}|#{word4})#{trailing}/",)end@@ -126,7 +130,11 @@ RSpec.describe WordWatcher doit "works correctly when regular expressions are disabled" doregexps = described_class.compiled_regexps_for_action(:block)expect(regexps).to be_an(Array)- expect(regexps).to contain_exactly(/(?:[^[:word:]]|^)(\S*abc)(?=[^[:word:]]|$)/i)+ expect(regexps.size).to eq(1)+ expect(regexps.first).to match("xyzabc")+ expect(regexps.first).to match(" abc")+ expect(regexps.first).to match("testabc")+ expect(regexps.first).not_to match("abcdef")endit "skips invalid watched words when regular expression are enabled" do@@ -257,6 +265,56 @@ RSpec.describe WordWatcher do).to eq("love")end+ it "handles CJK characters as word boundaries" do+ Fabricate(:watched_word, word: "测试", action: WatchedWord.actions[:require_approval])++ expect(described_class.new("测试").word_matches_for_action?(:require_approval)[1]).to eq("测试")+ expect(+ described_class.new("这是一个测试文本").word_matches_for_action?(:require_approval)[1],+ ).to eq("测试")+ expect(+ described_class.new("hello 测试 world").word_matches_for_action?(:require_approval)[1],+ ).to eq("测试")+ expect(described_class.new("API测试结果").word_matches_for_action?(:require_approval)[1]).to eq(+ "测试",+ )+ end++ it "handles Latin watched words adjacent to CJK text" do+ Fabricate(:watched_word, word: "Test", action: WatchedWord.actions[:require_approval])++ expect(+ described_class.new("我的Test很好").word_matches_for_action?(:require_approval)[1],+ ).to eq("Test")+ expect(+ described_class.new("Testing").word_matches_for_action?(:require_approval),+ ).to be_falsey+ end++ it "handles CJK boundaries with the JS engine" do+ Fabricate(:watched_word, word: "测试", action: WatchedWord.actions[:require_approval])++ regexps = described_class.compiled_regexps_for_action(:require_approval, engine: :js)+ expect(regexps.size).to eq(1)++ re = regexps.first+ expect(re).to match("测试")+ expect(re).to match("这是一个测试文本")+ expect(re).to match("hello 测试 world")+ expect(re).to match("API测试结果")+ end++ it "handles Latin words adjacent to CJK text with the JS engine" do+ Fabricate(:watched_word, word: "Test", action: WatchedWord.actions[:require_approval])++ regexps = described_class.compiled_regexps_for_action(:require_approval, engine: :js)+ expect(regexps.size).to eq(1)++ re = regexps.first+ expect(re).to match("我的Test很好")+ expect(re).not_to match("Testing")+ end+context "when there are multiple matches" docontext "with non regexp words" doit "lists all matching words" do