Skip to content

feat(rules): hidden-only-at-some-widths reason for controls a width-only media query hides #988 - #1012

Merged
nc9 merged 2 commits into
mainfrom
feat/988-width-media-reason
Oct 22, 2026
Merged

nc9 merged 2 commits into
mainfrom
feat/988-width-media-reason

Conversation

@nc9

@nc9 nc9 commented Oct 22, 2026 •

Copy link
Copy Markdown
Contributor

Closes #988

Choice recorded (Nik decided option 2): keep <check>-unconfirmed, with a distinct reason, rather than report a fail with a width qualifier. Reason: no new false-fail risk if the width analysis is wrong, and the cause is distinguishable from undecidable CSS. The reason is hidden-only-at-some-widths; each item's label says where it shows ("shown below 990px") and carries meta: { reason, shown }; when every unconfirmed item has this reason the row message reads N button(s) without accessible names hidden only at some widths by a media query (unconfirmed). The message carries no px, so one defect keeps one identity across pages.

What changed

  • shared/media-widths.ts (new): reads a plain width-only @media prelude into viewport-width ranges: min-width, max-width, width (colon and range syntax, one or two comparisons), px/em/rem (1em = 16px), and, comma lists, optional only, screen/all. Anything else (not, orientation, height, prefers-*, hover, resolution, calc(), vw, or, nested feature groups) is null and keeps today's unknown.
  • content/hidden-text.ts parseRules: each conditional rule now carries its enclosing at-rule preludes (at).
  • shared/page-styles.ts: a plain display: none on plain (single-compound) selectors, inside width-only @media only, in a sheet that is not media/title/svg/@namespace, is kept with its widths. The cascade collects the widths where it outranks the decidable rules instead of answering unknown. A decided hide, an unrelated undecided rule, a case-only match, or a visibility hide still gives the generic unknown. @supports/@layer/@container around it is not a plain @media chain, so it is generic unknown.
  • shared/static-hidden.ts: widths accumulate down the tree (an ancestor and the element can each hide a part). If the union covers every width the element is hidden (no fail, no new reason); a gap of up to 0.02px counts as covered (the min-width: N / max-width: N - 0.02px idiom), a whole-pixel gap or a single excluded point does not. Otherwise the element is unknown with reason width. An external stylesheet (incomplete sheet) or a tree a browser may rebuild keeps the generic unknown. button-name, aria-hidden-focus and the shared sortUnnamedControls/unconfirmedRow use the new label/meta/message.

CHROMIUM table: 25 width: ... rows added to fixtures/static-hidden-chromium.json, rendered in chrome-headless-shell 1248 at 320, 600, 989, 990, 991 and 1200px (one iframe per width, so the media queries see that width); chromium is "shown" when any width shows it, widths has each. Covers complementary ranges (colon, comma, range syntax, em), a 1px gap, the >/< point gap, comma bands, nesting, show-rule pairs, specificity both ways, ancestor/element splits, hidden, inline, !important, orientation, not, print, @supports. The existing invariant (never "hidden" where Chromium shows) covers them via chromium; static-hidden-width-media-988.test.ts adds: hidden at every sampled width is never "shown", the width reason only where Chromium shows it at some width (the whole-pixel-gap row is the listed exception: whole-pixel viewports never land in it), and the complementary rows must answer "hidden". Note whole-pixel iframe widths snap, so the 989.98 side of the idiom is by convention, not measured; that is the documented tolerance.

Tests: static-hidden-width-media-988.test.ts 56, static-hidden.test.ts plus the audit-engine canonical golden 344, and the other page-styles/a11y hidden tests (select-name, button-name, 671, 938, 940, 951, 978, 984, 989, 991, aria-hidden-focus, hidden-text, input-zoom) all pass. tsgo --noEmit clean in packages/rules; tangly check --strict passes.

Docs: "What squirrel can see" in button-name and aria-hidden-focus describes the width-media behaviour and the reporting, and still says hide-until-JavaScript classes on unrendered pages stay unconfirmed and a rendered crawl settles them. CHANGELOG bullet added. Agent skill: no change needed.

