Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: do not dup text siblings in reparent_node_with(xmlAddChild) #2918

Merged
merged 1 commit into from
Jul 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ Nokogiri follows [Semantic Versioning](https://semver.org/), please see the [REA

### Fixed

* [CRuby] Replacing a node's children via methods like `Node#inner_html=`, `#children=`, and `#replace` no longer defensively dups the node's next sibling if it is a Text node. This behavior was originally adopted to work around libxml2's memory management (see [#283](https://github.com/sparklemotion/nokogiri/issues/283) and [#595](https://github.com/sparklemotion/nokogiri/issues/595)) but should not have included operations involving `xmlAddChild()`. [[#2916](https://github.com/sparklemotion/nokogiri/issues/2916)]
* [JRuby] Fixed NPE when serializing an unparented HTML node. [[#2559](https://github.com/sparklemotion/nokogiri/issues/2559), [#2895](https://github.com/sparklemotion/nokogiri/issues/2895)] (Thanks, [@cbasguti](https://github.com/cbasguti)!)


Expand Down
2 changes: 1 addition & 1 deletion ext/nokogiri/xml_node.c
Original file line number Diff line number Diff line change
Expand Up @@ -350,7 +350,7 @@ reparent_node_with(VALUE pivot_obj, VALUE reparentee_obj, pivot_reparentee_func

xmlUnlinkNode(original_reparentee);

if (prf != xmlAddPrevSibling && prf != xmlAddNextSibling
if (prf != xmlAddPrevSibling && prf != xmlAddNextSibling && prf != xmlAddChild
&& reparentee->type == XML_TEXT_NODE && pivot->next && pivot->next->type == XML_TEXT_NODE) {
/*
* libxml merges text nodes in a right-to-left fashion, meaning that if
Expand Down
19 changes: 19 additions & 0 deletions test/xml/test_node_reparenting.rb
Original file line number Diff line number Diff line change
Expand Up @@ -673,7 +673,9 @@ def coerce(data)
it "should not merge text nodes during the operation" do
xml = Nokogiri::XML(%(<root>text node</root>))
replacee = xml.root.children.first

replacee.replace("new text node")

assert_equal "new text node", xml.root.children.first.content
end
end
Expand All @@ -682,7 +684,9 @@ def coerce(data)
doc = Nokogiri::XML(%{<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
Expand All @@ -691,11 +695,26 @@ def coerce(data)
doc = Nokogiri::XML(%{<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

it "replacing a child should not dup sibling text nodes" do
# https://github.com/sparklemotion/nokogiri/issues/2916
xml = "<root><parent>asdf</parent>qwer</root>"
doc = Nokogiri::XML.parse(xml)
nodes = doc.root.children
parent = nodes.first
sibling = parent.next

parent.inner_html = "foo"

assert_same(sibling, parent.next)
end

describe "when a document has a default namespace" do
before do
@fruits = Nokogiri::XML(<<~eoxml)
Expand Down