Skip to content

Lightpanda: the headless browser designed for AI and automation

webapi: width is relative to its boxed container, not its parent

Karl Seguin454d3d506065
diff --git a/src/browser/StyleManager.zig b/src/browser/StyleManager.zig
index 0ea22c5e5..5c1b98add 100644
--- a/src/browser/StyleManager.zig
+++ b/src/browser/StyleManager.zig
@@ -914,6 +914,41 @@ fn generatesBox(self: *StyleManager, el: *Element) bool {
return parent._type == .document;
}
+/// The nearest ancestor whose width el's percentages and fill resolve
+/// against. A plain inline box is skipped: a block inside a span sizes off
+/// the block around the span.
+pub fn containingBlock(self: *StyleManager, el: *Element) ?*Element {
+ self.assertOwns(el);
+ return self.blockContainer(self.boxParent(el) orelse return null);
+}
+
+/// el, if it's a block container (anything but a plain inline box), else
+/// its containing block. Text in el wraps at this box's width.
+pub fn blockContainer(self: *StyleManager, el: *Element) ?*Element {
+ self.assertOwns(el);
+ self.rebuildIfDirty() catch return el;
+ var current: ?*Element = el;
+ while (current) |c| : (current = self.boxParent(c)) {
+ if (self.isPlainInline(c) == false) {
+ return c;
+ }
+ }
+ return null;
+}
+
+fn isPlainInline(self: *StyleManager, el: *Element) bool {
+ if (self.visibilityProps(el).box != .inline_level or el.getTag().isReplacedOrControl()) {
+ return false;
+ }
+ // Floats, out-of-flow boxes and flex/grid items are blockified
+ const flow = self.flowProps(el);
+ if (flow.position.outOfFlow() or flow.floated) {
+ return false;
+ }
+ const parent = self.boxParent(el) orelse return false;
+ return self.visibilityProps(parent).display != .flex and self.visibilityProps(parent).display != .grid;
+}
+
/// The children lay out side by side: a flex row or a grid. Elsewhere block
/// children stack and only inline-level ones share a line.
pub fn laysOutInRow(self: *StyleManager, el: *Element) bool {
@@ -928,7 +963,7 @@ pub fn isBlockLevel(self: *StyleManager, el: *Element) bool {
self.rebuildIfDirty() catch return false;
switch (self.visibilityProps(el).box) {
.block_level, .table => {},
- .inline_level, .contents => return false,
+ .inline_level, .atomic_inline, .contents => return false,
}
const flow = self.flowProps(el);
return flow.position.outOfFlow() == false and flow.floated == false;
@@ -1613,9 +1648,12 @@ pub const Display = enum(u2) {
}
};
-const Box = enum(u2) {
+const Box = enum(u3) {
block_level,
+ // A plain inline box: not a block container, so not a containing block
inline_level,
+ // inline-block, inline-flex, ...: inline outside, block container inside
+ atomic_inline,
contents,
table,
@@ -1629,9 +1667,12 @@ const Box = enum(u2) {
return .table;
}
- if (std.ascii.startsWithIgnoreCase(trimmed, "inline")) {
+ if (std.ascii.eqlIgnoreCase(trimmed, "inline") or std.ascii.eqlIgnoreCase(trimmed, "inline flow")) {
return .inline_level;
}
+ if (std.ascii.startsWithIgnoreCase(trimmed, "inline")) {
+ return .atomic_inline;
+ }
inline for (.{ "block", "flow-root", "list-item", "flex", "grid", "flow" }) |keyword| {
if (std.ascii.eqlIgnoreCase(trimmed, keyword)) {
@@ -1971,7 +2012,7 @@ const Visibility = struct {
outside_flat_tree: bool = false,
};
- const Computed = packed struct(u10) {
+ const Computed = packed struct(u11) {
display: Display = .other,
box: Box = .inline_level,
flex_column: bool = false,
@@ -2981,7 +3022,7 @@ test "StyleManager: inlineDeclared: scan matches the parsed style object" {
.{ .geometry = .{ .width = .{ .value = 50, .unit = .percent }, .height = .{} } },
.{ .geometry = .{ .width = .{} } },
.{ .visibility = .{ .display = .flex, .box = .block_level } },
- .{ .visibility = .{ .display = .grid, .box = .inline_level } },
+ .{ .visibility = .{ .display = .grid, .box = .atomic_inline } },
.{ .geometry = .{ .width = .{ .unit = .content } }, .flow = .{ .position = .absolute, .floated = true } },
.{ .geometry = .{ .width = .{ .unit = .content } }, .flow = .{ .position = .relative, .floated = false } },
};
diff --git a/src/browser/tests/element/position.html b/src/browser/tests/element/position.html
index c43e54965..41b21e826 100644
--- a/src/browser/tests/element/position.html
+++ b/src/browser/tests/element/position.html
@@ -474,6 +474,64 @@
}
</script>
+<script id="inlineBoxIsNotAContainingBlock">
+{
+ // A block inside an inline box sizes off the block around it (#4213: a
+ // width:100% flex row inside an unstyled custom element or span).
+ const outer = document.createElement('div');
+ outer.style.width = '800px';
+ document.body.appendChild(outer);
+
+ for (const wrapperTag of ['demo-app', 'span']) {
+ const wrapper = document.createElement(wrapperTag);
+ const row = document.createElement('div');
+ row.style.cssText = 'display: flex; width: 100%';
+ const menu = document.createElement('div');
+ menu.style.cssText = 'width: 40px; flex-shrink: 0';
+ const pane = document.createElement('div');
+ pane.style.cssText = 'flex: 1; min-width: 0';
+ row.append(menu, pane);
+ wrapper.appendChild(row);
+ outer.appendChild(wrapper);
+ testing.expectEqual(800, row.offsetWidth);
+ testing.expectEqual(760, pane.clientWidth);
+
+ const fill = document.createElement('div');
+ wrapper.appendChild(fill);
+ testing.expectEqual(800, fill.offsetWidth);
+
+ // Nested inlines are skipped too
+ const inner = document.createElement('span');
+ const half = document.createElement('div');
+ half.style.width = '50%';
+ inner.appendChild(half);
+ wrapper.appendChild(inner);
+ testing.expectEqual(400, half.offsetWidth);
+
+ wrapper.style.display = 'inline';
+ testing.expectEqual(760, pane.clientWidth);
+ wrapper.style.display = 'block';
+ testing.expectEqual(760, pane.clientWidth);
+ wrapper.remove();
+ }
+
+ // Inline-blocks, floats and flex items are block containers
+ const box = document.createElement('span');
+ const child = document.createElement('div');
+ child.style.width = '100%';
+ box.appendChild(child);
+ outer.appendChild(box);
+ for (const css of ['display: inline-block; width: 300px', 'float: left; width: 300px', 'position: absolute; width: 300px']) {
+ box.style.cssText = css;
+ testing.expectEqual(300, child.offsetWidth);
+ }
+ box.style.cssText = 'width: 300px';
+ outer.style.display = 'flex';
+ testing.expectEqual(300, child.offsetWidth);
+ outer.remove();
+}
+</script>
+
<script id="growShareAfterPercentSiblings">
{
// A percentage sibling takes its share of the row before the growers
diff --git a/src/browser/tests/range.html b/src/browser/tests/range.html
index f249545c0..4b953c6ec 100644
--- a/src/browser/tests/range.html
+++ b/src/browser/tests/range.html
@@ -1109,6 +1109,30 @@
testing.expectEqual(one.height, nowrap.height);
}
+ // Text wraps in the nearest block container: inline-blocks, floats,
+ // out-of-flow boxes and flex items are ones, a plain inline isn't
+ {
+ function spanTextRect(html) {
+ host.innerHTML = html;
+ const range = new Range();
+ range.selectNode(host.querySelector('span').firstChild);
+ return range.getBoundingClientRect();
+ }
+
+ const block = textRect('<div style="width:2px">abc ab</div>');
+ for (const html of [
+ '<span style="display:inline-block;width:2px">abc ab</span>',
+ '<span style="float:left;width:2px">abc ab</span>',
+ '<span style="position:absolute;width:2px">abc ab</span>',
+ '<div style="display:flex"><span style="width:2px">abc ab</span></div>',
+ '<div style="width:2px"><span>abc ab</span></div>',
+ ]) {
+ const rect = spanTextRect(html);
+ testing.expectEqual(block.width, rect.width);
+ testing.expectEqual(block.height, rect.height);
+ }
+ }
+
// A range that only covers part of the text measures that part
{
host.innerHTML = '<div>abcdef</div>';
diff --git a/src/browser/webapi/Element.zig b/src/browser/webapi/Element.zig
index 0276102d3..6e0bae015 100644
--- a/src/browser/webapi/Element.zig
+++ b/src/browser/webapi/Element.zig
@@ -1745,9 +1745,9 @@ fn resolveWidthLimit(self: *Element, limit: StyleManager.WidthLimit, resolve_per
};
}
-/// The width of the nearest ancestor with a box; the viewport above the root.
+/// The width of the containing block; the viewport above the root.
fn containingBlockWidth(self: *Element, owner: *Frame) f64 {
- const parent = owner._style_manager.boxParent(self) orelse {
+ const parent = owner._style_manager.containingBlock(self) orelse {
return @floatFromInt(owner.page.getViewport().width);
};
return parent.boxWidth(owner);
diff --git a/src/browser/webapi/Range.zig b/src/browser/webapi/Range.zig
index c39309faa..755ef8e38 100644
--- a/src/browser/webapi/Range.zig
+++ b/src/browser/webapi/Range.zig
@@ -916,14 +916,10 @@ fn measureText(parent: *Node.Element, text: []const u8, frame: *Frame) ?DOMRect.
}
fn lineWidth(box: *Node.Element, owner: *Frame) f64 {
- const style_manager = &owner._style_manager;
- var current: ?*Node.Element = box;
- while (current) |el| : (current = style_manager.boxParent(el)) {
- if (style_manager.isBlockLevel(el)) {
- return el.boxAxis(owner, .width);
- }
- }
- return @floatFromInt(owner.page.getViewport().width);
+ const block = owner._style_manager.blockContainer(box) orelse {
+ return @floatFromInt(owner.page.getViewport().width);
+ };
+ return block.boxAxis(owner, .width);
}
fn getContainerElement(self: *const Range, frame: *Frame) ?*Node.Element {