Stop traversal of child elements at @Loofah::Scrubber::STOP@ signal (#42737).

Patch by Takashi Kato (user:tohosaku).


git-svn-id: https://svn.redmine.org/redmine/trunk@24400 e93f8b46-1217-0410-a6f0-8f06a7374b81
This commit is contained in:
Marius Balteanu
2026-02-07 10:38:27 +00:00
parent a56413532c
commit 84483d6382
6 changed files with 32 additions and 6 deletions

View File

@@ -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,12 +73,15 @@ module Redmine
html = MarkdownFilter.new(@text, PIPELINE_CONFIG).call
fragment = Redmine::WikiFormatting::HtmlParser.parse(html)
SANITIZER.call(fragment)
scrubber = Loofah::Scrubber.new do |node|
SCRUBBERS.each do |s|
s.scrub(node)
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

View File

@@ -35,6 +35,7 @@ module Redmine
lang = $1
text = node.inner_text
process node, text, lang
Loofah::Scrubber::STOP
end
end
end

View File

@@ -27,12 +27,15 @@ module Redmine
def self.call(html)
fragment = HtmlParser.parse(html)
SANITIZER.call(fragment)
scrubber = Loofah::Scrubber.new do |node|
SCRUBBERS.each do |s|
s.scrub(node)
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

View File

@@ -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,12 +39,15 @@ module Redmine
def to_html(*rules)
html = @filter.to_html(rules)
fragment = Loofah.html5_fragment(html)
scrubber = Loofah::Scrubber.new do |node|
SCRUBBERS.each do |s|
s.scrub(node)
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

View File

@@ -34,6 +34,7 @@ module Redmine
end
process node, text, lang
Loofah::Scrubber::STOP
end
end
end

View File

@@ -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">