Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow reparenting nodes to be a child of an empty document #1777

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
29 changes: 21 additions & 8 deletions ext/java/nokogiri/XmlNode.java
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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
*
Expand Down Expand Up @@ -1542,6 +1551,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()) {
Expand Down
11 changes: 11 additions & 0 deletions test/xml/test_node_reparenting.rb
Expand Up @@ -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("<value>3</value>")
doc2 = Nokogiri::XML::Document.new
node = doc1.at_xpath("//value")
node.remove
doc2.add_child(node)
assert_match /<value>3<\/value>/, doc2.to_xml
end
end

describe "given a parent node with a default namespace" do
before do
@doc = Nokogiri::XML(<<-eoxml)
Expand Down