The design language that makes your AI harness better at design.
JavaScript
Fix: don't build findings on an unresolved var() font size (#883)
Abdul Wahab1486a6f9dfa8
diff --git a/crates/html/src/adapters.rs b/crates/html/src/adapters.rsindex 8371fe0d5..052dae47c 100644--- a/crates/html/src/adapters.rs+++ b/crates/html/src/adapters.rs@@ -855,7 +855,9 @@ pub fn check_element_oversized_h1(el: &StaticElement<'_>, tag: &str) -> Vec<Ruleif tag != "h1" {return Vec::new();}- let font_size = resolve_font_size_px(el);+ let Some(font_size) = resolve_font_size_px(el) else {+ return Vec::new();+ };let heading_text = collapse_ws(js::trim(&el.text_content()));hits(check_oversized_h1(&OversizedH1Input {tag,diff --git a/crates/html/src/quality.rs b/crates/html/src/quality.rsindex 43bc78906..3269e13b6 100644--- a/crates/html/src/quality.rs+++ b/crates/html/src/quality.rs@@ -44,8 +44,13 @@ pub fn pf0(s: &str) -> f64 {}}-/// JS: checks.mjs#resolveFontSizePx(el, win)-pub fn resolve_font_size_px(el: &StaticElement<'_>) -> f64 {+/// JS: checks.mjs#resolveFontSizePx(el, win). `None` when the font size is+/// an unresolved var() (not defined in any stylesheet this engine read): the+/// inherited size is not the element's, so nothing may be built on it.+pub fn resolve_font_size_px(el: &StaticElement<'_>) -> Option<f64> {+ if has_unresolved_var(sv(el.style(), "fontSize")) {+ return None;+ }let mut chain: Vec<String> = Vec::new();let mut cur = Some(*el);while let Some(e) = cur {@@ -73,7 +78,7 @@ pub fn resolve_font_size_px(el: &StaticElement<'_>) -> f64 {px = num;}}- px+ Some(px)}/// JS: checks.mjs#hasVisibleBackgroundBoundary(style, el, win)@@ -163,7 +168,7 @@ pub struct QualityInput<'a, 'b> {pub style: &'a StyleValues,pub has_direct_text: bool,pub text_len: usize,- pub font_size: f64,+ pub font_size: Option<f64>,pub line_height_px: Option<f64>,pub letter_spacing_px: Option<f64>,}@@ -184,12 +189,17 @@ fn has_unresolved_var(value: &str) -> bool {js::to_lower_case(value).contains("var(")}-fn side_len(style: &StyleValues, key: &str, font_size: f64) -> Option<f64> {+fn side_len(style: &StyleValues, key: &str, font_size: Option<f64>) -> Option<f64> {let value = sv_opt(style, key)?;if has_unresolved_var(value) {return None;}- Some(resolve_length_px(Some(value), font_size).unwrap_or(0.0))+ let at = |fs: f64| resolve_length_px(Some(value), fs).unwrap_or(0.0);+ match font_size {+ Some(fs) => Some(at(fs)),+ // Unknown only when the side depends on the font size (em, %).+ None => (at(0.0) == at(1.0)).then(|| at(0.0)),+ }}/// JS: checks.mjs#checkQuality(opts), static (`rect: null`) branches.@@ -400,7 +410,7 @@ pub fn check_quality(q: &QualityInput<'_, '_>) -> Vec<RuleHit> {// --- Tight line height ---if q.has_direct_text && text_len > 50 && !is_heading {- if let Some(lh) = q.line_height_px {+ if let (Some(lh), Some(font_size)) = (q.line_height_px, font_size) {if font_size > 0.0 {let ratio = lh / font_size;if ratio > 0.0 && ratio < 1.3 {@@ -432,7 +442,9 @@ pub fn check_quality(q: &QualityInput<'_, '_>) -> Vec<RuleHit> {}// --- Tiny body text ---- if q.has_direct_text && text_len > 20 && font_size < 12.0 {+ if let Some(font_size) =+ font_size.filter(|fs| q.has_direct_text && text_len > 20 && *fs < 12.0)+ {let skip_tags = ["sub","sup",@@ -462,12 +474,13 @@ pub fn check_quality(q: &QualityInput<'_, '_>) -> Vec<RuleHit> {let direct_text = js::trim(&collapse_ws(&el.direct_text())).to_string();let dt_len = utf16_len(&direct_text);let ui_skip_tags = ["sub", "sup", "option"];- if font_size > 0.0- && font_size < 11.0- && dt_len >= 2- && !ui_skip_tags.contains(&tag)- && !is_non_rendered_text(el, tag, Some(style))- {+ if let Some(font_size) = font_size.filter(|fs| {+ *fs > 0.0+ && *fs < 11.0+ && dt_len >= 2+ && !ui_skip_tags.contains(&tag)+ && !is_non_rendered_text(el, tag, Some(style))+ }) {let is_exempt_context = el.closest(EXEMPT_CONTEXT).is_some();if !is_exempt_context && !is_visually_hidden(el, style) {let is_interactive = el.closest(INTERACTIVE).is_some();@@ -511,7 +524,7 @@ pub fn check_quality(q: &QualityInput<'_, '_>) -> Vec<RuleHit> {// --- Wide letter spacing on body text ---if q.has_direct_text && text_len > 20 && sv_opt(style, "textTransform") != Some("uppercase") {- if let Some(ls) = q.letter_spacing_px {+ if let (Some(ls), Some(font_size)) = (q.letter_spacing_px, font_size) {if ls > 0.0 && font_size > 0.0 {let tracking_em = ls / font_size;if tracking_em > 0.05 {@@ -528,9 +541,9 @@ pub fn check_quality(q: &QualityInput<'_, '_>) -> Vec<RuleHit> {}// --- Crushed letter spacing ---- if q.has_direct_text && text_len > 20 && font_size > 0.0 {- if let Some(ls) = q.letter_spacing_px {- if ls < 0.0 {+ if q.has_direct_text && text_len > 20 {+ if let (Some(ls), Some(font_size)) = (q.letter_spacing_px, font_size) {+ if ls < 0.0 && font_size > 0.0 {let tracking_em = ls / font_size;if tracking_em <= -0.05 {let excerpt =@@ -560,8 +573,10 @@ pub fn check_element_quality(let has_direct_text = el.has_direct_text_longer_than(10);let text_len = utf16_len(js::trim(&el.text_content()));let font_size = resolve_font_size_px(el);- let line_height_px = resolve_length_px(sv_opt(style, "lineHeight"), font_size);- let letter_spacing_px = resolve_length_px(sv_opt(style, "letterSpacing"), font_size);+ let line_height_px =+ font_size.and_then(|fs| resolve_length_px(sv_opt(style, "lineHeight"), fs));+ let letter_spacing_px =+ font_size.and_then(|fs| resolve_length_px(sv_opt(style, "letterSpacing"), fs));check_quality(&QualityInput {el,tag,diff --git a/crates/html/tests/unresolved_custom_properties.rs b/crates/html/tests/unresolved_custom_properties.rsindex 5f95e4e06..c8f1a562a 100644--- a/crates/html/tests/unresolved_custom_properties.rs+++ b/crates/html/tests/unresolved_custom_properties.rs@@ -199,3 +199,86 @@ fn unresolved_outline_style_is_not_a_visible_boundary() {assert!(cramped_padding_ids(html, None).is_empty());}++fn finding_ids(html: &str) -> Vec<String> {+ detect_html_source(+ html,+ Path::new("/nonexistent/samples/image-card.html"),+ &DetectHtmlOptions::default(),+ )+ .into_iter()+ .map(|finding| finding.antipattern)+ .collect()+}++#[test]+fn unresolved_font_size_is_not_replaced_by_the_inherited_size() {+ let html = r#"<!doctype html>+ <html>+ <head>+ <style>+ body { font-size: 20px; }+ .tight { font-size: var(--missing-size); letter-spacing: -1.75px; }+ .wide { font-size: var(--missing-size); letter-spacing: 1.2px; }+ .leading { font-size: var(--missing-size); line-height: 1.5; }+ .inherits { font-size: var(--missing-size); }+ .inherits h2 { letter-spacing: -1.75px; }+ .shrunk { font-size: 10px; }+ .shrunk p { font-size: var(--missing-size); }+ .big { font-size: 80px; }+ .big h1 { font-size: var(--missing-size); }+ </style>+ </head>+ <body>+ <div class="big"><h1>A huge headline with plenty of characters in it for sure</h1></div>+ <h2 class="tight">A closing statement long enough to count</h2>+ <div class="inherits"><h2>An inherited size long enough to count</h2></div>+ <div class="shrunk"><p>Body copy that is comfortably longer than twenty characters.</p><p>Short copy</p></div>+ <p class="wide">Body copy that is comfortably longer than twenty characters.</p>+ <p class="leading">Body copy that is comfortably longer than fifty characters, so the leading rule has enough text to consider.</p>+ </body>+ </html>+ "#;++ let ids = finding_ids(html);+ for rule in [+ "extreme-negative-tracking",+ "wide-tracking",+ "tight-leading",+ "tiny-text",+ "undersized-ui-text",+ "oversized-h1",+ ] {+ assert!(!ids.iter().any(|id| id == rule), "{rule} in {ids:?}");+ }+}++#[test]+fn unresolved_font_size_keeps_zero_padding_cramped() {+ let html = r#"+ <style>+ * { margin: 0; }+ .card { font-size: var(--missing-size); background: #f5f5f5; padding: 0; }+ .em { font-size: var(--missing-size); background: #f5f5f5; padding: 0.1em; }+ </style>+ <div class="card"><p>Readable card copy</p></div>+ <div class="em"><p>Readable card copy</p></div>+ "#;++ assert_eq!(cramped_padding_ids(html, None).len(), 1);+}++#[test]+fn resolved_font_size_var_still_reports_crushed_tracking() {+ let html = r#"+ <style>+ :root { --size: 20px; }+ .tight { font-size: var(--size); letter-spacing: -1.75px; }+ </style>+ <h2 class="tight">A closing statement long enough to count</h2>+ "#;++ assert!(finding_ids(html)+ .iter()+ .any(|id| id == "extreme-negative-tracking"));+}