Skip to content

Lightpanda: the headless browser designed for AI and automation

StyleManager: inherit through the flat tree

Adrià Arrufatc33e52361e80
diff --git a/src/browser/StyleManager.zig b/src/browser/StyleManager.zig
index 3cad6d5cb..2fb4dbaba 100644
--- a/src/browser/StyleManager.zig
+++ b/src/browser/StyleManager.zig
@@ -702,6 +702,18 @@ fn visibilityProps(self: *StyleManager, el: *Element) Visibility.Computed {
return self.visibility.inheritedProps(self.arena.allocator(), el, self.frame);
}
+/// The element `el` inherits from: its flat-tree parent, so shadow content
+/// inherits from the host and a slotted node from its slot.
+fn inheritanceParent(el: *Element, frame: *const Frame) ?*Element {
+ var node = el.asNode().flatTreeParent(frame);
+ while (node) |n| : (node = n.flatTreeParent(frame)) {
+ if (n.is(Element)) |parent| {
+ return parent;
+ }
+ }
+ return null;
+}
+
/// Callers must have run rebuildIfDirty, which resets the memo.
fn geometryProps(self: *StyleManager, el: *Element) Geometry.Computed {
return self.geometry.ownProps(self.arena.allocator(), el, self.frame);
@@ -843,7 +855,7 @@ fn Group(comptime Spec: type) type {
self.path.clearRetainingCapacity();
var above: Computed = .{};
var current: ?*Element = el;
- while (current) |elem| : (current = elem.parentElement()) {
+ while (current) |elem| : (current = inheritanceParent(elem, frame)) {
if (memo.get(elem)) |cached| {
above = cached;
break;
@@ -1653,7 +1665,7 @@ pub fn customPropertyValue(self: *StyleManager, el: *Element, property_name: Str
};
var current: ?*Element = el;
- while (current) |elem| : (current = elem.parentElement()) {
+ while (current) |elem| : (current = inheritanceParent(elem, self.frame)) {
if (self.inlineStyleValue(elem, property_name)) |value| {
return value;
}
@@ -1732,7 +1744,7 @@ fn computedFontSizeAt(self: *StyleManager, element: ?*Element, depth: u8) f64 {
return DEFAULT_FONT_SIZE;
}
const current = element orelse return DEFAULT_FONT_SIZE;
- const parent = current.parentElement();
+ const parent = inheritanceParent(current, self.frame);
if (self.inlineStyleValue(current, comptime .wrap("font-size"))) |raw| {
if (self.parseFontSize(raw, parent, depth + 1)) |size| {
diff --git a/src/browser/tests/element/check_visibility.html b/src/browser/tests/element/check_visibility.html
index c8070c9ea..a9e286ab8 100644
--- a/src/browser/tests/element/check_visibility.html
+++ b/src/browser/tests/element/check_visibility.html
@@ -800,3 +800,53 @@
style.remove();
}
</script>
+
+<script id="inherits_across_shadow_boundary">
+ {
+ const none = document.createElement("div");
+ none.style.display = "none";
+ document.body.appendChild(none);
+ const button = document.createElement("button");
+ none.attachShadow({ mode: "open" }).appendChild(button);
+ testing.expectEqual(false, button.checkVisibility());
+
+ none.style.display = "block";
+ testing.expectEqual(true, button.checkVisibility());
+
+ const hidden = document.createElement("div");
+ hidden.style.cssText = "visibility: hidden; --c: red";
+ document.body.appendChild(hidden);
+ const span = document.createElement("span");
+ hidden.attachShadow({ mode: "open" }).appendChild(span);
+ testing.expectEqual(false, span.checkVisibility({ visibilityProperty: true }));
+ testing.expectEqual("hidden", getComputedStyle(span).visibility);
+ testing.expectEqual("red", getComputedStyle(span).getPropertyValue("--c"));
+
+ none.remove();
+ hidden.remove();
+ }
+</script>
+
+<script id="slotted_inherits_from_slot">
+ {
+ 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 id="shown"></slot><div style="visibility: hidden"><slot id="hidden"></slot></div>';
+ const shown = shadow.getElementById("shown");
+ const hidden = shadow.getElementById("hidden");
+
+ shown.assign(child);
+ testing.expectEqual(true, child.checkVisibility({ visibilityProperty: true }));
+
+ // Reassigning changes no style or DOM, only the slot it inherits from
+ hidden.assign(child);
+ testing.expectEqual(false, child.checkVisibility({ visibilityProperty: true }));
+ testing.expectEqual("hidden", getComputedStyle(child).visibility);
+
+ host.remove();
+ }
+</script>
diff --git a/src/browser/webapi/element/slotting.zig b/src/browser/webapi/element/slotting.zig
index 1f72f8705..44ec1dc06 100644
--- a/src/browser/webapi/element/slotting.zig
+++ b/src/browser/webapi/element/slotting.zig
@@ -129,6 +129,8 @@ fn _assignSlottables(slot: *Slot, frame: *Frame) !void {
}
frame.signalSlotChange(slot);
+ // A slotted node inherits from its slot
+ frame.styleChanged();
const page = frame.page;
for (old) |node| {