Skip to content

Commit

Permalink
Simplified the removal of xpaths in remove_xpaths. Added more tests f…
Browse files Browse the repository at this point in the history
…or remove_xpaths.
  • Loading branch information
kaspth committed Jun 16, 2014
1 parent c1a7864 commit 6217178
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 9 deletions.
Expand Up @@ -13,7 +13,7 @@ def sanitize(html, options = {})

def remove_xpaths(html, xpaths)
if html.respond_to?(:xpath)
xpaths.each { |xpath| html.xpath(xpath).remove }
html.xpath(*xpaths).remove
html
else
remove_xpaths(Loofah.fragment(html), xpaths).to_s
Expand Down
19 changes: 11 additions & 8 deletions actionview/test/template/sanitizers_test.rb
Expand Up @@ -23,23 +23,26 @@ def test_remove_xpaths_removes_all_occurences_of_xpath
assert_equal %(<section><header></header><p>hello </p></section>), sanitizer.remove_xpaths(html, %w(.//script))
end

def test_remove_xpaths_not_enumerable_xpaths_parameter
def test_remove_xpaths_called_with_faulty_xpath
sanitizer = ActionView::Sanitizer.new
assert_raises NoMethodError do
sanitizer.remove_xpaths('<h1>hello<h1>', './not_enumerable')
assert_raises Nokogiri::XML::XPath::SyntaxError do
sanitizer.remove_xpaths('<h1>hello<h1>', %w(..faulty_xpath))
end
end

def test_remove_xpaths_faulty_xpath
def test_remove_xpaths_called_with_xpath_string
sanitizer = ActionView::Sanitizer.new
assert_raises Nokogiri::XML::XPath::SyntaxError do
sanitizer.remove_xpaths('<h1>hello<h1>', %w(..faulty_xpath))
end
assert_equal '', sanitizer.remove_xpaths('<a></a>', './/a')
end

def test_remove_xpaths_called_with_enumerable_xpaths
sanitizer = ActionView::Sanitizer.new
assert_equal '', sanitizer.remove_xpaths('<a><span></span></a>', %w(.//a .//span))
end

def test_remove_xpaths_called_with_string_returns_string
sanitizer = ActionView::Sanitizer.new
assert '<a></a>', sanitizer.remove_xpaths('<a></a>', [])
assert_equal '<a></a>', sanitizer.remove_xpaths('<a></a>', [])
end

def test_remove_xpaths_called_with_fragment_returns_fragment
Expand Down

0 comments on commit 6217178

Please sign in to comment.