Merge pull request #1320 from lightpanda-io/fix-replaceChildren

remove children from previous parent
This commit is contained in:
Karl Seguin
2026-01-06 19:07:15 +08:00
committed by GitHub
2 changed files with 6 additions and 4 deletions

View File

@@ -2188,10 +2188,6 @@ pub fn insertAllChildrenBefore(self: *Page, fragment: *Node, target: *Node, ref_
} }
} }
fn _appendNode(self: *Page, comptime from_parser: bool, parent: *Node, child: *Node, opts: InsertNodeOpts) !void {
self._insertNodeRelative(from_parser, parent, child, .append, opts);
}
const InsertNodeRelative = union(enum) { const InsertNodeRelative = union(enum) {
append, append,
after: *Node, after: *Node,

View File

@@ -156,6 +156,12 @@ pub fn replaceChildren(self: *DocumentFragment, nodes: []const Node.NodeOrText,
const parent_is_connected = parent.isConnected(); const parent_is_connected = parent.isConnected();
for (nodes) |node_or_text| { for (nodes) |node_or_text| {
const child = try node_or_text.toNode(page); const child = try node_or_text.toNode(page);
// If the new children has already a parent, remove from it.
if (child._parent) |p| {
page.removeNode(p, child, .{ .will_be_reconnected = true });
}
try page.appendNode(parent, child, .{ .child_already_connected = parent_is_connected }); try page.appendNode(parent, child, .{ .child_already_connected = parent_is_connected });
} }
} }