Skip to content

Commit

Permalink
[JRuby] Node#replace supports Comment and CDATA
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
flavorjones committed Mar 21, 2018
1 parent f9b71cb commit 80205c4
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 7 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 0 additions & 7 deletions ext/java/nokogiri/XmlNode.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -1591,7 +1588,6 @@ protected IRubyObject adoptAs(ThreadContext context, AdoptScheme scheme,
}

other.relink_namespace(context);
// post_add_child(context, this, other);

return nodeOrTags;
}
Expand Down Expand Up @@ -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());
Expand Down
18 changes: 18 additions & 0 deletions test/xml/test_node_reparenting.rb
Original file line number Diff line number Diff line change
Expand Up @@ -459,6 +459,24 @@ class TestNodeReparenting < Nokogiri::TestCase
end
end

it "can replace with a comment node" do
doc = Nokogiri::XML %Q{<parent><child>text}
replacee = doc.at_css("child")
replacer = doc.create_comment("<b>text</b>")
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{<parent><child>text}
replacee = doc.at_css("child")
replacer = doc.create_cdata("<b>text</b>")
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)
Expand Down

0 comments on commit 80205c4

Please sign in to comment.