From caa558a25886638427d3005030260af8518612ea Mon Sep 17 00:00:00 2001 From: Jan Klimo Date: Tue, 20 Mar 2018 01:18:17 +0700 Subject: [PATCH] Optimize memory usage --- lib/sanitize.rb | 6 ++++-- lib/sanitize/transformers/clean_element.rb | 2 +- test/test_clean_element.rb | 19 ++++++++++++++++++- 3 files changed, 23 insertions(+), 4 deletions(-) diff --git a/lib/sanitize.rb b/lib/sanitize.rb index 4dc1174..935fcaa 100644 --- a/lib/sanitize.rb +++ b/lib/sanitize.rb @@ -198,7 +198,7 @@ def to_html(node) # the original document didn't actually include a content-type meta tag. replace_meta = !@config[:elements].include?('meta') || node.xpath('/html/head/meta[@http-equiv]').none? do |meta| - meta['http-equiv'].downcase == 'content-type' + meta['http-equiv'].casecmp('content-type').zero? end end @@ -217,12 +217,14 @@ 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.downcase, + :node_name => node_name, :node_whitelist => node_whitelist ) diff --git a/lib/sanitize/transformers/clean_element.rb b/lib/sanitize/transformers/clean_element.rb index f883d52..3431be4 100644 --- a/lib/sanitize/transformers/clean_element.rb +++ b/lib/sanitize/transformers/clean_element.rb @@ -99,7 +99,7 @@ def call(env) if @protocols.include?(name) && @protocols[name].include?(attr_name) attr_protocols = @protocols[name][attr_name] - if attr.value.to_s.downcase =~ REGEX_PROTOCOL + if attr.value =~ REGEX_PROTOCOL attr.unlink unless attr_protocols.include?($1.downcase) else attr.unlink unless attr_protocols.include?(:relative) diff --git a/test/test_clean_element.rb b/test/test_clean_element.rb index 6355d78..562464c 100644 --- a/test/test_clean_element.rb +++ b/test/test_clean_element.rb @@ -402,6 +402,23 @@ s.fragment('foo
bar
baz').must_equal "foo\nbar\nbaz" s.fragment('foo
bar
baz').must_equal "foo\nbar\nbaz" end - end + it 'handles protocols correctly regardless of case' do + input = 'Text' + + Sanitize.fragment(input, { + :elements => ['a'], + :attributes => {'a' => ['href']}, + :protocols => {'a' => {'href' => ['https']}} + }).must_equal input + + input = 'Text' + + Sanitize.fragment(input, { + :elements => ['a'], + :attributes => {'a' => ['href']}, + :protocols => {'a' => {'href' => ['https']}} + }).must_equal "Text" + end + end end