Skip to content

Commit 1697442

Browse files
committed
Allow reparenting nodes to be a child of an empty document.
Fixes #1773
1 parent 254f341 commit 1697442

2 files changed

Lines changed: 15 additions & 0 deletions

File tree

ext/java/nokogiri/XmlNode.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1542,6 +1542,10 @@ protected IRubyObject adoptAs(ThreadContext context, AdoptScheme scheme,
15421542
try {
15431543
Document prev = otherNode.getOwnerDocument();
15441544
Document doc = thisNode.getOwnerDocument();
1545+
if (doc == null && thisNode instanceof Document) {
1546+
// we are adding the new node to a new empty document
1547+
doc = (Document) thisNode;
1548+
}
15451549
clearXpathContext(prev);
15461550
clearXpathContext(doc);
15471551
if (doc != null && doc != otherNode.getOwnerDocument()) {

test/xml/test_node_reparenting.rb

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,17 @@ class TestNodeReparenting < Nokogiri::TestCase
197197
end
198198
end
199199

200+
describe "given the new document is empty" do
201+
it "adds the node to the new document" do
202+
doc1 = Nokogiri::XML.parse("<value>3</value>")
203+
doc2 = Nokogiri::XML::Document.new
204+
node = doc1.at_xpath("//value")
205+
node.remove
206+
doc2.add_child(node)
207+
assert_match /<value>3<\/value>/, doc2.to_xml
208+
end
209+
end
210+
200211
describe "given a parent node with a default namespace" do
201212
before do
202213
@doc = Nokogiri::XML(<<-eoxml)

0 commit comments

Comments
 (0)