fix Node.replaceChild when of new child equals old

This commit is contained in:
Pierre Tachoire
2026-01-05 21:48:59 +01:00
parent dcde19de3c
commit f81a9b54a7
2 changed files with 18 additions and 0 deletions

View File

@@ -37,4 +37,7 @@
testing.expectEqual(null, c2.parentNode); testing.expectEqual(null, c2.parentNode);
assertChildren([c3, c4], d1) assertChildren([c3, c4], d1)
assertChildren([], d2) assertChildren([], d2)
testing.expectEqual(c3, d1.replaceChild(c3, c3));
assertChildren([c3, c4], d1)
</script> </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); 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); _ = try self.insertBefore(new_child, old_child, page);
page.removeNode(self, old_child, .{ .will_be_reconnected = false }); page.removeNode(self, old_child, .{ .will_be_reconnected = false });
return old_child; return old_child;