Skip to content

Commit 7feb4c1

Browse files
committed
Merge branch 'fix-1773'
2 parents 7b8cd0f + 7cc6cf6 commit 7feb4c1

File tree

3 files changed

+33
-8
lines changed

3 files changed

+33
-8
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
## Bug fixes
99

1010
* [MRI] Fix regression in installation when building against system libraries, where some systems would not be able to find libxml2 or libxslt when present. (Regression introduced in v1.8.3.) [#1722]
11+
* [JRuby] Fix node reparenting when the destination doc is empty. [#1773]
1112

1213

1314
# 1.8.4 / 2018-07-03

ext/java/nokogiri/XmlNode.java

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,16 @@
3333
package nokogiri;
3434

3535
import static java.lang.Math.max;
36-
import static nokogiri.internals.NokogiriHelpers.*;
36+
import static nokogiri.internals.NokogiriHelpers.clearXpathContext;
37+
import static nokogiri.internals.NokogiriHelpers.convertEncoding;
38+
import static nokogiri.internals.NokogiriHelpers.convertString;
39+
import static nokogiri.internals.NokogiriHelpers.getCachedNodeOrCreate;
40+
import static nokogiri.internals.NokogiriHelpers.getNokogiriClass;
41+
import static nokogiri.internals.NokogiriHelpers.isBlank;
42+
import static nokogiri.internals.NokogiriHelpers.nodeArrayToRubyArray;
43+
import static nokogiri.internals.NokogiriHelpers.nonEmptyStringOrNil;
44+
import static nokogiri.internals.NokogiriHelpers.rubyStringToString;
45+
import static nokogiri.internals.NokogiriHelpers.stringOrNil;
3746

3847
import java.io.ByteArrayInputStream;
3948
import java.io.InputStream;
@@ -43,18 +52,12 @@
4352
import java.util.Iterator;
4453
import java.util.List;
4554

46-
import nokogiri.internals.HtmlDomParserContext;
47-
import nokogiri.internals.NokogiriHelpers;
48-
import nokogiri.internals.NokogiriNamespaceCache;
49-
import nokogiri.internals.SaveContextVisitor;
50-
import nokogiri.internals.XmlDomParserContext;
51-
5255
import org.apache.xerces.dom.CoreDocumentImpl;
5356
import org.jruby.Ruby;
5457
import org.jruby.RubyArray;
5558
import org.jruby.RubyClass;
56-
import org.jruby.RubyInteger;
5759
import org.jruby.RubyFixnum;
60+
import org.jruby.RubyInteger;
5861
import org.jruby.RubyModule;
5962
import org.jruby.RubyObject;
6063
import org.jruby.RubyString;
@@ -76,6 +79,12 @@
7679
import org.w3c.dom.NodeList;
7780
import org.w3c.dom.Text;
7881

82+
import nokogiri.internals.HtmlDomParserContext;
83+
import nokogiri.internals.NokogiriHelpers;
84+
import nokogiri.internals.NokogiriNamespaceCache;
85+
import nokogiri.internals.SaveContextVisitor;
86+
import nokogiri.internals.XmlDomParserContext;
87+
7988
/**
8089
* Class for Nokogiri::XML::Node
8190
*
@@ -1542,6 +1551,10 @@ protected IRubyObject adoptAs(ThreadContext context, AdoptScheme scheme,
15421551
try {
15431552
Document prev = otherNode.getOwnerDocument();
15441553
Document doc = thisNode.getOwnerDocument();
1554+
if (doc == null && thisNode instanceof Document) {
1555+
// we are adding the new node to a new empty document
1556+
doc = (Document) thisNode;
1557+
}
15451558
clearXpathContext(prev);
15461559
clearXpathContext(doc);
15471560
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)