Skip to content

Lightpanda: the headless browser designed for AI and automation

fix: close shadow iframes when their host disconnects

Aaron Wongcaa2d8e93fb6
diff --git a/src/browser/Frame.zig b/src/browser/Frame.zig
index ef6fce960..752212d5d 100644
--- a/src/browser/Frame.zig
+++ b/src/browser/Frame.zig
@@ -3138,6 +3138,7 @@ pub fn removeNode(self: *Frame, parent: *Node, child: *Node, opts: RemoveNodeOpt
_ = Element.Build.call(el, "disconnected", .{ el, self }) catch |err| {
log.err(.bug, "build.disconnected", .{ .tag = el.getTag(), .err = err, .type = self._type, .url = self.url });
};
+ self.disconnectShadowIFrames(el);
// If a <style> element is being removed, remove its sheet from the list.
// `self` is the calling frame — Node.removeChild passes its own — so
@@ -3178,6 +3179,19 @@ fn removeStyleSheet(owner: ?*Frame, sheet: *CSSStyleSheet) void {
sheets.remove(sheet);
}
+fn disconnectShadowIFrames(self: *Frame, host: *Element) void {
+ const shadow = host.hostedShadowRoot(self) orelse return;
+ var tw = TreeWalker.Full.Elements.init(shadow.asNode(), .{});
+ while (tw.next()) |el| {
+ if (el.is(IFrame)) |_| {
+ IFrame.Build.disconnected(el, self) catch |err| {
+ log.err(.bug, "shadow.iframe.disconnected", .{ .err = err });
+ };
+ }
+ self.disconnectShadowIFrames(el);
+ }
+}
+
// The TreeWalker isn't shadow DOM aware, so this is correctly scoped to direct
// descendants (ids nested under a shadow DOM are owned by that shadow DOM).
fn unregisterSubtreeIds(self: *Frame, node: *Node, id_maps: ElementIdMaps) void {
@@ -3636,20 +3650,26 @@ fn nodeIsReadySubtree(self: *Frame, node: *Node) !void {
// Scripts can mutate the tree. Safe to do this since nodeIsReady re-checks
// connectivity.
var elements: std.ArrayList(*Node) = .empty;
- var pending: std.ArrayList(*Node) = .empty;
- try pending.append(self.call_arena, node);
- while (pending.pop()) |current| {
+ const Pending = struct { node: *Node, shadow: bool };
+ var pending: std.ArrayList(Pending) = .empty;
+ try pending.append(self.call_arena, .{ .node = node, .shadow = false });
+ while (pending.pop()) |entry| {
+ const current = entry.node;
if (current.is(Element)) |_| {
- try elements.append(self.call_arena, current);
+ // Shadow CSS has separate tree-scoping requirements. Only iframe
+ // activation is added while crossing a hosted shadow root here.
+ if (!entry.shadow or current.is(IFrame) != null) {
+ try elements.append(self.call_arena, current);
+ }
}
var child = current.lastChild();
while (child) |c| : (child = c.previousSibling()) {
- try pending.append(self.call_arena, c);
+ try pending.append(self.call_arena, .{ .node = c, .shadow = entry.shadow });
}
// 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());
+ try pending.append(self.call_arena, .{ .node = shadow.asNode(), .shadow = true });
}
}
}
diff --git a/src/browser/tests/frames/connected_shadow_iframe.html b/src/browser/tests/frames/connected_shadow_iframe.html
index cecf7341d..24f1d9688 100644
--- a/src/browser/tests/frames/connected_shadow_iframe.html
+++ b/src/browser/tests/frames/connected_shadow_iframe.html
@@ -48,3 +48,50 @@
});
})();
</script>
+
+<script id="shadow_iframes_disconnect_and_reconnect">
+(async () => {
+ const state = await testing.async();
+ for (const mode of ['open', 'closed']) {
+ const host = document.createElement('section');
+ const shadow = host.attachShadow({ mode });
+ const nested = document.createElement('article');
+ const nestedShadow = nested.attachShadow({ mode: 'closed' });
+ const iframe = document.createElement('iframe');
+ iframe.srcdoc = '<p id="marker">Reconnected</p>';
+ nestedShadow.appendChild(iframe);
+ shadow.appendChild(nested);
+ const load = () => new Promise(resolve => iframe.addEventListener('load', () => resolve('load'), { once: true }));
+ const firstLoad = load();
+ document.body.appendChild(host);
+ testing.expectEqual('load', await Promise.race([firstLoad, new Promise(resolve => setTimeout(() => resolve('timeout'), 500))]));
+ const original = iframe.contentWindow;
+ host.remove();
+ testing.expectEqual(null, iframe.contentWindow);
+ testing.expectEqual(null, iframe.contentDocument);
+ const reloaded = load();
+ document.body.appendChild(host);
+ testing.expectEqual('load', await Promise.race([reloaded, new Promise(resolve => setTimeout(() => resolve('timeout'), 500))]));
+ testing.expectTrue(iframe.contentWindow !== original);
+ testing.expectEqual('Reconnected', iframe.contentDocument.getElementById('marker').textContent);
+ host.remove();
+ }
+ state.resolve();
+ await state.done();
+})();
+</script>
+
+<script id="connecting_host_does_not_promote_shadow_styles">
+const outside = document.createElement('button');
+outside.textContent = 'Outside';
+document.body.appendChild(outside);
+const styledHost = document.createElement('section');
+const styledShadow = styledHost.attachShadow({ mode: 'closed' });
+const scopedStyle = document.createElement('style');
+scopedStyle.textContent = 'button { display: none; }';
+styledShadow.appendChild(scopedStyle);
+document.body.appendChild(styledHost);
+testing.expectEqual('inline-block', getComputedStyle(outside).display);
+styledHost.remove();
+outside.remove();
+</script>