require File.expand_path(File.join(File.dirname(__FILE__), '..', "helper")) module Nokogiri module HTML class TestDocument < Nokogiri::TestCase def setup @html = Nokogiri::HTML.parse(File.read(HTML_FILE)) end def test_HTML_function html = Nokogiri::HTML(File.read(HTML_FILE)) assert html.html? end def test_parse_io assert doc = File.open(HTML_FILE, 'rb') { |f| Document.read_io(f, nil, 'UTF-8', PARSE_NOERROR | PARSE_NOWARNING) } end def test_to_xhtml assert_match 'XHTML', @html.to_xhtml assert_match 'XHTML', @html.to_xhtml('UTF-8') assert_match 'UTF-8', @html.to_xhtml('UTF-8') end def test_no_xml_header html = Nokogiri::HTML(<<-eohtml) eohtml assert html.to_html.length > 0, 'html length is too short' assert_no_match(/^<\?xml/, html.to_html) end def test_document_has_error html = Nokogiri::HTML(<<-eohtml)

").children.length end def test_fragment fragment = Nokogiri::HTML.fragment(<<-eohtml)
Hello World
eohtml assert_equal 1, fragment.children.length assert_equal 'div', fragment.children.first.name assert_match(/Hello World/, fragment.to_html) # libxml2 is broken in 2.6.16 and 2.6.17 unless [16, 17].include?(Nokogiri::LIBXML_VERSION.split('.').last.to_i) assert_equal 1, fragment.css('div').length end end def test_relative_css_finder doc = Nokogiri::HTML(<<-eohtml)

inside red

inside green

eohtml red_divs = doc.css('div.red') assert_equal 1, red_divs.length p_tags = red_divs.first.css('p') assert_equal 1, p_tags.length assert_equal 'inside red', p_tags.first.text.strip end def test_find_classes doc = Nokogiri::HTML(<<-eohtml)

RED

RED

GREEN

GREEN

eohtml list = doc.css('.red') assert_equal 2, list.length assert_equal %w{ RED RED }, list.map { |x| x.text } end def test_parse_can_take_io html = nil File.open(HTML_FILE, 'rb') { |f| html = Nokogiri::HTML(f) } assert html.html? end def test_html? assert !@html.xml? assert @html.html? end def test_serialize assert @html.serialize assert @html.to_html end end end end