Lots going on
[lyrix.git] / vendor / rails / actionpack / test / controller / html-scanner / document_test.rb
bloba6ba70dde1d37ed1bfe5e3616e4f315e18521631
1 require File.dirname(__FILE__) + '/../../abstract_unit'
2 require 'test/unit'
4 class DocumentTest < Test::Unit::TestCase
5   def test_handle_doctype
6     doc = nil
7     assert_nothing_raised do
8       doc = HTML::Document.new <<-HTML.strip
9         <!DOCTYPE "blah" "blah" "blah">
10         <html>
11         </html>
12       HTML
13     end
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
18   end
19   
20   def test_find_img
21     doc = HTML::Document.new <<-HTML.strip
22       <html>
23         <body>
24           <p><img src="hello.gif"></p>
25         </body>
26       </html>
27     HTML
28     assert doc.find(:tag=>"img", :attributes=>{"src"=>"hello.gif"})
29   end
31   def test_find_all
32     doc = HTML::Document.new <<-HTML.strip
33       <html>
34         <body>
35           <p class="test"><img src="hello.gif"></p>
36           <div class="foo">
37             <p class="test">something</p>
38             <p>here is <em class="test">more</em></p>
39           </div>
40         </body>
41       </html>
42     HTML
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 }
46   end
48   def test_find_with_text
49     doc = HTML::Document.new <<-HTML.strip
50       <html>
51         <body>
52           <p>Some text</p>
53         </body>
54       </html>
55     HTML
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")
60   end
62   def test_parse_xml
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) }
65   end
67   def test_parse_document
68     doc = HTML::Document.new(<<-HTML)
69       <div>
70         <h2>blah</h2>
71         <table>
72         </table>
73       </div>
74     HTML
75     assert_not_nil doc.find(:tag => "div", :children => { :count => 1, :only => { :tag => "table" } })
76   end
78   def test_parse_cdata
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">
83   <head>
84     <title><![CDATA[<br>]]></title>
85    </head>
86   <body>
87     <p>this document has &lt;br&gt; for a title</p>
88   </body>
89 </html>
90 HTML
92     assert_nil doc.find(:tag => "title", :descendant => { :tag => "br" })
93     assert doc.find(:tag => "title", :child => "<br>")
94   end
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)
103   end