Skip to content

Commit 2eaa944

Browse files
committed
Do not scrub attributes on a removed node
This should improve performance by eliminating unneeded scrubbing of attributes.
1 parent c5734e5 commit 2eaa944

File tree

2 files changed

+38
-1
lines changed

2 files changed

+38
-1
lines changed

lib/rails/html/scrubbers.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,8 @@ def scrub(node)
7272
return CONTINUE if skip_node?(node)
7373

7474
unless (node.element? || node.comment?) && keep_node?(node)
75-
return STOP if scrub_node(node) == STOP
75+
scrub_node(node)
76+
return STOP
7677
end
7778

7879
scrub_attributes(node)

test/scrubbers_test.rb

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,3 +215,39 @@ def test_returns_stop_from_scrub_if_scrub_node_does
215215
assert_scrub_stopped "<script>remove me</script>"
216216
end
217217
end
218+
219+
class PermitScrubberMinimalOperationsTest < ScrubberTest
220+
class TestPermitScrubber < Rails::HTML::PermitScrubber
221+
def initialize
222+
@scrub_attribute_args = []
223+
@scrub_attributes_args = []
224+
225+
super
226+
227+
self.tags = ["div"]
228+
self.attributes = ["class"]
229+
end
230+
231+
def scrub_attributes(node)
232+
@scrub_attributes_args << node.name
233+
234+
super
235+
end
236+
237+
def scrub_attribute(node, attr)
238+
@scrub_attribute_args << [node.name, attr.name]
239+
240+
super
241+
end
242+
end
243+
244+
def test_does_not_scrub_attributes_of_a_removed_node
245+
@scrubber = TestPermitScrubber.new
246+
247+
input = "<div class='foo' href='bar'><svg xlink:href='asdf'><set></set></svg></div>"
248+
frag = scrub_fragment(input)
249+
assert_equal("<div class=\"foo\"></div>", frag)
250+
251+
assert_equal(["div"], @scrubber.instance_variable_get(:@scrub_attributes_args))
252+
end
253+
end

0 commit comments

Comments
 (0)