Skip to content

Lightpanda: the headless browser designed for AI and automation

webapi: un-rendered light DOM elements are now treated as hidden

Karl Seguina641b667e650
diff --git a/src/browser/StyleManager.zig b/src/browser/StyleManager.zig
index 3317c9767..2ec9262b7 100644
--- a/src/browser/StyleManager.zig
+++ b/src/browser/StyleManager.zig
@@ -1737,6 +1737,8 @@ const Visibility = struct {
visibility_hidden: ?bool = null,
opacity_zero: bool = false,
pointer_events_none: ?bool = null,
+ // see Node.isOutsideFlatTree. Hidden like display:none
+ outside_flat_tree: bool = false,
};
const Computed = packed struct(u10) {
@@ -1757,7 +1759,7 @@ const Visibility = struct {
.author_display = own.author_display,
.visibility_hidden = own.visibility_hidden orelse parent.visibility_hidden,
.pointer_events_none = own.pointer_events_none orelse parent.pointer_events_none,
- .in_display_none = own.display == .none or parent.in_display_none,
+ .in_display_none = own.display == .none or own.outside_flat_tree or parent.in_display_none,
.in_opacity_zero = own.opacity_zero or parent.in_opacity_zero,
};
}
@@ -1775,6 +1777,7 @@ const Visibility = struct {
// origin regardless of specificity, so `.x { display: flex }` on a
// `<div class="x" hidden>` must report visible.
pub fn finish(p: *Cascaded, el: *Element, frame: *Frame, priorities: *const Priorities(Declared)) void {
+ p.outside_flat_tree = el.asNode().isOutsideFlatTree();
p.author_display = priorities.get(.display) != 0;
if (priorities.get(.box) == 0) {
p.box = Box.uaDefault(el.getTag());
diff --git a/src/browser/tests/element/check_visibility.html b/src/browser/tests/element/check_visibility.html
index 0a745e694..14e766ad5 100644
--- a/src/browser/tests/element/check_visibility.html
+++ b/src/browser/tests/element/check_visibility.html
@@ -903,3 +903,54 @@
host.remove();
}
</script>
+
+<script id="outside_flat_tree_is_not_rendered">
+ {
+ const host = document.createElement("div");
+ host.innerHTML = '<span id="title" slot="title">Title</span><span id="lost" slot="nope"><b>Lost</b></span><span id="body">Body</span>';
+ document.body.appendChild(host);
+ const title = host.querySelector("#title");
+ const lost = host.querySelector("#lost");
+ const body = host.querySelector("#body");
+ testing.expectEqual(true, body.checkVisibility());
+
+ // No slots yet: attaching takes every child out of the flat tree
+ const shadow = host.attachShadow({ mode: "open" });
+ testing.expectEqual(false, body.checkVisibility());
+ testing.expectEqual(false, lost.firstChild.checkVisibility());
+ testing.expectEqual(0, body.getBoundingClientRect().width);
+
+ shadow.innerHTML = '<h2><slot name="title"></slot></h2><slot id="def"><i>Fallback</i></slot>';
+ const fallback = shadow.querySelector("i");
+ testing.expectEqual(true, title.checkVisibility());
+ testing.expectEqual(true, body.checkVisibility());
+ // a slot name nothing matches
+ testing.expectEqual(false, lost.checkVisibility());
+ testing.expectEqual(false, lost.firstChild.checkVisibility());
+ // the default slot has assigned nodes, so its fallback isn't rendered
+ testing.expectEqual(false, fallback.checkVisibility());
+
+ body.remove();
+ testing.expectEqual(true, fallback.checkVisibility());
+
+ host.remove();
+ }
+</script>
+
+<script id="manual_slot_unassigned_is_not_rendered">
+ {
+ const host = document.createElement("div");
+ const child = document.createElement("span");
+ host.appendChild(child);
+ document.body.appendChild(host);
+
+ const shadow = host.attachShadow({ mode: "open", slotAssignment: "manual" });
+ shadow.innerHTML = "<slot></slot>";
+ testing.expectEqual(false, child.checkVisibility());
+
+ shadow.querySelector("slot").assign(child);
+ testing.expectEqual(true, child.checkVisibility());
+
+ host.remove();
+ }
+</script>
diff --git a/src/browser/webapi/Element.zig b/src/browser/webapi/Element.zig
index ad7cfbb7b..2f7a516d5 100644
--- a/src/browser/webapi/Element.zig
+++ b/src/browser/webapi/Element.zig
@@ -954,6 +954,8 @@ pub fn attachShadow(self: *Element, opts: ShadowRoot.AttachOptions, frame: *Fram
const page = frame.page;
try page.element_shadow_roots.put(page.arena, self, shadow_root);
self._flags.shadow_host = true;
+ // The host's children leave the flat tree until a slot takes them
+ frame.renderingChanged();
return shadow_root;
}
diff --git a/src/browser/webapi/Node.zig b/src/browser/webapi/Node.zig
index 7bee4f8d0..ec837703d 100644
--- a/src/browser/webapi/Node.zig
+++ b/src/browser/webapi/Node.zig
@@ -1796,6 +1796,18 @@ pub fn flatTreeParent(self: *Node, frame: *const Frame) ?*Node {
return self._parent;
}
+/// A shadow host's child that no slot takes, or the fallback content that isn't
+// used. flatTreeParent returns the parent for these, so we can't use that.
+pub fn isOutsideFlatTree(self: *const Node) bool {
+ const parent = self._parent orelse return false;
+ const el = parent.is(Element) orelse return false;
+ if (el._flags.shadow_host) {
+ return self._flags.assigned_slot == false;
+ }
+ const slot = parent.is(Element.Html.Slot) orelse return false;
+ return slot._assigned.items.len > 0;
+}
+
/// The flat-tree parent element: shadow content inherits from its host, a
/// slotted node from its slot.
pub fn flatTreeParentElement(self: *Node, frame: *const Frame) ?*Element {