diff --git a/test/html4/test_comments.rb b/test/html4/test_comments.rb index bef48b134e..5fc4d55d0e 100644 --- a/test/html4/test_comments.rb +++ b/test/html4/test_comments.rb @@ -96,7 +96,19 @@ class TestComment < Nokogiri::TestCase let(:doc) { Nokogiri::HTML4(html) } let(:subject) { doc.at_css("div#under-test") } - if Nokogiri.uses_libxml? + if Nokogiri.uses_libxml?(">= 2.14.0") + it "behaves as if the comment is closed immediately before the end of the input stream" do # COMPLIANT + assert_pattern do + subject => { + name: "div", + attributes: [{ name: "id", value: "under-test" }], + children: [ + { name: "comment", content: "start of unterminated comment" } + ] + } + end + end + elsif Nokogiri.uses_libxml? it "behaves as if the comment is unterminated and doesn't exist" do # NON-COMPLIANT assert_equal 0, subject.children.length assert_equal 1, doc.errors.length @@ -132,8 +144,12 @@ class TestComment < Nokogiri::TestCase assert_equal inner_div, subject.children[1] assert_predicate subject.children[2], :comment? assert_equal "bar", subject.children[2].content - assert_equal 1, doc.errors.length - assert_match(/Comment incorrectly closed/, doc.errors.first.to_s) + if Nokogiri.uses_libxml?(">= 2.14.0") + assert_empty doc.errors + else + assert_equal 1, doc.errors.length + assert_match(/Comment incorrectly closed/, doc.errors.first.to_s) + end end else # jruby, or libxml2 system lib less than 2.9.11 it "behaves as if the comment encompasses the inner div" do # NON-COMPLIANT @@ -161,7 +177,22 @@ class TestComment < Nokogiri::TestCase let(:body) { doc.at_css("body") } let(:subject) { doc.at_css("div#under-test") } - if Nokogiri.uses_libxml?("= 2.9.14") + if Nokogiri.uses_libxml?(">= 2.14.0") + it "parses as comments" do # COMPLIANT + assert_pattern do + body.children => [ + { + name: "div", + children: [ + { name: "comment", content: " comment
hello" }, + ] + end + end + elsif Nokogiri.uses_libxml?("= 2.9.14") it "parses as PCDATA" do # NON-COMPLIANT assert_equal 1, body.children.length assert_equal subject, body.children.first @@ -212,7 +243,21 @@ class TestComment < Nokogiri::TestCase let(:body) { doc.at_css("body") } let(:subject) { doc.at_css("div#under-test") } - if Nokogiri.uses_libxml?("= 2.9.14") + if Nokogiri.uses_libxml?(">= 2.14.0") + it "parses the [ + { + name: "div", children: [ + { name: "comment", content: "[if foo]" }, + { name: "div", attributes: [{name: "id", value: "do-i-exist"}] }, + { name: "comment", content: "[endif]" }, + ] + } + ] + end + end + elsif Nokogiri.uses_libxml?("= 2.9.14") it "parses the = 2.14.0") + [Nokogiri::XML::Node::COMMENT_NODE, Nokogiri::XML::Node::COMMENT_NODE] elsif Nokogiri.uses_libxml?(">= 2.10.0") [Nokogiri::XML::Node::COMMENT_NODE] else diff --git a/test/html4/test_document_fragment.rb b/test/html4/test_document_fragment.rb index a157ff025e..eca57ce147 100644 --- a/test/html4/test_document_fragment.rb +++ b/test/html4/test_document_fragment.rb @@ -188,7 +188,29 @@ def test_element_children_counts def test_malformed_fragment_is_corrected fragment = Nokogiri::HTML4::DocumentFragment.parse("
") - assert_equal("
", fragment.to_s) + + if Nokogiri.uses_libxml?(">= 2.14.0") + assert_pattern do + fragment => [ + { name: "div", attributes: [ + { name: "<", value: ""}, + { name: "div", value: ""}, + ]} + ] + end + else + assert_equal("
", fragment.to_s) + end + end + + def test_malformed_html5_fragment_serializes_like_gumbo + skip_unless_libxml2(">= 2.14.0") + + fragment = Nokogiri::HTML4::DocumentFragment.parse("
") + + pending "libxml2 does not serialize HTML5 like gumbo (yet)" do + assert_equal('
', fragment.to_s) + end end def test_unclosed_script_tag diff --git a/test/xml/test_node.rb b/test/xml/test_node.rb index 4a8e1fb40c..abb11161a3 100644 --- a/test/xml/test_node.rb +++ b/test/xml/test_node.rb @@ -105,9 +105,19 @@ def test_node_context_parsing_of_malformed_html_fragment context_node = doc.at_css("div") nodeset = context_node.parse("
") - assert_equal(1, doc.errors.length) - assert_equal(1, nodeset.length) - assert_equal("
", nodeset.to_s) + if Nokogiri.uses_libxml?(">= 2.14.0") + assert_empty(doc.errors) + assert_pattern do + nodeset => [ + { name: "div", attributes: [{name: "<", value: ""}, { name: "div", value: ""}] }, + ] + end + else + assert_equal(1, doc.errors.length) + assert_equal(1, nodeset.length) + assert_equal("
", nodeset.to_s) + end + assert_instance_of(Nokogiri::HTML4::Document, nodeset.document) assert_instance_of(Nokogiri::HTML4::Document, nodeset.first.document) end @@ -117,14 +127,25 @@ def test_node_context_parsing_of_malformed_html_fragment_with_recover_is_correct context_node = doc.at_css("div") nodeset = context_node.parse("
", &:recover) - assert_equal(1, doc.errors.length) - assert_equal(1, nodeset.length) - assert_equal("
", nodeset.to_s) + if Nokogiri.uses_libxml?(">= 2.14.0") + assert_empty(doc.errors) + assert_pattern do + nodeset => [ + { name: "div", attributes: [{name: "<", value: ""}, { name: "div", value: ""}] }, + ] + end + else + assert_equal(1, doc.errors.length) + assert_equal(1, nodeset.length) + assert_equal("
", nodeset.to_s) + end assert_instance_of(Nokogiri::HTML4::Document, nodeset.document) assert_instance_of(Nokogiri::HTML4::Document, nodeset.first.document) end def test_node_context_parsing_of_malformed_html_fragment_without_recover_is_not_corrected + skip("libxml2 2.14.0 no longer raises this error") if Nokogiri.uses_libxml?(">= 2.14.0") + doc = HTML4.parse("
") context_node = doc.at_css("div") assert_raises(Nokogiri::XML::SyntaxError) do