Lightpanda: the headless browser designed for AI and automation
render: block-level flex and grid containers don't grow with items
Scott Taylorb4124931d361
diff --git a/src/browser/StyleManager.zig b/src/browser/StyleManager.zigindex d45cb1471..27f261694 100644--- a/src/browser/StyleManager.zig+++ b/src/browser/StyleManager.zig@@ -58,6 +58,7 @@ visibility: Group(Visibility) = .{},geometry: Group(Geometry) = .{},touch_action: Group(TouchAction) = .{},text: Group(Text) = .{},+flow: Group(Flow) = .{},// Keyed by property name, pruning to what is (hopefully) one or few rulescustom_rules: std.StringHashMapUnmanaged(CustomProperty) = .empty,@@ -744,6 +745,26 @@ pub fn inlineSize(self: *StyleManager, el: *Element, comptime axis: Element.Axisreturn length.resolve(self.frame.page.getViewport());}+/// A block-level flex or grid container in normal flow, with an auto width,+/// that isn't a flex or grid item itself. Its width comes from its containing+/// block: its items overflow it instead of widening it.+pub fn widthFromContainingBlock(self: *StyleManager, el: *Element) bool {+ self.assertOwns(el);+ self.rebuildIfDirty() catch return false;+ if (!self.visibilityProps(el).block_flex_or_grid or self.geometryProps(el).width.unit != .auto) {+ return false;+ }+ const flow = self.flow.ownProps(self.arena.allocator(), el, self.frame);+ if (flow.absolutely_positioned or flow.floated) {+ return false;+ }+ const parent = el.asNode().flatTreeParentElement(self.frame) orelse return true;+ return switch (self.visibilityProps(parent).display) {+ .flex, .grid => false,+ else => true,+ };+}+/// Callers must have run rebuildIfDirty, which resets the memo.fn visibilityProps(self: *StyleManager, el: *Element) Visibility.Computed {return self.visibility.inheritedProps(self.arena.allocator(), el, self.frame);@@ -1342,14 +1363,18 @@ pub const TextTransform = enum(u3) {};/// A declared width or height. Values that need layout (auto, %, em) are-/// `auto`: they win the cascade but give no size.-pub const Length = packed struct(u34) {+/// `auto`: they win the cascade but give no size. The intrinsic sizes, which+/// follow the content, are `content`.+pub const Length = packed struct(u35) {value: f32 = 0,unit: Unit = .auto,- const Unit = enum(u2) { auto, px, vw, vh };+ const Unit = enum(u3) { auto, content, px, vw, vh };fn parse(text: []const u8) Length {+ if (isIntrinsic(text)) {+ return .{ .unit = .content };+ }const parsed = units.parse(text) catch return .{};if (parsed.value < 0) {return .{};@@ -1363,10 +1388,19 @@ pub const Length = packed struct(u34) {};}+ fn isIntrinsic(text: []const u8) bool {+ inline for (.{ "min-content", "max-content", "fit-content" }) |keyword| {+ if (std.ascii.eqlIgnoreCase(text, keyword)) {+ return true;+ }+ }+ return std.ascii.startsWithIgnoreCase(text, "fit-content(");+ }+fn resolve(self: Length, viewport: Viewport) ?f64 {const value: f64 = self.value;return switch (self.unit) {- .auto => null,+ .auto, .content => null,.px => value,.vw, .vh => value * @as(f64, @floatFromInt(if (self.unit == .vw) viewport.width else viewport.height)) / 100.0,};@@ -1379,6 +1413,7 @@ const Visibility = struct {const names = [_][]const u8{ "display", "visibility", "opacity", "pointer-events" };display: ?Display = null,+ block_flex_or_grid: ?bool = null,// Inner null: an explicit inherit, which still wins the cascadevisibility_hidden: ??bool = null,opacity_zero: ?bool = null,@@ -1387,6 +1422,7 @@ const Visibility = struct {fn apply(self: *Declared, name: []const u8, value: []const u8) void {if (std.ascii.eqlIgnoreCase(name, "display")) {self.display = Display.parse(value);+ self.block_flex_or_grid = std.ascii.eqlIgnoreCase(value, "flex") or std.ascii.eqlIgnoreCase(value, "grid");} else if (std.ascii.eqlIgnoreCase(name, "visibility")) {self.visibility_hidden = parseInheritedFlag(value, &.{ "hidden", "collapse" }, &.{ "visible", "initial" });} else if (std.ascii.eqlIgnoreCase(name, "opacity")) {@@ -1406,6 +1442,7 @@ const Visibility = struct {// Author value (inline or sheet). Without `author_display` it's the UA// fallback: .none when matchesUaDisplayNoneRule, else .other.display: Display = .other,+ block_flex_or_grid: bool = false,author_display: bool = false,// Null takes the parent's valuevisibility_hidden: ?bool = null,@@ -1413,8 +1450,9 @@ const Visibility = struct {pointer_events_none: ?bool = null,};- const Computed = packed struct(u7) {+ const Computed = packed struct(u8) {display: Display = .other,+ block_flex_or_grid: bool = false,author_display: bool = false,visibility_hidden: bool = false, // inheritedpointer_events_none: bool = false, // inherited@@ -1424,6 +1462,7 @@ const Visibility = struct {fn resolve(own: Cascaded, parent: Computed) Computed {return .{.display = own.display,+ .block_flex_or_grid = own.block_flex_or_grid,.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,@@ -1495,7 +1534,7 @@ const Geometry = struct {}};- const Computed = packed struct(u72) {+ const Computed = packed struct(u74) {overflow_x_scrolls: bool = false,overflow_y_scrolls: bool = false,overscroll_x_contains: bool = false,@@ -1583,15 +1622,40 @@ const Text = struct {};};+/// Whether a box is out of normal flow, which sizes it to its content.+const Flow = struct {+ const Declared = struct {+ const names = [_][]const u8{ "position", "float" };++ absolutely_positioned: ?bool = 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");+ } else if (std.ascii.eqlIgnoreCase(name, "float")) {+ self.floated = std.ascii.eqlIgnoreCase(value, "left") or std.ascii.eqlIgnoreCase(value, "right") or+ std.ascii.eqlIgnoreCase(value, "inline-start") or std.ascii.eqlIgnoreCase(value, "inline-end");+ }+ }+ };++ const Computed = packed struct(u2) {+ absolutely_positioned: bool = false,+ floated: bool = false,+ };+};+/// Every group's share of one declaration block, so a sheet's block is folded/// once. Field names match the StyleManager's group fields.const Declarations = struct {- const names = Visibility.Declared.names ++ Geometry.Declared.names ++ TouchAction.Declared.names ++ Text.Declared.names;+ const names = Visibility.Declared.names ++ Geometry.Declared.names ++ TouchAction.Declared.names ++ Text.Declared.names ++ Flow.Declared.names;visibility: Visibility.Declared = .{},geometry: Geometry.Declared = .{},touch_action: TouchAction.Declared = .{},text: Text.Declared = .{},+ flow: Flow.Declared = .{},fn apply(self: *Declarations, name: []const u8, value: []const u8) void {inline for (group_fields) |field| {@@ -2242,13 +2306,17 @@ test "StyleManager: inlineDeclared: scan matches the parsed style object" {\\<i style="height: 10vh; height: auto"></i>\\<i style="width: 50%; height: -5px"></i>\\<i style="width: 2em !important; width: 10vw"></i>+ \\<i style="display: flex"></i>+ \\<i style="display: inline-grid"></i>+ \\<i style="width: max-content; position: absolute; float: left"></i>+ \\<i style="width: fit-content(10px); position: relative; float: none"></i>);const expected = [_]Declarations{- .{ .visibility = .{ .display = .none } },- .{ .visibility = .{ .display = .none } },- .{ .visibility = .{ .display = .none } },- .{ .visibility = .{ .display = .other } },- .{ .visibility = .{ .display = .none, .visibility_hidden = true } },+ .{ .visibility = .{ .display = .none, .block_flex_or_grid = false } },+ .{ .visibility = .{ .display = .none, .block_flex_or_grid = false } },+ .{ .visibility = .{ .display = .none, .block_flex_or_grid = false } },+ .{ .visibility = .{ .display = .other, .block_flex_or_grid = false } },+ .{ .visibility = .{ .display = .none, .block_flex_or_grid = false, .visibility_hidden = true } },.{ .visibility = .{ .opacity_zero = true, .pointer_events_none = true } },.{ .visibility = .{ .opacity_zero = false, .pointer_events_none = true } },.{ .visibility = .{ .visibility_hidden = true } },@@ -2265,6 +2333,10 @@ test "StyleManager: inlineDeclared: scan matches the parsed style object" {.{ .geometry = .{ .height = .{} } },.{ .geometry = .{ .width = .{}, .height = .{} } },.{ .geometry = .{ .width = .{} } },+ .{ .visibility = .{ .display = .flex, .block_flex_or_grid = true } },+ .{ .visibility = .{ .display = .grid, .block_flex_or_grid = false } },+ .{ .geometry = .{ .width = .{ .unit = .content } }, .flow = .{ .absolutely_positioned = true, .floated = true } },+ .{ .geometry = .{ .width = .{ .unit = .content } }, .flow = .{ .absolutely_positioned = false, .floated = false } },};var i: usize = 0;@@ -2276,6 +2348,7 @@ test "StyleManager: inlineDeclared: scan matches the parsed style object" {try std.testing.expectEqual(expected[i], scanned);try std.testing.expectEqual(expected[i].visibility, inlineDeclared(Visibility.Declared, el, frame).props(.any));try std.testing.expectEqual(expected[i].geometry, inlineDeclared(Geometry.Declared, el, frame).props(.any));+ try std.testing.expectEqual(expected[i].flow, inlineDeclared(Flow.Declared, el, frame).props(.any));// scanning never creates the style objecttry testing.expectEqual(null, el.existingStyle(frame));const materialized = extractDeclared(Declarations, try el.getOrCreateStyle(frame)).props(.any);diff --git a/src/browser/tests/element/position.html b/src/browser/tests/element/position.htmlindex 03011d772..76ef84db9 100644--- a/src/browser/tests/element/position.html+++ b/src/browser/tests/element/position.html@@ -280,6 +280,59 @@}</script>+<style>+ .flex-row { display: flex; }+</style>++<script id="flexContainerMarqueeLoop">+{+ // A marquee clones its item into a block-level flex container until there+ // are ceil(container / item) + 2 of them, re-reading both widths after each+ // clone.+ const width = (el) => el.getBoundingClientRect().width;+ const container = document.createElement('div');+ container.className = 'flex-row';+ const item = document.createElement('div');+ item.textContent = 'item';+ container.appendChild(item);+ document.body.appendChild(container);++ let clones = 0;+ for (let i = 0; i < Math.ceil(width(container) / width(item)) + 2 && clones < 50; i++) {+ container.appendChild(item.cloneNode(true));+ clones++;+ }+ testing.expectTrue(clones < 50);+ testing.expectTrue(container.scrollWidth > container.clientWidth);+}+</script>++<script id="boxesSizedByTheirItems">+{+ const grows = (setup) => {+ const box = document.createElement('div');+ box.appendChild(document.createElement('span'));+ (setup(box) ?? document.body).appendChild(box);+ const before = box.clientWidth;+ box.appendChild(document.createElement('span'));+ box.appendChild(document.createElement('span'));+ return box.clientWidth > before;+ };++ testing.expectTrue(grows((box) => { box.style.display = 'inline-flex'; }));+ testing.expectTrue(grows((box) => { box.style.cssText = 'display: flex; width: max-content'; }));+ testing.expectTrue(grows((box) => { box.style.cssText = 'display: grid; position: absolute'; }));+ testing.expectTrue(grows((box) => { box.style.cssText = 'display: flex; float: left'; }));+ testing.expectTrue(grows((box) => {+ box.style.display = 'flex';+ const row = document.createElement('div');+ row.className = 'flex-row';+ document.body.appendChild(row);+ return row;+ }));+}+</script>+<script id="scrollHeightFromContent">{// An empty element has no content to overflow its box.diff --git a/src/browser/webapi/Element.zig b/src/browser/webapi/Element.zigindex 12b85d2ef..e43faa8f4 100644--- a/src/browser/webapi/Element.zig+++ b/src/browser/webapi/Element.zig@@ -1688,6 +1688,13 @@ pub fn boxAxis(self: *Element, frame: *Frame, comptime axis: Axis) f64 {return own.value;}+ if (axis == .width) {+ const owner = self.ownerFrame(frame) orelse return own.value;+ if (owner._style_manager.widthFromContainingBlock(self)) {+ return own.value;+ }+ }+return @max(own.value, self.contentAxis(frame, axis));}@@ -1837,9 +1844,8 @@ fn scrollExtent(self: *Element, frame: *Frame, comptime axis: Axis) ?f64 {// The dummy layout engine has no line-breaking, and an element only overflows// horizontally when its children don't wrap (white-space:nowrap, a flex row, an// inline-block strip), so the single-row assumption covers the case that-// matters. We can't detect the layout mode to do better: existingStyle() sees only-// the inline `style=` attribute, and the computed cascade resolves stylesheet-// rules for `display:none` and `visibility` alone.+// matters. We don't detect the layout mode to do better: the cascade resolves+// too few properties to lay out lines.//// Only direct children are measured, never the whole subtree. This runs on// every size read, and recursing would make an element's cost O(subtree)