diff --git a/CHANGELOG.md b/CHANGELOG.md index 220233b638c..94fc3e4f017 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,7 @@ ## Bug fixes * CSS attribute selectors now gracefully handle queries using integers. [#711] +* [JRuby] Allow Node#replace to insert Comment and CDATA nodes. [#1666] # 1.8.2 / 2018-01-29 diff --git a/ext/java/nokogiri/XmlNode.java b/ext/java/nokogiri/XmlNode.java index 508da09eb86..5e126817fca 100644 --- a/ext/java/nokogiri/XmlNode.java +++ b/ext/java/nokogiri/XmlNode.java @@ -437,9 +437,6 @@ protected String getAttribute(String key) { return value.length() == 0 ? null : value; } - public void post_add_child(ThreadContext context, XmlNode current, XmlNode child) { - } - /** * This method should be called after a node has been adopted in a new * document. This method will ensure that the node is renamed with the @@ -1591,7 +1588,6 @@ protected IRubyObject adoptAs(ThreadContext context, AdoptScheme scheme, } other.relink_namespace(context); - // post_add_child(context, this, other); return nodeOrTags; } @@ -1691,9 +1687,6 @@ protected void adoptAsReplacement(ThreadContext context, try { parentNode.replaceChild(otherNode, thisNode); - if (otherNode.getNodeType() != Node.TEXT_NODE) { - NokogiriHelpers.renameNode(otherNode, thisNode.getNamespaceURI(), otherNode.getNodeName()); - } } catch (Exception e) { String prefix = "could not replace child: "; throw context.getRuntime().newRuntimeError(prefix + e.toString()); diff --git a/test/xml/test_node_reparenting.rb b/test/xml/test_node_reparenting.rb index c67f87e5ad8..02fbe56cc52 100644 --- a/test/xml/test_node_reparenting.rb +++ b/test/xml/test_node_reparenting.rb @@ -459,6 +459,24 @@ class TestNodeReparenting < Nokogiri::TestCase end end + it "can replace with a comment node" do + doc = Nokogiri::XML %Q{text} + replacee = doc.at_css("child") + replacer = doc.create_comment("text") + replacee.replace replacer + assert_equal 1, doc.root.children.length + assert_equal replacer, doc.root.children.first + end + + it "can replace with a CDATA node" do + doc = Nokogiri::XML %Q{text} + replacee = doc.at_css("child") + replacer = doc.create_cdata("text") + replacee.replace replacer + assert_equal 1, doc.root.children.length + assert_equal replacer, doc.root.children.first + end + describe "when a document has a default namespace" do before do @fruits = Nokogiri::XML(<<-eoxml)