require File.expand_path(File.join(File.dirname(__FILE__), '..', 'helper')) class TestAdHoc < Test::Unit::TestCase context "blank input string" do context "fragment" do should "return a blank string" do assert_equal "", Loofah.scrub_fragment("", :prune).to_s end end context "document" do should "return a blank string" do assert_equal "", Loofah.scrub_document("", :prune).root.to_s end end end context "integration test" do context "xml document" do context "custom scrubber" do should "act as expected" do xml = Loofah.xml_document <<-EOXML Abraham Lincoln Abe Vigoda EOXML bring_out_your_dead = Loofah::Scrubber.new do |node| if node.name == "employee" and node["deceased"] == "true" node.remove Loofah::Scrubber::STOP # don't bother with the rest of the subtree end end assert_equal 2, xml.css("employee").length xml.scrub!(bring_out_your_dead) employees = xml.css "employee" assert_equal 1, employees.length assert_equal "Abe Vigoda", employees.first.inner_text end end end context "xml fragment" do context "custom scrubber" do should "act as expected" do xml = Loofah.xml_fragment <<-EOXML Abraham Lincoln Abe Vigoda EOXML bring_out_your_dead = Loofah::Scrubber.new do |node| if node.name == "employee" and node["deceased"] == "true" node.remove Loofah::Scrubber::STOP # don't bother with the rest of the subtree end end assert_equal 2, xml.css("employee").length xml.scrub!(bring_out_your_dead) employees = xml.css "employee" assert_equal 1, employees.length assert_equal "Abe Vigoda", employees.first.inner_text end end end context "html fragment" do context "#to_s" do should "not include head tags (like style)" do html = Loofah.fragment "
bar
" assert_equal "
bar
", html.to_s end end context "#text" do should "not include head tags (like style)" do html = Loofah.fragment "
bar
" assert_equal "bar", html.text end end end context "html document" do should "not include head tags (like style)" do html = Loofah.document "
bar
" assert_equal "bar", html.text end end end def test_removal_of_illegal_tag html = <<-HTML following this there should be no jim tag jim was there? HTML sane = Nokogiri::HTML(Loofah.scrub_fragment(html, :escape).to_xml) assert sane.xpath("//jim").empty? end def test_removal_of_illegal_attribute html = "

" sane = Nokogiri::HTML(Loofah.scrub_fragment(html, :escape).to_xml) node = sane.xpath("//p").first assert node.attributes['class'] assert node.attributes['abbr'] assert_nil node.attributes['foo'] end def test_removal_of_illegal_url_in_href html = <<-HTML this link should have its href removed because of illegal url this link should be fine HTML sane = Nokogiri::HTML(Loofah.scrub_fragment(html, :escape).to_xml) nodes = sane.xpath("//a") assert_nil nodes.first.attributes['href'] assert nodes.last.attributes['href'] end def test_css_sanitization html = "

" sane = Nokogiri::HTML(Loofah.scrub_fragment(html, :escape).to_xml) assert_match(/#000/, sane.inner_html) assert_no_match(/foo\.com/, sane.inner_html) end def test_fragment_with_no_tags assert_equal "This fragment has no tags.", Loofah.scrub_fragment("This fragment has no tags.", :escape).to_xml end def test_fragment_in_p_tag assert_equal "

This fragment is in a p.

", Loofah.scrub_fragment("

This fragment is in a p.

", :escape).to_xml end def test_fragment_in_p_tag_plus_stuff assert_equal "

This fragment is in a p.

foobar", Loofah.scrub_fragment("

This fragment is in a p.

foobar", :escape).to_xml end def test_fragment_with_text_nodes_leading_and_trailing assert_equal "text

fragment

text", Loofah.scrub_fragment("text

fragment

text", :escape).to_xml end def test_whitewash_on_fragment html = "safe description" whitewashed = Loofah.scrub_document(html, :whitewash).xpath("/html/body/*").to_s assert_equal "

safe

description", whitewashed.gsub("\n","") end MSWORD_HTML = <<-EOHTML

Foo BOLD

EOHTML def test_fragment_whitewash_on_microsofty_markup whitewashed = Loofah.fragment(MSWORD_HTML).scrub!(:whitewash) assert_equal "

Foo BOLD

", whitewashed.to_s end def test_document_whitewash_on_microsofty_markup whitewashed = Loofah.document(MSWORD_HTML).scrub!(:whitewash) assert_contains whitewashed.to_s, %r(

Foo BOLD

) assert_equal "

Foo BOLD

", whitewashed.xpath("/html/body/*").to_s end def test_return_empty_string_when_nothing_left assert_equal "", Loofah.scrub_document('', :prune).text end def test_removal_of_all_tags html = <<-HTML What's up doc? HTML stripped = Loofah.scrub_document(html, :prune).text assert_equal %Q(What\'s up doc?).strip, stripped.strip end def test_dont_remove_whitespace html = "Foo\nBar" assert_equal html, Loofah.scrub_document(html, :prune).text end def test_dont_remove_whitespace_between_tags html = "

Foo

\n

Bar

" assert_equal "Foo\nBar", Loofah.scrub_document(html, :prune).text end end