From 71d84a880d4d9668968a6de5fb8d73d9589410e5 Mon Sep 17 00:00:00 2001 From: Chris Zetter Date: Tue, 20 Mar 2018 12:33:56 +0000 Subject: [PATCH] Remove optimization to get back the previous behaviour of transformers 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] - https://github.com/rgrove/sanitize/commit/caa558a25886638427d3005030260af8518612ea --- lib/sanitize.rb | 4 +--- test/test_transformers.rb | 18 ++++++++++++++++++ 2 files changed, 19 insertions(+), 3 deletions(-) diff --git a/lib/sanitize.rb b/lib/sanitize.rb index 935fcaa..a256b3c 100644 --- a/lib/sanitize.rb +++ b/lib/sanitize.rb @@ -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 ) diff --git a/test/test_transformers.rb b/test/test_transformers.rb index a0779c6..cb751db 100644 --- a/test/test_transformers.rb +++ b/test/test_transformers.rb @@ -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 tag to be changed to a tag' do + input = 'text' + + Sanitize.fragment(input, :elements => ['strong'], :transformers => b_to_strong_tag_transformer) + .must_equal 'text' + end + end end