The design language that makes your AI harness better at design.
JavaScript
line-length counts the characters that reached the rendered lines
Paul Bakaus8808f9065dac
diff --git a/crates/browser/tests/line_length_text_count.rs b/crates/browser/tests/line_length_text_count.rsnew file mode 100644index 000000000..57a626103--- /dev/null+++ b/crates/browser/tests/line_length_text_count.rs@@ -0,0 +1,108 @@+//! `line-length` counts the characters that reached the rendered lines, on+//! `line-length-text-count.html`, against an installed browser. Skips cleanly+//! when there is none.+//!+//! The rule divides an element's characters among its line rects. Counted+//! from `textContent`, a paragraph with an inline `<style>` child, deep+//! source indentation or a script written with combining marks was charged+//! with characters that are on no line, and a comfortable measure read as a+//! long column.++use std::collections::HashMap;+use std::io::{Read, Write};+use std::net::{TcpListener, TcpStream};+use std::path::{Path, PathBuf};++use impeccable_browser::BrowserEngine;+use impeccable_detect::engines::{ScanOptions, UrlEngine};++fn fixtures_dir() -> PathBuf {+ Path::new(env!("CARGO_MANIFEST_DIR")).join("../../tests/fixtures/antipatterns")+}++fn serve() -> u16 {+ let listener = TcpListener::bind("127.0.0.1:0").expect("bind");+ let port = listener.local_addr().unwrap().port();+ std::thread::spawn(move || {+ for stream in listener.incoming().flatten() {+ std::thread::spawn(move || handle(stream));+ }+ });+ port+}++fn handle(mut stream: TcpStream) {+ let mut buf = [0u8; 8192];+ let n = stream.read(&mut buf).unwrap_or(0);+ let request = String::from_utf8_lossy(&buf[..n]);+ let path = request+ .lines()+ .next()+ .and_then(|l| l.split_whitespace().nth(1))+ .unwrap_or("/")+ .split('?')+ .next()+ .unwrap_or("/")+ .to_string();+ let (status, body) = match std::fs::read(fixtures_dir().join(path.trim_start_matches('/'))) {+ Ok(body) => ("200 OK", body),+ Err(_) => ("404 Not Found", b"missing".to_vec()),+ };+ let head = format!(+ "HTTP/1.0 {status}\r\nContent-Type: text/html; charset=utf-8\r\nContent-Length: {}\r\nConnection: close\r\n\r\n",+ body.len()+ );+ let _ = stream.write_all(head.as_bytes());+ let _ = stream.write_all(&body);+ let _ = stream.flush();+}++fn engine() -> Option<BrowserEngine> {+ let env: HashMap<String, String> = std::env::vars().collect();+ if impeccable_browser::discovery::find_browser(&env).is_err() {+ eprintln!("skip: no installed browser found");+ return None;+ }+ Some(BrowserEngine::new(env))+}++/// `(snippet, selector)` for each finding of `rule` on one fixture.+fn findings(engine: &BrowserEngine, port: u16, fixture: &str, rule: &str) -> Vec<(String, String)> {+ let url = format!("http://127.0.0.1:{port}/{fixture}");+ engine+ .detect_url(&url, &ScanOptions::default())+ .expect("scan")+ .into_iter()+ .filter(|f| f.antipattern == rule)+ .map(|f| {+ let selector = f.extras.get("selector").and_then(|s| s.as_str()).unwrap_or("").to_string();+ (f.snippet, selector)+ })+ .collect()+}++#[test]+fn line_length_counts_the_characters_on_the_lines() {+ let Some(engine) = engine() else { return };+ let port = serve();+ let mut found: Vec<String> = findings(&engine, port, "line-length-text-count.html", "line-length")+ .into_iter()+ .map(|(snippet, _)| snippet)+ .collect();+ found.sort();+ // The two wide columns with a style child and with source indentation+ // read the same 141 characters a plain wide column does, and the+ // `pre-wrap` column, whose runs of spaces are on the line, reads 146.+ // The four comfortable measures (a style child, a hidden child and a+ // script, deep indentation, Devanagari) are absent: counted from+ // `textContent` they read 221, 222, 147 and 109, and the two wide+ // columns 156 and 164.+ assert_eq!(+ found,+ vec![+ "~141 chars on 3 of 3 rendered lines (aim for <80)".to_string(),+ "~141 chars on 3 of 3 rendered lines (aim for <80)".to_string(),+ "~146 chars on 2 of 3 rendered lines (aim for <80)".to_string(),+ ]+ );+}diff --git a/crates/core/src/browser/quality.rs b/crates/core/src/browser/quality.rsindex 67fe93ba2..e2426752d 100644--- a/crates/core/src/browser/quality.rs+++ b/crates/core/src/browser/quality.rs@@ -443,6 +443,84 @@ fn rendered_line_widths(dom: &dyn Dom, el: ElId) -> Option<Vec<f64>> {)}+/// Elements whose text is in `textContent` and never on a line.+const UNRENDERED_TEXT_TAGS: [&str; 4] = ["style", "script", "noscript", "template"];++static COMBINING_OR_FORMAT_RE: Lazy<Regex> =+ Lazy::new(|| Regex::new(r"[\p{M}\p{Cf}]").expect("COMBINING_OR_FORMAT_RE"));++/// The `textContent` of each topmost descendant of `el` that renders no+/// text: a `<style>`, `<script>`, `<noscript>` or `<template>`, and a+/// `display: none` box. Document order.+fn unrendered_text_runs(dom: &dyn Dom, el: ElId, out: &mut Vec<String>) {+ for child in dom.children(el) {+ let tag = tag_lower(dom, child);+ if UNRENDERED_TEXT_TAGS.contains(&tag.as_str()) || dom.style(child, "display") == "none" {+ let text = dom.text_content(child);+ if !text.is_empty() {+ out.push(text);+ }+ } else {+ unrendered_text_runs(dom, child, out);+ }+ }+}++/// How many characters of `el`'s text reached its rendered lines, which is+/// the count the line rects have to be divided among.+///+/// `textContent` is the source, not the rendering, and it runs over in three+/// ways. It includes the text of an inline `<style>` or `<script>` child and+/// of a `display: none` one, none of which is on any line. It keeps the+/// source's indentation and newlines, which `white-space: normal` collapses+/// to a single space. And it counts code units, so a combining mark, which+/// sits on its base and advances nothing, is charged as a character of its+/// own: a third of a Devanagari paragraph is marks.+///+/// So: the text with its unrendered descendants cut out, collapsible white+/// space folded to one space unless the element preserves it, and combining+/// marks and format characters (zero-width joiners, soft hyphens) left out.+fn rendered_text_len(dom: &dyn Dom, el: ElId) -> usize {+ let full = dom.text_content(el);+ let mut cuts = Vec::new();+ unrendered_text_runs(dom, el, &mut cuts);+ let mut text = String::with_capacity(full.len());+ let mut rest = full.as_str();+ // The runs are in document order and each is a substring of what is+ // left, so cutting the first occurrence of each walks the text once.+ for cut in &cuts {+ if let Some(at) = rest.find(cut.as_str()) {+ text.push_str(&rest[..at]);+ rest = &rest[at + cut.len()..];+ }+ }+ text.push_str(rest);++ let white_space = dom.style(el, "whiteSpace");+ let preserved = white_space == "pre" || white_space == "pre-wrap" || white_space == "break-spaces";+ let collapsible = |c: char| matches!(c, ' ' | '\t' | '\n' | '\r' | '\u{c}');+ let mut count = 0usize;+ let mut pending_space = false;+ let mut buf = [0u8; 4];+ for c in js::trim(&text).chars() {+ if collapsible(c) && !preserved {+ // Leading white space is dropped, a run counts once, and a+ // trailing run never gets counted because nothing follows it.+ pending_space = count > 0;+ continue;+ }+ if COMBINING_OR_FORMAT_RE.is_match(c.encode_utf8(&mut buf)) {+ continue;+ }+ if pending_space {+ count += 1;+ pending_space = false;+ }+ count += 1;+ }+ count+}+/// JS: checks.mjs#textDescendantsFlushSides(el, rect) → [top, right, bottom, left]////// The side is flush when the *text* lands on it, not when a text-bearing box@@ -1038,9 +1116,12 @@ pub fn check_quality(dom: &dyn Dom, q: &QualityInput) -> Vec<RuleHit> {// at one size, so the average advance is the same on every line of it.//// The rects cover the element's whole rendered text, descendants and all,- // which is the same text `text_len` counts: measuring the direct text- // alone and then charging it with the characters of an inline `<strong>`- // inflated every paragraph that had one.+ // so the characters divided among them are the rendered ones too+ // (`rendered_text_len`): measuring the direct text alone and then+ // charging it with the characters of an inline `<strong>` inflated every+ // paragraph that had one, and so does charging the lines with an inline+ // `<style>` child, the source's indentation, or a script's combining+ // marks, none of which takes up any of a line.//// A DOM that cannot say where the lines are gets no finding. The union of// a long first line and a short tail is the same union as two even lines,@@ -1070,7 +1151,8 @@ pub fn check_quality(dom: &dyn Dom, q: &QualityInput) -> Vec<RuleHit> {let total: f64 = widths.iter().sum();if total > 0.0 {let over = line_max + 5.0;- let chars = |w: f64| (text_len as f64) * w / total;+ let rendered_len = rendered_text_len(dom, el) as f64;+ let chars = |w: f64| rendered_len * w / total;let long = widths.iter().filter(|w| chars(**w) > over).count();if long >= 2 {let longest = widths.iter().copied().fold(0.0, js::math_max);@@ -4278,3 +4360,134 @@ mod tests {assert!(is_non_rendered_text(&d, s, "span"));}}++#[cfg(test)]+mod rendered_text_tests {+ use super::*;+ use crate::browser::fake_dom::FakeDom;++ /// A paragraph over two rendered lines of 640px and 260px, as+ /// directus.io's hero paragraph rendered.+ fn two_line_p(d: &mut FakeDom, body: ElId) -> ElId {+ let p = d.add(Some(body), "p");+ d.set_styles(+ p,+ &[+ ("fontSize", "18px"),+ ("lineHeight", "normal"),+ ("letterSpacing", "normal"),+ ("backgroundColor", "rgba(0, 0, 0, 0)"),+ ("position", "static"),+ ("textTransform", "none"),+ ("textAlign", "start"),+ ("whiteSpace", "normal"),+ ],+ );+ d.set_rect(p, 100.0, 270.0, 700.0, 56.0);+ d.set_text_lines(p, &[(100.0, 272.0, 640.0, 24.0), (100.0, 300.0, 260.0, 24.0)]);+ p+ }++ fn line_length(d: &FakeDom, el: ElId) -> Option<String> {+ check_element_quality_dom(d, el, &BrowserConfig::default())+ .into_iter()+ .find(|h| h.id == "line-length")+ .map(|h| h.snippet)+ }++ const SENTENCE: &str = "The collaborative backend that turns any database into an API your whole team can use today.";++ /// An inline `<style>` child is in `textContent` and on no line. Its 240+ /// characters were shared out between the two rendered lines and turned+ /// a 66-character line into a 240-character one.+ #[test]+ fn an_inline_style_child_is_not_counted() {+ let mut d = FakeDom::new();+ let (_h, body) = d.with_page();+ let p = two_line_p(&mut d, body);+ let style = d.add(Some(p), "style");+ d.set_style(style, "display", "none");+ d.add_text(style, &"@keyframes blink { 0%, 50% { opacity: 1; } } ".repeat(6));+ d.add_text(p, SENTENCE);+ assert_eq!(rendered_text_len(&d, p), SENTENCE.len());+ assert_eq!(line_length(&d, p), None);++ // The same lines with that many characters of prose do report.+ let q = two_line_p(&mut d, body);+ d.add_text(q, &"w".repeat(SENTENCE.len() + 270));+ assert_eq!(+ line_length(&d, q).as_deref(),+ Some("~257 chars on 2 of 2 rendered lines (aim for <80)")+ );+ }++ /// A `display: none` child and a `<script>` are cut the same way, and a+ /// rendered inline child stays in.+ #[test]+ fn hidden_and_script_children_are_cut_and_inline_children_kept() {+ let mut d = FakeDom::new();+ let (_h, body) = d.with_page();+ let p = two_line_p(&mut d, body);+ d.add_text(p, "Before ");+ let hidden = d.add(Some(p), "span");+ d.set_style(hidden, "display", "none");+ d.add_text(hidden, &"hidden ".repeat(40));+ let strong = d.add(Some(p), "strong");+ d.set_style(strong, "display", "inline");+ d.add_text(strong, "kept");+ let script = d.add(Some(p), "script");+ d.add_text(script, &"var x = 1; ".repeat(30));+ d.add_text(p, " after");+ assert_eq!(rendered_text_len(&d, p), "Before kept after".len());+ }++ /// Source indentation collapses to one space under `white-space: normal`+ /// and is kept under `pre-wrap`.+ #[test]+ fn source_indentation_collapses_unless_preserved() {+ let mut d = FakeDom::new();+ let (_h, body) = d.with_page();+ let p = two_line_p(&mut d, body);+ d.add_text(p, "\n Copyright 2017, all rights reserved\n with the Directorate.\n ");+ assert_eq!(+ rendered_text_len(&d, p),+ "Copyright 2017, all rights reserved with the Directorate.".len()+ );+ d.set_style(p, "whiteSpace", "pre-wrap");+ assert_eq!(+ rendered_text_len(&d, p),+ "Copyright 2017, all rights reserved\n with the Directorate.".len()+ );+ }++ /// A combining mark sits on its base and a zero-width joiner or soft+ /// hyphen draws nothing, so none of them is a character on the line.+ #[test]+ fn combining_marks_and_format_characters_are_not_counted() {+ let mut d = FakeDom::new();+ let (_h, body) = d.with_page();+ let p = two_line_p(&mut d, body);+ // "राजस्थान के": र ा ज स ् थ ा न, a space, क े.+ d.add_text(p, "\u{930}\u{93e}\u{91c}\u{938}\u{94d}\u{925}\u{93e}\u{928} \u{915}\u{947}");+ assert_eq!(rendered_text_len(&d, p), 7);+ let q = two_line_p(&mut d, body);+ d.add_text(q, "cafe\u{301} co\u{ad}operate\u{200b}s");+ assert_eq!(rendered_text_len(&d, q), "cafe cooperates".len());+ }++ /// Plain prose counts as before, so a long column still reports the same+ /// number.+ #[test]+ fn plain_prose_counts_every_character() {+ let mut d = FakeDom::new();+ let (_h, body) = d.with_page();+ let p = two_line_p(&mut d, body);+ d.set_text_lines(p, &[(100.0, 272.0, 640.0, 24.0), (100.0, 300.0, 640.0, 24.0)]);+ d.add_text(p, &"word ".repeat(40));+ assert_eq!(rendered_text_len(&d, p), 199);+ assert_eq!(+ line_length(&d, p).as_deref(),+ Some("~100 chars on 2 of 2 rendered lines (aim for <80)")+ );+ }+}diff --git a/tests/fixtures/antipatterns/line-length-text-count.html b/tests/fixtures/antipatterns/line-length-text-count.htmlnew file mode 100644index 000000000..56ccb26ae--- /dev/null+++ b/tests/fixtures/antipatterns/line-length-text-count.html@@ -0,0 +1,77 @@+<!doctype html>+<html lang="en">+<head>+ <meta charset="utf-8" />+ <meta name="viewport" content="width=device-width, initial-scale=1.0">+ <title>Line length: which characters are on the lines (browser-only)</title>+ <!--+ line-length divides an element's characters among its rendered lines, so+ the count has to be of the characters that rendered. textContent is not+ that: it holds an inline <style> child's CSS, the source's indentation,+ and one code unit for every combining mark. Each pass case here read as a+ long column on textContent and is a comfortable one on the page. Explicit+ pixel widths throughout.+ -->+ <style>+ body { font-family: system-ui, sans-serif; margin: 0; padding: 24px 40px; color: #1a1a1a; background: #fff; }+ h2 { font-size: 14px; line-height: 20px; margin: 32px 0 8px; color: #475569; }+ .copy { font-size: 16px; line-height: 24px; margin: 0; }+ .wide { width: 1000px; }+ .measure { width: 560px; }+ .marks { width: 640px; font-size: 16px; line-height: 28px; }+ .keep { white-space: pre-wrap; }+ </style>+</head>+<body>+ <!-- FLAG: the count still charges real long columns -->+ <h2>Flag: a wide column that also carries an inline style child</h2>+ <p class="copy wide flag-style-child"><style>.flag-style-child { min-height: 48px; }</style>Reading copy set across a column this wide asks the eye to travel a long way back to the start of every line, and after a few lines of it a reader starts to lose their place. The measure is what a designer sets with a max-width on the text block, and this block sets none at all, so every line runs the full width of the column it sits in from one edge to the other.</p>++ <h2>Flag: a wide column written with source indentation</h2>+ <p class="copy wide flag-indented">+ Reading copy set across a column this wide asks the eye to travel a long way back to the start+ of every line, and after a few lines of it a reader starts to lose their place. The measure is+ what a designer sets with a max-width on the text block, and this block sets none at all, so+ every line runs the full width of the column it sits in from one edge to the other.+ </p>++ <!-- PASS: a comfortable measure that textContent overcounts -->+ <h2>Pass: a 560px measure with an inline style child</h2>+ <!-- directus.io's hero paragraph: a typewriter component ships its+ keyframes in a style element inside the paragraph. -->+ <p class="copy measure pass-style-child"><style>+@keyframes pass-style-child-blink {+ 0%, 50% { opacity: 1; }+ 51%, 100% { opacity: 0; }+}+.pass-style-child { min-height: 48px; }+@media (max-width: 768px) {+ .pass-style-child { min-height: 96px; }+}+.pass-style-child .cursor { animation: pass-style-child-blink 1s step-end infinite; }+</style>A paragraph held to a comfortable measure wraps long before the eye has far to travel back, and this one keeps to about seventy characters a line.</p>++ <h2>Pass: a 560px measure with a hidden child and a script child</h2>+ <p class="copy measure pass-hidden-child">A paragraph held to a comfortable measure wraps long before the eye has far to travel back,<span style="display: none"> with a long run of words that a script reveals later and that take up no part of any line until then, however many of them there are in the source of the page,</span><script type="application/json">{"note": "data a component reads on hydration, which is text content and never a rendered character on any line of this paragraph"}</script> and this one keeps to about seventy characters a line.</p>++ <h2>Pass: a 560px measure written with deep source indentation</h2>+ <!-- odishatreasury.gov.in: a template indents every wrapped source line. -->+ <p class="copy measure pass-indented">+ A paragraph held to a comfortable+ measure wraps long before the eye has+ far to travel back, and this one keeps+ to about seventy characters a line+ from its first sentence to its last.+ </p>++ <h2>Pass: Devanagari prose on a 640px measure at 16px</h2>+ <!-- v0-tripura-sundari-mandir.vercel.app: a third of the code units are+ vowel signs and viramas, which sit on their consonant. -->+ <p class="marks pass-combining-marks" lang="hi">राजस्थान के दक्षिणी भाग की अरावली पर्वतमाला के बीच स्थित यह प्राचीन मंदिर श्रद्धालुओं के लिए आस्था का प्रमुख केंद्र है और प्रतिवर्ष नवरात्रि के अवसर पर यहाँ विशाल मेले का आयोजन किया जाता है जिसमें दूर-दूर से भक्त आते हैं। इस क्षेत्र से बहने वाली माही नदी को पुराणों में पवित्र माना गया है और मंदिर की मूर्ति काले पत्थर से निर्मित है जिसकी अठारह भुजाएँ हैं।</p>++ <h2>Flag: preserved white space is on the line</h2>+ <!-- Under pre-wrap the runs of spaces render, so they are characters of+ the line and the column is as long as it looks. -->+ <p class="copy wide keep flag-preserved">Reading copy set across a column this wide asks the eye to travel a long way back to the start of every line, and after a few lines of it a reader starts to lose their place. The measure is what a designer sets with a max-width on the text block, and this block sets none at all, so every line runs the full width.</p>+</body>+</html>diff --git a/tests/oracle/golden/detect-fixture-json-line-length-text-count-html.json b/tests/oracle/golden/detect-fixture-json-line-length-text-count-html.jsonnew file mode 100644index 000000000..afe20a428--- /dev/null+++ b/tests/oracle/golden/detect-fixture-json-line-length-text-count-html.json@@ -0,0 +1,7 @@+{+ "stdout": "[]\n",+ "stderr": "",+ "exit": 0,+ "signal": null,+ "files": {}+}diff --git a/tests/oracle/golden/detect-fixture-text-line-length-text-count-html.json b/tests/oracle/golden/detect-fixture-text-line-length-text-count-html.jsonnew file mode 100644index 000000000..7ea122487--- /dev/null+++ b/tests/oracle/golden/detect-fixture-text-line-length-text-count-html.json@@ -0,0 +1,7 @@+{+ "stdout": "",+ "stderr": "",+ "exit": 0,+ "signal": null,+ "files": {}+}