Froze rails gems
[depot.git] / vendor / rails / actionpack / test / controller / html-scanner / document_test.rb
1 require 'abstract_unit'
2
3 class DocumentTest < Test::Unit::TestCase
4 def test_handle_doctype
5 doc = nil
6 assert_nothing_raised do
7 doc = HTML::Document.new <<-HTML.strip
8 <!DOCTYPE "blah" "blah" "blah">
9 <html>
10 </html>
11 HTML
12 end
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
17 end
18
19 def test_find_img
20 doc = HTML::Document.new <<-HTML.strip
21 <html>
22 <body>
23 <p><img src="hello.gif"></p>
24 </body>
25 </html>
26 HTML
27 assert doc.find(:tag=>"img", :attributes=>{"src"=>"hello.gif"})
28 end
29
30 def test_find_all
31 doc = HTML::Document.new <<-HTML.strip
32 <html>
33 <body>
34 <p class="test"><img src="hello.gif"></p>
35 <div class="foo">
36 <p class="test">something</p>
37 <p>here is <em class="test">more</em></p>
38 </div>
39 </body>
40 </html>
41 HTML
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 }
45 end
46
47 def test_find_with_text
48 doc = HTML::Document.new <<-HTML.strip
49 <html>
50 <body>
51 <p>Some text</p>
52 </body>
53 </html>
54 HTML
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")
59 end
60
61 def test_parse_xml
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) }
64 end
65
66 def test_parse_document
67 doc = HTML::Document.new(<<-HTML)
68 <div>
69 <h2>blah</h2>
70 <table>
71 </table>
72 </div>
73 HTML
74 assert_not_nil doc.find(:tag => "div", :children => { :count => 1, :only => { :tag => "table" } })
75 end
76
77 def test_tag_nesting_nothing_to_s
78 doc = HTML::Document.new("<tag></tag>")
79 assert_equal "<tag></tag>", doc.root.to_s
80 end
81
82 def test_tag_nesting_space_to_s
83 doc = HTML::Document.new("<tag> </tag>")
84 assert_equal "<tag> </tag>", doc.root.to_s
85 end
86
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
90 end
91
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
95 end
96
97 def test_parse_cdata
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">
102 <head>
103 <title><![CDATA[<br>]]></title>
104 </head>
105 <body>
106 <p>this document has &lt;br&gt; for a title</p>
107 </body>
108 </html>
109 HTML
110
111 assert_nil doc.find(:tag => "title", :descendant => { :tag => "br" })
112 assert doc.find(:tag => "title", :child => "<br>")
113 end
114
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)
122 end
123
124 def test_parse_invalid_document
125 assert_nothing_raised do
126 doc = HTML::Document.new("<html>
127 <table>
128 <tr>
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>
130 </tr>
131 </table>
132 </html>")
133 end
134 end
135
136 def test_invalid_document_raises_exception_when_strict
137 assert_raises RuntimeError do
138 doc = HTML::Document.new("<html>
139 <table>
140 <tr>
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>
142 </tr>
143 </table>
144 </html>", true)
145 end
146 end
147
148 end