Skip to content

The design language that makes your AI harness better at design.

JavaScript

heading-rhythm folds a label set apart by colour, italics or weight

Paul Bakausd10840c4859f
diff --git a/crates/browser/tests/heading_rhythm.rs b/crates/browser/tests/heading_rhythm.rs
index 1aa55f5fc..9be716dfc 100644
--- a/crates/browser/tests/heading_rhythm.rs
+++ b/crates/browser/tests/heading_rhythm.rs
@@ -5,7 +5,9 @@
//! misread once the rule pass ran after the reveal sweep: an eyebrow wrapped
//! in its own box, a block that ends in a rule, an accordion trigger that ends
//! its row, a caption under its photo, a title band that draws its own rules,
-//! and a standfirst behind a `display: contents` wrapper.
+//! and a standfirst behind a `display: contents` wrapper. Round 7 adds labels
+//! at body size set apart by colour, italics or a hairline weight, and a band
+//! painted with a background image.
use std::collections::HashMap;
use std::io::{Read, Write};
@@ -92,6 +94,8 @@ fn fixture_flags_crowded_headings_and_passes_the_misread_shapes() {
// layout row that shares a class with its neighbour repeats nothing.
"\"Flag Code Block Above\" has 12px above vs 36px below",
"\"Flag Title Alone In A Row\" has 16px above vs 48px below",
+ // A grey line too long to read as a label is still content.
+ "\"Flag Grey Sentence Above\" has 10px above vs 48px below",
] {
assert!(
snippets.iter().any(|s| s.contains(heading)),
@@ -100,5 +104,5 @@ fn fixture_flags_crowded_headings_and_passes_the_misread_shapes() {
}
let stray: Vec<&&str> = snippets.iter().filter(|s| s.contains("\"Pass ")).collect();
assert!(stray.is_empty(), "should-pass headings flagged: {stray:?}");
- assert_eq!(snippets.len(), 15, "{snippets:?}");
+ assert_eq!(snippets.len(), 16, "{snippets:?}");
}
diff --git a/crates/core/src/browser/page_checks.rs b/crates/core/src/browser/page_checks.rs
index 0278e5f42..8e16fef70 100644
--- a/crates/core/src/browser/page_checks.rs
+++ b/crates/core/src/browser/page_checks.rs
@@ -21,7 +21,7 @@ use crate::checks::embedded_content::{
};
use crate::checks::measures::{
cream_from_class_list, is_cream_color, is_opaque_decorated_box,
- is_screen_reader_only_text_style, SrOnlyMetrics, StyleMap,
+ is_screen_reader_only_text_style, resolve_length_px, SrOnlyMetrics, StyleMap,
};
use crate::checks::rules::{
check_flat_type_hierarchy_samples, flat_type_hierarchy_severity, is_card_like_from_props,
@@ -812,8 +812,10 @@ fn rhythm_overlaps_x(sr: &Rect, rect: &Rect) -> bool {
math_min(sr.right, rect.right) - math_max(sr.left, rect.left) >= 8.0
}
-/// A box that paints an edge on `side` ("Top" or "Bottom"): a background,
-/// a border on that side, or a shadow.
+/// A box that paints an edge on `side` ("Top" or "Bottom"): a background
+/// colour or image, a border on that side, or a shadow. A band painted with
+/// `background-image` (jyes.com.tw's grey news band is a `url()` texture) has
+/// an edge a reader sees as plainly as one painted with a colour.
fn rhythm_paints_edge(dom: &dyn Dom, el: ElId, side: &str) -> bool {
if rhythm_is_contents(dom, el) {
return false;
@@ -823,6 +825,10 @@ fn rhythm_paints_edge(dom: &dyn Dom, el: ElId, side: &str) -> bool {
return true;
}
}
+ let image = dom.style(el, "backgroundImage");
+ if !image.is_empty() && image != "none" {
+ return true;
+ }
if style_px(dom, el, &format!("border{side}Width")) > 0.0 {
return true;
}
@@ -1061,10 +1067,77 @@ fn rhythm_reads_as_eyebrow(dom: &dyn Dom, line: ElId, heading: ElId, text_size:
if cased.len() >= 3 && cased.iter().all(|c| c.is_uppercase()) {
return true;
}
+ if rhythm_set_apart(dom, line, heading, e) {
+ return true;
+ }
}
false
}
+/// The longest line, in UTF-16 units, that reads as a label on colour,
+/// italics or weight alone. cnnbrasil.com.br's section links ("Política",
+/// "Eleições") and outreign.io's italic eyebrows ("Five screens", "Compare
+/// plans") are a word or two; a sentence closing the block above is longer.
+const RHYTHM_SET_APART_MAX_CHARS: usize = 40;
+
+/// How far apart, on any channel, two text colours must be to read as two
+/// colours: grey-400 on a black card, purple on off-white.
+const RHYTHM_COLOUR_STEP: f64 = 32.0;
+
+/// How much lighter than the text around it a line must be set to stand
+/// apart on weight alone: a hairline 100 or 200 italic against 400 body copy.
+const RHYTHM_LIGHTER_WEIGHT_STEP: f64 = 300.0;
+
+fn rhythm_colours_differ(dom: &dyn Dom, a: ElId, b: ElId) -> bool {
+ let (Some(ca), Some(cb)) = (
+ parse_any_color(Some(&dom.style(a, "color"))),
+ parse_any_color(Some(&dom.style(b, "color"))),
+ ) else {
+ return false;
+ };
+ (ca.r - cb.r).abs() >= RHYTHM_COLOUR_STEP
+ || (ca.g - cb.g).abs() >= RHYTHM_COLOUR_STEP
+ || (ca.b - cb.b).abs() >= RHYTHM_COLOUR_STEP
+ || (ca.alpha_or_one() - cb.alpha_or_one()).abs() >= 0.25
+}
+
+fn rhythm_is_italic(dom: &dyn Dom, el: ElId) -> bool {
+ let style = dom.style(el, "fontStyle");
+ style.starts_with("italic") || style.starts_with("oblique")
+}
+
+/// A short line at body size that a reader still sees as a label: one
+/// rendered line of a few words whose colour, italics or much lighter weight
+/// sets it apart both from the text it sits in (its container's own type)
+/// and from the heading under it. A grey category link over a black
+/// headline, a purple italic eyebrow over a white title. A date or a closing
+/// sentence set like the copy around it stays content of its own.
+fn rhythm_set_apart(dom: &dyn Dom, line: ElId, heading: ElId, words: ElId) -> bool {
+ let Some(container) = dom.parent(line) else { return false };
+ if utf16_len(js::trim(&collapse_ws(&dom.text_content(line)))) > RHYTHM_SET_APART_MAX_CHARS {
+ return false;
+ }
+ let size = rhythm_font_size(dom, words);
+ let pitch = resolve_length_px(Some(&dom.style(words, "lineHeight")), size)
+ .filter(|lh| lh.is_finite() && *lh > 0.0)
+ .unwrap_or(size * 1.2);
+ let one_line = dom
+ .direct_text_rect(words)
+ .map_or(false, |t| t.height > 0.0 && t.height < math_max(pitch, size * 1.2) * 1.5);
+ if !one_line {
+ return false;
+ }
+ if rhythm_colours_differ(dom, words, container) && rhythm_colours_differ(dom, words, heading) {
+ return true;
+ }
+ if rhythm_is_italic(dom, words) && !rhythm_is_italic(dom, container) && !rhythm_is_italic(dom, heading) {
+ return true;
+ }
+ let weight = parse_font_weight(&dom.style(words, "fontWeight"));
+ let around = parse_font_weight(&dom.style(container, "fontWeight"));
+ weight.is_finite() && around.is_finite() && weight <= around - RHYTHM_LIGHTER_WEIGHT_STEP
+}
+
fn rhythm_painted_background(dom: &dyn Dom, el: ElId) -> Option<crate::color::Rgba> {
parse_any_color(Some(&dom.style(el, "backgroundColor"))).filter(|c| c.alpha_or_one() > 0.05)
}
diff --git a/tests/fixtures/antipatterns/heading-rhythm.html b/tests/fixtures/antipatterns/heading-rhythm.html
index d5d092e96..0ff2fd9b5 100644
--- a/tests/fixtures/antipatterns/heading-rhythm.html
+++ b/tests/fixtures/antipatterns/heading-rhythm.html
@@ -212,6 +212,13 @@
.lrow { display: flex; gap: 24px; }
.lrow > div { flex: 1; }
.lrow h2 { margin: 16px 0 48px; }
+ /* PASS: a label at body size set apart by colour, italics or a light weight
+ (cnnbrasil.com.br's grey section links, outreign.io's italic eyebrows) */
+ .kick-grey { color: #9ca3af; text-decoration: none; }
+ .kick-italic { font-style: italic; }
+ .kick-light { font-weight: 100; }
+ /* PASS: a band painted with a background image, not a colour (jyes.com.tw) */
+ .img-band { background-image: linear-gradient(#ececec, #ececec); padding: 25px 0; }
</style>
<h1>Wrapper spacing</h1>
@@ -299,6 +306,38 @@ npx impeccable detect src/</pre>
<div class="ruled above-10 below-36"><h3>Pass Heading Ends Ruled Box</h3></div>
<p class="blk">The rule under the box ends the heading's box, so this content belongs to the next one.</p>
</section>
+
+ <section class="case">
+ <p class="blk">A closing paragraph, then a news card whose grey section link sits a few pixels above its headline.</p>
+ <div class="above-24"><a class="kick-grey" href="#">Política</a><div><h3 class="below-24">Pass Grey Kicker</h3></div></div>
+ <p class="blk">The grey link is the headline's label, so the cluster starts at the link, twenty-four pixels under the block above.</p>
+ </section>
+
+ <section class="case">
+ <p class="blk">A closing paragraph, then an italic eyebrow at body size twelve pixels above a section title.</p>
+ <p class="kick-italic above-24">Five screens</p>
+ <h2 class="above-12 below-24">Pass Italic Eyebrow</h2>
+ <p class="blk">The italic line is the title's label, and the cluster sits twenty-four pixels from both neighbours.</p>
+ </section>
+
+ <section class="case">
+ <p class="blk">A closing paragraph, then a hairline eyebrow at body size twelve pixels above a section title.</p>
+ <p class="kick-light above-24">Compare plans</p>
+ <h2 class="above-12 below-24">Pass Light Eyebrow</h2>
+ <p class="blk">The weight sets the line apart from the copy around it, so it is read as the title's label.</p>
+ </section>
+
+ <section class="case">
+ <p class="blk">A closing paragraph, then a grey sentence ten pixels above the next title, too long to read as a label.</p>
+ <p class="kick-grey above-24">Filed under politics and the week in review</p>
+ <h2 class="above-10 below-48">Flag Grey Sentence Above</h2>
+ <p class="blk">A sentence is content of its own whatever its colour, so the title sits ten pixels under it and forty-eight above this.</p>
+ </section>
+
+ <section class="case">
+ <p class="blk">A closing paragraph directly above a band whose grey is a background image rather than a colour.</p>
+ <div class="img-band"><h2 class="below-40">Pass Image Band Heading</h2><p class="blk">The band's edge separates the title from the paragraph above, as a colour band's does.</p></div>
+ </section>
</section>
</main>
</body>