From 80205c49f9c10ddc78488352c4e98d5409a71547 Mon Sep 17 00:00:00 2001 From: Mike Dalessio Date: Wed, 21 Mar 2018 00:03:17 -0400 Subject: [PATCH] [JRuby] Node#replace supports Comment and CDATA Note that I deleted the offending line; this is because the calling method `adoptAs` always calls `relink_namespace`. I've also deleted the unused `post_add_child` function. --- CHANGELOG.md | 1 + ext/java/nokogiri/XmlNode.java | 7 ------- test/xml/test_node_reparenting.rb | 18 ++++++++++++++++++ 3 files changed, 19 insertions(+), 7 deletions(-) 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)