Skip to content

Commit

Permalink
Merge branch 'fix-1773'
Browse files Browse the repository at this point in the history
  • Loading branch information
jvshahid committed Sep 17, 2018
2 parents 7b8cd0f + 7cc6cf6 commit 7feb4c1
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 8 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
## Bug fixes

* [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]
* [JRuby] Fix node reparenting when the destination doc is empty. [#1773]


# 1.8.4 / 2018-07-03
Expand Down
29 changes: 21 additions & 8 deletions ext/java/nokogiri/XmlNode.java
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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 7feb4c1

Please sign in to comment.