require File.expand_path(File.join(File.dirname(__FILE__), 'helper')) if defined? Nokogiri::VERSION_INFO puts "=> running with Nokogiri #{Nokogiri::VERSION_INFO.inspect}" else puts "=> running with Nokogiri #{Nokogiri::VERSION} / libxml #{Nokogiri::LIBXML_PARSER_VERSION}" end class TestBasic < Test::Unit::TestCase MSWORD_HTML = <<-EOHTML

Foo BOLD

EOHTML def test_nil assert_nil Dryopteris.sanitize(nil) end def test_empty_string assert_equal "", Dryopteris.sanitize("") end def test_removal_of_illegal_tag html = <<-HTML following this there should be no jim tag jim was there? HTML sane = Nokogiri::HTML(Dryopteris.sanitize(html)) assert sane.xpath("//jim").empty? end def test_removal_of_illegal_attribute html = "

" sane = Nokogiri::HTML(Dryopteris.sanitize(html)) 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(Dryopteris.sanitize(html)) nodes = sane.xpath("//a") assert_nil nodes.first.attributes['href'] assert nodes.last.attributes['href'] end def test_css_sanitization html = "

" sane = Nokogiri::HTML(Dryopteris.sanitize(html)) 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.", Dryopteris.sanitize("This fragment has no tags.") end def test_fragment_in_p_tag assert_equal "

This fragment is in a p.

", Dryopteris.sanitize("

This fragment is in a p.

") end def test_fragment_in_a_nontrivial_p_tag assert_equal " \n

This fragment is in a p.

", Dryopteris.sanitize(" \n

This fragment is in a p.

") end def test_fragment_in_p_tag_plus_stuff assert_equal "

This fragment is in a p.

foobar", Dryopteris.sanitize("

This fragment is in a p.

foobar") end def test_fragment_with_text_nodes_leading_and_trailing assert_equal "text

fragment

text", Dryopteris.sanitize("text

fragment

text") end def test_whitewash_on_fragment html = "safe description" whitewashed = Dryopteris.whitewash_document(html) assert_equal "

safe

description", whitewashed end def test_whitewash_fragment_on_microsofty_markup whitewashed = Dryopteris.whitewash(MSWORD_HTML.chomp) assert_equal "

Foo BOLD

", whitewashed end def test_whitewash_on_microsofty_markup whitewashed = Dryopteris.whitewash_document(MSWORD_HTML) assert_equal "

Foo BOLD

", whitewashed end end