# -*- coding: utf-8 -*- require "helper" module Nokogiri module HTML class TestDocumentFragment < Nokogiri::TestCase def setup super @html = Nokogiri::HTML.parse(File.read(HTML_FILE), HTML_FILE) end def test_no_contextual_parsing_on_unlinked_nodes node = @html.css('body').first node.unlink assert_raises(RuntimeError) do node.parse('
') end end def test_parse_in_context assert_equal('
', @html.root.parse('
').to_s) end def test_ancestors_search html = %q{
} fragment = Nokogiri::HTML.fragment html li = fragment.at('li') assert li.matches?('li') end def test_fun_encoding string = %Q(こんにちは) html = Nokogiri::HTML::DocumentFragment.parse( string ).to_html(:encoding => 'UTF-8') assert_equal string, html end def test_new fragment = Nokogiri::HTML::DocumentFragment.new(@html) end def test_fragment_should_have_document fragment = Nokogiri::HTML::DocumentFragment.new(@html) assert_equal @html, fragment.document end def test_empty_fragment_should_be_searchable_by_css fragment = Nokogiri::HTML.fragment("") assert_equal 0, fragment.css("a").size end def test_empty_fragment_should_be_searchable fragment = Nokogiri::HTML.fragment("") assert_equal 0, fragment.search("//a").size end def test_name fragment = Nokogiri::HTML::DocumentFragment.new(@html) assert_equal '#document-fragment', fragment.name end def test_static_method fragment = Nokogiri::HTML::DocumentFragment.parse("
a
") assert_instance_of Nokogiri::HTML::DocumentFragment, fragment end def test_many_fragments 100.times { Nokogiri::HTML::DocumentFragment.new(@html) } end def test_subclass klass = Class.new(Nokogiri::HTML::DocumentFragment) fragment = klass.new(@html, "
a
") assert_instance_of klass, fragment end def test_subclass_parse klass = Class.new(Nokogiri::HTML::DocumentFragment) doc = klass.parse("
a
") assert_instance_of klass, doc end def test_html_fragment fragment = Nokogiri::HTML.fragment("
a
") assert_equal "
a
", fragment.to_s end def test_html_fragment_has_outer_text doc = "a
b
c" fragment = Nokogiri::HTML::Document.new.fragment(doc) if Nokogiri::VERSION_INFO['libxml']['loaded'] <= "2.6.16" assert_equal "a
b

c

", fragment.to_s else assert_equal "a
b
c", fragment.to_s end end def test_html_fragment_case_insensitivity doc = "
b
" fragment = Nokogiri::HTML::Document.new.fragment(doc) assert_equal "
b
", fragment.to_s end def test_html_fragment_with_leading_whitespace doc = "
b
" fragment = Nokogiri::HTML::Document.new.fragment(doc) assert_equal "
b
", fragment.to_s end def test_html_fragment_with_leading_whitespace_and_newline doc = " \n
b
" fragment = Nokogiri::HTML::Document.new.fragment(doc) assert_equal "
b
", fragment.to_s end def test_html_fragment_with_leading_text_and_newline fragment = HTML::Document.new.fragment("First line\nSecond line
Broken line") assert_equal fragment.to_s, "First line\nSecond line
Broken line" end def test_html_fragment_with_leading_whitespace_and_text_and_newline fragment = HTML::Document.new.fragment(" First line\nSecond line
Broken line") assert_equal "First line\nSecond line
Broken line", fragment.to_s end def test_html_fragment_with_leading_entity failed = ""test
test"" fragment = Nokogiri::HTML::DocumentFragment.parse(failed) assert_equal '"test
test"', fragment.to_html end def test_to_s doc = "foo
bar" fragment = Nokogiri::HTML::Document.new.fragment(doc) assert_equal "foo
bar", fragment.to_s end def test_to_html doc = "foo
bar" fragment = Nokogiri::HTML::Document.new.fragment(doc) assert_equal "foo
bar", fragment.to_html end def test_to_xhtml doc = "foo
bar" fragment = Nokogiri::HTML::Document.new.fragment(doc) if Nokogiri::VERSION_INFO['libxml']['loaded'] >= "2.7.0" assert_equal "foo
bar", fragment.to_xhtml else assert_equal "foo
bar", fragment.to_xhtml end end def test_to_xml doc = "foo
bar" fragment = Nokogiri::HTML::Document.new.fragment(doc) assert_equal "foo
bar", fragment.to_xml end def test_fragment_script_tag_with_cdata doc = HTML::Document.new fragment = doc.fragment("") assert_equal("", fragment.to_s) end def test_fragment_with_comment doc = HTML::Document.new fragment = doc.fragment("

hello

") assert_equal("

hello

", fragment.to_s) end end end end