Skip to content

Commit 9de78a5

Browse files
committed
Do not scrub removed attributes
This should improve performance by eliminating needless sanitization of attributes that are already removed.
1 parent 2eaa944 commit 9de78a5

File tree

2 files changed

+15
-2
lines changed

2 files changed

+15
-2
lines changed

Diff for: lib/rails/html/scrubbers.rb

+5-2
Original file line numberDiff line numberDiff line change
@@ -108,8 +108,11 @@ def scrub_node(node)
108108
def scrub_attributes(node)
109109
if @attributes
110110
node.attribute_nodes.each do |attr|
111-
attr.remove if scrub_attribute?(attr.name)
112-
scrub_attribute(node, attr)
111+
if scrub_attribute?(attr.name)
112+
attr.remove
113+
else
114+
scrub_attribute(node, attr)
115+
end
113116
end
114117

115118
scrub_css_attribute(node)

Diff for: test/scrubbers_test.rb

+10
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,16 @@ def scrub_attribute(node, attr)
241241
end
242242
end
243243

244+
def test_does_not_scrub_removed_attributes
245+
@scrubber = TestPermitScrubber.new
246+
247+
input = "<div class='foo' href='bar'></div>"
248+
frag = scrub_fragment(input)
249+
assert_equal("<div class=\"foo\"></div>", frag)
250+
251+
assert_equal([["div", "class"]], @scrubber.instance_variable_get(:@scrub_attribute_args))
252+
end
253+
244254
def test_does_not_scrub_attributes_of_a_removed_node
245255
@scrubber = TestPermitScrubber.new
246256

0 commit comments

Comments
 (0)