Skip to content

Commit

Permalink
removing unnecessary xpath() lookups, fixing version-specific test
Browse files Browse the repository at this point in the history
  • Loading branch information
flavorjones committed Dec 5, 2008
1 parent 0c3105b commit b97b572
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 7 deletions.
14 changes: 8 additions & 6 deletions lib/dryopteris/sanitize.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -13,23 +13,25 @@ def strip_tags(string_or_io, encoding=nil)
return "" if string_or_io.strip.size == 0 return "" if string_or_io.strip.size == 0


doc = Nokogiri::HTML.parse(string_or_io, nil, encoding) doc = Nokogiri::HTML.parse(string_or_io, nil, encoding)
doc.xpath("html/body/*").each do |node| body = doc.xpath("/html/body").first
return "" if body.nil?
body.children.each do |node|
traverse_conditionally_top_down(node, :remove_tags_from_node) traverse_conditionally_top_down(node, :remove_tags_from_node)
end end
snippet = doc.xpath("html/body").first body.inner_html
snippet.nil? ? "" : snippet.inner_html
end end


def sanitize(string_or_io, encoding=nil) def sanitize(string_or_io, encoding=nil)
return nil if string_or_io.nil? return nil if string_or_io.nil?
return "" if string_or_io.strip.size == 0 return "" if string_or_io.strip.size == 0


doc = Nokogiri::HTML.parse(string_or_io, nil, encoding) doc = Nokogiri::HTML.parse(string_or_io, nil, encoding)
doc.xpath("html/body/*").each do |node| body = doc.xpath("/html/body").first
return "" if body.nil?
body.children.each do |node|
traverse_conditionally_top_down(node, :sanitize_node) traverse_conditionally_top_down(node, :sanitize_node)
end end
snippet = doc.xpath("html/body").first body.inner_html
snippet.nil? ? "" : snippet.inner_html
end end


private private
Expand Down
2 changes: 1 addition & 1 deletion test/test_sanitizer.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ def check_sanitization(input, htmloutput, xhtmloutput, rexmloutput)
# xhtmloutput = htmloutput # xhtmloutput = htmloutput
# rexmloutput = "<image title='1'>foo &lt;bad&gt;bar&lt;/bad&gt; baz</image>" # rexmloutput = "<image title='1'>foo &lt;bad&gt;bar&lt;/bad&gt; baz</image>"
if WhiteList::VOID_ELEMENTS.include?(tag_name) if WhiteList::VOID_ELEMENTS.include?(tag_name)
if Nokogiri::LIBXML_VERSION >= "2.6.16" if Nokogiri::LIBXML_VERSION <= "2.6.16"
htmloutput = "<#{tag_name} title='1'/><p>foo &lt;bad&gt;bar&lt;/bad&gt; baz</p>" htmloutput = "<#{tag_name} title='1'/><p>foo &lt;bad&gt;bar&lt;/bad&gt; baz</p>"
else else
htmloutput = "<#{tag_name} title='1'/>foo &lt;bad&gt;bar&lt;/bad&gt; baz" htmloutput = "<#{tag_name} title='1'/>foo &lt;bad&gt;bar&lt;/bad&gt; baz"
Expand Down

0 comments on commit b97b572

Please sign in to comment.