From 169744261c5c023dff40de0811a826ad4d1fcc05 Mon Sep 17 00:00:00 2001 From: John Shahid Date: Fri, 6 Jul 2018 15:38:47 -0400 Subject: [PATCH 1/2] Allow reparenting nodes to be a child of an empty document. Fixes #1773 --- ext/java/nokogiri/XmlNode.java | 4 ++++ test/xml/test_node_reparenting.rb | 11 +++++++++++ 2 files changed, 15 insertions(+) diff --git a/ext/java/nokogiri/XmlNode.java b/ext/java/nokogiri/XmlNode.java index 5e126817fca..24439bab9eb 100644 --- a/ext/java/nokogiri/XmlNode.java +++ b/ext/java/nokogiri/XmlNode.java @@ -1542,6 +1542,10 @@ protected IRubyObject adoptAs(ThreadContext context, AdoptScheme scheme, try { Document prev = otherNode.getOwnerDocument(); Document doc = thisNode.getOwnerDocument(); + if (doc == null && thisNode instanceof Document) { + // we are adding the new node to a new empty document + doc = (Document) thisNode; + } clearXpathContext(prev); clearXpathContext(doc); if (doc != null && doc != otherNode.getOwnerDocument()) { diff --git a/test/xml/test_node_reparenting.rb b/test/xml/test_node_reparenting.rb index 02fbe56cc52..2375035c20e 100644 --- a/test/xml/test_node_reparenting.rb +++ b/test/xml/test_node_reparenting.rb @@ -197,6 +197,17 @@ class TestNodeReparenting < Nokogiri::TestCase end end + describe "given the new document is empty" do + it "adds the node to the new document" do + doc1 = Nokogiri::XML.parse("3") + doc2 = Nokogiri::XML::Document.new + node = doc1.at_xpath("//value") + node.remove + doc2.add_child(node) + assert_match /3<\/value>/, doc2.to_xml + end + end + describe "given a parent node with a default namespace" do before do @doc = Nokogiri::XML(<<-eoxml) From 7cc6cf6a74bd718b46182f0e646b63ff0a00f728 Mon Sep 17 00:00:00 2001 From: John Shahid Date: Fri, 6 Jul 2018 15:47:50 -0400 Subject: [PATCH 2/2] Organize imports in XmlNode.java. --- ext/java/nokogiri/XmlNode.java | 25 +++++++++++++++++-------- 1 file changed, 17 insertions(+), 8 deletions(-) diff --git a/ext/java/nokogiri/XmlNode.java b/ext/java/nokogiri/XmlNode.java index 24439bab9eb..81f614f10ac 100644 --- a/ext/java/nokogiri/XmlNode.java +++ b/ext/java/nokogiri/XmlNode.java @@ -33,7 +33,16 @@ package nokogiri; import static java.lang.Math.max; -import static nokogiri.internals.NokogiriHelpers.*; +import static nokogiri.internals.NokogiriHelpers.clearXpathContext; +import static nokogiri.internals.NokogiriHelpers.convertEncoding; +import static nokogiri.internals.NokogiriHelpers.convertString; +import static nokogiri.internals.NokogiriHelpers.getCachedNodeOrCreate; +import static nokogiri.internals.NokogiriHelpers.getNokogiriClass; +import static nokogiri.internals.NokogiriHelpers.isBlank; +import static nokogiri.internals.NokogiriHelpers.nodeArrayToRubyArray; +import static nokogiri.internals.NokogiriHelpers.nonEmptyStringOrNil; +import static nokogiri.internals.NokogiriHelpers.rubyStringToString; +import static nokogiri.internals.NokogiriHelpers.stringOrNil; import java.io.ByteArrayInputStream; import java.io.InputStream; @@ -43,18 +52,12 @@ import java.util.Iterator; import java.util.List; -import nokogiri.internals.HtmlDomParserContext; -import nokogiri.internals.NokogiriHelpers; -import nokogiri.internals.NokogiriNamespaceCache; -import nokogiri.internals.SaveContextVisitor; -import nokogiri.internals.XmlDomParserContext; - import org.apache.xerces.dom.CoreDocumentImpl; import org.jruby.Ruby; import org.jruby.RubyArray; import org.jruby.RubyClass; -import org.jruby.RubyInteger; import org.jruby.RubyFixnum; +import org.jruby.RubyInteger; import org.jruby.RubyModule; import org.jruby.RubyObject; import org.jruby.RubyString; @@ -76,6 +79,12 @@ import org.w3c.dom.NodeList; import org.w3c.dom.Text; +import nokogiri.internals.HtmlDomParserContext; +import nokogiri.internals.NokogiriHelpers; +import nokogiri.internals.NokogiriNamespaceCache; +import nokogiri.internals.SaveContextVisitor; +import nokogiri.internals.XmlDomParserContext; + /** * Class for Nokogiri::XML::Node *