@ngbracket/a11y-devtools 0.9.0 → 0.10.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +44 -0
- package/README.md +95 -5
- package/bin/ngbr-a11y-report.mjs +84 -18
- package/dist/report/baseline.d.ts +61 -0
- package/dist/report/baseline.js +96 -0
- package/dist/report/format.d.ts +27 -4
- package/dist/report/format.js +56 -12
- package/dist/report/headless.d.ts +43 -4
- package/dist/report/headless.js +78 -22
- package/dist/report/html.d.ts +4 -0
- package/dist/report/html.js +168 -0
- package/dist/report/index.d.ts +4 -2
- package/dist/report/index.js +4 -2
- package/package.json +1 -1
package/CHANGELOG.md
CHANGED
|
@@ -3,6 +3,50 @@
|
|
|
3
3
|
All notable changes to `@ngbracket/a11y-devtools` are documented here.
|
|
4
4
|
This project adheres to [Semantic Versioning](https://semver.org/).
|
|
5
5
|
|
|
6
|
+
## 0.10.0
|
|
7
|
+
|
|
8
|
+
### Added
|
|
9
|
+
|
|
10
|
+
- **Baseline mode: fail CI only on new issues.** `--baseline <file>` compares a run
|
|
11
|
+
with a previous run's JSON report (from `--out`) and reports what's **new**,
|
|
12
|
+
**fixed** and **unchanged**. With a baseline, `--fail-on` counts only new findings,
|
|
13
|
+
so an app with known issues can turn the gate on today and stop the count going up.
|
|
14
|
+
- A finding's identity is route + rule + owning component + element selector.
|
|
15
|
+
Angular `_ngcontent`/`_nghost` hashes are ignored (they change between builds),
|
|
16
|
+
and so are impact and help text (an axe upgrade rewording a message isn't "new").
|
|
17
|
+
- Matching counts duplicates. A route missing from the baseline is all new. A route
|
|
18
|
+
that fails to scan isn't reported as "fixed".
|
|
19
|
+
- A bad baseline path or a non-report file fails fast (exit 2), before the scan.
|
|
20
|
+
- Markdown gets a "Compared with baseline" section, and new findings are marked. JSON
|
|
21
|
+
gets a `baseline` block (counts + `newFindings`) and remains a valid baseline.
|
|
22
|
+
- New API from `./report`: `diffAgainstBaseline`, `parseBaseline`, `findingKey`, and
|
|
23
|
+
the `BaselineDiff` / `BaselineReport` / `NewFinding` types. `toMarkdown` / `toJson`
|
|
24
|
+
take the diff as an optional second argument.
|
|
25
|
+
- **HTML report.** `--format html` (or `all`) writes one self-contained `.html` file:
|
|
26
|
+
inline CSS, no scripts, no external requests. It's accessible itself (landmarks,
|
|
27
|
+
sequential headings, a real data table, severity as text, AA contrast in light and
|
|
28
|
+
dark), and its own report-mode scan comes back clean. New `toHtml(report, diff?)`
|
|
29
|
+
export.
|
|
30
|
+
- **Dark-mode scanning.** Report mode used to run with the browser's default *light*
|
|
31
|
+
colour scheme, so a dark theme was never switched on or contrast-checked.
|
|
32
|
+
- `--color-scheme light|dark|both` (`colorScheme` in `scanPages`): the browser
|
|
33
|
+
emulates that `prefers-color-scheme`. `both` scans every route twice. Dark pages
|
|
34
|
+
are labelled `/route (dark)` and list only the issues that **don't also** appear
|
|
35
|
+
in light, so findings aren't doubled (`PageReport.darkOnly`). The keyboard-trap walk
|
|
36
|
+
runs once, in the light pass.
|
|
37
|
+
- Class/attribute-toggled themes: `--dark-class <name>` / `--dark-attribute
|
|
38
|
+
<name=value>` (`darkClass` / `darkAttribute`) set that on `<html>` before each
|
|
39
|
+
dark-pass scan. Either implies `both`.
|
|
40
|
+
- Hooks for anything else: `setup(page, { colorScheme })` now runs once per pass
|
|
41
|
+
in a fresh browser context, and the new `beforeScan(page, { colorScheme, route })`
|
|
42
|
+
runs after every route loads.
|
|
43
|
+
- New exports: `findingsNotIn`, `DARK_ONLY_NOTE`, and the `ColorScheme` /
|
|
44
|
+
`ThemeContext` types.
|
|
45
|
+
- `--format` now takes a comma-separated list (`md,json,html`), plus `both` (md + json,
|
|
46
|
+
still the default) and `all`.
|
|
47
|
+
- The real-browser E2E spec now runs the CLI itself: output formats and the baseline
|
|
48
|
+
gate (passes with no new issues, fails on a new serious one, rejects a bad baseline).
|
|
49
|
+
|
|
6
50
|
## 0.9.0
|
|
7
51
|
|
|
8
52
|
### Added
|
package/README.md
CHANGED
|
@@ -18,10 +18,11 @@ Part of the `@ngbracket` Angular tooling family.
|
|
|
18
18
|
|
|
19
19
|
Ships attribution — component **and** directive-level — + axe scan + grouped
|
|
20
20
|
console reporter (with a summary line) + the dev-only provider + the visual in-app
|
|
21
|
-
overlay (severity-coloured highlights, click-to-scroll) + headless report mode
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
21
|
+
overlay (severity-coloured highlights, click-to-scroll) + headless report mode
|
|
22
|
+
(Markdown / JSON / HTML, with a baseline so CI fails only on new issues) +
|
|
23
|
+
**Keyboard & Focus Mode** — tab-order visualisation, keyboard-reachability
|
|
24
|
+
findings, a focus-follow accessibility-tree preview, and focus-trap detection (the
|
|
25
|
+
part of accessibility axe can't test).
|
|
25
26
|
|
|
26
27
|
## How the attribution works
|
|
27
28
|
|
|
@@ -128,10 +129,93 @@ Start your app (`ng serve`), then point the CLI at it:
|
|
|
128
129
|
# writes a11y.md and a11y.json
|
|
129
130
|
npx ngbr-a11y-report --base http://localhost:4200 --route / --route /dashboard --out a11y
|
|
130
131
|
|
|
132
|
+
# …or pick formats: md, json, html (comma-separated), both (= md,json) or all
|
|
133
|
+
npx ngbr-a11y-report --base http://localhost:4200 --route / --out a11y --format all
|
|
134
|
+
|
|
131
135
|
# CI gate: exit non-zero on any serious/critical finding
|
|
132
136
|
npx ngbr-a11y-report --base http://localhost:4200 --route / --fail-on serious
|
|
133
137
|
```
|
|
134
138
|
|
|
139
|
+
### HTML report
|
|
140
|
+
|
|
141
|
+
`--format html` (or `all`) writes one self-contained `.html` file. It has no
|
|
142
|
+
scripts and makes no external requests, so you can attach it to a CI run, email it,
|
|
143
|
+
or open it offline. It has the same content as the Markdown report, and it's
|
|
144
|
+
accessible itself: landmarks, a sequential heading outline, a real data table,
|
|
145
|
+
severity written as text (not colour alone), and AA contrast in light and dark
|
|
146
|
+
mode. Its own report-mode scan comes back clean.
|
|
147
|
+
|
|
148
|
+
### Dark mode (and other colour schemes)
|
|
149
|
+
|
|
150
|
+
By default the headless browser reports a **light** colour-scheme preference, so a
|
|
151
|
+
dark theme is never switched on and its contrast is never checked. Pick the
|
|
152
|
+
scheme(s) to scan with `--color-scheme`:
|
|
153
|
+
|
|
154
|
+
```bash
|
|
155
|
+
# dark theme driven by prefers-color-scheme (the OS setting)
|
|
156
|
+
npx ngbr-a11y-report --base http://localhost:4200 --route / --color-scheme both
|
|
157
|
+
```
|
|
158
|
+
|
|
159
|
+
- `light` (default) or `dark` scans once in that scheme. Dark pages are labelled
|
|
160
|
+
`/route (dark)`.
|
|
161
|
+
- `both` scans each route twice. To avoid listing everything twice, the dark page
|
|
162
|
+
shows only the issues that **don't also** appear in light mode (typically contrast
|
|
163
|
+
problems specific to the dark palette).
|
|
164
|
+
|
|
165
|
+
Many apps switch themes with a class or attribute instead of the OS setting. For
|
|
166
|
+
those, tell report mode what to set on `<html>` for the dark pass (either flag
|
|
167
|
+
implies `--color-scheme both`):
|
|
168
|
+
|
|
169
|
+
```bash
|
|
170
|
+
npx ngbr-a11y-report --base http://localhost:4200 --route / --dark-class dark # Tailwind-style
|
|
171
|
+
npx ngbr-a11y-report --base http://localhost:4200 --route / --dark-attribute data-bs-theme=dark # Bootstrap
|
|
172
|
+
```
|
|
173
|
+
|
|
174
|
+
For anything else (a theme picker, a setting in localStorage, a class on `<body>`),
|
|
175
|
+
use the programmatic hooks. Both receive the pass's `colorScheme`:
|
|
176
|
+
|
|
177
|
+
```ts
|
|
178
|
+
await scanPages({
|
|
179
|
+
baseUrl: 'http://localhost:4200',
|
|
180
|
+
routes: ['/', '/settings'],
|
|
181
|
+
colorScheme: 'both',
|
|
182
|
+
// once per pass, e.g. pick the theme in a persisted setting
|
|
183
|
+
setup: async (page, { colorScheme }) => {
|
|
184
|
+
await page.evaluate((s) => localStorage.setItem('theme', s), colorScheme);
|
|
185
|
+
},
|
|
186
|
+
// after every route loads, for a theme that doesn't persist between pages
|
|
187
|
+
beforeScan: async (page, { colorScheme }) => {
|
|
188
|
+
if (colorScheme === 'dark') await page.click('button[aria-label="Dark theme"]');
|
|
189
|
+
},
|
|
190
|
+
});
|
|
191
|
+
```
|
|
192
|
+
|
|
193
|
+
### Baseline: fail CI only on new issues
|
|
194
|
+
|
|
195
|
+
An app with a backlog of known issues can't turn on `--fail-on` without every build
|
|
196
|
+
failing. A **baseline** fixes that. Save one run's JSON report, commit it, and
|
|
197
|
+
compare later runs against it:
|
|
198
|
+
|
|
199
|
+
```bash
|
|
200
|
+
# once: record today's state (commit a11y-baseline.json)
|
|
201
|
+
npx ngbr-a11y-report --base http://localhost:4200 --route / --route /dashboard \
|
|
202
|
+
--out a11y-baseline --format json
|
|
203
|
+
|
|
204
|
+
# in CI: fail only on findings that aren't in the baseline
|
|
205
|
+
npx ngbr-a11y-report --base http://localhost:4200 --route / --route /dashboard \
|
|
206
|
+
--baseline a11y-baseline.json --fail-on serious --out a11y --format all
|
|
207
|
+
```
|
|
208
|
+
|
|
209
|
+
- With `--baseline`, `--fail-on` counts only **new** findings. Known ones don't fail
|
|
210
|
+
the build, and the report shows what's **new**, **fixed** and **unchanged**.
|
|
211
|
+
- A finding's identity is its route, rule, owning component and element selector.
|
|
212
|
+
Angular's per-build `_ngcontent-…` style hashes are ignored, and so is reworded help
|
|
213
|
+
text after an axe upgrade.
|
|
214
|
+
- A route the baseline never scanned counts as entirely new, so re-record the
|
|
215
|
+
baseline when you add routes. A route that fails to scan isn't reported as "fixed".
|
|
216
|
+
- To ratchet down, re-record the baseline after fixing issues. The JSON from any run
|
|
217
|
+
(including a `--baseline` run) is itself a valid baseline.
|
|
218
|
+
|
|
135
219
|
For apps gated behind a login or theme picker, use the programmatic API with a
|
|
136
220
|
`setup` hook (run once before the routes are scanned):
|
|
137
221
|
|
|
@@ -150,6 +234,10 @@ const report = await scanPages({
|
|
|
150
234
|
console.log(toMarkdown(report));
|
|
151
235
|
```
|
|
152
236
|
|
|
237
|
+
The same pieces are exported for your own pipeline: `toJson`, `toHtml`,
|
|
238
|
+
`parseBaseline` and `diffAgainstBaseline` (pass the diff as the second argument to
|
|
239
|
+
`toMarkdown` / `toJson` / `toHtml`).
|
|
240
|
+
|
|
153
241
|
The report leads with a summary of **distinct rules vs. raw node-instances** (a
|
|
154
242
|
"54" is usually one rule firing dozens of times), then lists findings under each
|
|
155
243
|
owning component. When a flagged control is itself a third-party UI primitive
|
|
@@ -288,7 +376,6 @@ npm run build # tsc -> dist/ (ESM + .d.ts)
|
|
|
288
376
|
|
|
289
377
|
- Headless "linear walkthrough" — the tab sequence as an SR-ish reading list per
|
|
290
378
|
route in report-mode (M2's preview is currently overlay-only).
|
|
291
|
-
- HTML report + baseline/diff mode for CI.
|
|
292
379
|
- Per-component filtering and a violation-count badge.
|
|
293
380
|
|
|
294
381
|
### Done
|
|
@@ -301,6 +388,9 @@ npm run build # tsc -> dist/ (ESM + .d.ts)
|
|
|
301
388
|
- Dev-only provider.
|
|
302
389
|
- In-app overlay.
|
|
303
390
|
- Headless report mode (CLI + `./report`).
|
|
391
|
+
- HTML report output.
|
|
392
|
+
- Baseline/diff mode: CI fails only on new issues.
|
|
393
|
+
- Dark-mode scanning: `--color-scheme`, `--dark-class` / `--dark-attribute`, and theme-aware hooks.
|
|
304
394
|
- **Keyboard & Focus Mode M1** — tab-order visualisation + keyboard-reachability findings.
|
|
305
395
|
- **Keyboard & Focus Mode M2** — focus-follow accessibility-tree preview.
|
|
306
396
|
- **Keyboard & Focus Mode M3** — missing focus-trap detection (uncontained
|
package/bin/ngbr-a11y-report.mjs
CHANGED
|
@@ -1,6 +1,13 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
|
-
import { writeFileSync } from 'node:fs';
|
|
3
|
-
import {
|
|
2
|
+
import { readFileSync, writeFileSync } from 'node:fs';
|
|
3
|
+
import {
|
|
4
|
+
diffAgainstBaseline,
|
|
5
|
+
parseBaseline,
|
|
6
|
+
scanPages,
|
|
7
|
+
toHtml,
|
|
8
|
+
toJson,
|
|
9
|
+
toMarkdown,
|
|
10
|
+
} from '../dist/report/index.js';
|
|
4
11
|
|
|
5
12
|
const USAGE = `ngbr-a11y-report — component-attributed a11y scan of a running Angular dev build
|
|
6
13
|
|
|
@@ -11,11 +18,16 @@ Options:
|
|
|
11
18
|
--base <url> Origin of the running dev server (e.g. http://localhost:4200)
|
|
12
19
|
--route <path> Route to scan; repeat for multiple routes
|
|
13
20
|
--tags <list> Comma-separated axe tags to scope the ruleset (e.g. wcag22aa,best-practice)
|
|
14
|
-
--out <prefix> Write <prefix>.md /
|
|
15
|
-
--format <
|
|
21
|
+
--out <prefix> Write <prefix>.md / .json / .html (default: print Markdown to stdout)
|
|
22
|
+
--format <list> Comma-separated: md, json, html — or both (= md,json) or all
|
|
23
|
+
(default: both when --out is set)
|
|
16
24
|
--wait <ms> Settle time after load before scanning (default 1500)
|
|
17
25
|
--fail-on <impact> Exit non-zero if any finding is at/above impact
|
|
18
|
-
(minor | moderate | serious | critical) — for CI gating
|
|
26
|
+
(minor | moderate | serious | critical) — for CI gating.
|
|
27
|
+
With --baseline, only NEW findings count
|
|
28
|
+
--baseline <file> Compare with a previous run's JSON report (from --out) and
|
|
29
|
+
mark what's new, fixed and unchanged — so CI can fail only
|
|
30
|
+
on regressions while known issues are worked down
|
|
19
31
|
--framework-prefixes <list>
|
|
20
32
|
Comma-separated component-name prefixes to treat as
|
|
21
33
|
third-party UI primitives to walk past during attribution
|
|
@@ -29,6 +41,15 @@ Options:
|
|
|
29
41
|
--focus-traps Also walk each route with real Tab presses to find keyboard
|
|
30
42
|
traps — focus that cycles inside part of the page and
|
|
31
43
|
never moves on (ngbr/focus-trap). Runs after the scan
|
|
44
|
+
--color-scheme <s> light | dark | both — scan with the browser emulating that
|
|
45
|
+
prefers-color-scheme (default light). "both" scans each
|
|
46
|
+
route twice; dark pages list only issues light doesn't have
|
|
47
|
+
--dark-class <name>
|
|
48
|
+
Dark theme toggled by a class: add it to <html> for the
|
|
49
|
+
dark pass (e.g. dark, dark-theme). Implies --color-scheme both
|
|
50
|
+
--dark-attribute <name=value>
|
|
51
|
+
Dark theme toggled by an attribute on <html>, e.g.
|
|
52
|
+
data-bs-theme=dark. Implies --color-scheme both
|
|
32
53
|
--headed Launch a visible browser (debugging)
|
|
33
54
|
-h, --help Show this help
|
|
34
55
|
`;
|
|
@@ -48,12 +69,16 @@ function parseArgs(argv) {
|
|
|
48
69
|
case '--format': opts.format = next(); break;
|
|
49
70
|
case '--wait': opts.wait = Number(next()); break;
|
|
50
71
|
case '--fail-on': opts.failOn = next(); break;
|
|
72
|
+
case '--baseline': opts.baseline = next(); break;
|
|
51
73
|
case '--framework-prefixes':
|
|
52
74
|
opts.frameworkPrefixes = next().split(',').map((s) => s.trim()).filter(Boolean);
|
|
53
75
|
break;
|
|
54
76
|
case '--no-skip-primitives': opts.frameworkPrefixes = []; break;
|
|
55
77
|
case '--keyboard': opts.keyboard = true; break;
|
|
56
78
|
case '--focus-traps': opts.focusTraps = true; break;
|
|
79
|
+
case '--color-scheme': opts.colorScheme = next(); break;
|
|
80
|
+
case '--dark-class': opts.darkClass = next(); break;
|
|
81
|
+
case '--dark-attribute': opts.darkAttribute = next(); break;
|
|
57
82
|
case '--headed': opts.headed = true; break;
|
|
58
83
|
case '-h': case '--help': opts.help = true; break;
|
|
59
84
|
default: console.error(`Unknown argument: ${arg}\n`); opts.help = true;
|
|
@@ -69,6 +94,41 @@ if (opts.help || !opts.base || opts.routes.length === 0) {
|
|
|
69
94
|
process.exit(opts.help ? 0 : 1);
|
|
70
95
|
}
|
|
71
96
|
|
|
97
|
+
const FORMATS = { md: ['md'], json: ['json'], html: ['html'], both: ['md', 'json'], all: ['md', 'json', 'html'] };
|
|
98
|
+
const formats = new Set();
|
|
99
|
+
for (const name of String(opts.format).split(',').map((s) => s.trim()).filter(Boolean)) {
|
|
100
|
+
if (!FORMATS[name]) {
|
|
101
|
+
process.stderr.write(`Invalid --format "${name}" (use md, json, html, both or all)\n`);
|
|
102
|
+
process.exit(2);
|
|
103
|
+
}
|
|
104
|
+
FORMATS[name].forEach((f) => formats.add(f));
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
if (opts.colorScheme && !['light', 'dark', 'both'].includes(opts.colorScheme)) {
|
|
108
|
+
process.stderr.write(`Invalid --color-scheme "${opts.colorScheme}" (use light, dark or both)\n`);
|
|
109
|
+
process.exit(2);
|
|
110
|
+
}
|
|
111
|
+
let darkAttribute;
|
|
112
|
+
if (opts.darkAttribute) {
|
|
113
|
+
const eq = opts.darkAttribute.indexOf('=');
|
|
114
|
+
if (eq <= 0) {
|
|
115
|
+
process.stderr.write(`Invalid --dark-attribute "${opts.darkAttribute}" (use name=value, e.g. data-bs-theme=dark)\n`);
|
|
116
|
+
process.exit(2);
|
|
117
|
+
}
|
|
118
|
+
darkAttribute = { name: opts.darkAttribute.slice(0, eq), value: opts.darkAttribute.slice(eq + 1) };
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
// Read the baseline before the (slow) scan, so a bad path fails fast.
|
|
122
|
+
let baseline;
|
|
123
|
+
if (opts.baseline) {
|
|
124
|
+
try {
|
|
125
|
+
baseline = parseBaseline(readFileSync(opts.baseline, 'utf8'));
|
|
126
|
+
} catch (err) {
|
|
127
|
+
process.stderr.write(`Can't use --baseline ${opts.baseline}: ${err.message}\n`);
|
|
128
|
+
process.exit(2);
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
|
|
72
132
|
const report = await scanPages({
|
|
73
133
|
baseUrl: opts.base,
|
|
74
134
|
routes: opts.routes,
|
|
@@ -78,12 +138,21 @@ const report = await scanPages({
|
|
|
78
138
|
frameworkPrefixes: opts.frameworkPrefixes,
|
|
79
139
|
keyboard: opts.keyboard,
|
|
80
140
|
focusTraps: opts.focusTraps,
|
|
141
|
+
colorScheme: opts.colorScheme,
|
|
142
|
+
darkClass: opts.darkClass,
|
|
143
|
+
darkAttribute,
|
|
81
144
|
});
|
|
82
145
|
|
|
83
146
|
const allFindings = report.pages.flatMap((p) => p.findings);
|
|
84
147
|
process.stderr.write(
|
|
85
148
|
`Scanned ${report.pages.length} route(s) · ${allFindings.length} node-instance(s).\n`,
|
|
86
149
|
);
|
|
150
|
+
const diff = baseline ? diffAgainstBaseline(report, baseline) : undefined;
|
|
151
|
+
if (diff) {
|
|
152
|
+
process.stderr.write(
|
|
153
|
+
`Compared with baseline: ${diff.added.length} new · ${diff.fixed.length} fixed · ${diff.unchanged} unchanged.\n`,
|
|
154
|
+
);
|
|
155
|
+
}
|
|
87
156
|
if (allFindings.length > 0 && allFindings.every((f) => f.component === null)) {
|
|
88
157
|
process.stderr.write(
|
|
89
158
|
'Note: no component attribution (window.ng absent) — is this a production build? Report mode names components only against a dev build.\n',
|
|
@@ -91,16 +160,12 @@ if (allFindings.length > 0 && allFindings.every((f) => f.component === null)) {
|
|
|
91
160
|
}
|
|
92
161
|
|
|
93
162
|
if (!opts.out) {
|
|
94
|
-
process.stdout.write(toMarkdown(report) + '\n');
|
|
163
|
+
process.stdout.write(toMarkdown(report, diff) + '\n');
|
|
95
164
|
} else {
|
|
96
|
-
const
|
|
97
|
-
|
|
98
|
-
writeFileSync(`${opts.out}
|
|
99
|
-
process.stderr.write(`Wrote ${opts.out}
|
|
100
|
-
}
|
|
101
|
-
if (format === 'json' || format === 'both') {
|
|
102
|
-
writeFileSync(`${opts.out}.json`, toJson(report));
|
|
103
|
-
process.stderr.write(`Wrote ${opts.out}.json\n`);
|
|
165
|
+
const render = { md: toMarkdown, json: toJson, html: toHtml };
|
|
166
|
+
for (const format of formats) {
|
|
167
|
+
writeFileSync(`${opts.out}.${format}`, render[format](report, diff));
|
|
168
|
+
process.stderr.write(`Wrote ${opts.out}.${format}\n`);
|
|
104
169
|
}
|
|
105
170
|
}
|
|
106
171
|
|
|
@@ -110,11 +175,12 @@ if (opts.failOn) {
|
|
|
110
175
|
process.stderr.write(`Invalid --fail-on "${opts.failOn}" (use minor|moderate|serious|critical)\n`);
|
|
111
176
|
process.exit(2);
|
|
112
177
|
}
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
178
|
+
// With a baseline, only regressions fail the gate; known issues don't.
|
|
179
|
+
const gated = diff ? diff.added.map((a) => a.finding) : allFindings;
|
|
180
|
+
const worst = gated.reduce((max, f) => Math.max(max, IMPACT_RANK[f.impact] ?? 0), 0);
|
|
116
181
|
if (worst >= threshold) {
|
|
117
|
-
|
|
182
|
+
const which = diff ? 'new violation(s)' : 'violation(s)';
|
|
183
|
+
process.stderr.write(`Failing: found ${which} at or above "${opts.failOn}".\n`);
|
|
118
184
|
process.exit(1);
|
|
119
185
|
}
|
|
120
186
|
}
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
import type { A11yFinding } from '../scan.js';
|
|
2
|
+
import type { ScanReport } from './format.js';
|
|
3
|
+
/**
|
|
4
|
+
* Baseline/diff for report-mode: compare a run against a previous one so CI can
|
|
5
|
+
* fail only on *new* problems. An app with 40 known issues can still adopt a
|
|
6
|
+
* gate today — the known ones are the baseline, and the gate stops the count
|
|
7
|
+
* going up. Pure (no Node imports), so it's unit-testable and shares the
|
|
8
|
+
* browser-safe constraint of `format.ts`.
|
|
9
|
+
*
|
|
10
|
+
* The baseline is simply an earlier run's JSON report (`--out` writes it), so
|
|
11
|
+
* there's no separate file format to learn or migrate.
|
|
12
|
+
*/
|
|
13
|
+
/** The parts of a report the diff needs — satisfied by a parsed `toJson` report. */
|
|
14
|
+
export interface BaselineReport {
|
|
15
|
+
pages: {
|
|
16
|
+
label: string;
|
|
17
|
+
findings: A11yFinding[];
|
|
18
|
+
}[];
|
|
19
|
+
}
|
|
20
|
+
/** One finding that's in the current run but not the baseline. */
|
|
21
|
+
export interface NewFinding {
|
|
22
|
+
/** Label of the page it's on. */
|
|
23
|
+
page: string;
|
|
24
|
+
finding: A11yFinding;
|
|
25
|
+
}
|
|
26
|
+
/** Result of comparing a run with its baseline. */
|
|
27
|
+
export interface BaselineDiff {
|
|
28
|
+
/** In this run, not in the baseline — the ones a CI gate should fail on. */
|
|
29
|
+
added: NewFinding[];
|
|
30
|
+
/** In the baseline, gone from this run. */
|
|
31
|
+
fixed: NewFinding[];
|
|
32
|
+
/** Present in both. */
|
|
33
|
+
unchanged: number;
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* A finding's identity across runs: the page, the rule, the owning component,
|
|
37
|
+
* and the (normalized) element selector. Impact and help text are left out on
|
|
38
|
+
* purpose — an axe upgrade rewording a message shouldn't make it "new".
|
|
39
|
+
*/
|
|
40
|
+
export declare function findingKey(page: string, finding: A11yFinding): string;
|
|
41
|
+
/**
|
|
42
|
+
* The findings in `findings` that aren't also in `reference`, matched by
|
|
43
|
+
* {@link findingKey} with counts. Used for a dark-mode pass, so it reports only
|
|
44
|
+
* what dark mode adds rather than repeating every light-mode finding.
|
|
45
|
+
*/
|
|
46
|
+
export declare function findingsNotIn(findings: A11yFinding[], reference: A11yFinding[]): A11yFinding[];
|
|
47
|
+
/**
|
|
48
|
+
* Compare `current` with `baseline`. Matching is by {@link findingKey} with
|
|
49
|
+
* counts, so two identical keys in the current run against one in the baseline
|
|
50
|
+
* leaves one new. Pages are matched by label; a route the baseline never
|
|
51
|
+
* scanned counts entirely as new (add it to the baseline when you add it to CI).
|
|
52
|
+
* Pages that failed to scan in the current run are skipped, so an outage
|
|
53
|
+
* doesn't report everything on that page as fixed.
|
|
54
|
+
*/
|
|
55
|
+
export declare function diffAgainstBaseline(current: ScanReport, baseline: BaselineReport): BaselineDiff;
|
|
56
|
+
/**
|
|
57
|
+
* Read a baseline from its JSON text (a previous `toJson` report). Throws a
|
|
58
|
+
* readable error for anything that isn't one, rather than silently diffing
|
|
59
|
+
* against nothing and failing every finding as new.
|
|
60
|
+
*/
|
|
61
|
+
export declare function parseBaseline(json: string): BaselineReport;
|
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Angular stamps component-style scoping attributes (`_ngcontent-ng-c123…`,
|
|
3
|
+
* `_nghost-…`) whose hash changes between builds. They can appear in an axe
|
|
4
|
+
* selector, so strip them to keep a finding's identity stable across builds.
|
|
5
|
+
*/
|
|
6
|
+
function normalizeTarget(target) {
|
|
7
|
+
return target.replace(/\[_ng(?:content|host)-[^\]]*\]/g, '').trim();
|
|
8
|
+
}
|
|
9
|
+
/**
|
|
10
|
+
* A finding's identity across runs: the page, the rule, the owning component,
|
|
11
|
+
* and the (normalized) element selector. Impact and help text are left out on
|
|
12
|
+
* purpose — an axe upgrade rewording a message shouldn't make it "new".
|
|
13
|
+
*/
|
|
14
|
+
export function findingKey(page, finding) {
|
|
15
|
+
return [page, finding.id, finding.component ?? '', normalizeTarget(finding.target)].join('\u0000');
|
|
16
|
+
}
|
|
17
|
+
/**
|
|
18
|
+
* The findings in `findings` that aren't also in `reference`, matched by
|
|
19
|
+
* {@link findingKey} with counts. Used for a dark-mode pass, so it reports only
|
|
20
|
+
* what dark mode adds rather than repeating every light-mode finding.
|
|
21
|
+
*/
|
|
22
|
+
export function findingsNotIn(findings, reference) {
|
|
23
|
+
const counts = new Map();
|
|
24
|
+
for (const finding of reference) {
|
|
25
|
+
const key = findingKey('', finding);
|
|
26
|
+
counts.set(key, (counts.get(key) ?? 0) + 1);
|
|
27
|
+
}
|
|
28
|
+
return findings.filter((finding) => {
|
|
29
|
+
const key = findingKey('', finding);
|
|
30
|
+
const left = counts.get(key) ?? 0;
|
|
31
|
+
if (left === 0)
|
|
32
|
+
return true;
|
|
33
|
+
counts.set(key, left - 1);
|
|
34
|
+
return false;
|
|
35
|
+
});
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* Compare `current` with `baseline`. Matching is by {@link findingKey} with
|
|
39
|
+
* counts, so two identical keys in the current run against one in the baseline
|
|
40
|
+
* leaves one new. Pages are matched by label; a route the baseline never
|
|
41
|
+
* scanned counts entirely as new (add it to the baseline when you add it to CI).
|
|
42
|
+
* Pages that failed to scan in the current run are skipped, so an outage
|
|
43
|
+
* doesn't report everything on that page as fixed.
|
|
44
|
+
*/
|
|
45
|
+
export function diffAgainstBaseline(current, baseline) {
|
|
46
|
+
const failed = new Set(current.pages.filter((p) => p.error).map((p) => p.label));
|
|
47
|
+
const remaining = new Map();
|
|
48
|
+
for (const page of baseline.pages) {
|
|
49
|
+
if (failed.has(page.label))
|
|
50
|
+
continue;
|
|
51
|
+
for (const finding of page.findings) {
|
|
52
|
+
const key = findingKey(page.label, finding);
|
|
53
|
+
const bucket = remaining.get(key);
|
|
54
|
+
if (bucket)
|
|
55
|
+
bucket.push({ page: page.label, finding });
|
|
56
|
+
else
|
|
57
|
+
remaining.set(key, [{ page: page.label, finding }]);
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
const added = [];
|
|
61
|
+
let unchanged = 0;
|
|
62
|
+
for (const page of current.pages) {
|
|
63
|
+
for (const finding of page.findings) {
|
|
64
|
+
const bucket = remaining.get(findingKey(page.label, finding));
|
|
65
|
+
if (bucket && bucket.length > 0) {
|
|
66
|
+
bucket.pop();
|
|
67
|
+
unchanged++;
|
|
68
|
+
}
|
|
69
|
+
else {
|
|
70
|
+
added.push({ page: page.label, finding });
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
const fixed = [...remaining.values()].flat();
|
|
75
|
+
return { added, fixed, unchanged };
|
|
76
|
+
}
|
|
77
|
+
/**
|
|
78
|
+
* Read a baseline from its JSON text (a previous `toJson` report). Throws a
|
|
79
|
+
* readable error for anything that isn't one, rather than silently diffing
|
|
80
|
+
* against nothing and failing every finding as new.
|
|
81
|
+
*/
|
|
82
|
+
export function parseBaseline(json) {
|
|
83
|
+
let data;
|
|
84
|
+
try {
|
|
85
|
+
data = JSON.parse(json);
|
|
86
|
+
}
|
|
87
|
+
catch {
|
|
88
|
+
throw new Error('baseline is not valid JSON — pass a report written by `--out` (the .json file).');
|
|
89
|
+
}
|
|
90
|
+
const pages = data.pages;
|
|
91
|
+
if (!Array.isArray(pages) ||
|
|
92
|
+
!pages.every((p) => typeof p?.label === 'string' && Array.isArray(p.findings))) {
|
|
93
|
+
throw new Error('baseline has no `pages[].findings` — pass a report written by `--out` (the .json file).');
|
|
94
|
+
}
|
|
95
|
+
return { pages: pages };
|
|
96
|
+
}
|
package/dist/report/format.d.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import type { A11yFinding } from '../scan.js';
|
|
2
|
+
import type { BaselineDiff } from './baseline.js';
|
|
2
3
|
/**
|
|
3
4
|
* Pure formatting for report-mode: grouping + Markdown/JSON serialization. Kept
|
|
4
5
|
* free of any Node or Playwright imports so the browser side (`report.ts`) can
|
|
@@ -13,7 +14,16 @@ export interface PageReport {
|
|
|
13
14
|
findings: A11yFinding[];
|
|
14
15
|
/** Set when this route failed to scan; findings will be empty. */
|
|
15
16
|
error?: string;
|
|
17
|
+
/** Colour scheme the page was scanned in; absent means the browser default (light). */
|
|
18
|
+
colorScheme?: 'light' | 'dark';
|
|
19
|
+
/**
|
|
20
|
+
* True on the dark pass of a `colorScheme: 'both'` run: findings are only the
|
|
21
|
+
* ones that don't also appear in the light pass of the same route.
|
|
22
|
+
*/
|
|
23
|
+
darkOnly?: boolean;
|
|
16
24
|
}
|
|
25
|
+
/** One-line explanation for a dark-only page, shared by the Markdown and HTML reports. */
|
|
26
|
+
export declare const DARK_ONLY_NOTE = "Dark mode: lists only issues that don\u2019t also appear in light mode on this route.";
|
|
17
27
|
/** A whole report-mode run across one or more pages. */
|
|
18
28
|
export interface ScanReport {
|
|
19
29
|
/** ISO timestamp of the run. */
|
|
@@ -32,7 +42,20 @@ export declare function groupByComponent(findings: A11yFinding[]): Map<string, A
|
|
|
32
42
|
* actually describes an app. Reports surface both so neither can mislead.
|
|
33
43
|
*/
|
|
34
44
|
export declare function distinctRuleCount(findings: A11yFinding[]): number;
|
|
35
|
-
/**
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
45
|
+
/**
|
|
46
|
+
* Serialize a report to JSON, with per-page and overall summary counts. With a
|
|
47
|
+
* baseline `diff`, adds a `baseline` block (counts + the new findings). The
|
|
48
|
+
* output stays a valid baseline for the next run.
|
|
49
|
+
*/
|
|
50
|
+
export declare function toJson(report: ScanReport, diff?: BaselineDiff): string;
|
|
51
|
+
/** UI primitives and directives a finding came through, for a "via …" note. */
|
|
52
|
+
export declare function viaNames(finding: A11yFinding): string[];
|
|
53
|
+
/** Findings sorted most severe first (stable within an impact level). */
|
|
54
|
+
export declare function bySeverity(findings: A11yFinding[]): A11yFinding[];
|
|
55
|
+
/** A baseline diff's new findings, most severe first — what to look at first. */
|
|
56
|
+
export declare function addedBySeverity(diff: BaselineDiff): BaselineDiff['added'];
|
|
57
|
+
/**
|
|
58
|
+
* Render a human-readable Markdown report grouped by owning component. With a
|
|
59
|
+
* baseline `diff`, adds a comparison section and marks new findings.
|
|
60
|
+
*/
|
|
61
|
+
export declare function toMarkdown(report: ScanReport, diff?: BaselineDiff): string;
|
package/dist/report/format.js
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
/** One-line explanation for a dark-only page, shared by the Markdown and HTML reports. */
|
|
2
|
+
export const DARK_ONLY_NOTE = 'Dark mode: lists only issues that don’t also appear in light mode on this route.';
|
|
1
3
|
/**
|
|
2
4
|
* Group findings by the component that rendered them — the readable form the
|
|
3
5
|
* attribution makes possible ("♿ HeaderComponent — 2 issues" beats a list of
|
|
@@ -32,8 +34,12 @@ const IMPACT_RANK = {
|
|
|
32
34
|
function impactRank(impact) {
|
|
33
35
|
return impact ? (IMPACT_RANK[impact] ?? 4) : 4;
|
|
34
36
|
}
|
|
35
|
-
/**
|
|
36
|
-
|
|
37
|
+
/**
|
|
38
|
+
* Serialize a report to JSON, with per-page and overall summary counts. With a
|
|
39
|
+
* baseline `diff`, adds a `baseline` block (counts + the new findings). The
|
|
40
|
+
* output stays a valid baseline for the next run.
|
|
41
|
+
*/
|
|
42
|
+
export function toJson(report, diff) {
|
|
37
43
|
const all = report.pages.flatMap((p) => p.findings);
|
|
38
44
|
return JSON.stringify({
|
|
39
45
|
generatedAt: report.generatedAt,
|
|
@@ -42,6 +48,14 @@ export function toJson(report) {
|
|
|
42
48
|
distinctRules: distinctRuleCount(all),
|
|
43
49
|
nodeInstances: all.length,
|
|
44
50
|
},
|
|
51
|
+
...(diff && {
|
|
52
|
+
baseline: {
|
|
53
|
+
new: diff.added.length,
|
|
54
|
+
fixed: diff.fixed.length,
|
|
55
|
+
unchanged: diff.unchanged,
|
|
56
|
+
newFindings: diff.added.map(({ page, finding }) => ({ page, ...finding })),
|
|
57
|
+
},
|
|
58
|
+
}),
|
|
45
59
|
pages: report.pages.map((p) => ({
|
|
46
60
|
label: p.label,
|
|
47
61
|
url: p.url,
|
|
@@ -51,8 +65,26 @@ export function toJson(report) {
|
|
|
51
65
|
})),
|
|
52
66
|
}, null, 2);
|
|
53
67
|
}
|
|
54
|
-
/**
|
|
55
|
-
export function
|
|
68
|
+
/** UI primitives and directives a finding came through, for a "via …" note. */
|
|
69
|
+
export function viaNames(finding) {
|
|
70
|
+
// e.g. an `<button nbButton>` shows as "via NbButtonComponent" under its app owner.
|
|
71
|
+
const ownerIndex = finding.component ? finding.componentPath.indexOf(finding.component) : -1;
|
|
72
|
+
const wrappers = ownerIndex > 0 ? finding.componentPath.slice(0, ownerIndex) : [];
|
|
73
|
+
return [...wrappers, ...finding.directives];
|
|
74
|
+
}
|
|
75
|
+
/** Findings sorted most severe first (stable within an impact level). */
|
|
76
|
+
export function bySeverity(findings) {
|
|
77
|
+
return [...findings].sort((a, b) => impactRank(a.impact) - impactRank(b.impact));
|
|
78
|
+
}
|
|
79
|
+
/** A baseline diff's new findings, most severe first — what to look at first. */
|
|
80
|
+
export function addedBySeverity(diff) {
|
|
81
|
+
return [...diff.added].sort((a, b) => impactRank(a.finding.impact) - impactRank(b.finding.impact));
|
|
82
|
+
}
|
|
83
|
+
/**
|
|
84
|
+
* Render a human-readable Markdown report grouped by owning component. With a
|
|
85
|
+
* baseline `diff`, adds a comparison section and marks new findings.
|
|
86
|
+
*/
|
|
87
|
+
export function toMarkdown(report, diff) {
|
|
56
88
|
const all = report.pages.flatMap((p) => p.findings);
|
|
57
89
|
const lines = [];
|
|
58
90
|
lines.push('# Accessibility report');
|
|
@@ -70,9 +102,25 @@ export function toMarkdown(report) {
|
|
|
70
102
|
}
|
|
71
103
|
lines.push(`| **Total** | **${distinctRuleCount(all)}** | **${all.length}** | **${groupByComponent(all).size}** |`);
|
|
72
104
|
lines.push('');
|
|
105
|
+
const added = new Set(diff?.added.map((a) => a.finding));
|
|
106
|
+
if (diff) {
|
|
107
|
+
lines.push('## Compared with baseline');
|
|
108
|
+
lines.push('');
|
|
109
|
+
lines.push(`**${diff.added.length} new** · ${diff.fixed.length} fixed · ${diff.unchanged} unchanged`);
|
|
110
|
+
lines.push('');
|
|
111
|
+
for (const { page, finding } of addedBySeverity(diff)) {
|
|
112
|
+
lines.push(`- 🆕 **${finding.impact ?? 'n/a'} · ${finding.id}** on ${page}, in ${finding.component ?? '(unknown component)'}: \`${finding.target}\``);
|
|
113
|
+
}
|
|
114
|
+
if (diff.added.length)
|
|
115
|
+
lines.push('');
|
|
116
|
+
}
|
|
73
117
|
for (const page of report.pages) {
|
|
74
118
|
lines.push(`## ${page.label} — \`${page.url}\``);
|
|
75
119
|
lines.push('');
|
|
120
|
+
if (page.darkOnly) {
|
|
121
|
+
lines.push(`_${DARK_ONLY_NOTE}_`);
|
|
122
|
+
lines.push('');
|
|
123
|
+
}
|
|
76
124
|
if (page.error) {
|
|
77
125
|
lines.push(`⚠️ Scan failed: ${page.error}`);
|
|
78
126
|
lines.push('');
|
|
@@ -89,15 +137,11 @@ export function toMarkdown(report) {
|
|
|
89
137
|
for (const [component, items] of byComponent) {
|
|
90
138
|
lines.push(`### ♿ ${component} — ${items.length} issue(s)`);
|
|
91
139
|
lines.push('');
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
// UI primitives the app component rendered through, e.g. an `<button
|
|
95
|
-
// nbButton>` shows as "via NbButtonComponent" under its app owner.
|
|
96
|
-
const ownerIndex = finding.component ? finding.componentPath.indexOf(finding.component) : -1;
|
|
97
|
-
const wrappers = ownerIndex > 0 ? finding.componentPath.slice(0, ownerIndex) : [];
|
|
98
|
-
const via = [...wrappers, ...finding.directives];
|
|
140
|
+
for (const finding of bySeverity(items)) {
|
|
141
|
+
const via = viaNames(finding);
|
|
99
142
|
const suffix = via.length ? ` _(via ${via.join(', ')})_` : '';
|
|
100
|
-
|
|
143
|
+
const isNew = added.has(finding) ? '🆕 ' : '';
|
|
144
|
+
lines.push(`- ${isNew}**${finding.impact ?? 'n/a'} · ${finding.id}**: ${finding.help}${suffix}`);
|
|
101
145
|
lines.push(` - \`${finding.target}\``);
|
|
102
146
|
lines.push(` - ${finding.helpUrl}`);
|
|
103
147
|
}
|
|
@@ -1,5 +1,12 @@
|
|
|
1
1
|
import type { Page } from 'playwright';
|
|
2
2
|
import type { ScanReport } from './format.js';
|
|
3
|
+
/** A colour scheme a pass is scanned in. */
|
|
4
|
+
export type ColorScheme = 'light' | 'dark';
|
|
5
|
+
/** Passed to the `setup` and `beforeScan` hooks, so they can pick the matching theme. */
|
|
6
|
+
export interface ThemeContext {
|
|
7
|
+
/** The scheme this pass scans in. The browser already emulates it for `prefers-color-scheme`. */
|
|
8
|
+
colorScheme: ColorScheme;
|
|
9
|
+
}
|
|
3
10
|
export interface ScanPagesOptions {
|
|
4
11
|
/** Origin of the running dev server, e.g. `http://localhost:4200`. */
|
|
5
12
|
baseUrl: string;
|
|
@@ -12,11 +19,43 @@ export interface ScanPagesOptions {
|
|
|
12
19
|
/** Optional human labels keyed by route path (falls back to the route). */
|
|
13
20
|
labels?: Record<string, string>;
|
|
14
21
|
/**
|
|
15
|
-
* Run once against `baseUrl` before scanning — e.g.
|
|
16
|
-
* theme (ngx-admin gates its dashboard behind a theme
|
|
17
|
-
* persists (localStorage/cookies) carries into the scanned
|
|
22
|
+
* Run once per pass against `baseUrl` before scanning — e.g. log in, dismiss a
|
|
23
|
+
* splash, or pick a theme (ngx-admin gates its dashboard behind a theme
|
|
24
|
+
* picker). State that persists (localStorage/cookies) carries into the scanned
|
|
25
|
+
* routes. With `colorScheme: 'both'` it runs once for each pass, in a fresh
|
|
26
|
+
* browser context, and `colorScheme` says which theme to select.
|
|
27
|
+
*/
|
|
28
|
+
setup?: (page: Page, context: ThemeContext) => Promise<void>;
|
|
29
|
+
/**
|
|
30
|
+
* Run on every route after it loads and before it's scanned — for a theme that
|
|
31
|
+
* doesn't persist between pages. Gets the pass's `colorScheme` and the route.
|
|
32
|
+
*/
|
|
33
|
+
beforeScan?: (page: Page, context: ThemeContext & {
|
|
34
|
+
route: string;
|
|
35
|
+
}) => Promise<void>;
|
|
36
|
+
/**
|
|
37
|
+
* Which colour scheme(s) to scan in. The browser emulates the scheme, so
|
|
38
|
+
* `prefers-color-scheme` styles apply. `'both'` scans every route twice, and
|
|
39
|
+
* the dark pages list only issues that don't also appear in light (see
|
|
40
|
+
* `PageReport.darkOnly`). Default `'light'`, or `'both'` when `darkClass` /
|
|
41
|
+
* `darkAttribute` is set.
|
|
42
|
+
*/
|
|
43
|
+
colorScheme?: ColorScheme | 'both';
|
|
44
|
+
/**
|
|
45
|
+
* For a class-toggled dark theme: add this class to `<html>` before each
|
|
46
|
+
* dark-pass scan (e.g. `'dark'` for Tailwind, `'dark-theme'` for many Material
|
|
47
|
+
* apps). Implies `colorScheme: 'both'` unless set.
|
|
48
|
+
*/
|
|
49
|
+
darkClass?: string;
|
|
50
|
+
/**
|
|
51
|
+
* For an attribute-toggled dark theme: set this attribute on `<html>` before
|
|
52
|
+
* each dark-pass scan, e.g. `{ name: 'data-bs-theme', value: 'dark' }`
|
|
53
|
+
* (Bootstrap). Implies `colorScheme: 'both'` unless set.
|
|
18
54
|
*/
|
|
19
|
-
|
|
55
|
+
darkAttribute?: {
|
|
56
|
+
name: string;
|
|
57
|
+
value: string;
|
|
58
|
+
};
|
|
20
59
|
/** Launch a headed browser (for debugging). Default false (headless). */
|
|
21
60
|
headed?: boolean;
|
|
22
61
|
/**
|
package/dist/report/headless.js
CHANGED
|
@@ -2,6 +2,7 @@ import { readFileSync } from 'node:fs';
|
|
|
2
2
|
import { dirname, resolve } from 'node:path';
|
|
3
3
|
import { fileURLToPath } from 'node:url';
|
|
4
4
|
import { detectTabTrap } from '../keyboard/focus-walk.js';
|
|
5
|
+
import { findingsNotIn } from './baseline.js';
|
|
5
6
|
async function loadPlaywright() {
|
|
6
7
|
try {
|
|
7
8
|
return await import('playwright');
|
|
@@ -55,42 +56,47 @@ async function walkForFocusTrap(page, frameworkPrefixes) {
|
|
|
55
56
|
}
|
|
56
57
|
return null;
|
|
57
58
|
}
|
|
58
|
-
/**
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
export async function scanPages(options) {
|
|
65
|
-
const { baseUrl, routes, tags, waitMs = 1500, labels = {}, setup, headed = false, frameworkPrefixes, keyboard, focusTraps, } = options;
|
|
66
|
-
const axeOptions = tags
|
|
67
|
-
? { runOnly: { type: 'tag', values: tags } }
|
|
68
|
-
: undefined;
|
|
69
|
-
const inPageScript = readFileSync(inPageScriptPath(), 'utf8');
|
|
70
|
-
const { chromium } = await loadPlaywright();
|
|
71
|
-
const browser = await chromium.launch({ headless: !headed });
|
|
59
|
+
/** Settle time after switching a class/attribute theme, so colour transitions finish before axe reads them. */
|
|
60
|
+
const THEME_SETTLE_MS = 300;
|
|
61
|
+
/** Scan every route in one colour scheme, in its own browser context. */
|
|
62
|
+
async function scanPass(browser, colorScheme, config) {
|
|
63
|
+
const { baseUrl, routes, labels, waitMs, axeOptions, inPageScript, frameworkPrefixes } = config;
|
|
64
|
+
const context = await browser.newContext({ viewport: { width: 1280, height: 900 }, colorScheme });
|
|
72
65
|
const pages = [];
|
|
73
66
|
try {
|
|
74
|
-
const context = await browser.newContext({ viewport: { width: 1280, height: 900 } });
|
|
75
67
|
const page = await context.newPage();
|
|
76
|
-
if (setup) {
|
|
68
|
+
if (config.setup) {
|
|
77
69
|
await page.goto(baseUrl, { waitUntil: 'domcontentloaded' });
|
|
78
|
-
await setup(page);
|
|
70
|
+
await config.setup(page, { colorScheme });
|
|
79
71
|
}
|
|
80
72
|
for (const route of routes) {
|
|
81
73
|
const url = new URL(route, baseUrl).toString();
|
|
82
|
-
const
|
|
74
|
+
const base = labels[route] ?? route;
|
|
75
|
+
// Dark pages get their own label, so a dark page's baseline identity never
|
|
76
|
+
// collides with the same route's light page.
|
|
77
|
+
const label = colorScheme === 'dark' ? `${base} (dark)` : base;
|
|
83
78
|
try {
|
|
84
79
|
// networkidle is the right settle signal for an SPA, but some apps keep a
|
|
85
80
|
// socket open and never reach it — fall back to a plain load.
|
|
86
81
|
await page.goto(url, { waitUntil: 'networkidle' }).catch(() => page.goto(url));
|
|
87
82
|
await page.waitForTimeout(waitMs);
|
|
83
|
+
if (colorScheme === 'dark' && (config.darkClass || config.darkAttribute)) {
|
|
84
|
+
await page.evaluate(({ cls, attr }) => {
|
|
85
|
+
if (cls)
|
|
86
|
+
document.documentElement.classList.add(cls);
|
|
87
|
+
if (attr)
|
|
88
|
+
document.documentElement.setAttribute(attr.name, attr.value);
|
|
89
|
+
}, { cls: config.darkClass, attr: config.darkAttribute });
|
|
90
|
+
await page.waitForTimeout(THEME_SETTLE_MS);
|
|
91
|
+
}
|
|
92
|
+
if (config.beforeScan)
|
|
93
|
+
await config.beforeScan(page, { colorScheme, route });
|
|
88
94
|
await page.addScriptTag({ content: inPageScript });
|
|
89
95
|
const findings = (await page.evaluate((args) => {
|
|
90
96
|
const w = window;
|
|
91
97
|
return w.__ngbA11yScan(args.axe, args.scan);
|
|
92
|
-
}, { axe: axeOptions, scan: { frameworkPrefixes, keyboard } }));
|
|
93
|
-
if (focusTraps) {
|
|
98
|
+
}, { axe: axeOptions, scan: { frameworkPrefixes, keyboard: config.keyboard } }));
|
|
99
|
+
if (config.focusTraps) {
|
|
94
100
|
// A failed walk shouldn't throw away the scan's findings — warn and move on.
|
|
95
101
|
try {
|
|
96
102
|
const trap = await walkForFocusTrap(page, frameworkPrefixes);
|
|
@@ -102,15 +108,65 @@ export async function scanPages(options) {
|
|
|
102
108
|
console.warn(`ngbr-a11y-report: focus-trap walk failed on ${label}: ${message}`);
|
|
103
109
|
}
|
|
104
110
|
}
|
|
105
|
-
pages.push({ label, url: page.url(), findings });
|
|
111
|
+
pages.push({ label, url: page.url(), findings, colorScheme });
|
|
106
112
|
}
|
|
107
113
|
catch (err) {
|
|
108
114
|
// One bad route shouldn't sink the whole run — record it and continue.
|
|
109
115
|
const message = err instanceof Error ? err.message : String(err);
|
|
110
|
-
pages.push({ label, url, findings: [], error: message });
|
|
116
|
+
pages.push({ label, url, findings: [], error: message, colorScheme });
|
|
111
117
|
}
|
|
112
118
|
}
|
|
113
119
|
}
|
|
120
|
+
finally {
|
|
121
|
+
await context.close();
|
|
122
|
+
}
|
|
123
|
+
return pages;
|
|
124
|
+
}
|
|
125
|
+
/**
|
|
126
|
+
* Drive a real headless browser over a running Angular **dev** build and return
|
|
127
|
+
* component-attributed findings per route. The scan runs *in the page* (via the
|
|
128
|
+
* injected in-page bundle), so `window.ng` is live and each violation is mapped
|
|
129
|
+
* to the component that rendered it — with no change to the target app.
|
|
130
|
+
*/
|
|
131
|
+
export async function scanPages(options) {
|
|
132
|
+
const { baseUrl, routes, tags, waitMs = 1500, labels = {}, setup, beforeScan, headed = false, frameworkPrefixes, keyboard, focusTraps = false, darkClass, darkAttribute, } = options;
|
|
133
|
+
const colorScheme = options.colorScheme ?? (darkClass || darkAttribute ? 'both' : 'light');
|
|
134
|
+
const config = {
|
|
135
|
+
baseUrl,
|
|
136
|
+
routes,
|
|
137
|
+
labels,
|
|
138
|
+
waitMs,
|
|
139
|
+
axeOptions: tags ? { runOnly: { type: 'tag', values: tags } } : undefined,
|
|
140
|
+
inPageScript: readFileSync(inPageScriptPath(), 'utf8'),
|
|
141
|
+
frameworkPrefixes,
|
|
142
|
+
keyboard,
|
|
143
|
+
focusTraps,
|
|
144
|
+
setup,
|
|
145
|
+
beforeScan,
|
|
146
|
+
darkClass,
|
|
147
|
+
darkAttribute,
|
|
148
|
+
};
|
|
149
|
+
const { chromium } = await loadPlaywright();
|
|
150
|
+
const browser = await chromium.launch({ headless: !headed });
|
|
151
|
+
let pages;
|
|
152
|
+
try {
|
|
153
|
+
if (colorScheme !== 'both') {
|
|
154
|
+
pages = await scanPass(browser, colorScheme, config);
|
|
155
|
+
}
|
|
156
|
+
else {
|
|
157
|
+
const light = await scanPass(browser, 'light', config);
|
|
158
|
+
// A keyboard trap doesn't depend on the theme; walking it twice only costs time.
|
|
159
|
+
const dark = await scanPass(browser, 'dark', { ...config, focusTraps: false });
|
|
160
|
+
// Interleave per route (light, then its dark) and keep only what dark adds.
|
|
161
|
+
pages = light.flatMap((lightPage, i) => {
|
|
162
|
+
const darkPage = dark[i];
|
|
163
|
+
const findings = lightPage.error
|
|
164
|
+
? darkPage.findings
|
|
165
|
+
: findingsNotIn(darkPage.findings, lightPage.findings);
|
|
166
|
+
return [lightPage, { ...darkPage, findings, darkOnly: true }];
|
|
167
|
+
});
|
|
168
|
+
}
|
|
169
|
+
}
|
|
114
170
|
finally {
|
|
115
171
|
await browser.close();
|
|
116
172
|
}
|
|
@@ -0,0 +1,168 @@
|
|
|
1
|
+
import { addedBySeverity, DARK_ONLY_NOTE, bySeverity, distinctRuleCount, groupByComponent, viaNames, } from './format.js';
|
|
2
|
+
/**
|
|
3
|
+
* A single self-contained HTML report: inline CSS, no scripts, no external
|
|
4
|
+
* requests — so it can be attached to a CI run, emailed, or opened offline.
|
|
5
|
+
* Same content as the Markdown report (summary, optional baseline comparison,
|
|
6
|
+
* findings grouped by owning component, most severe first).
|
|
7
|
+
*
|
|
8
|
+
* An accessibility tool's own report has to be accessible: landmarks, a
|
|
9
|
+
* sequential heading outline, a real data table with header cells, severity
|
|
10
|
+
* given as text (never colour alone), and AA contrast in light and dark
|
|
11
|
+
* schemes. Component groups are plain `<h3>` sections rather than
|
|
12
|
+
* `<details>`: a heading inside `<summary>` loses its heading semantics in some
|
|
13
|
+
* browsers, and heading navigation is how screen-reader users move through a
|
|
14
|
+
* long report.
|
|
15
|
+
*/
|
|
16
|
+
function escapeHtml(text) {
|
|
17
|
+
return text
|
|
18
|
+
.replace(/&/g, '&')
|
|
19
|
+
.replace(/</g, '<')
|
|
20
|
+
.replace(/>/g, '>')
|
|
21
|
+
.replace(/"/g, '"')
|
|
22
|
+
.replace(/'/g, ''');
|
|
23
|
+
}
|
|
24
|
+
/** Only http(s) links are rendered as links; anything else stays plain text. */
|
|
25
|
+
function safeHref(url) {
|
|
26
|
+
return /^https?:\/\//i.test(url) ? escapeHtml(url) : null;
|
|
27
|
+
}
|
|
28
|
+
const STYLES = `
|
|
29
|
+
:root {
|
|
30
|
+
color-scheme: light dark;
|
|
31
|
+
--bg: #ffffff; --fg: #1a1a1a; --muted: #555555; --line: #d9d9d9; --panel: #f6f6f6;
|
|
32
|
+
--critical: #a4001d; --serious: #9a4400; --moderate: #6b5300; --minor: #1f4e9c;
|
|
33
|
+
--new: #0b6b3a; --link: #0b57d0;
|
|
34
|
+
}
|
|
35
|
+
@media (prefers-color-scheme: dark) {
|
|
36
|
+
:root {
|
|
37
|
+
--bg: #161616; --fg: #ececec; --muted: #b5b5b5; --line: #3a3a3a; --panel: #222222;
|
|
38
|
+
--critical: #ff8a9a; --serious: #ffb77a; --moderate: #f0d060; --minor: #9cc3ff;
|
|
39
|
+
--new: #7ee0a8; --link: #9cc3ff;
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
* { box-sizing: border-box; }
|
|
43
|
+
body { margin: 0; background: var(--bg); color: var(--fg);
|
|
44
|
+
font: 15px/1.55 system-ui, -apple-system, "Segoe UI", sans-serif; }
|
|
45
|
+
main { max-width: 960px; margin: 0 auto; padding: 32px 16px 64px; }
|
|
46
|
+
h1 { font-size: 1.6rem; margin: 0 0 4px; }
|
|
47
|
+
h2 { font-size: 1.25rem; margin: 40px 0 12px; padding-top: 16px; border-top: 1px solid var(--line); }
|
|
48
|
+
h3 { font-size: 1.05rem; margin: 0 0 4px; }
|
|
49
|
+
.meta { color: var(--muted); margin: 0 0 24px; }
|
|
50
|
+
a { color: var(--link); }
|
|
51
|
+
code { font: 0.9em/1.4 ui-monospace, SFMono-Regular, Menlo, monospace; overflow-wrap: anywhere; }
|
|
52
|
+
table { border-collapse: collapse; width: 100%; margin: 8px 0 16px; }
|
|
53
|
+
th, td { text-align: left; padding: 6px 10px; border-bottom: 1px solid var(--line); }
|
|
54
|
+
th[scope="col"]:not(:first-child), td:not(:first-child) { text-align: right; }
|
|
55
|
+
tfoot th, tfoot td { font-weight: 700; }
|
|
56
|
+
.table-wrap { overflow-x: auto; }
|
|
57
|
+
.component { background: var(--panel); border: 1px solid var(--line); border-radius: 8px;
|
|
58
|
+
margin: 12px 0; padding: 12px 14px 0; }
|
|
59
|
+
.count { color: var(--muted); font-weight: 400; }
|
|
60
|
+
a:focus-visible { outline: 3px solid var(--link); outline-offset: 2px; }
|
|
61
|
+
.visually-hidden { position: absolute; width: 1px; height: 1px; margin: -1px; padding: 0;
|
|
62
|
+
overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; border: 0; }
|
|
63
|
+
ul.findings { list-style: none; margin: 0 0 12px; padding: 0; }
|
|
64
|
+
ul.findings > li { padding: 10px 0; border-top: 1px solid var(--line); }
|
|
65
|
+
.impact { font-weight: 700; text-transform: uppercase; font-size: 0.8em; letter-spacing: 0.04em; }
|
|
66
|
+
.impact-critical { color: var(--critical); } .impact-serious { color: var(--serious); }
|
|
67
|
+
.impact-moderate { color: var(--moderate); } .impact-minor { color: var(--minor); }
|
|
68
|
+
.new { font-weight: 700; color: var(--new); }
|
|
69
|
+
.rule { font-weight: 600; }
|
|
70
|
+
.via, .detail { color: var(--muted); font-size: 0.92em; }
|
|
71
|
+
.detail { margin: 4px 0 0; }
|
|
72
|
+
.ok { color: var(--new); font-weight: 600; }
|
|
73
|
+
.error { color: var(--critical); font-weight: 600; }
|
|
74
|
+
.diff { background: var(--panel); border: 1px solid var(--line); border-radius: 8px;
|
|
75
|
+
margin-top: 32px; padding: 4px 16px; }
|
|
76
|
+
.diff h2 { border-top: 0; margin-top: 12px; padding-top: 0; }
|
|
77
|
+
`;
|
|
78
|
+
/** Render the report as one self-contained, accessible HTML document. */
|
|
79
|
+
export function toHtml(report, diff) {
|
|
80
|
+
const all = report.pages.flatMap((p) => p.findings);
|
|
81
|
+
const added = new Set(diff?.added.map((a) => a.finding));
|
|
82
|
+
const out = [];
|
|
83
|
+
out.push('<!doctype html>');
|
|
84
|
+
out.push('<html lang="en">');
|
|
85
|
+
out.push('<head>');
|
|
86
|
+
out.push('<meta charset="utf-8">');
|
|
87
|
+
out.push('<meta name="viewport" content="width=device-width, initial-scale=1">');
|
|
88
|
+
out.push('<title>Accessibility report</title>');
|
|
89
|
+
out.push(`<style>${STYLES}</style>`);
|
|
90
|
+
out.push('</head>');
|
|
91
|
+
out.push('<body>');
|
|
92
|
+
out.push('<main>');
|
|
93
|
+
out.push('<h1>Accessibility report</h1>');
|
|
94
|
+
out.push(`<p class="meta">Generated <time datetime="${escapeHtml(report.generatedAt)}">${escapeHtml(report.generatedAt)}</time> · @ngbracket/a11y-devtools report-mode</p>`);
|
|
95
|
+
// Summary — distinct rules next to raw node-instances, so the bigger number
|
|
96
|
+
// can't be read on its own as "how bad is it".
|
|
97
|
+
out.push('<section aria-labelledby="summary">');
|
|
98
|
+
out.push('<h2 id="summary">Summary</h2>');
|
|
99
|
+
out.push('<div class="table-wrap"><table>');
|
|
100
|
+
out.push('<thead><tr><th scope="col">Page</th><th scope="col">Distinct rules</th><th scope="col">Node-instances</th><th scope="col">Components</th></tr></thead>');
|
|
101
|
+
out.push('<tbody>');
|
|
102
|
+
for (const page of report.pages) {
|
|
103
|
+
out.push(`<tr><th scope="row">${escapeHtml(page.label)}</th><td>${distinctRuleCount(page.findings)}</td><td>${page.findings.length}</td><td>${groupByComponent(page.findings).size}</td></tr>`);
|
|
104
|
+
}
|
|
105
|
+
out.push('</tbody>');
|
|
106
|
+
out.push(`<tfoot><tr><th scope="row">Total</th><td>${distinctRuleCount(all)}</td><td>${all.length}</td><td>${groupByComponent(all).size}</td></tr></tfoot>`);
|
|
107
|
+
out.push('</table></div>');
|
|
108
|
+
out.push('</section>');
|
|
109
|
+
if (diff) {
|
|
110
|
+
out.push('<section aria-labelledby="baseline" class="diff">');
|
|
111
|
+
out.push('<h2 id="baseline">Compared with baseline</h2>');
|
|
112
|
+
out.push(`<p><strong class="new">${diff.added.length} new</strong> · ${diff.fixed.length} fixed · ${diff.unchanged} unchanged</p>`);
|
|
113
|
+
if (diff.added.length) {
|
|
114
|
+
out.push('<ul>');
|
|
115
|
+
for (const { page, finding } of addedBySeverity(diff)) {
|
|
116
|
+
out.push(`<li><span class="impact impact-${escapeHtml(finding.impact ?? 'none')}">${escapeHtml(finding.impact ?? 'n/a')}</span> <span class="rule">${escapeHtml(finding.id)}</span> on ${escapeHtml(page)}, in ${escapeHtml(finding.component ?? '(unknown component)')}: <code>${escapeHtml(finding.target)}</code></li>`);
|
|
117
|
+
}
|
|
118
|
+
out.push('</ul>');
|
|
119
|
+
}
|
|
120
|
+
out.push('</section>');
|
|
121
|
+
}
|
|
122
|
+
report.pages.forEach((page, i) => {
|
|
123
|
+
const headingId = `page-${i + 1}`;
|
|
124
|
+
out.push(`<section aria-labelledby="${headingId}">`);
|
|
125
|
+
out.push(`<h2 id="${headingId}">${escapeHtml(page.label)} <code>${escapeHtml(page.url)}</code></h2>`);
|
|
126
|
+
if (page.darkOnly)
|
|
127
|
+
out.push(`<p class="meta">${escapeHtml(DARK_ONLY_NOTE)}</p>`);
|
|
128
|
+
if (page.error) {
|
|
129
|
+
out.push(`<p class="error">Scan failed: ${escapeHtml(page.error)}</p>`);
|
|
130
|
+
out.push('</section>');
|
|
131
|
+
return;
|
|
132
|
+
}
|
|
133
|
+
if (page.findings.length === 0) {
|
|
134
|
+
out.push('<p class="ok">No violations found.</p>');
|
|
135
|
+
out.push('</section>');
|
|
136
|
+
return;
|
|
137
|
+
}
|
|
138
|
+
out.push(`<p>${page.findings.length} node-instance(s) across ${distinctRuleCount(page.findings)} distinct rule(s).</p>`);
|
|
139
|
+
const byComponent = [...groupByComponent(page.findings)].sort((a, b) => b[1].length - a[1].length);
|
|
140
|
+
for (const [component, items] of byComponent) {
|
|
141
|
+
out.push('<div class="component">');
|
|
142
|
+
out.push(`<h3>${escapeHtml(component)} <span class="count">— ${items.length} issue(s)</span></h3>`);
|
|
143
|
+
out.push('<ul class="findings">');
|
|
144
|
+
for (const finding of bySeverity(items)) {
|
|
145
|
+
const via = viaNames(finding);
|
|
146
|
+
const href = safeHref(finding.helpUrl);
|
|
147
|
+
const impact = finding.impact ?? 'n/a';
|
|
148
|
+
out.push('<li>');
|
|
149
|
+
out.push(`${added.has(finding) ? '<span class="new">New</span> · ' : ''}<span class="impact impact-${escapeHtml(finding.impact ?? 'none')}">${escapeHtml(impact)}</span> · <span class="rule">${escapeHtml(finding.id)}</span>: ${escapeHtml(finding.help)}`);
|
|
150
|
+
if (via.length)
|
|
151
|
+
out.push(` <span class="via">(via ${escapeHtml(via.join(', '))})</span>`);
|
|
152
|
+
out.push(`<p class="detail"><code>${escapeHtml(finding.target)}</code>`);
|
|
153
|
+
if (href) {
|
|
154
|
+
out.push(` · <a href="${href}">Rule reference<span class="visually-hidden"> for ${escapeHtml(finding.id)}</span></a>`);
|
|
155
|
+
}
|
|
156
|
+
out.push('</p>');
|
|
157
|
+
out.push('</li>');
|
|
158
|
+
}
|
|
159
|
+
out.push('</ul>');
|
|
160
|
+
out.push('</div>');
|
|
161
|
+
}
|
|
162
|
+
out.push('</section>');
|
|
163
|
+
});
|
|
164
|
+
out.push('</main>');
|
|
165
|
+
out.push('</body>');
|
|
166
|
+
out.push('</html>');
|
|
167
|
+
return out.join('\n');
|
|
168
|
+
}
|
package/dist/report/index.d.ts
CHANGED
|
@@ -1,2 +1,4 @@
|
|
|
1
|
-
export { scanPages, type ScanPagesOptions } from './headless.js';
|
|
2
|
-
export { groupByComponent, distinctRuleCount, toJson, toMarkdown, type PageReport, type ScanReport, } from './format.js';
|
|
1
|
+
export { scanPages, type ColorScheme, type ScanPagesOptions, type ThemeContext, } from './headless.js';
|
|
2
|
+
export { groupByComponent, distinctRuleCount, toJson, toMarkdown, DARK_ONLY_NOTE, type PageReport, type ScanReport, } from './format.js';
|
|
3
|
+
export { toHtml } from './html.js';
|
|
4
|
+
export { diffAgainstBaseline, findingKey, findingsNotIn, parseBaseline, type BaselineDiff, type BaselineReport, type NewFinding, } from './baseline.js';
|
package/dist/report/index.js
CHANGED
|
@@ -1,2 +1,4 @@
|
|
|
1
|
-
export { scanPages } from './headless.js';
|
|
2
|
-
export { groupByComponent, distinctRuleCount, toJson, toMarkdown, } from './format.js';
|
|
1
|
+
export { scanPages, } from './headless.js';
|
|
2
|
+
export { groupByComponent, distinctRuleCount, toJson, toMarkdown, DARK_ONLY_NOTE, } from './format.js';
|
|
3
|
+
export { toHtml } from './html.js';
|
|
4
|
+
export { diffAgainstBaseline, findingKey, findingsNotIn, parseBaseline, } from './baseline.js';
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ngbracket/a11y-devtools",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.10.0",
|
|
4
4
|
"description": "Dev-only in-app accessibility auditing for Angular that maps each axe violation back to the component that rendered it — the attribution React overlay tools can't do.",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"author": "Duncan Faulkner",
|