Review finding (DoS) and fix: deeply nested @media made rule parsing quadratic (depth x rules): "@media all{" x14000 + ".a{display:none}" x4900 + "}" x14000 (240 KB, under the size and rule bounds) took about 25 s, in one synchronous loop the rule time budget cannot interrupt. Cause: parseRules copied the whole at-rule stack per rule and readAtRuleWidths re-parsed every level per rule. Fix: the at-rule chain is kept only up to depth 8 (MAX_AT_DEPTH; real themes nest 1 to 3), a deeper rule stays generic unknown, and the copy is bounded at O(8). Regression tests (3000 deep x 1000 rules, and the 14000 x 4900 case) assert under 500 ms and the generic unknown result; nesting of 7 plus the inner query is still read, 8 plus it is not.

Also from review: a query pair that can never both match ((min-width:990px) and (max-width:989px)) hides nothing, so the element reads as shown (a prior unconfirmed can now fail, as in Chromium; tested, CHANGELOG notes it); describeShownWidths([]) is safe; the 0.02px tolerance trade-off is in the docs.

Related, pre-existing and not fixed here: #1013 (CSS nesting such as b{@media (min-width:0){display:none}} reads as shown on main while Chromium hides it).

@claude

claude Bot commented Oct 22, 2026

Copy link
Copy Markdown

Review of #1012 (width-only @media reason). I read the full diff but did not run the tests; the Chromium-verified fixture table is a strong design here.

No critical or high findings. The change stays conservative: it only produces unknown or hidden, and hidden only when the ranges cover every width.

  • (medium) shared/media-widths.ts readQuery: only is stripped, but a bare only (min-width: 990px) with no media type is invalid CSS, so a browser drops that query and it never matches. Here it is read as a normal width range. Combined with a complementary query it could be counted as covering every width and give a false "hidden", which silently drops a real failure. Require a media type after only, and add a test.
  • (low) coversAllWidths treats gaps up to 0.02px as covered, so a viewport such as 989.99px (browser zoom, fractional DPR) would show the element and the control would be dropped. This is documented and deliberate. The fixture can't measure that side, since whole-pixel iframe widths snap. Say so in the rule docs, so users know a skipped control may be shown at fractional widths.
  • (low) parseRules atStack stays in sync only through the closing-brace handler. Malformed CSS (unbalanced braces) gives a wrong at chain, but atDepth already has the same limitation. A test with an unterminated nested @media would document the behaviour.
  • (nit) unconfirmedMessage("focusable element(s) inside aria-hidden", ...) produces "N focusable element(s) inside aria-hidden hidden only at some widths by a media query", which reads awkwardly. Consider a rule-specific phrasing.
  • (nit) readAtRuleWidths accepts @media( with no space. A one-line test for @media(min-width:1px) would pin it.
  • (nit) ControlItem.meta is typed Record<string, unknown>. A narrower { reason: string; shown: string } type would stop typos at call sites.
  • (nit) The if (!reach) return "unknown" guard in cascade is a good safety net. A short test that a non-display caller still gets unknown would lock it in.

Test coverage is good: 56 new tests, 25 Chromium-rendered rows, and the invariant "never hidden where Chromium shows". CI checks were still pending when I looked.

@nc9
nc9 force-pushed the feat/988-width-media-reason branch from 97d6a3d to 4fff8b8 Compare October 22, 2026 03:52
@claude

claude Bot commented Oct 22, 2026

Copy link
Copy Markdown

Reviewed the diff (media-widths parser, page-styles/cascade changes, static-hidden verdicts, rule messages, docs, fixtures and tests). I found no blocking problems.

  • (low) packages/rules/src/shared/media-widths.ts WIDTH_GAP_TOLERANCE (0.02px): gaps up to 0.02px count as covered. That matches the Bootstrap-style N / N-0.02px idiom. It does mean a real gap of 0.01 to 0.02px is treated as hidden at every width. The behaviour is documented and tested, so I'd only suggest a short comment saying it is deliberate.
  • (nit) readMediaWidths rejects a prelude over 500 chars, and MAX_AT_DEPTH = 8 caps nesting. Both fall back to the generic unknown, which is safe. A test for the depth-9 fallback would pin it down, if one isn't already there.
  • (nit) unconfirmedItem and the sortUnnamedControls branch build the same meta object (reason and shown) in two places. A small helper would keep them from drifting.

Test coverage is strong: unit tests for the width parser, cascade and ancestor-union cases, and the Chromium-verified fixtures with per-width expectations. The changelog and rule docs are updated.

@nc9
nc9 merged commit d803aa8 into main Oct 22, 2026
15 checks passed
@nc9
nc9 deleted the feat/988-width-media-reason branch October 22, 2026 04:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

1 participant