The design language that makes your AI harness better at design.
JavaScript
tight-leading and text-overflow measure the lines and text a reader sees
Paul Bakaus7ec92d7dba12
diff --git a/crates/browser/tests/text_geometry.rs b/crates/browser/tests/text_geometry.rsindex b652567b8..c4ea591ac 100644--- a/crates/browser/tests/text_geometry.rs+++ b/crates/browser/tests/text_geometry.rs@@ -172,7 +172,9 @@ fn the_text_geometry_rules_measure_the_text() {// Bold titles of two lines or fewer, or in a -webkit-box line clamp, are// exempt (taste call r3-03), a clamp that hides nothing included; bold// body text of four lines, a weight-500 title, regular copy in a clamp and- // bold text a max-height clip shows three lines of keep the floor.+ // bold text a max-height clip shows three lines of keep the floor. A flex+ // row is measured run by run: short runs wrapped as whole items pass, a+ // run long enough to wrap in its item keeps the floor.let leading = findings(&engine, port, "tight-leading.html", "tight-leading");assert_cases(&leading,@@ -183,8 +185,10 @@ fn the_text_geometry_rules_measure_the_text() {"medium-slot-title","clamped-regular-blurb","bold-clipped-summary",+ "flex-long-run",],&[+ "items-row","teaser-copy","link-run","bold-slot-title",@@ -194,7 +198,7 @@ fn the_text_geometry_rules_measure_the_text() {],"tight-leading",);- assert_eq!(leading.len(), 9, "the nine flag cases: {leading:?}");+ assert_eq!(leading.len(), 10, "the ten flag cases: {leading:?}");// A chip under 27.5px tall is measured by its glyphs (taste call r3-20):// the step chip's line box holds them 5px off its edges, the 20px chip's@@ -241,6 +245,8 @@ fn text_overflow_and_edge_flush_cards_read_the_x_axis() {"pass-free-space-pre","pass-free-space-headline","pass-under-hairline",+ "pass-hover-tooltip",+ "pass-corner-badge",],"text-overflow",);diff --git a/crates/core/src/browser/element_checks.rs b/crates/core/src/browser/element_checks.rsindex 4388fb7b5..f237cd9c4 100644--- a/crates/core/src/browser/element_checks.rs+++ b/crates/core/src/browser/element_checks.rs@@ -2589,24 +2589,30 @@ fn generated_content_unmeasured(dom: &dyn Dom, el: ElId) -> bool {/// The rects of what `el`'s descendants paint, for deciding whether its/// overflow is seen: text by its text rect, replaced elements and painted-/// boxes by their border boxes. A descendant with no text that is absolutely-/// or fixed positioned (a ripple layer), or that paints nothing (an empty-/// wrapper, a `min-width` reserve), adds nothing of its own. A descendant that-/// clips on the x axis keeps its content inside its own box. `unmeasured` is-/// set when a descendant carries generated content no rect covers.+/// boxes by their border boxes. A descendant that is absolutely or fixed+/// positioned is out of the flow `el` lays out: a ripple layer, a hover+/// tooltip parked above a link (clipto.com's QR card at opacity 0), a badge+/// pinned past a tab's corner (coachcall.ai's `-right-8` "Popular"). Its+/// author placed it there, its text is not `el`'s text running out of its+/// box, and it adds nothing, text or not. So does a descendant that paints+/// nothing (an empty wrapper, a `min-width` reserve). A descendant that clips+/// on the x axis keeps its content inside its own box. `unmeasured` is set+/// when a descendant carries generated content no rect covers.fn painted_descendant_extents(dom: &dyn Dom, el: ElId, out: &mut Vec<Rect>, unmeasured: &mut bool) {for child in dom.children(el) {if dom.style(child, "display") == "none" {continue;}+ // Generated content no rect covers leaves the overflow unmeasured,+ // on a positioned descendant too: what it adds cannot be told apart.if generated_content_unmeasured(dom, child) {*unmeasured = true;}- let has_text = !js::trim(&dom.text_content(child)).is_empty();let position = dom.style(child, "position");- if !has_text && (position == "absolute" || position == "fixed") {+ if position == "absolute" || position == "fixed" {continue;}+ let has_text = !js::trim(&dom.text_content(child)).is_empty();let r = dom.rect(child);let has_area = r.all_finite() && r.width > 0.0 && r.height > 0.0;if REPLACED_TAGS.contains(&tag_lower(dom, child).as_str()) {@@ -5533,6 +5539,35 @@ mod tests {assert_eq!(hits[0].snippet, "div.cell overflows its box by 40px");}+ /// observations-28 row 24: `scrollWidth` counts absolutely positioned+ /// descendants. coachcall.ai's tab carries a "Popular" badge pinned past+ /// its corner, into the next tab; the tab's own text fits its box.+ #[test]+ fn text_overflow_leaves_out_positioned_descendants() {+ let (mut d, body) = page();+ let tab = d.add(Some(body), "button");+ visible(&mut d, tab);+ d.set_attr(tab, "class", "tab");+ d.add_text(tab, "ACCOUNTABILITY");+ d.set_rect(tab, 16.0, 128.0, 120.0, 40.0);+ d.set_text_rect(tab, 16.0, 141.0, 120.0, 17.0);+ d.el_mut(tab).client_width = 120.0;+ d.el_mut(tab).client_height = 40.0;+ d.el_mut(tab).scroll_width = 152.0;+ d.set_styles(tab, &[("display", "block"), ("position", "relative"), ("overflowX", "visible"), ("fontSize", "14px")]);+ let badge = d.add(Some(tab), "div");+ visible(&mut d, badge);+ d.add_text(badge, "Popular");+ d.set_rect(badge, 109.0, 124.0, 59.0, 16.0);+ d.set_text_rect(badge, 116.0, 124.0, 45.0, 15.0);+ d.set_styles(badge, &[("position", "absolute"), ("backgroundColor", "rgb(219, 234, 254)")]);+ neighbor(&mut d, body, 168.0, 128.0, 60.0, 40.0, "Pricing");+ assert!(check_element_text_overflow_dom(&d, tab).is_empty(), "positioned badge");+ // The same badge laid out in flow is the tab's content running out.+ d.set_style(badge, "position", "static");+ assert_eq!(check_element_text_overflow_dom(&d, tab).len(), 1, "in-flow badge");+ }+#[test]fn text_overflow_skips_a_marked_or_clamped_truncation() {let (mut d, body) = page();diff --git a/crates/core/src/browser/quality.rs b/crates/core/src/browser/quality.rsindex 9f4e28936..97bc7160d 100644--- a/crates/core/src/browser/quality.rs+++ b/crates/core/src/browser/quality.rs@@ -228,6 +228,57 @@ pub fn glyph_band(dom: &dyn Dom, node: ElId, t: &Rect) -> Rect {Rect::from_xywh(t.left, t.top + inset, t.width, t.height - inset * 2.0)}+/// The widest a glyph of `c` sets, in ems, as an upper bound: a full-width+/// CJK, kana or Hangul glyph is an em, a capital runs to about three+/// quarters, and every other glyph of a proportional face stays under 0.7em.+fn glyph_advance_bound_em(c: char) -> f64 {+ let cp = c as u32;+ let wide = matches!(cp,+ 0x1100..=0x115F | 0x2E80..=0x303F | 0x3040..=0x33FF | 0x3400..=0x4DBF+ | 0x4E00..=0x9FFF | 0xA960..=0xA97F | 0xAC00..=0xD7FF | 0xF900..=0xFAFF+ | 0xFE30..=0xFE4F | 0xFF00..=0xFF60 | 0xFFE0..=0xFFE6 | 0x20000..=0x3FFFF);+ if wide {+ 1.05+ } else if c.is_uppercase() {+ 0.8+ } else {+ 0.7+ }+}++/// Whether `el` is a flex or grid container whose own text is split into+/// several runs, each an anonymous item too short to wrap in the box. Each+/// run then sets one line: the union of their rects is two or more rows of+/// items (outreign.io's wrapped row of "100 free lead searches", "14-day+/// trial", ...), not two line boxes of one run, and there is no leading+/// between lines to crowd. A single run, or any run long enough that it could+/// wrap by the widest advance a face sets, is measured as before.+fn items_each_fit_one_line(dom: &dyn Dom, el: ElId, font_size: f64) -> bool {+ let display = dom.style(el, "display");+ if !matches!(display.as_str(), "flex" | "inline-flex" | "grid" | "inline-grid") {+ return false;+ }+ let runs: Vec<String> = dom+ .direct_text_nodes(el)+ .into_iter()+ .map(|t| collapse_ws(js::trim(&t)))+ .filter(|t| !t.is_empty())+ .collect();+ if runs.len() < 2 {+ return false;+ }+ let content_width = dom.client_width(el)+ - js::math_max(0.0, style_px(dom, el, "paddingLeft"))+ - js::math_max(0.0, style_px(dom, el, "paddingRight"));+ if !(content_width.is_finite() && content_width > 0.0 && font_size > 0.0) {+ return false;+ }+ runs.iter().all(|run| {+ let width: f64 = run.chars().map(glyph_advance_bound_em).sum::<f64>() * font_size;+ width < content_width+ })+}+/// JS: checks.mjs#textDescendantsFlushSides(el, rect) → [top, right, bottom, left]////// Each candidate is measured by its own text rect, not by its border box: a@@ -1127,7 +1178,8 @@ pub fn check_quality(dom: &dyn Dom, q: &QualityInput) -> Vec<RuleHit> {let shown = js::math_round(ratio * 100.0) / 100.0;if ratio > 0.0 && shown < 1.3 {let text_rect = dom.direct_text_rect(el).unwrap_or(*rect);- let wraps = text_rect.height >= lh * LEADING_MIN_LINE_BOXES;+ let wraps = text_rect.height >= lh * LEADING_MIN_LINE_BOXES+ && !items_each_fit_one_line(dom, el, font_size);// A bold run of two rendered lines or fewer, or one in a line// clamp, is a title set on a div or span: it gets the heading// exemption. Lines a clipping box cuts off do not render.@@ -3212,6 +3264,39 @@ mod tests {assert_eq!(snippets(&d, run, "tight-leading"), vec!["line-height 1.00x (need >=1.3)"]);}+ /// observations-28 row 25: outreign.io's hero meta row is a flex+ /// container whose three short text runs wrap as whole items onto two+ /// rows. The union of their rects is two rows tall, but no run has a+ /// second line box. A run long enough to wrap in the box is measured as+ /// before, and so is a single run.+ #[test]+ fn tight_leading_reads_flex_text_runs_as_items() {+ let mut d = FakeDom::new();+ let (_h, body) = d.with_page();+ let row = d.add(Some(body), "p");+ d.add_text(row, "100 free lead searches");+ let sep = d.add(Some(row), "span");+ d.set_rect(sep, 182.0, 674.0, 1.0, 12.0);+ d.add_text(row, "14-day trial on paid plans");+ d.add(Some(row), "span");+ d.add_text(row, "Built with you on a call");+ d.set_styles(+ row,+ &[("display", "flex"), ("fontSize", "12px"), ("lineHeight", "15px"), ("position", "static")],+ );+ d.set_rect(row, 27.0, 673.0, 336.0, 34.0);+ d.set_text_rect(row, 43.3, 673.0, 292.4, 34.0);+ d.el_mut(row).client_width = 336.0;+ assert!(snippets(&d, row, "tight-leading").is_empty(), "short runs, whole items");+ // A block that sets the same text as one run wraps its lines.+ d.set_style(row, "display", "block");+ assert_eq!(snippets(&d, row, "tight-leading"), vec!["line-height 1.25x (need >=1.3)"]);+ // A run wider than the box wraps inside its own item.+ d.set_style(row, "display", "flex");+ d.el_mut(row).client_width = 150.0;+ assert_eq!(snippets(&d, row, "tight-leading"), vec!["line-height 1.25x (need >=1.3)"]);+ }+/// walkthroughs-20 note 13: tchibo.de sets its teaser headlines as/// `<h5><div>…</div></h5>`. Any box inside a heading carries heading text,/// unless it is a reading block nested there.diff --git a/tests/fixtures/antipatterns/text-overflow.html b/tests/fixtures/antipatterns/text-overflow.htmlindex 609fb1e19..4628a6b65 100644--- a/tests/fixtures/antipatterns/text-overflow.html+++ b/tests/fixtures/antipatterns/text-overflow.html@@ -104,6 +104,19 @@<a class="box pass-ripple" href="#" style="display: inline-flex; position: relative; overflow: hidden; width: 120px">Shop now<span style="position: absolute; display: block; width: 340px; height: 340px; left: -110px; top: -150px"></span></a><a class="box pass-image-replacement" href="#" style="display: block; overflow: hidden; text-indent: -9999px; height: 20px">Internet access plans</a><div class="box pass-reserve">Durable streams on <span style="display: inline-block; min-width: 220px">storage</span></div>+ <!-- scrollWidth counts absolutely positioned descendants too. A hover+ tooltip parked above a platform name at opacity 0 (clipto.com) and+ a badge pinned past a tab's corner (coachcall.ai's "Popular") are+ placed there by their author, beside the next item; neither is the+ element's own text running out of its box. -->+ <div class="row pass-positioned-row" style="padding: 48px 0 0">+ <span class="pass-hover-tooltip" style="display: inline-block; position: relative">iOS<span style="position: absolute; left: 0; bottom: 100%; width: 184px; opacity: 0; background: #fff">Scan to download the app</span></span>+ <span>Android</span>+ </div>+ <div class="row" style="display: flex; column-gap: 32px; padding: 12px 0 0">+ <button class="pass-corner-badge" style="position: relative; border: 0; background: none; font: inherit; padding: 12px 0 8px">ACCOUNTABILITY<span style="position: absolute; top: -4px; right: -32px; padding: 0 6px; font-size: 12px; background: #dbeafe; border-radius: 999px">Popular</span></button>+ <button style="border: 0; background: none; font: inherit; padding: 12px 0 8px">Pricing</button>+ </div></section></div>diff --git a/tests/fixtures/antipatterns/tight-leading.html b/tests/fixtures/antipatterns/tight-leading.htmlindex c8e5605df..cd897b67e 100644--- a/tests/fixtures/antipatterns/tight-leading.html+++ b/tests/fixtures/antipatterns/tight-leading.html@@ -33,6 +33,9 @@.bold-clipped-summary { display: flow-root; overflow: hidden; max-height: 48px; width: 260px; font-size: 14px; line-height: 16px; font-weight: 700; }.clamped-regular-blurb { display: -webkit-box; -webkit-line-clamp: 3; -webkit-box-orient: vertical; overflow: hidden; width: 260px; font-size: 15px; line-height: 18px; font-weight: 400; }+ .flex-long-run { display: flex; flex-wrap: wrap; column-gap: 10px; width: 260px; font-size: 12px; line-height: 14px; margin: 0; }+ .run-sep { width: 1px; height: 12px; background: #cbd5e1; }+/* Should pass: display type, heading text, single lines, non-rendered. */.display-statement { width: 560px; font-size: 32px; line-height: 36px; margin: 0; }.headline-link { font-size: 30px; line-height: 34px; text-decoration: none; color: #1a1a1a; }@@ -49,6 +52,7 @@.bold-ticker-link { font-size: 16px; line-height: 19px; font-weight: 670; color: #1a1a1a; text-decoration: none; }.ticker-item { width: 440px; }.clamped-exact-title { display: -webkit-box; -webkit-line-clamp: 3; -webkit-box-orient: vertical; overflow: hidden; width: 260px; font-size: 14px; line-height: 16px; font-weight: 700; }+ .items-row { display: flex; flex-wrap: wrap; column-gap: 10px; row-gap: 4px; width: 260px; font-size: 12px; line-height: 15px; margin: 0; }.clamped-video-title { display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden; width: 290px; font-size: 15px; line-height: 18px; font-weight: 670; }</style><!-- SHOULD PASS: stylesheet source is text the browser never typesets. -->@@ -99,6 +103,12 @@show three of the lines, and bold text of three lines keeps thefloor. --><div class="bold-clipped-summary">The whole summary is set in bold and runs on well past three lines in this column, but a max-height with overflow hidden shows only the first three of them to the reader.</div>++ <h3>Flex row whose long text item wraps at 1.17</h3>+ <!-- A flex row that holds its text as separate runs is measured run by+ run: this first run is longer than the row is wide, so it wraps+ inside its own item and its lines crowd each other. -->+ <p class="flex-long-run">The first item of this flex row is a whole sentence of reading copy, long enough to wrap inside its own box<span class="run-sep"></span>Short note</p></div><div class="col" data-col="pass">@@ -178,6 +188,13 @@clamp itself is what exempts it. --><div class="clamped-exact-title">Two southern residents charged over an international trafficking ring that ran for years across the border</div>+ <h3>Flex row whose short text items wrap as whole items (browser engine only)</h3>+ <!-- SHOULD PASS in the browser engine: each run is an anonymous flex+ item that sets one line; the row wraps whole items onto a second+ row, which is not a second line box of any run (outreign.io's+ hero meta row). The static engine cannot see the items. -->+ <p class="items-row">100 free lead searches<span class="run-sep"></span>14-day trial on paid plans<span class="run-sep"></span>Built with you on a call</p>+<h3>Bold title in a two-line clamp</h3><!-- SHOULD PASS: weight 670 in a -webkit-box line clamp, however manylines the full text would run to. -->