Skip to content

Commit

Permalink
Optimize memory usage
Browse files Browse the repository at this point in the history
  • Loading branch information
janklimo committed Mar 19, 2018
1 parent 184709b commit caa558a
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 4 deletions.
6 changes: 4 additions & 2 deletions lib/sanitize.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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
)

Expand Down
2 changes: 1 addition & 1 deletion lib/sanitize/transformers/clean_element.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
19 changes: 18 additions & 1 deletion test/test_clean_element.rb
Original file line number Diff line number Diff line change
Expand Up @@ -402,6 +402,23 @@
s.fragment('foo<div>bar</div>baz').must_equal "foo\nbar\nbaz"
s.fragment('foo<br>bar<br>baz').must_equal "foo\nbar\nbaz"
end
end

it 'handles protocols correctly regardless of case' do
input = '<a href="hTTpS://foo.com/">Text</a>'

Sanitize.fragment(input, {
:elements => ['a'],
:attributes => {'a' => ['href']},
:protocols => {'a' => {'href' => ['https']}}
}).must_equal input

input = '<a href="mailto:someone@example.com?Subject=Hello">Text</a>'

Sanitize.fragment(input, {
:elements => ['a'],
:attributes => {'a' => ['href']},
:protocols => {'a' => {'href' => ['https']}}
}).must_equal "<a>Text</a>"
end
end
end

0 comments on commit caa558a

Please sign in to comment.