Merge pull request #1315 from lightpanda-io/replaceChild-itself

fix Node.replaceChild when of new child equals old
This commit is contained in:
Karl Seguin
2026-01-06 08:00:03 +08:00
committed by GitHub
2 changed files with 18 additions and 0 deletions

View File

@@ -37,4 +37,7 @@
testing.expectEqual(null, c2.parentNode);
assertChildren([c3, c4], d1)
assertChildren([], d2)
testing.expectEqual(c3, d1.replaceChild(c3, c3));
assertChildren([c3, c4], d1)
</script>

View File

@@ -500,6 +500,21 @@ pub fn replaceChild(self: *Node, new_child: *Node, old_child: *Node, page: *Page
try validateNodeInsertion(self, new_child);
// special case: we replace a node by itself
if (new_child == old_child) {
page.domChanged();
if (page.hasMutationObservers()) {
const parent = new_child._parent.?;
const previous_sibling = new_child.previousSibling();
const next_sibling = new_child.nextSibling();
const replaced = [_]*Node{new_child};
page.childListChange(parent, &replaced, &replaced, previous_sibling, next_sibling);
}
return old_child;
}
_ = try self.insertBefore(new_child, old_child, page);
page.removeNode(self, old_child, .{ .will_be_reconnected = false });
return old_child;