From 9497c70af6794a531d82f54f941dde86133dc565 Mon Sep 17 00:00:00 2001
From: Takashi Kato <tohosaku@cosmichorror.org>
Date: Fri, 7 Nov 2025 09:18:12 +0000
Subject: [PATCH] Fix performance issue

---
 .../common_mark/alerts_icons_scrubber.rb      | 11 +++----
 .../common_mark/syntax_highlight_scrubber.rb  | 33 ++++++++++---------
 2 files changed, 22 insertions(+), 22 deletions(-)

diff --git a/lib/redmine/wiki_formatting/common_mark/alerts_icons_scrubber.rb b/lib/redmine/wiki_formatting/common_mark/alerts_icons_scrubber.rb
index 7e36be00daf..0ae0b86b3eb 100644
--- a/lib/redmine/wiki_formatting/common_mark/alerts_icons_scrubber.rb
+++ b/lib/redmine/wiki_formatting/common_mark/alerts_icons_scrubber.rb
@@ -31,22 +31,22 @@ module Redmine
       }.freeze
 
       class AlertsIconsScrubber < Loofah::Scrubber
-        def scrub(doc)
-          doc.search("p.markdown-alert-title").each do |node|
+        def scrub(node)
+          if node.name == 'p' && node['class'] == 'markdown-alert-title'
             parent_node = node.parent
             parent_class_attr = parent_node['class'] # e.g., "markdown-alert markdown-alert-note"
-            next unless parent_class_attr
+            return unless parent_class_attr
 
             # Extract the specific alert type (e.g., "note", "tip", "warning")
             # from the parent div's classes.
             match_data = parent_class_attr.match(/markdown-alert-(\w+)/)
-            next unless match_data && match_data[1] # Ensure a type is found
+            return unless match_data && match_data[1] # Ensure a type is found
 
             alert_type = match_data[1]
 
             # Get the corresponding icon name from our map.
             icon_name = ALERT_TYPE_TO_ICON_NAME[alert_type]
-            next unless icon_name # Skip if no specific icon is defined for this alert type
+            return unless icon_name # Skip if no specific icon is defined for this alert type
 
             # Translate the alert title only if it matches a known alert type
             # (i.e., the title has not been overridden)
@@ -61,7 +61,6 @@ module Redmine
               node.children.first.replace(icon_html)
             end
           end
-          doc
         end
       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 8b907b82cc4..5d9dfb38082 100644
--- a/lib/redmine/wiki_formatting/common_mark/syntax_highlight_scrubber.rb
+++ b/lib/redmine/wiki_formatting/common_mark/syntax_highlight_scrubber.rb
@@ -24,26 +24,27 @@ module Redmine
       # blocks as generated by commonmarker
       class SyntaxHighlightScrubber < Loofah::Scrubber
         def scrub(node)
-          if node.matches?("pre > code")
-            return unless lang = node["class"].presence
-            return unless lang =~ /\Alanguage-(\S+)\z/
+          # Equivalent to the CSS selector "pre > code". Implemented for performance
+          return unless node.name == 'code' && node.parent.name == 'pre'
 
-            lang = $1
-            text = node.inner_text
+          return unless lang = node["class"].presence
+          return unless lang =~ /\Alanguage-(\S+)\z/
 
-            # original language for extension development
-            node["data-language"] = lang unless node["data-language"]
+          lang = $1
+          text = node.inner_text
 
-            if Redmine::SyntaxHighlighting.language_supported?(lang)
-              html = Redmine::SyntaxHighlighting.highlight_by_language(text, lang)
-              return if html.nil?
+          # original language for extension development
+          node["data-language"] = lang unless node["data-language"]
 
-              node.inner_html = html
-              node["class"] = "#{lang} syntaxhl"
-            else
-              # unsupported language, remove the class attribute
-              node.remove_attribute("class")
-            end
+          if Redmine::SyntaxHighlighting.language_supported?(lang)
+            html = Redmine::SyntaxHighlighting.highlight_by_language(text, lang)
+            return if html.nil?
+
+            node.inner_html = html
+            node["class"] = "#{lang} syntaxhl"
+          else
+            # unsupported language, remove the class attribute
+            node.remove_attribute("class")
           end
         end
       end
-- 
2.47.3

