Skip to content

Commit

Permalink
Allow reparenting nodes to be a child of an empty document.
Browse files Browse the repository at this point in the history
Fixes #1773
  • Loading branch information
jvshahid committed Jul 8, 2018
1 parent 254f341 commit 24536fe
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 0 deletions.
4 changes: 4 additions & 0 deletions ext/java/nokogiri/XmlNode.java
Expand Up @@ -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()) {
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

0 comments on commit 24536fe

Please sign in to comment.