From ca200c557fb7067fcca24266abfa8ac0346eaba6 Mon Sep 17 00:00:00 2001 From: Derek Abdine Date: Wed, 8 Dec 2021 02:12:02 +0000 Subject: [PATCH 1/5] Add tests to cover line numbering edge cases with JRuby --- test/xml/test_node.rb | 48 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) diff --git a/test/xml/test_node.rb b/test/xml/test_node.rb index 94302c766d..0686dae32d 100644 --- a/test/xml/test_node.rb +++ b/test/xml/test_node.rb @@ -1262,6 +1262,54 @@ def test_wrap end describe "#line" do + it "properly numbers lines with documents containing XML prolog" do + xml = Nokogiri::XML(<<~eoxml) + + + + Test + + + eoxml + + set = xml.search("//b") + assert_equal(3, set[0].line) + end + + it "properly numbers lines with documents containing XML comments" do + xml = Nokogiri::XML(<<~eoxml) + + + + + Test + + + + eoxml + + set = xml.search("//c") + assert_equal(4, set[0].line) + end + + it "properly numbers lines with documents containing XML multiline comments" do + xml = Nokogiri::XML(<<~eoxml) + + + + + Test + + + + eoxml + + set = xml.search("//c") + assert_equal(6, set[0].line) + end + it "returns a sensible line number for each node" do xml = Nokogiri::XML(<<~eoxml) From 469163eec66381ac647e7366957ef48cf23bf4e3 Mon Sep 17 00:00:00 2001 From: Mike Dalessio Date: Thu, 16 Dec 2021 17:55:07 -0500 Subject: [PATCH 2/5] test: mark the jruby tests from #2379 as pending See context in #2380. The fix on JRuby is expensive and is probably not worth the investment of developer time right now. But let's preserve those failures as a known issue by introducing the `pending` and `pending_if` test helpers. --- test/helper.rb | 14 ++++++++++++++ test/xml/test_node.rb | 8 ++++++-- 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/test/helper.rb b/test/helper.rb index 224525200b..0ec60b86c4 100644 --- a/test/helper.rb +++ b/test/helper.rb @@ -196,6 +196,20 @@ def assert_not_send(send_ary, m = nil) refute(recv.__send__(msg, *args), m) end unless method_defined?(:assert_not_send) + def pending(msg) + begin + yield + rescue MiniTest::Assertion + skip("pending #{msg}") + end + flunk("pending test unexpectedly passed: #{msg}") + end + + def pending_if(msg, pend_eh, &block) + return yield unless pend_eh + pending(msg, &block) + end + def i_am_ruby_matching(gem_version_requirement_string) Gem::Requirement.new(gem_version_requirement_string).satisfied_by?(Gem::Version.new(RUBY_VERSION)) end diff --git a/test/xml/test_node.rb b/test/xml/test_node.rb index 0686dae32d..bbf5390aa1 100644 --- a/test/xml/test_node.rb +++ b/test/xml/test_node.rb @@ -1273,7 +1273,9 @@ def test_wrap eoxml set = xml.search("//b") - assert_equal(3, set[0].line) + pending_if("https://github.com/sparklemotion/nokogiri/issues/2380", Nokogiri.jruby?) do + assert_equal(3, set[0].line) + end end it "properly numbers lines with documents containing XML comments" do @@ -1307,7 +1309,9 @@ def test_wrap eoxml set = xml.search("//c") - assert_equal(6, set[0].line) + pending_if("https://github.com/sparklemotion/nokogiri/issues/2380", Nokogiri.jruby?) do + assert_equal(6, set[0].line) + end end it "returns a sensible line number for each node" do From d08025cce8805a49dca13abf3a573e7a3462b055 Mon Sep 17 00:00:00 2001 From: Mike Dalessio Date: Thu, 16 Dec 2021 20:49:57 -0500 Subject: [PATCH 3/5] fix: jruby Node#line counts multiline comments correctly --- ext/java/nokogiri/XmlNode.java | 8 +++++++- test/xml/test_node.rb | 4 +--- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/ext/java/nokogiri/XmlNode.java b/ext/java/nokogiri/XmlNode.java index 9475e4c561..234f8847d3 100644 --- a/ext/java/nokogiri/XmlNode.java +++ b/ext/java/nokogiri/XmlNode.java @@ -36,6 +36,7 @@ import org.w3c.dom.Node; import org.w3c.dom.NodeList; import org.w3c.dom.Text; +import org.w3c.dom.Comment; import nokogiri.internals.HtmlDomParserContext; import nokogiri.internals.NokogiriHelpers; @@ -1599,9 +1600,14 @@ public class XmlNode extends RubyObject NodeList list = node.getChildNodes(); for (int jchild = 0; jchild < list.getLength(); jchild++) { Node child = list.item(jchild); + String text = null; if (child instanceof Text) { - String text = ((Text)child).getData(); + text = ((Text)child).getData(); + } else if (child instanceof Comment) { + text = ((Comment)child).getData(); + } + if (text != null) { int textLength = text.length(); for (int jchar = 0; jchar < textLength; jchar++) { if (text.charAt(jchar) == '\n') { diff --git a/test/xml/test_node.rb b/test/xml/test_node.rb index bbf5390aa1..7c9e3dbb2e 100644 --- a/test/xml/test_node.rb +++ b/test/xml/test_node.rb @@ -1309,9 +1309,7 @@ def test_wrap eoxml set = xml.search("//c") - pending_if("https://github.com/sparklemotion/nokogiri/issues/2380", Nokogiri.jruby?) do - assert_equal(6, set[0].line) - end + assert_equal(6, set[0].line) end it "returns a sensible line number for each node" do From d08f118333f923f78a01544769557ff3045d81af Mon Sep 17 00:00:00 2001 From: Mike Dalessio Date: Fri, 17 Dec 2021 09:07:00 -0500 Subject: [PATCH 4/5] style: clean up xml/test_node.rb --- test/xml/test_node.rb | 125 ++++++++++++++++++++---------------------- 1 file changed, 59 insertions(+), 66 deletions(-) diff --git a/test/xml/test_node.rb b/test/xml/test_node.rb index 7c9e3dbb2e..24e9947e79 100644 --- a/test/xml/test_node.rb +++ b/test/xml/test_node.rb @@ -299,11 +299,11 @@ def test_append_with_attr end def test_inspect_ns - xml = Nokogiri::XML(<<~eoxml, &:noblanks) + xml = Nokogiri::XML(<<~XML, &:noblanks) - eoxml + XML ins = xml.inspect xml.traverse do |node| @@ -323,30 +323,30 @@ def test_inspect_ns end def test_namespace_definitions_when_some_exist - xml = Nokogiri::XML(<<~eoxml) + xml = Nokogiri::XML(<<~XML) - eoxml + XML namespace_definitions = xml.root.namespace_definitions assert_equal(2, namespace_definitions.length) end def test_namespace_definitions_when_no_exist - xml = Nokogiri::XML(<<~eoxml) + xml = Nokogiri::XML(<<~XML) - eoxml + XML namespace_definitions = xml.at_xpath("//xmlns:awesome").namespace_definitions assert_equal(0, namespace_definitions.length) end def test_default_namespace_goes_to_children - fruits = Nokogiri::XML(<<~eoxml) + fruits = Nokogiri::XML(<<~XML) - eoxml + XML apple = Nokogiri::XML::Node.new("Apple", fruits) orange = Nokogiri::XML::Node.new("Orange", fruits) apple << orange @@ -356,11 +356,11 @@ def test_default_namespace_goes_to_children end def test_parent_namespace_is_not_inherited - fruits = Nokogiri::XML(<<-eoxml) + fruits = Nokogiri::XML(<<-XML) - eoxml + XML apple = fruits.at_xpath("//fruit:apple", { "fruit" => "http://fruits.org" }) assert(apple) @@ -412,11 +412,11 @@ def test_different_document_compare end def test_duplicate_node_removes_namespace - fruits = Nokogiri::XML(<<~eoxml) + fruits = Nokogiri::XML(<<~XML) - eoxml + XML apple = fruits.root.xpath("fruit:Apple", { "fruit" => "www.fruits.org" })[0] new_apple = apple.dup fruits.root << new_apple @@ -450,10 +450,10 @@ def test_fragment_creates_elements end def test_node_added_to_root_should_get_namespace - fruits = Nokogiri::XML(<<~eoxml) + fruits = Nokogiri::XML(<<~XML) - eoxml + XML apple = fruits.fragment("") fruits.root << apple assert_equal(1, fruits.xpath("//xmlns:Apple").length) @@ -466,15 +466,15 @@ def test_new_node_can_have_ancestors end def test_children - doc = Nokogiri::XML(<<~eoxml) + doc = Nokogiri::XML(<<~XML) #{"" * 9} - eoxml + XML assert_equal(9, doc.root.children.length) assert_equal(9, doc.root.children.to_a.length) - doc = Nokogiri::XML(<<~eoxml) + doc = Nokogiri::XML(<<~XML) #{"" * 15} - eoxml + XML assert_equal(15, doc.root.children.length) assert_equal(15, doc.root.children.to_a.length) end @@ -658,13 +658,13 @@ def test_serialize_with_block end def test_hold_refence_to_subnode - doc = Nokogiri::XML(<<~eoxml) + doc = Nokogiri::XML(<<~XML) - eoxml + XML assert(node_a = doc.css("a").first) assert(node_b = node_a.css("b").first) node_a.unlink @@ -730,7 +730,7 @@ def test_find_by_css_class_with_nonstandard_whitespace end def test_find_by_css_with_tilde_eql - xml = Nokogiri::XML.parse(<<~eoxml) + xml = Nokogiri::XML.parse(<<~XML) Hello world Bar @@ -740,14 +740,14 @@ def test_find_by_css_with_tilde_eql Awesome Awesome - eoxml + XML set = xml.css('a[@class~="bar"]') assert_equal(4, set.length) assert_equal(["Bar"], set.map(&:content).uniq) end def test_unlink - xml = Nokogiri::XML.parse(<<~eoxml) + xml = Nokogiri::XML.parse(<<~XML) Bar Bar @@ -757,7 +757,7 @@ def test_unlink Awesome Awesome - eoxml + XML node = xml.xpath("//a")[3] assert_equal("Hello world", node.text) assert_match(/Hello world/, xml.to_s) @@ -921,7 +921,7 @@ def test_node_equality end def test_namespace_search_with_xpath_and_hash - xml = Nokogiri::XML.parse(<<~eoxml) + xml = Nokogiri::XML.parse(<<~XML) Michelin Model XGV @@ -930,14 +930,14 @@ def test_namespace_search_with_xpath_and_hash I'm a bicycle tire! - eoxml + XML tires = xml.xpath("//bike:tire", { "bike" => "http://schwinn.com/" }) assert_equal(1, tires.length) end def test_namespace_search_with_xpath_and_hash_with_symbol_keys - xml = Nokogiri::XML.parse(<<~eoxml) + xml = Nokogiri::XML.parse(<<~XML) Michelin Model XGV @@ -946,14 +946,14 @@ def test_namespace_search_with_xpath_and_hash_with_symbol_keys I'm a bicycle tire! - eoxml + XML tires = xml.xpath("//bike:tire", bike: "http://schwinn.com/") assert_equal(1, tires.length) end def test_namespace_search_with_css - xml = Nokogiri::XML.parse(<<~eoxml) + xml = Nokogiri::XML.parse(<<~XML) Michelin Model XGV @@ -962,7 +962,7 @@ def test_namespace_search_with_css I'm a bicycle tire! - eoxml + XML tires = xml.css("bike|tire", "bike" => "http://schwinn.com/") assert_equal(1, tires.length) @@ -970,13 +970,13 @@ def test_namespace_search_with_css def test_namespaced_attribute_search_with_xpath # from #593 - xml_content = <<~EOXML + xml_content = <<~XML with namespace no namespace - EOXML + XML xml_doc = Nokogiri::XML(xml_content) no_ns = xml_doc.xpath("//*[@att]") @@ -990,13 +990,13 @@ def test_namespaced_attribute_search_with_xpath def test_namespaced_attribute_search_with_css # from #593 - xml_content = <<~EOXML + xml_content = <<~XML with namespace no namespace - EOXML + XML xml_doc = Nokogiri::XML(xml_content) no_ns = xml_doc.css("*[att]") @@ -1009,14 +1009,14 @@ def test_namespaced_attribute_search_with_css end def test_namespaces_should_include_all_namespace_definitions - xml = Nokogiri::XML.parse(<<~EOF) + xml = Nokogiri::XML.parse(<<~XML) hello - EOF + XML namespaces = xml.namespaces # Document#namespace assert_equal({ "xmlns" => "http://quux.com/", @@ -1048,7 +1048,7 @@ def test_namespaces_should_include_all_namespace_definitions end def test_namespace - xml = Nokogiri::XML.parse(<<~EOF) + xml = Nokogiri::XML.parse(<<~XML) hello a @@ -1058,7 +1058,7 @@ def test_namespace
hello moon
- EOF + XML set = xml.search("//y/*") assert_equal("a", set[0].namespace.prefix) assert_equal("http://foo.com/", set[0].namespace.href) @@ -1082,9 +1082,9 @@ def test_namespace_without_an_href_on_html_node # describe how we handle microsoft word's HTML formatting. # this test is descriptive, not prescriptive. # - html = Nokogiri::HTML.parse(<<~EOF) + html = Nokogiri::HTML.parse(<<~XML)
foo
- EOF + XML node = html.at("div").children.first refute_nil(node) @@ -1171,11 +1171,11 @@ def test_namespace_in_rendered_xml # issue 771 def test_format_noblank - content = <<~eoxml + content = <<~XML hello - eoxml + XML subject = Nokogiri::XML(content) do |conf| conf.default_xml.noblanks end @@ -1212,7 +1212,7 @@ def test_processing_instruction_eh end def test_node_lang - document = Nokogiri::XML(<<~EOXML) + document = Nokogiri::XML(<<~XML)
foo
@@ -1220,7 +1220,7 @@ def test_node_lang
bar
bar
- EOXML + XML assert_equal("en", document.at_css(".english").lang) assert_equal("en", document.at_css(".english_child").lang) assert_equal("jp", document.at_css(".japanese").lang) @@ -1263,39 +1263,33 @@ def test_wrap describe "#line" do it "properly numbers lines with documents containing XML prolog" do - xml = Nokogiri::XML(<<~eoxml) + xml = Nokogiri::XML(<<~XML)
- - Test - - - eoxml + Test + + XML - set = xml.search("//b") pending_if("https://github.com/sparklemotion/nokogiri/issues/2380", Nokogiri.jruby?) do - assert_equal(3, set[0].line) + assert_equal(3, xml.at_css("b").line) end end it "properly numbers lines with documents containing XML comments" do - xml = Nokogiri::XML(<<~eoxml) + xml = Nokogiri::XML(<<~XML) - - Test - + Test - eoxml + XML - set = xml.search("//c") - assert_equal(4, set[0].line) + assert_equal(4, xml.at_css("c").line) end it "properly numbers lines with documents containing XML multiline comments" do - xml = Nokogiri::XML(<<~eoxml) + xml = Nokogiri::XML(<<~XML)