From a014e9e1807c58b9c94e1709eaed39132d5cf990 Mon Sep 17 00:00:00 2001
From: Takashi Kato <tohosaku@cosmichorror.org>
Date: Thu, 5 Feb 2026 04:13:09 +0000
Subject: [PATCH 1/2] If not needed, stop traverse

---
 .../wiki_formatting/common_mark/formatter.rb      | 12 +++++++++---
 .../common_mark/syntax_highlight_scrubber.rb      |  1 +
 lib/redmine/wiki_formatting/html_sanitizer.rb     |  9 +++++++--
 lib/redmine/wiki_formatting/textile/formatter.rb  | 13 +++++++++----
 .../textile/syntax_highlight_scrubber.rb          |  1 +
 test/helpers/application_helper_test.rb           | 15 +++++++++++++++
 6 files changed, 42 insertions(+), 9 deletions(-)

diff --git a/lib/redmine/wiki_formatting/common_mark/formatter.rb b/lib/redmine/wiki_formatting/common_mark/formatter.rb
index 9ef52692eb7..46ed8e21b44 100644
--- a/lib/redmine/wiki_formatting/common_mark/formatter.rb
+++ b/lib/redmine/wiki_formatting/common_mark/formatter.rb
@@ -54,9 +54,9 @@ module Redmine
 
       SANITIZER = SanitizationFilter.new
       SCRUBBERS = [
+        Redmine::WikiFormatting::CopypreScrubber.new,
         SyntaxHighlightScrubber.new,
         Redmine::WikiFormatting::TablesortScrubber.new,
-        Redmine::WikiFormatting::CopypreScrubber.new,
         FixupAutoLinksScrubber.new,
         ExternalLinksScrubber.new,
         AlertsIconsScrubber.new
@@ -73,9 +73,15 @@ module Redmine
           html = MarkdownFilter.new(@text, PIPELINE_CONFIG).call
           fragment = Redmine::WikiFormatting::HtmlParser.parse(html)
           SANITIZER.call(fragment)
-          SCRUBBERS.each do |scrubber|
-            fragment.scrub!(scrubber)
+
+          scrubber = Loofah::Scrubber.new do |node|
+            SCRUBBERS.each do |s|
+              result = s.scrub(node)
+              break result if result == Loofah::Scrubber::STOP
+              break if node.parent.nil?
+            end
           end
+          fragment.scrub!(scrubber)
           fragment.to_s
         end
       end
diff --git a/lib/redmine/wiki_formatting/common_mark/syntax_highlight_scrubber.rb b/lib/redmine/wiki_formatting/common_mark/syntax_highlight_scrubber.rb
index f727bacd4d0..5a93f0df7e2 100644
--- a/lib/redmine/wiki_formatting/common_mark/syntax_highlight_scrubber.rb
+++ b/lib/redmine/wiki_formatting/common_mark/syntax_highlight_scrubber.rb
@@ -35,6 +35,7 @@ module Redmine
           lang = $1
           text = node.inner_text
           process node, text, lang
+          Loofah::Scrubber::STOP
         end
       end
     end
diff --git a/lib/redmine/wiki_formatting/html_sanitizer.rb b/lib/redmine/wiki_formatting/html_sanitizer.rb
index d818687b0bc..88f68ad2c17 100644
--- a/lib/redmine/wiki_formatting/html_sanitizer.rb
+++ b/lib/redmine/wiki_formatting/html_sanitizer.rb
@@ -27,9 +27,14 @@ module Redmine
       def self.call(html)
         fragment = HtmlParser.parse(html)
         SANITIZER.call(fragment)
-        SCRUBBERS.each do |scrubber|
-          fragment.scrub!(scrubber)
+        scrubber = Loofah::Scrubber.new do |node|
+          SCRUBBERS.each do |s|
+            result = s.scrub(node)
+            break result if result == Loofah::Scrubber::STOP
+            break if node.parent.nil?
+          end
         end
+        fragment.scrub!(scrubber)
         fragment.to_s
       end
     end
diff --git a/lib/redmine/wiki_formatting/textile/formatter.rb b/lib/redmine/wiki_formatting/textile/formatter.rb
index 57d8dbab408..015845122cd 100644
--- a/lib/redmine/wiki_formatting/textile/formatter.rb
+++ b/lib/redmine/wiki_formatting/textile/formatter.rb
@@ -21,9 +21,9 @@ module Redmine
   module WikiFormatting
     module Textile
       SCRUBBERS = [
+        Redmine::WikiFormatting::CopypreScrubber.new,
         SyntaxHighlightScrubber.new,
-        Redmine::WikiFormatting::TablesortScrubber.new,
-        Redmine::WikiFormatting::CopypreScrubber.new
+        Redmine::WikiFormatting::TablesortScrubber.new
       ]
 
       class Formatter
@@ -39,9 +39,14 @@ module Redmine
         def to_html(*rules)
           html = @filter.to_html(rules)
           fragment = Loofah.html5_fragment(html)
-          SCRUBBERS.each do |scrubber|
-            fragment.scrub!(scrubber)
+          scrubber = Loofah::Scrubber.new do |node|
+            SCRUBBERS.each do |s|
+              result = s.scrub(node)
+              break result if result == Loofah::Scrubber::STOP
+              break if node.parent.nil?
+            end
           end
+          fragment.scrub!(scrubber)
           fragment.to_s
         end
       end
diff --git a/lib/redmine/wiki_formatting/textile/syntax_highlight_scrubber.rb b/lib/redmine/wiki_formatting/textile/syntax_highlight_scrubber.rb
index d5e976cec06..172f97316a5 100644
--- a/lib/redmine/wiki_formatting/textile/syntax_highlight_scrubber.rb
+++ b/lib/redmine/wiki_formatting/textile/syntax_highlight_scrubber.rb
@@ -34,6 +34,7 @@ module Redmine
           end
 
           process node, text, lang
+          Loofah::Scrubber::STOP
         end
       end
     end
diff --git a/test/helpers/application_helper_test.rb b/test/helpers/application_helper_test.rb
index 87b57c7f26b..3484c8a72bd 100644
--- a/test/helpers/application_helper_test.rb
+++ b/test/helpers/application_helper_test.rb
@@ -1447,6 +1447,21 @@ class ApplicationHelperTest < Redmine::HelperTest
     end
   end
 
+  def test_syntax_highlight_common_mark
+    raw = <<~RAW
+      ```ECMA_script
+      /* Hello */
+      document.write("Hello World!");
+      ```
+    RAW
+    expected = <<~EXPECTED
+      #{pre_wrapper('<pre data-clipboard-target="pre"><code class="ECMA_script syntaxhl" data-language="ECMA_script"><span class="cm">/* Hello */</span><span class="nb">document</span><span class="p">.</span><span class="nf">write</span><span class="p">(</span><span class="dl">"</span><span class="s2">Hello World!</span><span class="dl">"</span><span class="p">);</span></code></pre>')}
+    EXPECTED
+    with_settings :text_formatting => 'common_mark' do
+      assert_equal expected.gsub(%r{[\r\n\t]}, ''), textilizable(raw).gsub(%r{[\r\n\t]}, '')
+    end
+  end
+
   def test_syntax_highlight_ampersand_in_textile
     raw = <<~RAW
       <pre><code class="ruby">
-- 
2.47.3

