Skip to content

Lightpanda: the headless browser designed for AI and automation

dom: clone a snapshot of children, not the live list

Scott Taylor5ab9f69b443a
diff --git a/src/browser/tests/custom_elements/constructor.html b/src/browser/tests/custom_elements/constructor.html
index 2364c20d8..acef48756 100644
--- a/src/browser/tests/custom_elements/constructor.html
+++ b/src/browser/tests/custom_elements/constructor.html
@@ -107,6 +107,30 @@
}
</script>
+<div id=clone_siblings_container></div>
+
+<script id=clone_self_appending_siblings>
+ {
+ // Two trailing constructors append into the list being cloned.
+ let calls = 0;
+ class MyCloneElementB extends HTMLElement {
+ constructor() {
+ super();
+ calls += 1;
+ // Bound regressions so a live walk fails rather than hangs.
+ if (calls <= 8) $('#clone_siblings_container').appendChild(this);
+ }
+ }
+ customElements.define('my-clone_element_b', MyCloneElementB);
+ document.createElement('my-clone_element_b');
+ document.createElement('my-clone_element_b');
+ testing.expectEqual(2, calls);
+ $('#clone_siblings_container').cloneNode(true);
+ testing.expectEqual(4, calls);
+ testing.expectEqual(4, $('#clone_siblings_container').childElementCount);
+ }
+</script>
+
<div id=fragment_clone_container></div>
<script id=clone_fragment>
diff --git a/src/browser/webapi/DocumentFragment.zig b/src/browser/webapi/DocumentFragment.zig
index a1d0f58a7..aef884fcc 100644
--- a/src/browser/webapi/DocumentFragment.zig
+++ b/src/browser/webapi/DocumentFragment.zig
@@ -176,12 +176,7 @@ pub fn cloneFragment(self: *DocumentFragment, deep: bool, document: *const Node.
const fragment_node = fragment.asNode();
if (deep) {
- var child_it = self.asNode().childrenIterator();
- while (child_it.next()) |child| {
- if (try child.cloneNodeForAppending(true, document, frame)) |cloned_child| {
- try frame.appendNode(fragment_node, cloned_child, .{});
- }
- }
+ try self.asNode().cloneChildrenInto(fragment_node, document, frame);
}
return fragment_node;
diff --git a/src/browser/webapi/Element.zig b/src/browser/webapi/Element.zig
index edbf475b2..e5a2587ca 100644
--- a/src/browser/webapi/Element.zig
+++ b/src/browser/webapi/Element.zig
@@ -1942,23 +1942,12 @@ pub fn clone(self: *Element, deep: bool, document: *const Node.Document, frame:
.declarative = shadow._declarative,
}, frame) catch return error.CloneError;
- const cloned_shadow_node = cloned_shadow.asNode();
- var shadow_child_it = shadow.asNode().childrenIterator();
- while (shadow_child_it.next()) |child| {
- if (try child.cloneNodeForAppending(true, document, frame)) |cloned_child| {
- try frame.appendNode(cloned_shadow_node, cloned_child, .{});
- }
- }
+ try shadow.asNode().cloneChildrenInto(cloned_shadow.asNode(), document, frame);
}
}
if (deep) {
- var child_it = self.asNode().childrenIterator();
- while (child_it.next()) |child| {
- if (try child.cloneNodeForAppending(true, document, frame)) |cloned_child| {
- try frame.appendNode(node, cloned_child, .{});
- }
- }
+ try self.asNode().cloneChildrenInto(node, document, frame);
}
return node;
diff --git a/src/browser/webapi/Node.zig b/src/browser/webapi/Node.zig
index 2bf956a60..8bda6e872 100644
--- a/src/browser/webapi/Node.zig
+++ b/src/browser/webapi/Node.zig
@@ -1215,8 +1215,7 @@ pub fn cloneNodeInto(self: *Node, deep: bool, document: *const Document, frame:
cloned._ready_state = .complete;
if (deep) {
- var child_it = self.childrenIterator();
- while (child_it.next()) |child| {
+ for (try self.snapshotChildren(frame)) |child| {
if (try child.cloneNodeForAppending(true, cloned, frame)) |cloned_child| {
_ = cloned.asNode().appendChild(cloned_child, frame) catch return error.CloneError;
}
@@ -1259,6 +1258,26 @@ pub fn cloneNodeForAppending(self: *Node, deep: bool, document: *const Document,
return cloned;
}
+// A constructor run mid-clone can append to the node being cloned (a modal
+// that attaches itself to a shared root, say). Cloning walks a snapshot so
+// those additions aren't cloned in turn, which would never end.
+pub fn snapshotChildren(self: *Node, frame: *Frame) CloneError![]*Node {
+ var children: std.ArrayList(*Node) = .empty;
+ var it = self.childrenIterator();
+ while (it.next()) |child| {
+ children.append(frame.call_arena, child) catch return error.CloneError;
+ }
+ return children.items;
+}
+
+pub fn cloneChildrenInto(self: *Node, parent: *Node, document: *const Document, frame: *Frame) CloneError!void {
+ for (try self.snapshotChildren(frame)) |child| {
+ if (try child.cloneNodeForAppending(true, document, frame)) |cloned| {
+ frame.appendNode(parent, cloned, .{}) catch return error.CloneError;
+ }
+ }
+}
+
pub fn compareDocumentPosition(self: *Node, other: *Node) u16 {
const DISCONNECTED: u16 = 0x01;
const PRECEDING: u16 = 0x02;