1 require File.dirname(__FILE__) + '/../../abstract_unit'
4 class DocumentTest < Test::Unit::TestCase
5 def test_handle_doctype
7 assert_nothing_raised do
8 doc = HTML::Document.new <<-HTML.strip
9 <!DOCTYPE "blah" "blah" "blah">
14 assert_equal 3, doc.root.children.length
15 assert_equal %{<!DOCTYPE "blah" "blah" "blah">}, doc.root.children[0].content
16 assert_match %r{\s+}m, doc.root.children[1].content
17 assert_equal "html", doc.root.children[2].name
21 doc = HTML::Document.new <<-HTML.strip
24 <p><img src="hello.gif"></p>
28 assert doc.find(:tag=>"img", :attributes=>{"src"=>"hello.gif"})
32 doc = HTML::Document.new <<-HTML.strip
35 <p class="test"><img src="hello.gif"></p>
37 <p class="test">something</p>
38 <p>here is <em class="test">more</em></p>
43 all = doc.find_all :attributes => { :class => "test" }
44 assert_equal 3, all.length
45 assert_equal [ "p", "p", "em" ], all.map { |n| n.name }
48 def test_find_with_text
49 doc = HTML::Document.new <<-HTML.strip
56 assert doc.find(:content => "Some text")
57 assert doc.find(:tag => "p", :child => { :content => "Some text" })
58 assert doc.find(:tag => "p", :child => "Some text")
59 assert doc.find(:tag => "p", :content => "Some text")
63 assert_nothing_raised { HTML::Document.new("<tags><tag/></tags>", true, true) }
64 assert_nothing_raised { HTML::Document.new("<outer><link>something</link></outer>", true, true) }
67 def test_parse_document
68 doc = HTML::Document.new(<<-HTML)
75 assert_not_nil doc.find(:tag => "div", :children => { :count => 1, :only => { :tag => "table" } })
79 doc = HTML::Document.new(<<-HTML)
80 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
81 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
82 <html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
84 <title><![CDATA[<br>]]></title>
87 <p>this document has <br> for a title</p>
92 assert_nil doc.find(:tag => "title", :descendant => { :tag => "br" })
93 assert doc.find(:tag => "title", :child => "<br>")
96 def test_find_empty_tag
97 doc = HTML::Document.new("<div id='map'></div>")
98 assert_nil doc.find(:tag => "div", :attributes => { :id => "map" }, :content => /./)
99 assert doc.find(:tag => "div", :attributes => { :id => "map" }, :content => /\A\Z/)
100 assert doc.find(:tag => "div", :attributes => { :id => "map" }, :content => /^$/)
101 assert doc.find(:tag => "div", :attributes => { :id => "map" }, :content => "")
102 assert doc.find(:tag => "div", :attributes => { :id => "map" }, :content => nil)