Skip to content

Lightpanda: the headless browser designed for AI and automation

fix: activate resources in connected shadow subtrees

Aaron Wongb081770f8faf
diff --git a/src/browser/Frame.zig b/src/browser/Frame.zig
index 4c3c846b1..ef6fce960 100644
--- a/src/browser/Frame.zig
+++ b/src/browser/Frame.zig
@@ -3626,16 +3626,32 @@ fn updateRangesForNodeRemoval(self: *Frame, parent: *Node, child: *Node, child_i
// children, for its descendants in tree order: appending a subtree
// containing scripts must execute them all, after the whole insertion.
fn nodeIsReadySubtree(self: *Frame, node: *Node) !void {
- if (node._type != .element or node.firstChild() == null) {
+ if (node._type != .element) {
+ return self.nodeIsReady(false, node);
+ }
+ if (node.firstChild() == null and node.as(Element).hostedShadowRoot(self) == null) {
return self.nodeIsReady(false, node);
}
// Scripts can mutate the tree. Safe to do this since nodeIsReady re-checks
// connectivity.
var elements: std.ArrayList(*Node) = .empty;
- var tw = TreeWalker.Full.Elements.init(node, .{});
- while (tw.next()) |el| {
- try elements.append(self.call_arena, el.asNode());
+ var pending: std.ArrayList(*Node) = .empty;
+ try pending.append(self.call_arena, node);
+ while (pending.pop()) |current| {
+ if (current.is(Element)) |_| {
+ try elements.append(self.call_arena, current);
+ }
+ var child = current.lastChild();
+ while (child) |c| : (child = c.previousSibling()) {
+ try pending.append(self.call_arena, c);
+ }
+ // Shadow-including order also activates unslotted light children.
+ if (current.is(Element)) |el| {
+ if (el.hostedShadowRoot(self)) |shadow| {
+ try pending.append(self.call_arena, shadow.asNode());
+ }
+ }
}
for (elements.items) |el| {
try self.nodeIsReady(false, el);
diff --git a/src/browser/tests/frames/connected_shadow_iframe.html b/src/browser/tests/frames/connected_shadow_iframe.html
new file mode 100644
index 000000000..cecf7341d
--- /dev/null
+++ b/src/browser/tests/frames/connected_shadow_iframe.html
@@ -0,0 +1,50 @@
+<!DOCTYPE html>
+<script src="../testing.js"></script>
+<body></body>
+
+<script id="iframes_in_connected_shadow_subtrees">
+(async () => {
+ const state = await testing.async();
+ const pending = [];
+
+ function addFrame(parent, label) {
+ const iframe = document.createElement('iframe');
+ iframe.srcdoc = '<p id="marker">Loaded</p>';
+ const loaded = new Promise(resolve => {
+ iframe.addEventListener('load', () => resolve(label));
+ });
+ parent.appendChild(iframe);
+ pending.push({ iframe, label, loaded });
+ }
+
+ for (const mode of ['open', 'closed']) {
+ const host = document.createElement('section');
+ const shadow = host.attachShadow({ mode });
+ addFrame(shadow, mode);
+ // Unslotted light children still get browsing contexts.
+ addFrame(host, mode + '-light');
+ document.body.appendChild(host);
+ }
+
+ const outer = document.createElement('section');
+ const outerShadow = outer.attachShadow({ mode: 'open' });
+ document.body.appendChild(outer);
+ const inner = document.createElement('article');
+ const innerShadow = inner.attachShadow({ mode: 'closed' });
+ addFrame(innerShadow, 'nested');
+ outerShadow.appendChild(inner);
+
+ const result = await Promise.race([
+ Promise.all(pending.map(item => item.loaded)),
+ new Promise(resolve => setTimeout(() => resolve('timeout'), 500)),
+ ]);
+ state.resolve();
+ await state.done(() => {
+ testing.expectEqual(['open', 'open-light', 'closed', 'closed-light', 'nested'], result);
+ for (const { iframe } of pending) {
+ testing.expectTrue(iframe.contentWindow !== null);
+ testing.expectEqual('Loaded', iframe.contentDocument.getElementById('marker').textContent);
+ }
+ });
+})();
+</script>