Skip to content

Commit

Permalink
Do not scrub removed attributes
Browse files Browse the repository at this point in the history
This should improve performance by eliminating needless sanitization
of attributes that are already removed.
  • Loading branch information
flavorjones committed Aug 13, 2024
1 parent 2eaa944 commit 9de78a5
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
7 changes: 5 additions & 2 deletions lib/rails/html/scrubbers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,11 @@ def scrub_node(node)
def scrub_attributes(node)
if @attributes
node.attribute_nodes.each do |attr|
attr.remove if scrub_attribute?(attr.name)
scrub_attribute(node, attr)
if scrub_attribute?(attr.name)
attr.remove
else
scrub_attribute(node, attr)
end
end

scrub_css_attribute(node)
Expand Down
10 changes: 10 additions & 0 deletions test/scrubbers_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,16 @@ def scrub_attribute(node, attr)
end
end

def test_does_not_scrub_removed_attributes
@scrubber = TestPermitScrubber.new

input = "<div class='foo' href='bar'></div>"
frag = scrub_fragment(input)
assert_equal("<div class=\"foo\"></div>", frag)

assert_equal([["div", "class"]], @scrubber.instance_variable_get(:@scrub_attribute_args))
end

def test_does_not_scrub_attributes_of_a_removed_node
@scrubber = TestPermitScrubber.new

Expand Down

0 comments on commit 9de78a5

Please sign in to comment.