@ngbracket/a11y-devtools 0.8.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 +69 -0
- package/README.md +146 -16
- package/bin/ngbr-a11y-report.mjs +89 -18
- package/dist/index.d.ts +1 -0
- package/dist/index.js +1 -0
- package/dist/inpage.global.js +331 -231
- package/dist/inpage.js +4 -0
- package/dist/keyboard/focus-walk.d.ts +55 -0
- package/dist/keyboard/focus-walk.js +147 -0
- package/dist/keyboard/keyboard-scan.d.ts +4 -0
- package/dist/keyboard/keyboard-scan.js +3 -2
- 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 +51 -4
- package/dist/report/headless.js +128 -19
- 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
|
@@ -0,0 +1,147 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The keyboard layer, part D (M3 part 2): *bad* focus-trap detection — "you can
|
|
3
|
+
* Tab in, but never Tab out" (WCAG 2.1.2 No Keyboard Trap). Unlike the other
|
|
4
|
+
* keyboard checks this can't be read off the DOM: a trap is made by script that
|
|
5
|
+
* intercepts Tab or re-focuses on blur, and synthetic key events don't move focus
|
|
6
|
+
* in a browser. So report-mode drives **real** Tab presses (Playwright) and the
|
|
7
|
+
* walk is split in two:
|
|
8
|
+
*
|
|
9
|
+
* - {@link detectTabTrap} — pure: given the sequence of focused elements the walk
|
|
10
|
+
* observed, decide whether focus completed a full lap, is still going, or is
|
|
11
|
+
* cycling inside a subset of the page (a trap). Runs in Node; unit-tested.
|
|
12
|
+
* - {@link createFocusWalkProbe} — in-page: gives each focused element a stable
|
|
13
|
+
* id for the walk, and turns a detected cycle into an attributed finding.
|
|
14
|
+
*
|
|
15
|
+
* HONESTY GUARDRAIL: a candidate, not a verdict. Some widgets legitimately keep
|
|
16
|
+
* Tab (a code editor, a modal) and are fine if they document another exit — so
|
|
17
|
+
* the finding says "verify manually", and focus cycling inside an open modal is
|
|
18
|
+
* not reported at all (that's containment working; the missing-trap check covers
|
|
19
|
+
* a modal that leaks).
|
|
20
|
+
*/
|
|
21
|
+
import { appComponentFromPath, DEFAULT_FRAMEWORK_PREFIXES, resolveComponentPath, resolveDirectiveNames, } from '../attribution.js';
|
|
22
|
+
import { shortHtml, shortSelector } from './keyboard-scan.js';
|
|
23
|
+
import { tabSequence } from './tab-sequence.js';
|
|
24
|
+
/**
|
|
25
|
+
* Classify a Tab walk. Browsers pass focus through `<body>` (null here) when Tab
|
|
26
|
+
* wraps off the end of the page, so an element seen again *after* a null means
|
|
27
|
+
* focus made a full lap; seen again with no null in between means it looped
|
|
28
|
+
* inside part of the page — a trap. The walk may start mid-page (wherever the
|
|
29
|
+
* browser's navigation starting point is), which this handles for free.
|
|
30
|
+
*
|
|
31
|
+
* Consecutive repeats of an `iframe` are the browser tabbing *inside* the frame
|
|
32
|
+
* (the parent document only ever sees the frame element), so callers pass
|
|
33
|
+
* `collapsible` ids to skip those rather than read them as a trap.
|
|
34
|
+
*/
|
|
35
|
+
export function detectTabTrap(observations, collapsible = new Set()) {
|
|
36
|
+
const seq = [];
|
|
37
|
+
for (const obs of observations) {
|
|
38
|
+
const prev = seq[seq.length - 1];
|
|
39
|
+
if (obs !== null && obs === prev && collapsible.has(obs))
|
|
40
|
+
continue;
|
|
41
|
+
seq.push(obs);
|
|
42
|
+
}
|
|
43
|
+
const lastSeen = new Map();
|
|
44
|
+
for (let i = 0; i < seq.length; i++) {
|
|
45
|
+
const id = seq[i];
|
|
46
|
+
if (id === null)
|
|
47
|
+
continue;
|
|
48
|
+
const before = lastSeen.get(id);
|
|
49
|
+
if (before !== undefined) {
|
|
50
|
+
const between = seq.slice(before, i);
|
|
51
|
+
if (between.includes(null))
|
|
52
|
+
return { kind: 'complete' };
|
|
53
|
+
return { kind: 'trap', cycle: between };
|
|
54
|
+
}
|
|
55
|
+
lastSeen.set(id, i);
|
|
56
|
+
}
|
|
57
|
+
return { kind: 'pending' };
|
|
58
|
+
}
|
|
59
|
+
const NO_KEYBOARD_TRAP_URL = 'https://www.w3.org/WAI/WCAG22/Understanding/no-keyboard-trap.html';
|
|
60
|
+
/** The element that really has focus, through open shadow roots. */
|
|
61
|
+
function deepActiveElement(doc) {
|
|
62
|
+
let active = doc.activeElement;
|
|
63
|
+
while (active?.shadowRoot?.activeElement)
|
|
64
|
+
active = active.shadowRoot.activeElement;
|
|
65
|
+
return active;
|
|
66
|
+
}
|
|
67
|
+
/** True when focus cycling among `elements` is an open modal containing focus (fine). */
|
|
68
|
+
function containedByOpenModal(elements) {
|
|
69
|
+
const modalOf = (el) => {
|
|
70
|
+
const aria = el.closest('[aria-modal="true"]');
|
|
71
|
+
if (aria)
|
|
72
|
+
return aria;
|
|
73
|
+
try {
|
|
74
|
+
return el.closest('dialog:modal');
|
|
75
|
+
}
|
|
76
|
+
catch {
|
|
77
|
+
return null; // engine without :modal
|
|
78
|
+
}
|
|
79
|
+
};
|
|
80
|
+
const first = elements[0] ? modalOf(elements[0]) : null;
|
|
81
|
+
return first !== null && elements.every((el) => modalOf(el) === first);
|
|
82
|
+
}
|
|
83
|
+
/** Nearest element containing every element of the cycle. */
|
|
84
|
+
function commonAncestor(elements) {
|
|
85
|
+
let candidate = elements[0];
|
|
86
|
+
while (candidate && !elements.every((el) => candidate.contains(el))) {
|
|
87
|
+
candidate = candidate.parentElement;
|
|
88
|
+
}
|
|
89
|
+
return candidate ?? elements[0];
|
|
90
|
+
}
|
|
91
|
+
/**
|
|
92
|
+
* Create the in-page probe. Ids are assigned on first sight and held in page
|
|
93
|
+
* memory only — the walk never writes to the DOM.
|
|
94
|
+
*/
|
|
95
|
+
export function createFocusWalkProbe(doc = document, frameworkPrefixes = DEFAULT_FRAMEWORK_PREFIXES) {
|
|
96
|
+
const ids = new WeakMap();
|
|
97
|
+
const byId = new Map();
|
|
98
|
+
return {
|
|
99
|
+
probe() {
|
|
100
|
+
const active = deepActiveElement(doc);
|
|
101
|
+
if (!active || active === doc.body || active === doc.documentElement) {
|
|
102
|
+
return { id: null, iframe: false };
|
|
103
|
+
}
|
|
104
|
+
let id = ids.get(active);
|
|
105
|
+
if (id === undefined) {
|
|
106
|
+
id = byId.size + 1;
|
|
107
|
+
ids.set(active, id);
|
|
108
|
+
byId.set(id, active);
|
|
109
|
+
}
|
|
110
|
+
return { id, iframe: active.tagName === 'IFRAME' };
|
|
111
|
+
},
|
|
112
|
+
stopCount() {
|
|
113
|
+
return tabSequence(doc, { frameworkPrefixes }).length;
|
|
114
|
+
},
|
|
115
|
+
trapFinding(cycle, shiftTabEscapes) {
|
|
116
|
+
const elements = [...new Set(cycle)]
|
|
117
|
+
.map((id) => byId.get(id))
|
|
118
|
+
.filter((el) => el !== undefined);
|
|
119
|
+
if (elements.length === 0 || containedByOpenModal(elements))
|
|
120
|
+
return null;
|
|
121
|
+
const owner = elements.length === 1 ? elements[0] : commonAncestor(elements);
|
|
122
|
+
const reachable = new Set(byId.values());
|
|
123
|
+
const unreached = tabSequence(doc, { frameworkPrefixes }).filter((stop) => !reachable.has(stop.element)).length;
|
|
124
|
+
const where = elements.length === 1
|
|
125
|
+
? 'focus stays on this element when Tab is pressed'
|
|
126
|
+
: `Tab cycles through ${elements.length} controls inside this element and never leaves`;
|
|
127
|
+
const reach = unreached > 0 ? `, so ${unreached} tabbable control(s) in the page are never reached` : '';
|
|
128
|
+
const shift = shiftTabEscapes
|
|
129
|
+
? ' Shift+Tab does get out, but Tab alone should too.'
|
|
130
|
+
: ' Shift+Tab doesn\'t get out either.';
|
|
131
|
+
const componentPath = resolveComponentPath(owner);
|
|
132
|
+
return {
|
|
133
|
+
id: 'ngbr/focus-trap',
|
|
134
|
+
impact: shiftTabEscapes ? 'moderate' : 'serious',
|
|
135
|
+
help: `Possible keyboard trap: ${where}${reach}.${shift} If this widget deliberately keeps ` +
|
|
136
|
+
`Tab (e.g. a code editor), it must tell users how to leave (such as Escape); otherwise ` +
|
|
137
|
+
`let Tab move focus on. Found by real Tab presses — verify manually.`,
|
|
138
|
+
helpUrl: NO_KEYBOARD_TRAP_URL,
|
|
139
|
+
component: appComponentFromPath(componentPath, frameworkPrefixes),
|
|
140
|
+
componentPath,
|
|
141
|
+
directives: resolveDirectiveNames(owner),
|
|
142
|
+
target: shortSelector(owner),
|
|
143
|
+
html: shortHtml(owner),
|
|
144
|
+
};
|
|
145
|
+
},
|
|
146
|
+
};
|
|
147
|
+
}
|
|
@@ -5,6 +5,10 @@ export interface KeyboardScanOptions {
|
|
|
5
5
|
/** Visibility predicate override (tests); see {@link tabSequence}. */
|
|
6
6
|
isVisible?: (element: Element) => boolean;
|
|
7
7
|
}
|
|
8
|
+
/** A short, human CSS-ish selector for our own findings (not an axe target). */
|
|
9
|
+
export declare function shortSelector(element: Element): string;
|
|
10
|
+
/** Truncated outerHTML for a finding's `html`. */
|
|
11
|
+
export declare function shortHtml(element: Element): string;
|
|
8
12
|
/**
|
|
9
13
|
* Scan `root` for keyboard-operability problems and return them as findings.
|
|
10
14
|
* Runs entirely in the page (needs the live `window.ng` for listeners and
|
|
@@ -39,7 +39,7 @@ const KEYBOARD_URL = `${UNDERSTANDING}/keyboard.html`;
|
|
|
39
39
|
const FOCUS_ORDER_URL = `${UNDERSTANDING}/focus-order.html`;
|
|
40
40
|
const DIALOG_MODAL_URL = 'https://www.w3.org/WAI/ARIA/apg/patterns/dialog-modal/';
|
|
41
41
|
/** A short, human CSS-ish selector for our own findings (not an axe target). */
|
|
42
|
-
function shortSelector(element) {
|
|
42
|
+
export function shortSelector(element) {
|
|
43
43
|
const tag = element.tagName.toLowerCase();
|
|
44
44
|
if (element.id)
|
|
45
45
|
return `${tag}#${element.id}`;
|
|
@@ -50,7 +50,8 @@ function shortSelector(element) {
|
|
|
50
50
|
.slice(0, 2);
|
|
51
51
|
return classes.length ? `${tag}.${classes.join('.')}` : tag;
|
|
52
52
|
}
|
|
53
|
-
|
|
53
|
+
/** Truncated outerHTML for a finding's `html`. */
|
|
54
|
+
export function shortHtml(element) {
|
|
54
55
|
const html = element.outerHTML ?? '';
|
|
55
56
|
return html.length > 160 ? `${html.slice(0, 159)}…` : html;
|
|
56
57
|
}
|
|
@@ -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.
|
|
18
32
|
*/
|
|
19
|
-
|
|
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.
|
|
54
|
+
*/
|
|
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
|
/**
|
|
@@ -33,6 +72,14 @@ export interface ScanPagesOptions {
|
|
|
33
72
|
* Default false.
|
|
34
73
|
*/
|
|
35
74
|
keyboard?: boolean;
|
|
75
|
+
/**
|
|
76
|
+
* Also walk each route with **real** Tab presses to find keyboard traps —
|
|
77
|
+
* focus that cycles inside part of the page and never moves on (WCAG 2.1.2),
|
|
78
|
+
* reported as `ngbr/focus-trap`. Runs after the scan, since pressing Tab can
|
|
79
|
+
* change page state (focus handlers, menus). Focus cycling inside an open
|
|
80
|
+
* modal is containment, not a trap, and isn't reported. Default false.
|
|
81
|
+
*/
|
|
82
|
+
focusTraps?: boolean;
|
|
36
83
|
}
|
|
37
84
|
/**
|
|
38
85
|
* Drive a real headless browser over a running Angular **dev** build and return
|