require 'abstract_unit'
require 'action_view/vendor/html-scanner'
class DocumentTest < ActiveSupport::TestCase
def test_handle_doctype
doc = nil
assert_nothing_raised do
doc = HTML::Document.new <<-HTML.strip
HTML
end
assert_equal 3, doc.root.children.length
assert_equal %{}, doc.root.children[0].content
assert_match %r{\s+}m, doc.root.children[1].content
assert_equal "html", doc.root.children[2].name
end
def test_find_img
doc = HTML::Document.new <<-HTML.strip
HTML
assert doc.find(:tag=>"img", :attributes=>{"src"=>"hello.gif"})
end
def test_find_all
doc = HTML::Document.new <<-HTML.strip
HTML
all = doc.find_all :attributes => { :class => "test" }
assert_equal 3, all.length
assert_equal [ "p", "p", "em" ], all.map { |n| n.name }
end
def test_find_with_text
doc = HTML::Document.new <<-HTML.strip
Some text
HTML
assert doc.find(:content => "Some text")
assert doc.find(:tag => "p", :child => { :content => "Some text" })
assert doc.find(:tag => "p", :child => "Some text")
assert doc.find(:tag => "p", :content => "Some text")
end
def test_parse_xml
assert_nothing_raised { HTML::Document.new("", true, true) }
assert_nothing_raised { HTML::Document.new("something", true, true) }
end
def test_parse_document
doc = HTML::Document.new(<<-HTML)
HTML
assert_not_nil doc.find(:tag => "div", :children => { :count => 1, :only => { :tag => "table" } })
end
def test_tag_nesting_nothing_to_s
doc = HTML::Document.new("")
assert_equal "", doc.root.to_s
end
def test_tag_nesting_space_to_s
doc = HTML::Document.new(" ")
assert_equal " ", doc.root.to_s
end
def test_tag_nesting_text_to_s
doc = HTML::Document.new("text")
assert_equal "text", doc.root.to_s
end
def test_tag_nesting_tag_to_s
doc = HTML::Document.new("")
assert_equal "", doc.root.to_s
end
def test_parse_cdata
doc = HTML::Document.new(<<-HTML)
]]>
this document has <br> for a title
HTML
assert_nil doc.find(:tag => "title", :descendant => { :tag => "br" })
assert doc.find(:tag => "title", :child => "
")
end
def test_find_empty_tag
doc = HTML::Document.new("")
assert_nil doc.find(:tag => "div", :attributes => { :id => "map" }, :content => /./)
assert doc.find(:tag => "div", :attributes => { :id => "map" }, :content => /\A\Z/)
assert doc.find(:tag => "div", :attributes => { :id => "map" }, :content => /^$/)
assert doc.find(:tag => "div", :attributes => { :id => "map" }, :content => "")
assert doc.find(:tag => "div", :attributes => { :id => "map" }, :content => nil)
end
def test_parse_invalid_document
assert_nothing_raised do
HTML::Document.new("
")
end
end
def test_invalid_document_raises_exception_when_strict
assert_raise RuntimeError do
HTML::Document.new("
", true)
end
end
end