Skip to content

Lightpanda: the headless browser designed for AI and automation

webapi: allow custom element callback to move element during parse

Karl Seguina01824770ec5
diff --git a/src/browser/parser/Parser.zig b/src/browser/parser/Parser.zig
index 1bd8c7e71..93b9c7adb 100644
--- a/src/browser/parser/Parser.zig
+++ b/src/browser/parser/Parser.zig
@@ -748,22 +748,15 @@ fn _appendCallback(self: *Parser, parent_pn: *ParsedNode, node_or_text: h5e.Node
self.maybeCheckpoint();
self.inserted_since_checkpoint +|= 1;
const parent = parent_pn.node;
- if (wouldCycle(getParsed(cpn).node, parent)) {
- // Inserting would build a cycle (script moved it inside the adopting node)
+ const child_pn = getParsed(cpn);
+ if (leftWhereScriptPutIt(child_pn)) {
return;
}
- const child = try self.settleDocument(parent_pn, getParsed(cpn));
- if (child._parent) |previous_parent| {
- // html5ever says this can't happen, but we might be screwing up
- // the node on our side. We shouldn't be, but we're seeing this
- // in the wild, and I'm not sure why. In debug, let's crash so
- // we can try to figure it out. In release, let's disconnect
- // the child first.
- if (comptime lp.IS_DEBUG) {
- unreachable;
- }
- self.frame.removeNode(previous_parent, child, .{ .reconnect_to = parent });
+ if (wouldCycle(child_pn.node, parent)) {
+ // Inserting would build a cycle (script moved it inside the adopting node)
+ return;
}
+ const child = try self.settleDocument(parent_pn, child_pn);
try self.frame.appendNew(parent, child);
},
.text => |txt| {
@@ -780,12 +773,6 @@ fn settleDocument(self: *Parser, parent_pn: *ParsedNode, child_pn: *ParsedNode)
const parent = parent_pn.node;
const child = child_pn.node;
- // A custom element constructor may have put the child in the tree
- // itself; that is as observable as being placed by html5ever.
- if (child._parent != null) {
- child_pn.placed = true;
- }
-
var document = parent.getDocument(frame);
const child_document = child.getDocument(frame);
if (child_document != document) {
@@ -900,19 +887,16 @@ fn _appendBeforeSiblingCallback(self: *Parser, sibling_pn: *ParsedNode, node_or_
const parent = sibling.parentNode() orelse return error.NoParent;
const node: *Node = switch (node_or_text.toUnion()) {
.node => |cpn| blk: {
- if (wouldCycle(getParsed(cpn).node, parent)) {
+ const child_pn = getParsed(cpn);
+ if (leftWhereScriptPutIt(child_pn)) {
+ return;
+ }
+ if (wouldCycle(child_pn.node, parent)) {
// See _appendCallback: the would-be cycle drops the node.
return;
}
var parent_pn = ParsedNode{ .node = parent, .data = null, .placed = true };
- const child = try self.settleDocument(&parent_pn, getParsed(cpn));
- if (child._parent) |previous_parent| {
- // A custom element constructor may have inserted the node into the
- // DOM before the parser officially places it (e.g. via foster
- // parenting). Detach it first so insertNodeRelative's assertion holds.
- self.frame.removeNode(previous_parent, child, .{ .reconnect_to = parent });
- }
- break :blk child;
+ break :blk try self.settleDocument(&parent_pn, child_pn);
},
.text => |txt| blk: {
self.creation_document = parent.getDocument(self.frame);
@@ -943,6 +927,17 @@ fn _appendBasedOnParentNodeCallback(self: *Parser, element_pn: *ParsedNode, prev
}
}
+// A custom element's constructor or attributeChangedCallback runs between
+// the element's creation and its insertion. If the callback gives it a parent,
+// then the parser must leave it there and is considered placed into the tree.
+fn leftWhereScriptPutIt(node_pn: *ParsedNode) bool {
+ if (node_pn.node._parent == null) {
+ return false;
+ }
+ node_pn.placed = true;
+ return true;
+}
+
fn wouldCycle(node: *Node, parent: *Node) bool {
if (node == parent) {
return true;
diff --git a/src/browser/tests/custom_elements/parser_reparented_element.html b/src/browser/tests/custom_elements/parser_reparented_element.html
new file mode 100644
index 000000000..811611aa3
--- /dev/null
+++ b/src/browser/tests/custom_elements/parser_reparented_element.html
@@ -0,0 +1,41 @@
+<!DOCTYPE html>
+<body>
+<script src="../testing.js"></script>
+
+<!-- attributeChangedCallback runs after the parser creates the element but
+ before it inserts it. If the callback gives the element a parent, the
+ parser leaves it there (whatwg/html#12494), and the content that follows
+ still goes into the element. -->
+<div id=stash></div>
+<script>
+ class MoveToStash extends HTMLElement {
+ static get observedAttributes() { return ['x']; }
+ attributeChangedCallback() {
+ document.getElementById('stash').appendChild(this);
+ }
+ }
+ customElements.define('move-to-stash', MoveToStash);
+</script>
+
+<div id=append_target><move-to-stash id=appended x=1><p>y</p></move-to-stash></div>
+<script id=append_leaves_reparented_element>
+ {
+ const el = document.getElementById('appended');
+ testing.expectEqual(document.getElementById('stash'), el.parentNode);
+ testing.expectEqual(0, document.getElementById('append_target').childNodes.length);
+ testing.expectEqual('<p>y</p>', el.innerHTML);
+ }
+</script>
+
+<!-- The foster-parenting path (insert before the table) -->
+<div id=foster_target><table><move-to-stash id=fostered x=1></move-to-stash><tr><td>c</td></tr></table></div>
+<script id=foster_leaves_reparented_element>
+ {
+ const el = document.getElementById('fostered');
+ testing.expectEqual(document.getElementById('stash'), el.parentNode);
+ const target = document.getElementById('foster_target');
+ testing.expectEqual(1, target.childNodes.length);
+ testing.expectEqual('TABLE', target.firstChild.tagName);
+ }
+</script>
+</body>