From 62171784fe374d101aa7cfcb0d1e32c89a3629f8 Mon Sep 17 00:00:00 2001 From: Timm Date: Sat, 17 Aug 2013 11:02:09 +0200 Subject: [PATCH] Simplified the removal of xpaths in remove_xpaths. Added more tests for remove_xpaths. --- .../helpers/sanitize_helper/sanitizers.rb | 2 +- actionview/test/template/sanitizers_test.rb | 19 +++++++++++-------- 2 files changed, 12 insertions(+), 9 deletions(-) diff --git a/actionview/lib/action_view/helpers/sanitize_helper/sanitizers.rb b/actionview/lib/action_view/helpers/sanitize_helper/sanitizers.rb index 01ab9830f369a..75ba1a7debc73 100644 --- a/actionview/lib/action_view/helpers/sanitize_helper/sanitizers.rb +++ b/actionview/lib/action_view/helpers/sanitize_helper/sanitizers.rb @@ -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 diff --git a/actionview/test/template/sanitizers_test.rb b/actionview/test/template/sanitizers_test.rb index 6769c765fa6de..bd5b25a305c86 100644 --- a/actionview/test/template/sanitizers_test.rb +++ b/actionview/test/template/sanitizers_test.rb @@ -23,23 +23,26 @@ def test_remove_xpaths_removes_all_occurences_of_xpath assert_equal %(

hello

), 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('

hello

', './not_enumerable') + assert_raises Nokogiri::XML::XPath::SyntaxError do + sanitizer.remove_xpaths('

hello

', %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('

hello

', %w(..faulty_xpath)) - end + assert_equal '', sanitizer.remove_xpaths('', './/a') + end + + def test_remove_xpaths_called_with_enumerable_xpaths + sanitizer = ActionView::Sanitizer.new + assert_equal '', sanitizer.remove_xpaths('', %w(.//a .//span)) end def test_remove_xpaths_called_with_string_returns_string sanitizer = ActionView::Sanitizer.new - assert '', sanitizer.remove_xpaths('', []) + assert_equal '', sanitizer.remove_xpaths('', []) end def test_remove_xpaths_called_with_fragment_returns_fragment