Skip to content

fix(rules): color-contrast reports only contrast it measured #465 - #473

Merged
nc9 merged 1 commit into
mainfrom
fix/color-contrast-465
Oct 6, 2026
Merged

nc9 merged 1 commit into
mainfrom
fix/color-contrast-465

Conversation

@nc9

@nc9 nc9 commented Oct 5, 2026 •

Copy link
Copy Markdown
Contributor

Closes #465

What was wrong

a11y/color-contrast ran four passes, and three of them never computed a ratio:

  • class names: text-gray-3xx/4xx and the slate/zinc/neutral/stone equivalents, text-muted, text-secondary, opacity-2x/3x and others reported as p with class "..." may have low contrast;
  • <style> blocks: any rule containing a light hex value;
  • raw HTML: regex counts such as Light gray text: N instance(s) and White text (verify background).

None of these knows the background. On the issue's page, text-gray-300 (#d1d5db) sits on #111 from an external stylesheet, about 12.6:1, and was still reported. Only the inline-style pass measured anything. Even that one treated transparent as white, ignored alpha, and took the first color: declaration rather than the one the cascade applies.

The fix

  • The class, <style> and raw-HTML passes are removed: they cannot know the background, and an unresolved pair is not a violation (axe reports it as "incomplete"). I dropped them rather than adding a needs-review bucket, since the check has no such section and the brief said not to invent one. This also removes the <style> scanner (extractColorRules) and its oracle tests; it existed only for that pass.
  • An element is reported only when its own inline style settles on an opaque text color and an opaque background color, it has text, and the computed WCAG ratio is under 4.5:1:
    • Declarations are split by a linear scanner that respects quotes, escapes, parentheses and comments. They follow the cascade: the last one wins, and !important beats later ones without it.
    • The background comes from background-color, or from a background shorthand that is exactly one color. Any other shorthand, or a background-image other than none, leaves the pair unresolved.
    • Colors are parsed strictly on the whole value. transparent, alpha below 1 (rgba, 4-/8-digit hex), var(), inherit, currentcolor, out-of-range channels, malformed hex and unknown syntax all leave the pair unresolved.
    • Named colors are looked up with Object.hasOwn. Before, color:constructor crashed the rule.
  • The pass/info conditions are unchanged, to avoid score drift. details now carries measuredPairs in place of the per-pass counts, which nothing else read. The message is now N color contrast issue(s) below 4.5:1, and inline item ids keep their span: #999 on #fff (2.85:1) format.
  • ReDoS (Docs: fix page titles and meta descriptions (length, uniqueness, search intent) #2378): the only regexes left are anchored, bounded hex/rgb patterns. My first draft of the alpha pattern (\d*\.?\d+) was quadratic: about 60s on a 200 KB inline style. It is now \d+(?:\.\d+)?|\.\d+, and a test proves the quadratic form fails the 500 ms bound. Every pattern was probed at 4 KB vs 16 KB adversarial input, and only the discarded draft scaled worse than linear.
  • meta.description/solution and the rule page are rewritten to match the code.

Cloud note, filed as #474: on a page that still has a measured low-contrast pair, the old class-name and stylesheet guess findings stay open as carried findings until the page passes, because merge-core carries unmatched prior items while the page is still in the check's failing set. Pages that now pass resolve them.

Template/cache: verdictScope stays "page", so there is no fan-out. The rule now reads less (no raw ctx.page.html scan) and nothing from ctx.site.

Evidence (CLI from source, --http -f json --refresh, local fixtures)

Issue repro page, before:

{"status": "warn", "message": "1 potential color contrast issue(s)",
 "items": [{"id": "p with class \"text-gray-300...\" may have low contrast"}]}

After: no a11y/color-contrast finding (check passes).

True-positive page: the same hero paragraph plus <span style="color:#999;background:#fff">. Before, three items, two of them guesses:

{"status": "warn", "message": "3 potential color contrast issue(s)",
 "items": [{"id": "Light gray text: 1 instance(s)"}, {"id": "p with class \"text-gray-300...\" may have low contrast"}, {"id": "span: #999 on #fff (2.85:1)"}]}

After, the measured pair only:

{"status": "warn", "message": "1 color contrast issue(s) below 4.5:1",
 "items": [{"id": "span: #999 on #fff (2.85:1)"}]}

Tests

  • packages/rules/tests/color-contrast.test.ts is rewritten (55 tests). It covers:
    • the repro, and class/<style>/one-sided inline guesses that must not warn;
    • measured true positives: hex, named, rgb(), opaque rgba, 8-digit hex, !important and ! important, cascade order, !important precedence, an unrelated url(), background-image:none, and a shorthand after an image;
    • 20 unresolvable cases, including the codex repros (quoted ;, comments, parens, #c/**/cc, var() override, image-set(), !important junk, over-long hex);
    • the empty element and info branches;
    • linear-time tests on a 500 KB <style> and on 200 KB adversarial inline styles.
      44 of the 55 fail on main.
  • Green: color-contrast, template-verdict-page-independence, noscript-content.
  • tsgo --noEmit clean in packages/rules (and the test file). oxlint clean. [email protected] check --strict passes.

Filed while here: #472. Every rule page's "Enable only this rule" example sets disable = ["*"], which disables everything, because disable wins.

Changelog suggestion

@nc9
nc9 marked this pull request as ready for review October 6, 2026 00:29
Class names (text-gray-300), light colors in <style> blocks and raw color:
text in the HTML produced warnings with no ratio, including for text that
measures above 12:1 on its real background. Those passes are removed. An
element is reported only when its inline style settles on an opaque text and
background color and the computed ratio is under 4.5:1; an unresolved pair is
not a violation. Inline declarations follow the cascade (last wins,
!important first), split on real semicolons only, and transparent, alpha,
var(), image and gradient backgrounds stay unresolved. The alpha pattern is
linear (a \d*\.?\d+ form took 60s on 200KB).

Closes #465

Signed-off-by: Nik Cubrilovic <[email protected]>
@nc9
nc9 force-pushed the fix/color-contrast-465 branch from eb66169 to 09d78fb Compare October 6, 2026 00:29
@nc9
nc9 merged commit 72d4740 into main Oct 6, 2026
14 checks passed
@nc9
nc9 deleted the fix/color-contrast-465 branch October 6, 2026 00:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

a11y/color-contrast: reports a class-name guess with no measured ratio on a high-contrast element

1 participant