Lightpanda: the headless browser designed for AI and automation
browser: read position from the cascade, not just the inline style
Adrià Arrufata7e14a96442d
diff --git a/src/browser/StyleManager.zig b/src/browser/StyleManager.zigindex 3e497bc62..6f2668a30 100644--- a/src/browser/StyleManager.zig+++ b/src/browser/StyleManager.zig@@ -820,8 +820,8 @@ pub fn widthSizing(self: *StyleManager, el: *Element) WidthSizing {if (vis.box != .block_level or el.getTag().isReplacedOrControl()) {return .shrink;}- const flow = self.flow.ownProps(self.arena.allocator(), el, self.frame);- if (flow.absolutely_positioned or flow.floated) {+ const flow = self.flowProps(el);+ if (flow.position.outOfFlow() or flow.floated) {return .shrink;}const parent = self.boxParent(el) orelse return .fill;@@ -860,8 +860,15 @@ pub fn isBlockLevel(self: *StyleManager, el: *Element) bool {.block_level, .table => {},.inline_level, .contents => return false,}- const flow = self.flow.ownProps(self.arena.allocator(), el, self.frame);- return flow.absolutely_positioned == false and flow.floated == false;+ const flow = self.flowProps(el);+ return flow.position.outOfFlow() == false and flow.floated == false;+}++/// Own property, not inherited.+pub fn position(self: *StyleManager, el: *Element) Position {+ self.assertOwns(el);+ self.rebuildIfDirty() catch return .static;+ return self.flowProps(el).position;}/// Overflow along x is clipped or scrolled within the box, so it doesn't@@ -954,6 +961,11 @@ fn geometryProps(self: *StyleManager, el: *Element) Geometry.Computed {return self.geometry.ownProps(self.arena.allocator(), el, self.frame);}+/// Callers must have run rebuildIfDirty, which resets the memo.+fn flowProps(self: *StyleManager, el: *Element) Flow.Computed {+ return self.flow.ownProps(self.arena.allocator(), el, self.frame);+}+/// Taken before the arena resets, to presize the new containers.const Capacities = struct {id_rules: u32,@@ -1935,17 +1947,49 @@ const Text = struct {};};-/// Whether a box is out of normal flow, which sizes it to its content.+/// `inherit`, `unset` and `revert` are not handled, so they drop the+/// declaration. `initial` is the initial value, static.+pub const Position = enum(u3) {+ static,+ relative,+ absolute,+ fixed,+ sticky,++ // Null for an invalid value, which drops the declaration.+ fn parse(value: []const u8) ?Position {+ const trimmed = std.mem.trim(u8, value, &std.ascii.whitespace);+ if (std.ascii.eqlIgnoreCase(trimmed, "static") or std.ascii.eqlIgnoreCase(trimmed, "initial")) return .static;+ if (std.ascii.eqlIgnoreCase(trimmed, "relative")) return .relative;+ if (std.ascii.eqlIgnoreCase(trimmed, "absolute")) return .absolute;+ if (std.ascii.eqlIgnoreCase(trimmed, "fixed")) return .fixed;+ if (std.ascii.eqlIgnoreCase(trimmed, "sticky") or std.ascii.eqlIgnoreCase(trimmed, "-webkit-sticky")) return .sticky;+ return null;+ }++ /// Out of flow, so the box sizes to its content. Sticky is not: it lays+ /// out in flow and is only offset when painted.+ fn outOfFlow(self: Position) bool {+ return self == .absolute or self == .fixed;+ }++ /// The computed-value keyword, as getComputedStyle reports it.+ pub fn keyword(self: Position) []const u8 {+ return @tagName(self);+ }+};++/// Where a box sits: its position and whether it floats.const Flow = struct {const Declared = struct {const names = [_][]const u8{ "position", "float" };- absolutely_positioned: ?bool = null,+ position: ?Position = null,floated: ?bool = null,fn apply(self: *Declared, name: []const u8, value: []const u8) void {if (std.ascii.eqlIgnoreCase(name, "position")) {- self.absolutely_positioned = std.ascii.eqlIgnoreCase(value, "absolute") or std.ascii.eqlIgnoreCase(value, "fixed");+ self.position = Position.parse(value);} else if (std.ascii.eqlIgnoreCase(name, "float")) {self.floated = std.ascii.eqlIgnoreCase(value, "left") or std.ascii.eqlIgnoreCase(value, "right") orstd.ascii.eqlIgnoreCase(value, "inline-start") or std.ascii.eqlIgnoreCase(value, "inline-end");@@ -1953,8 +1997,8 @@ const Flow = struct {}};- const Computed = packed struct(u2) {- absolutely_positioned: bool = false,+ const Computed = packed struct(u4) {+ position: Position = .static,floated: bool = false,};};@@ -2401,6 +2445,10 @@ test "StyleManager: text-transform" {try testing.htmlRunner("css/text_transform.html", .{});}+test "StyleManager: position" {+ try testing.htmlRunner("css/position.html", .{});+}+test "StyleManager: text-transform without rules skips the memo" {const frame = try testing.createFrame();defer testing.test_session.closeAllPages();@@ -2648,8 +2696,8 @@ test "StyleManager: inlineDeclared: scan matches the parsed style object" {.{ .geometry = .{ .width = .{} } },.{ .visibility = .{ .display = .flex, .box = .block_level } },.{ .visibility = .{ .display = .grid, .box = .inline_level } },- .{ .geometry = .{ .width = .{ .unit = .content } }, .flow = .{ .absolutely_positioned = true, .floated = true } },- .{ .geometry = .{ .width = .{ .unit = .content } }, .flow = .{ .absolutely_positioned = false, .floated = false } },+ .{ .geometry = .{ .width = .{ .unit = .content } }, .flow = .{ .position = .absolute, .floated = true } },+ .{ .geometry = .{ .width = .{ .unit = .content } }, .flow = .{ .position = .relative, .floated = false } },};var i: usize = 0;diff --git a/src/browser/tests/css/position.html b/src/browser/tests/css/position.htmlnew file mode 100644index 000000000..e3a8bf71a--- /dev/null+++ b/src/browser/tests/css/position.html@@ -0,0 +1,74 @@+<!DOCTYPE html>+<head>+ <script src="../testing.js"></script>+</head>++<!--+ position: computed value from the cascade, not just the inline style, and+ the offsetParent that follows from it. offsetParent is the nearest+ positioned ancestor, so a rule in a stylesheet decides it as much as a+ style attribute does.+-->++<style>+ .rel { position: relative; }+ .abs { position: absolute; }+ .fix { position: fixed; }+ .stick { position: sticky; }+ .static { position: static; }+ #id-wins { position: fixed; }+ .bogus { position: floating; }+</style>++<span id=inline style="position: relative">a</span>+<span id=class class=rel>b</span>+<span id=id-wins class=rel>c</span>+<span id=bogus class=bogus>d</span>+<span id=reset class="rel static">e</span>+<span id=plain>f</span>+<span id=sticky class=stick>g</span>++<div class=rel><span id=in-rel>h</span></div>+<div class=fix><span id=in-fix>i</span></div>+<div class=stick><span id=in-stick>j</span></div>+<div class=abs><span id=in-abs>k</span></div>+<div class=rel><div><span id=deep>l</span></div></div>++<script id="computedValue">+{+ const pos = (id) => getComputedStyle($('#' + id)).position;++ // A stylesheet rule reaches the computed value, not only a style attribute.+ testing.expectEqual('relative', pos('inline'));+ testing.expectEqual('relative', pos('class'));+ testing.expectEqual('sticky', pos('sticky'));++ // Cascade order holds: the id rule beats the class, and `static` resets.+ testing.expectEqual('fixed', pos('id-wins'));+ testing.expectEqual('static', pos('reset'));++ // An unknown keyword drops the declaration; the initial value is static.+ testing.expectEqual('static', pos('bogus'));+ testing.expectEqual('static', pos('plain'));++ // It does not inherit: a child of a positioned box is static.+ testing.expectEqual('static', pos('in-fix'));+}+</script>++<script id="offsetParentFromStylesheet">+{+ // Each of the four positioned values makes the ancestor an offsetParent.+ for (const id of ['in-rel', 'in-fix', 'in-stick', 'in-abs']) {+ const el = $('#' + id);+ testing.expectEqual(el.parentElement, el.offsetParent);+ }++ // The search walks past unpositioned ancestors to the positioned one.+ testing.expectEqual($('#deep').parentElement.parentElement, $('#deep').offsetParent);++ // The element's own position:fixed, from a rule, still means no+ // offsetParent at all.+ testing.expectEqual(null, $('#id-wins').offsetParent);+}+</script>diff --git a/src/browser/webapi/Element.zig b/src/browser/webapi/Element.zigindex 02a664310..ad7cfbb7b 100644--- a/src/browser/webapi/Element.zig+++ b/src/browser/webapi/Element.zig@@ -1538,6 +1538,11 @@ pub fn isVisible(self: *Element, frame: *Frame) bool {return !owner._style_manager.isHidden(self, .{});}+fn computedPosition(self: *Element, frame: *Frame) StyleManager.Position {+ const owner = self.ownerFrame(frame) orelse return .static;+ return owner._style_manager.position(self);+}+const CheckVisibilityOpts = struct {checkOpacity: bool = false,checkVisibilityCSS: bool = false,@@ -2040,11 +2045,10 @@ pub fn getOffsetParent(self: *Element, frame: *Frame) ?*Element {else => {},}- const self_position = self.positionStyle(frame);- if (std.mem.eql(u8, self_position, "fixed")) {+ const self_position = self.computedPosition(frame);+ if (self_position == .fixed) {return null;}- const self_static = self_position.len == 0 or std.mem.eql(u8, self_position, "static");var node: ?*Node = self.asNode()._parent;while (node) |n| {@@ -2059,11 +2063,10 @@ pub fn getOffsetParent(self: *Element, frame: *Frame) ?*Element {if (tag == .body) {return ancestor;}- const position = ancestor.positionStyle(frame);- if (position.len > 0 and !std.mem.eql(u8, position, "static")) {+ if (ancestor.computedPosition(frame) != .static) {return ancestor;}- if (self_static and (tag == .td or tag == .th or tag == .table)) {+ if (self_position == .static and (tag == .td or tag == .th or tag == .table)) {return ancestor;}node = n._parent;@@ -2071,11 +2074,6 @@ pub fn getOffsetParent(self: *Element, frame: *Frame) ?*Element {return null;}-fn positionStyle(self: *Element, frame: *Frame) []const u8 {- const style = self.inlineStyle(frame) orelse return "";- return style.asCSSStyleDeclaration().getPropertyValue("position", frame);-}-pub fn getClientTop(_: *Element) f64 {// Border width - in our dummy layout, we don't apply borders to layoutreturn 0.0;diff --git a/src/browser/webapi/css/CSSStyleDeclaration.zig b/src/browser/webapi/css/CSSStyleDeclaration.zigindex 9efc08cc4..c44918e3c 100644--- a/src/browser/webapi/css/CSSStyleDeclaration.zig+++ b/src/browser/webapi/css/CSSStyleDeclaration.zig@@ -90,8 +90,9 @@ fn propertyValue(self: *const CSSStyleDeclaration, normalized: []const u8, wrapp// Computed styles must reflect stylesheet rules, not just the element's// inline `style=` attribute. Limited to display/visibility — what aria- // tree builders (Playwright ariaSnapshot) consult on every element — and- // pointer-events and text-transform, which innerText applies.+ // tree builders (Playwright ariaSnapshot) consult on every element —+ // pointer-events and text-transform, which innerText applies, and+ // position, which offsetParent consults.if (self._is_computed) {if (self._element) |element| {if (element.ownerFrame(frame)) |owner| {@@ -111,6 +112,8 @@ fn propertyValue(self: *const CSSStyleDeclaration, normalized: []const u8, wrapp} else if (wrapped.eqlSlice("text-transform")) {// Inherited: the nearest declaration up the tree wins.return style_manager.textTransform(element).keyword();+ } else if (wrapped.eqlSlice("position")) {+ return style_manager.position(element).keyword();}}}