1 require 'abstract_unit'
3 class DocumentTest
< Test
::Unit::TestCase
4 def test_handle_doctype
6 assert_nothing_raised
do
7 doc
= HTML
::Document.new
<<-HTML.strip
8 <!DOCTYPE "blah" "blah" "blah">
13 assert_equal
3, doc
.root
.children
.length
14 assert_equal %
{<!DOCTYPE
"blah" "blah" "blah">}, doc
.root
.children
[0].content
15 assert_match
%r
{\s
+}m
, doc
.root
.children
[1].content
16 assert_equal
"html", doc
.root
.children
[2].name
20 doc
= HTML
::Document.new
<<-HTML.strip
23 <p><img src="hello.gif"></p>
27 assert doc
.find(:tag=>"img", :attributes=>{"src"=>"hello.gif"})
31 doc
= HTML
::Document.new
<<-HTML.strip
34 <p class="test"><img src="hello.gif"></p>
36 <p class="test">something</p>
37 <p>here is <em class="test">more</em></p>
42 all
= doc
.find_all
:attributes => { :class => "test" }
43 assert_equal
3, all
.length
44 assert_equal
[ "p", "p", "em" ], all
.map
{ |n
| n
.name
}
47 def test_find_with_text
48 doc
= HTML
::Document.new
<<-HTML.strip
55 assert doc
.find(:content => "Some text")
56 assert doc
.find(:tag => "p", :child => { :content => "Some text" })
57 assert doc
.find(:tag => "p", :child => "Some text")
58 assert doc
.find(:tag => "p", :content => "Some text")
62 assert_nothing_raised
{ HTML
::Document.new("<tags><tag/></tags>", true, true) }
63 assert_nothing_raised
{ HTML
::Document.new("<outer><link>something</link></outer>", true, true) }
66 def test_parse_document
67 doc
= HTML
::Document.new(<<-HTML)
74 assert_not_nil doc
.find(:tag => "div", :children => { :count => 1, :only => { :tag => "table" } })
77 def test_tag_nesting_nothing_to_s
78 doc
= HTML
::Document.new("<tag></tag>")
79 assert_equal
"<tag></tag>", doc
.root
.to_s
82 def test_tag_nesting_space_to_s
83 doc
= HTML
::Document.new("<tag> </tag>")
84 assert_equal
"<tag> </tag>", doc
.root
.to_s
87 def test_tag_nesting_text_to_s
88 doc
= HTML
::Document.new("<tag>text</tag>")
89 assert_equal
"<tag>text</tag>", doc
.root
.to_s
92 def test_tag_nesting_tag_to_s
93 doc
= HTML
::Document.new("<tag><nested /></tag>")
94 assert_equal
"<tag><nested /></tag>", doc
.root
.to_s
98 doc
= HTML
::Document.new(<<-HTML)
99 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
100 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
101 <html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
103 <title><![CDATA[<br>]]></title>
106 <p>this document has <br> for a title</p>
111 assert_nil doc
.find(:tag => "title", :descendant => { :tag => "br" })
112 assert doc
.find(:tag => "title", :child => "<br>")
115 def test_find_empty_tag
116 doc
= HTML
::Document.new("<div id='map'></div>")
117 assert_nil doc
.find(:tag => "div", :attributes => { :id => "map" }, :content => /./)
118 assert doc
.find(:tag => "div", :attributes => { :id => "map" }, :content => /\A\Z/)
119 assert doc
.find(:tag => "div", :attributes => { :id => "map" }, :content => /^$/)
120 assert doc
.find(:tag => "div", :attributes => { :id => "map" }, :content => "")
121 assert doc
.find(:tag => "div", :attributes => { :id => "map" }, :content => nil)
124 def test_parse_invalid_document
125 assert_nothing_raised
do
126 doc
= HTML
::Document.new("<html>
129 <td style=\"color: #FFFFFF; height: 17px; onclick=\"window.location.href='http://www.rmeinc.com/about_rme.aspx'\" style=\"cursor:pointer; height: 17px;\"; nowrap onclick=\"window.location.href='http://www.rmeinc.com/about_rme.aspx'\" onmouseout=\"this.bgColor='#0066cc'; this.style.color='#FFFFFF'\" onmouseover=\"this.bgColor='#ffffff'; this.style.color='#0033cc'\">About Us</td>
136 def test_invalid_document_raises_exception_when_strict
137 assert_raise RuntimeError
do
138 doc
= HTML
::Document.new("<html>
141 <td style=\"color: #FFFFFF; height: 17px; onclick=\"window.location.href='http://www.rmeinc.com/about_rme.aspx'\" style=\"cursor:pointer; height: 17px;\"; nowrap onclick=\"window.location.href='http://www.rmeinc.com/about_rme.aspx'\" onmouseout=\"this.bgColor='#0066cc'; this.style.color='#FFFFFF'\" onmouseover=\"this.bgColor='#ffffff'; this.style.color='#0033cc'\">About Us</td>