Skip to content

Lightpanda: the headless browser designed for AI and automation

Trim comments

Adrià Arrufat20b0943a21a7
diff --git a/src/browser/tests/element/position.html b/src/browser/tests/element/position.html
index 45877d739..17568d68c 100644
--- a/src/browser/tests/element/position.html
+++ b/src/browser/tests/element/position.html
@@ -675,7 +675,7 @@
<script id="textWrapsAtExplicitWidth">
{
- // Text wraps at an explicit width, so it overflows the box vertically
+ // Text wraps at an explicit width
const box = $('#textBox');
testing.expectEqual(100, box.clientHeight);
testing.expectEqual(200, box.scrollWidth);
@@ -685,10 +685,9 @@
testing.expectTrue(box.scrollTop > 0);
testing.expectEqual(box.scrollHeight - box.clientHeight, box.scrollTop);
- // Without a width to wrap at, text isn't measured
+ // Without one, it isn't measured
testing.expectEqual(5, $('#textFree').scrollHeight);
- // A smaller font takes fewer, shorter lines
const small = $('#textSmall');
const before = small.clientHeight;
small.style.fontSize = '4px';
diff --git a/src/browser/text_measure.zig b/src/browser/text_measure.zig
index 5baae630d..31edde05f 100644
--- a/src/browser/text_measure.zig
+++ b/src/browser/text_measure.zig
@@ -66,17 +66,15 @@ pub fn substringWidth(text: []const u8, charnum: u32, nchars: u32, font_size: f6
/// Line height for `line-height: normal`, in em.
pub const LINE_HEIGHT = 1.2;
-/// Greedy line breaking under `white-space: normal`: whitespace runs collapse
-/// to one space, lines break between words, and a word wider than the line
-/// overflows it rather than splitting. Text can arrive in pieces, one per text
-/// node, and flows on from where the previous piece stopped.
+/// Greedy line breaking under `white-space: normal`. Text can be added in
+/// pieces (one per text node) that flow on from each other.
pub const LineWrap = struct {
line_width: f64,
font_size: f64,
lines: u32 = 0,
- // Width already taken on the current line.
+ // Width used on the current line.
x: f64 = 0,
- // A collapsed space is due before the next word.
+ // A space is due before the next word.
space: bool = false,
pub fn add(self: *LineWrap, text: []const u8) void {
@@ -157,22 +155,20 @@ test "fallback metrics count utf-16 units and ignore combining marks" {
}
test "LineWrap: collapses whitespace and breaks between words" {
- // At 10px a letter is 6px and a space 3.3px: "aaaa" is 24px wide
+ // At 10px: a letter is 6px, a space 3.3px
var wrap: LineWrap = .{ .line_width = 60, .font_size = 10 };
try std.testing.expectEqual(0, wrap.height());
- // Two words and a space fit in 60px, the third wraps
wrap.add(" aaaa \n\t aaaa aaaa ");
try std.testing.expectEqual(2, wrap.lines);
- // The next piece flows on after the trailing space
wrap.add("aaaa");
try std.testing.expectEqual(2, wrap.lines);
wrap.add("aaaa");
try std.testing.expectEqual(3, wrap.lines);
try std.testing.expectApproxEqAbs(36, wrap.height(), 0.0001);
- // A word wider than the line takes a line of its own, unsplit
+ // Long words aren't split
var narrow: LineWrap = .{ .line_width = 10, .font_size = 10 };
narrow.add("aaaa aaaa");
try std.testing.expectEqual(2, narrow.lines);
diff --git a/src/browser/webapi/Element.zig b/src/browser/webapi/Element.zig
index 5c5331ca1..373e142e1 100644
--- a/src/browser/webapi/Element.zig
+++ b/src/browser/webapi/Element.zig
@@ -1746,11 +1746,8 @@ fn scrollExtent(self: *Element, frame: *Frame, comptime axis: Axis) ?f64 {
// `scrollWidth` passes a threshold (the infinite-marquee idiom) never
// terminates when the metric ignores what it just inserted.
//
-// Text children count only toward the height, and only under an explicit
-// width: that is a line length to wrap at, and wrapped text never overflows
-// sideways. Without one, a few words would already exceed the default box and
-// report overflow for practically every element containing text. The advance
-// tracks the font size, so "shrink the font until it fits" loops converge.
+// Text children add height only under an explicit width to wrap at.
+// Otherwise almost every element with text would report overflow.
fn contentAxis(self: *Element, frame: *Frame, comptime axis: Axis) f64 {
var total: f64 = 0;
const owner = self.ownerFrame(frame) orelse return 0;
@@ -1778,7 +1775,7 @@ fn contentAxis(self: *Element, frame: *Frame, comptime axis: Axis) f64 {
}
if (wrap) |w| {
- // Whole pixels, like the scroll offsets clamped against it
+ // Whole pixels, like scroll offsets
total += @ceil(w.height());
}
return total;