Skip to content

Commit

Permalink
Remove optimization to get back the previous behaviour of transformers
Browse files Browse the repository at this point in the history
Before [1] transformers could modify the DOM, and transformers or
filtering that ran after would use the modified DOM. The change in [1]
meant that the node name was cached between the running of all
transformers so even if one transformer modified it subsequent
transformers would be given the previous node name.

The added test case fails without the code change because the 'strong'
tag is stripped out after the transformer is run since it thinks it is
still a 'b'.

[1] -
rgrove@caa558a
  • Loading branch information
zetter committed Mar 20, 2018
1 parent f5a2686 commit 71d84a8
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 3 deletions.
4 changes: 1 addition & 3 deletions lib/sanitize.rb
Original file line number Diff line number Diff line change
Expand Up @@ -217,14 +217,12 @@ def to_html(node)
end

def transform_node!(node, node_whitelist)
node_name = node.name.downcase

@transformers.each do |transformer|
result = transformer.call(
:config => @config,
:is_whitelisted => node_whitelist.include?(node),
:node => node,
:node_name => node_name,
:node_name => node.name.downcase,
:node_whitelist => node_whitelist
)

Expand Down
18 changes: 18 additions & 0 deletions test/test_transformers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -203,4 +203,22 @@ def transformer(env); end
.must_equal('')
end
end

describe 'DOM modification transformer' do
b_to_strong_tag_transformer = lambda do |env|
node = env[:node]
node_name = env[:node_name]

if node_name == 'b'
node.name = 'strong'
end
end

it 'should allow the <b> tag to be changed to a <strong> tag' do
input = '<b>text</b>'

Sanitize.fragment(input, :elements => ['strong'], :transformers => b_to_strong_tag_transformer)
.must_equal '<strong>text</strong>'
end
end
end

0 comments on commit 71d84a8

Please sign in to comment.