Repository navigation
fix(rules): color-contrast reports only contrast it measured #465 - #473
Merged
Merged
Conversation
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
force-pushed
the
fix/color-contrast-465
branch
from
October 6, 2026 00:29
eb66169 to
09d78fb
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #465
What was wrong
a11y/color-contrastran four passes, and three of them never computed a ratio:text-gray-3xx/4xxand the slate/zinc/neutral/stone equivalents,text-muted,text-secondary,opacity-2x/3xand others reported asp with class "..." may have low contrast;<style>blocks: any rule containing a light hex value;Light gray text: N instance(s)andWhite text (verify background).None of these knows the background. On the issue's page,
text-gray-300(#d1d5db) sits on#111from an external stylesheet, about 12.6:1, and was still reported. Only the inline-style pass measured anything. Even that one treatedtransparentas white, ignored alpha, and took the firstcolor:declaration rather than the one the cascade applies.The fix
<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.stylesettles on an opaque text color and an opaque background color, it has text, and the computed WCAG ratio is under 4.5:1:!importantbeats later ones without it.background-color, or from abackgroundshorthand that is exactly one color. Any other shorthand, or abackground-imageother thannone, leaves the pair unresolved.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.Object.hasOwn. Before,color:constructorcrashed the rule.detailsnow carriesmeasuredPairsin place of the per-pass counts, which nothing else read. The message is nowN color contrast issue(s) below 4.5:1, and inline item ids keep theirspan: #999 on #fff (2.85:1)format.\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/solutionand 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-corecarries unmatched prior items while the page is still in the check's failing set. Pages that now pass resolve them.Template/cache:
verdictScopestays"page", so there is no fan-out. The rule now reads less (no rawctx.page.htmlscan) and nothing fromctx.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-contrastfinding (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.tsis rewritten (55 tests). It covers:<style>/one-sided inline guesses that must not warn;rgb(), opaquergba, 8-digit hex,!importantand! important, cascade order,!importantprecedence, an unrelatedurl(),background-image:none, and a shorthand after an image;;, comments, parens,#c/**/cc,var()override,image-set(),!important junk, over-long hex);<style>and on 200 KB adversarial inline styles.44 of the 55 fail on
main.color-contrast,template-verdict-page-independence,noscript-content.tsgo --noEmitclean inpackages/rules(and the test file). oxlint clean.[email protected] check --strictpasses.Filed while here: #472. Every rule page's "Enable only this rule" example sets
disable = ["*"], which disables everything, because disable wins.Changelog suggestion
a11y/color-contrastreports only contrast it measured: an inline text and background color pair under 4.5:1. Class-name and stylesheet guesses such astext-gray-300are no longer findings. Thanks @deniskern (a11y/color-contrast: reports a class-name guess with no measured ratio on a high-contrast element #465)