@su-sws/synthetic-web-team 0.1.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/AGENTS.md +149 -0
- package/README.md +321 -0
- package/package.json +58 -0
- package/packages/cli/bin/sws.mjs +722 -0
- package/packages/cli/src/axe.mjs +164 -0
- package/packages/cli/src/browser.mjs +192 -0
- package/packages/cli/src/checks.mjs +1254 -0
- package/packages/cli/src/github.mjs +131 -0
- package/packages/cli/src/history.mjs +137 -0
- package/packages/cli/src/perf.mjs +245 -0
- package/packages/cli/src/preflight.mjs +185 -0
- package/packages/cli/src/report.mjs +315 -0
- package/packages/cli/src/states.mjs +556 -0
- package/packages/wizard/bin/wizard.mjs +767 -0
- package/packages/wizard/src/detect.mjs +135 -0
- package/packages/wizard/src/emit.mjs +553 -0
- package/skills/role-accessibility-lead/SKILL.md +183 -0
- package/skills/role-api-architect/SKILL.md +65 -0
- package/skills/role-backend-developer/SKILL.md +66 -0
- package/skills/role-compliance-officer/SKILL.md +73 -0
- package/skills/role-content-designer/SKILL.md +145 -0
- package/skills/role-devops/SKILL.md +200 -0
- package/skills/role-discoverability/SKILL.md +164 -0
- package/skills/role-frontend-developer/SKILL.md +167 -0
- package/skills/role-fullstack-developer/SKILL.md +56 -0
- package/skills/role-identity-engineer/SKILL.md +77 -0
- package/skills/role-information-architect/SKILL.md +136 -0
- package/skills/role-infrastructure-architect/SKILL.md +60 -0
- package/skills/role-qa-engineer/SKILL.md +81 -0
- package/skills/role-security-operations/SKILL.md +56 -0
- package/skills/role-software-architect/SKILL.md +75 -0
- package/skills/role-staff-technical-architect/SKILL.md +73 -0
- package/skills/role-strategist/SKILL.md +122 -0
- package/skills/role-user-researcher/SKILL.md +66 -0
- package/skills/role-ux-designer/SKILL.md +159 -0
- package/skills/sws-compliance-check/SKILL.md +153 -0
- package/skills/sws-decanter/SKILL.md +191 -0
- package/skills/sws-deploy/SKILL.md +162 -0
- package/skills/sws-diverge/SKILL.md +131 -0
- package/skills/sws-github/SKILL.md +171 -0
- package/skills/sws-install/SKILL.md +97 -0
- package/skills/sws-netlify/SKILL.md +110 -0
- package/skills/sws-onboard/SKILL.md +85 -0
- package/skills/sws-preflight/SKILL.md +87 -0
- package/skills/sws-prior-art/SKILL.md +137 -0
- package/skills/sws-vercel/SKILL.md +117 -0
- package/standards/fragments/global-footer.yml +137 -0
- package/standards/fragments/identity-bar.yml +180 -0
- package/standards/hosting/capabilities.yml +215 -0
- package/standards/hosting/github-pages.yml +306 -0
- package/standards/hosting/netlify.yml +234 -0
- package/standards/hosting/vercel.yml +284 -0
- package/standards/patterns/components.md +162 -0
- package/standards/patterns/content.md +130 -0
- package/standards/patterns/decanter.md +352 -0
- package/standards/patterns/discoverability.md +121 -0
- package/standards/patterns/forms.md +116 -0
- package/standards/patterns/ia.md +126 -0
- package/standards/patterns/motion.md +267 -0
- package/standards/patterns/sws-conventions.md +225 -0
- package/standards/policy/accessibility.md +241 -0
- package/standards/policy/brand.md +213 -0
- package/standards/policy/escalation.md +53 -0
- package/standards/policy/identity.md +96 -0
- package/standards/policy/minsec.md +119 -0
- package/standards/policy/minweb.md +87 -0
- package/standards/policy/privacy.md +114 -0
- package/standards/policy/procurement.md +79 -0
- package/standards/prior-art/README.md +205 -0
- package/standards/prior-art/catalog.yml +409 -0
- package/standards/prior-art/patterns/INDEX.md +53 -0
- package/standards/prior-art/repos.yml +734 -0
- package/standards/recipes/astro-ssr/RECIPE.md +274 -0
- package/standards/recipes/astro-ssr/acceptance.yml +168 -0
- package/standards/recipes/astro-static/RECIPE.md +468 -0
- package/standards/recipes/astro-static/acceptance.yml +800 -0
- package/standards/recipes/next-ssr/RECIPE.md +407 -0
- package/standards/recipes/next-ssr/acceptance.yml +187 -0
- package/standards/scope.md +111 -0
- package/standards/stack/performance-budget.yml +95 -0
- package/standards/stack/reference-versions.yml +168 -0
- package/standards/stack/requirements.yml +154 -0
|
@@ -0,0 +1,556 @@
|
|
|
1
|
+
// Interactive-state runner: does hover and focus feedback survive without
|
|
2
|
+
// colour vision?
|
|
3
|
+
//
|
|
4
|
+
// WHY THIS EXISTS AS A SEPARATE MEASUREMENT. axe-core cannot answer this. It
|
|
5
|
+
// audits one static snapshot of the DOM, and a hover or focus state does not
|
|
6
|
+
// exist in a snapshot -- it exists only while a pointer is over an element or
|
|
7
|
+
// the keyboard has landed on it. So `hover:text-poppy-light` with nothing else
|
|
8
|
+
// in the hover rule is invisible to axe and reads as a clean page, which is
|
|
9
|
+
// exactly the false negative this project treats as worse than a false
|
|
10
|
+
// positive. It was found on this project's own site by a person, not by a tool.
|
|
11
|
+
//
|
|
12
|
+
// WHAT IT CHECKS. For every interactive control, compare the computed style at
|
|
13
|
+
// rest against the same style under `:hover` and under keyboard `:focus`. If
|
|
14
|
+
// the only thing that changed is a colour, that is a finding: WCAG 2.1 SC 1.4.1
|
|
15
|
+
// via technique G183, which requires a non-colour cue on both hover and focus
|
|
16
|
+
// wherever colour distinguishes a control. If nothing changed under focus at
|
|
17
|
+
// all, that is SC 2.4.7. The usual fix is one class: add an underline on hover
|
|
18
|
+
// and focus, or remove one that is there at rest.
|
|
19
|
+
//
|
|
20
|
+
// HOW IT MEASURES. Real interaction, not simulation. `page.hover()` moves an
|
|
21
|
+
// actual mouse, and focus comes from actually pressing Tab, because Chromium
|
|
22
|
+
// only matches `:focus-visible` when focus arrived by keyboard -- calling
|
|
23
|
+
// `el.focus()` from script silently reads the wrong state and would report
|
|
24
|
+
// `focus-visible:` styles as missing. Tab traversal has a second benefit: it
|
|
25
|
+
// measures precisely the set of controls a keyboard user can reach.
|
|
26
|
+
//
|
|
27
|
+
// Same honesty rule as axe and perf: anything that could not be measured
|
|
28
|
+
// writes a status other than `ok`, and the reader in checks.mjs turns that into
|
|
29
|
+
// `unknown`. There is no path here that produces a clean result without the
|
|
30
|
+
// browser having actually reported one.
|
|
31
|
+
|
|
32
|
+
import { existsSync } from 'node:fs';
|
|
33
|
+
import { join, relative } from 'node:path';
|
|
34
|
+
import {
|
|
35
|
+
serve, launchChromium, settleAnimations, revealAll, routeFor, newestMtime, writeResults,
|
|
36
|
+
} from './browser.mjs';
|
|
37
|
+
|
|
38
|
+
export const RESULTS_PATH = join('.sws', 'state-results.json');
|
|
39
|
+
export const SCHEMA = 1;
|
|
40
|
+
|
|
41
|
+
// Everything focusable, plus the things authors make clickable without making
|
|
42
|
+
// them focusable. The unfocusable ones still get a hover verdict, and their
|
|
43
|
+
// absence from the Tab order is a different (and worse) finding that the
|
|
44
|
+
// keyboard section of the manual checklist owns.
|
|
45
|
+
export const CONTROL_SELECTOR = [
|
|
46
|
+
'a[href]', 'button', 'summary', 'input', 'select', 'textarea',
|
|
47
|
+
'[role="button"]', '[role="link"]', '[role="tab"]', '[role="menuitem"]',
|
|
48
|
+
'[tabindex]:not([tabindex="-1"])', '[contenteditable=""]', '[contenteditable="true"]',
|
|
49
|
+
].join(',');
|
|
50
|
+
|
|
51
|
+
// Distinct class-and-tag shapes to measure per route. In a utility-CSS project
|
|
52
|
+
// the class list IS the style, so one measurement per shape covers every
|
|
53
|
+
// instance of a component; the count of instances is reported alongside. The
|
|
54
|
+
// cap keeps a 200-link A-Z index from turning one route into a minute of mouse
|
|
55
|
+
// moves.
|
|
56
|
+
const MAX_SHAPES = 40;
|
|
57
|
+
|
|
58
|
+
// Hard ceiling on Tab presses. Generous enough for a long page, finite so a
|
|
59
|
+
// focus trap ends the loop instead of the run.
|
|
60
|
+
const MAX_TAB = 400;
|
|
61
|
+
|
|
62
|
+
// Longest wait for a hover or focus transition to finish. Resolves as soon as
|
|
63
|
+
// the transition does -- CSS transitions appear in `document.getAnimations()`
|
|
64
|
+
// -- so on the common case of no transition this costs nothing. It exists
|
|
65
|
+
// because a 200ms `transition: color` read at 0ms reports the colour it started
|
|
66
|
+
// from, which would make a real colour-only change look like no change at all.
|
|
67
|
+
const STATE_SETTLE_MS = 500;
|
|
68
|
+
|
|
69
|
+
// --------------------------------------------------------------------------
|
|
70
|
+
// property classification, which is the whole judgment of this check
|
|
71
|
+
// --------------------------------------------------------------------------
|
|
72
|
+
//
|
|
73
|
+
// A property is COLOUR if changing it alone leaves the control the same shape,
|
|
74
|
+
// weight, and decoration -- which is precisely the change someone with a colour
|
|
75
|
+
// vision deficiency, a monochrome display, or a high-contrast mode may not
|
|
76
|
+
// perceive. `filter` is here because the overwhelming use on a hover state is
|
|
77
|
+
// `brightness()` or `saturate()`, both of which are colour.
|
|
78
|
+
const COLOUR_PROPS = new Set([
|
|
79
|
+
'color', 'background-color', 'border-top-color', 'border-right-color',
|
|
80
|
+
'border-bottom-color', 'border-left-color', 'outline-color',
|
|
81
|
+
'text-decoration-color', 'fill', 'stroke', 'filter', '-webkit-text-fill-color',
|
|
82
|
+
]);
|
|
83
|
+
|
|
84
|
+
// Properties whose value carries a colour inside a larger structure. Compared
|
|
85
|
+
// with colours stripped out, so `box-shadow: 0 0 0 2px red` -> `... blue` reads
|
|
86
|
+
// as a colour change, while `none` -> `0 0 0 2px red` reads as a ring appearing.
|
|
87
|
+
const COLOUR_BEARING = new Set(['box-shadow', 'text-shadow', 'background-image']);
|
|
88
|
+
|
|
89
|
+
// Everything else in the watch list below is a non-colour cue: a shape, a
|
|
90
|
+
// weight, a decoration, a position, or something appearing.
|
|
91
|
+
const WATCH = [
|
|
92
|
+
...COLOUR_PROPS, ...COLOUR_BEARING,
|
|
93
|
+
'text-decoration-line', 'text-decoration-style', 'text-decoration-thickness',
|
|
94
|
+
'text-underline-offset', 'outline-style', 'outline-width', 'outline-offset',
|
|
95
|
+
'border-top-width', 'border-right-width', 'border-bottom-width', 'border-left-width',
|
|
96
|
+
'border-top-style', 'border-right-style', 'border-bottom-style', 'border-left-style',
|
|
97
|
+
'border-top-left-radius', 'border-top-right-radius',
|
|
98
|
+
'border-bottom-right-radius', 'border-bottom-left-radius',
|
|
99
|
+
'font-weight', 'font-style', 'letter-spacing', 'text-transform', 'font-variant-caps',
|
|
100
|
+
'transform', 'scale', 'rotate', 'translate',
|
|
101
|
+
'padding-top', 'padding-right', 'padding-bottom', 'padding-left',
|
|
102
|
+
'visibility', 'display', 'position', 'clip-path', 'content', 'opacity',
|
|
103
|
+
];
|
|
104
|
+
|
|
105
|
+
const stripColours = (v) => String(v)
|
|
106
|
+
.replace(/\b(?:rgba?|hsla?|hwb|lab|lch|oklab|oklch|color|color-mix)\([^()]*(?:\([^()]*\)[^()]*)*\)/g, 'C')
|
|
107
|
+
.replace(/#[0-9a-f]{3,8}\b/gi, 'C');
|
|
108
|
+
|
|
109
|
+
const num = (v) => parseFloat(v);
|
|
110
|
+
|
|
111
|
+
/**
|
|
112
|
+
* Classify one changed property as a colour change or a non-colour cue.
|
|
113
|
+
* Returns 'colour' | 'cue' | null (null means "not a real change").
|
|
114
|
+
*
|
|
115
|
+
* The two special cases are the ones that decide real sites:
|
|
116
|
+
*
|
|
117
|
+
* opacity 0 -> 1 is something APPEARING, which is a cue: it is how a
|
|
118
|
+
* pseudo-element underline bar is usually built. 1 -> 0.7 is
|
|
119
|
+
* dimming, which is colour. Treating all opacity as a cue would
|
|
120
|
+
* pass every "fade the text a bit" hover; treating none of it as a
|
|
121
|
+
* cue would fail the standard underline-bar pattern.
|
|
122
|
+
* width/height on a PSEUDO-ELEMENT is the same bar built the other way,
|
|
123
|
+
* `width: 0` -> `width: 100%`. On the element itself it is
|
|
124
|
+
* excluded, because an unrelated scrollbar appearing during the
|
|
125
|
+
* measurement changes widths and would fake a cue -- a false pass,
|
|
126
|
+
* which is the failure mode that matters here.
|
|
127
|
+
*/
|
|
128
|
+
function classify(prop, from, to, { pseudo }) {
|
|
129
|
+
if (from === to) return null;
|
|
130
|
+
|
|
131
|
+
if (prop === 'opacity') {
|
|
132
|
+
const a = num(from), b = num(to);
|
|
133
|
+
if (!Number.isFinite(a) || !Number.isFinite(b)) return 'colour';
|
|
134
|
+
// Appearing or disappearing entirely.
|
|
135
|
+
if ((a === 0) !== (b === 0)) return 'cue';
|
|
136
|
+
return 'colour';
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
if (prop === 'width' || prop === 'height') return pseudo ? 'cue' : null;
|
|
140
|
+
|
|
141
|
+
if (COLOUR_BEARING.has(prop)) {
|
|
142
|
+
return stripColours(from) === stripColours(to) ? 'colour' : 'cue';
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
return COLOUR_PROPS.has(prop) ? 'colour' : 'cue';
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
/**
|
|
149
|
+
* Diff two fingerprints. Keys are `scope|property`, where scope is `self`,
|
|
150
|
+
* `::before`, `::after`, `kidN`, or `kidN::after` -- so the pseudo-element rule
|
|
151
|
+
* above can be applied from the key alone.
|
|
152
|
+
*/
|
|
153
|
+
export function diffState(rest, state) {
|
|
154
|
+
const colours = [];
|
|
155
|
+
const cues = [];
|
|
156
|
+
for (const key of Object.keys(state)) {
|
|
157
|
+
if (!(key in rest)) continue;
|
|
158
|
+
const cut = key.indexOf('|');
|
|
159
|
+
const scope = key.slice(0, cut);
|
|
160
|
+
const prop = key.slice(cut + 1);
|
|
161
|
+
const verdict = classify(prop, rest[key], state[key], { pseudo: scope.includes('::') });
|
|
162
|
+
if (verdict === 'cue') cues.push(`${scope} ${prop}`);
|
|
163
|
+
else if (verdict === 'colour') colours.push(`${scope} ${prop}`);
|
|
164
|
+
}
|
|
165
|
+
return {
|
|
166
|
+
changed: cues.length > 0 || colours.length > 0,
|
|
167
|
+
// A cue anywhere in the control's own subtree is enough. `group-hover:` and
|
|
168
|
+
// `hover:*:` put the underline on a descendant, and a reader does not care
|
|
169
|
+
// which element carries it.
|
|
170
|
+
cues, colours,
|
|
171
|
+
verdict: cues.length ? 'cue' : (colours.length ? 'colour-only' : 'no-change'),
|
|
172
|
+
};
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
// --------------------------------------------------------------------------
|
|
176
|
+
// in-page helpers
|
|
177
|
+
// --------------------------------------------------------------------------
|
|
178
|
+
//
|
|
179
|
+
// Installed once per page, then called by index. Written as one string-passed
|
|
180
|
+
// function because `page.evaluate` serialises the callback and closures do not
|
|
181
|
+
// travel with it.
|
|
182
|
+
|
|
183
|
+
function installProbe(cfg) {
|
|
184
|
+
const { watch, selector, maxShapes, pseudoExtra } = cfg;
|
|
185
|
+
|
|
186
|
+
const styleOf = (el, pseudo) => {
|
|
187
|
+
const cs = getComputedStyle(el, pseudo || undefined);
|
|
188
|
+
const out = {};
|
|
189
|
+
const props = pseudo ? [...watch, ...pseudoExtra] : watch;
|
|
190
|
+
// `|` and not `:` -- a `::before` scope contains colons, and splitting on
|
|
191
|
+
// the first one yielded an empty scope and a property name of
|
|
192
|
+
// ":before:color", which matched nothing in the colour list and therefore
|
|
193
|
+
// scored every pseudo-element colour change as a non-colour CUE. That is a
|
|
194
|
+
// false pass, the exact failure mode this check exists to prevent, and it
|
|
195
|
+
// hid three real findings on this project's own site until the inventory
|
|
196
|
+
// printed the cue it thought it had found.
|
|
197
|
+
for (const p of props) out[`${pseudo || 'self'}|${p}`] = cs.getPropertyValue(p);
|
|
198
|
+
return out;
|
|
199
|
+
};
|
|
200
|
+
|
|
201
|
+
// The control, its two pseudo-elements, and a bounded number of descendants.
|
|
202
|
+
// Descendants matter because the underline is frequently on an inner span,
|
|
203
|
+
// and the pseudo-elements matter because it is frequently on `::after`.
|
|
204
|
+
const fingerprint = (el) => {
|
|
205
|
+
const out = { ...styleOf(el, null), ...styleOf(el, '::before'), ...styleOf(el, '::after') };
|
|
206
|
+
const kids = [...el.querySelectorAll('*')].slice(0, 8);
|
|
207
|
+
kids.forEach((kid, i) => {
|
|
208
|
+
const cs = getComputedStyle(kid);
|
|
209
|
+
for (const p of watch) out[`kid${i}|${p}`] = cs.getPropertyValue(p);
|
|
210
|
+
for (const pseudo of ['::before', '::after']) {
|
|
211
|
+
const ps = getComputedStyle(kid, pseudo);
|
|
212
|
+
for (const p of [...watch, ...pseudoExtra]) out[`kid${i}${pseudo}|${p}`] = ps.getPropertyValue(p);
|
|
213
|
+
}
|
|
214
|
+
});
|
|
215
|
+
return out;
|
|
216
|
+
};
|
|
217
|
+
|
|
218
|
+
// Short, stable, human-readable path. Long enough to find the element in a
|
|
219
|
+
// template, short enough to print in a terminal report.
|
|
220
|
+
const path = (el) => {
|
|
221
|
+
const parts = [];
|
|
222
|
+
for (let n = el; n && n.nodeType === 1 && parts.length < 4; n = n.parentElement) {
|
|
223
|
+
if (n.id) { parts.unshift(`#${n.id}`); break; }
|
|
224
|
+
const tag = n.tagName.toLowerCase();
|
|
225
|
+
const sibs = n.parentElement ? [...n.parentElement.children].filter((c) => c.tagName === n.tagName) : [];
|
|
226
|
+
parts.unshift(sibs.length > 1 ? `${tag}:nth-of-type(${sibs.indexOf(n) + 1})` : tag);
|
|
227
|
+
}
|
|
228
|
+
return parts.join(' > ');
|
|
229
|
+
};
|
|
230
|
+
|
|
231
|
+
const visible = (el) => {
|
|
232
|
+
const cs = getComputedStyle(el);
|
|
233
|
+
if (cs.display === 'none' || cs.visibility === 'hidden' || Number(cs.opacity) === 0) return false;
|
|
234
|
+
const r = el.getBoundingClientRect();
|
|
235
|
+
if (r.width < 1 || r.height < 1) return false;
|
|
236
|
+
return !el.closest('[aria-hidden="true"]');
|
|
237
|
+
};
|
|
238
|
+
|
|
239
|
+
const kindOf = (el) => {
|
|
240
|
+
const tag = el.tagName.toLowerCase();
|
|
241
|
+
const role = (el.getAttribute('role') || '').toLowerCase();
|
|
242
|
+
if (tag === 'a' || role === 'link') return 'link';
|
|
243
|
+
if (tag === 'button' || role === 'button' || (tag === 'input' && /^(submit|button|reset|image)$/i.test(el.type))) return 'button';
|
|
244
|
+
if (tag === 'input' || tag === 'select' || tag === 'textarea') return 'field';
|
|
245
|
+
if (tag === 'summary') return 'disclosure';
|
|
246
|
+
return role || tag;
|
|
247
|
+
};
|
|
248
|
+
|
|
249
|
+
// The state-variant classes on the element, e.g. `hover:text-poppy-light`.
|
|
250
|
+
// In a utility-CSS project this is usually the entire fix, so put it in the
|
|
251
|
+
// report rather than making someone open the template to find it.
|
|
252
|
+
const stateClasses = (el) => (el.getAttribute('class') || '').split(/\s+/)
|
|
253
|
+
.filter((c) => /^(?:[a-z0-9-]+:)*(?:hover|focus|focus-visible|hocus|active|group-hover|group-focus)[:-]/.test(c))
|
|
254
|
+
.slice(0, 6);
|
|
255
|
+
|
|
256
|
+
const controls = [];
|
|
257
|
+
const shapes = new Map();
|
|
258
|
+
let skipped = 0;
|
|
259
|
+
|
|
260
|
+
for (const el of document.querySelectorAll(selector)) {
|
|
261
|
+
if (el.hasAttribute('disabled') || el.getAttribute('aria-disabled') === 'true') continue;
|
|
262
|
+
if (!visible(el)) continue;
|
|
263
|
+
|
|
264
|
+
const cls = (el.getAttribute('class') || '').trim().replace(/\s+/g, ' ');
|
|
265
|
+
const shape = `${el.tagName.toLowerCase()}|${el.getAttribute('role') || ''}|${cls}`;
|
|
266
|
+
|
|
267
|
+
if (shapes.has(shape)) { shapes.get(shape).occurrences += 1; continue; }
|
|
268
|
+
if (shapes.size >= maxShapes) { skipped += 1; continue; }
|
|
269
|
+
|
|
270
|
+
const idx = controls.length;
|
|
271
|
+
el.setAttribute('data-sws-state', String(idx));
|
|
272
|
+
const label = (el.getAttribute('aria-label') || el.textContent || '').replace(/\s+/g, ' ').trim();
|
|
273
|
+
const entry = {
|
|
274
|
+
idx,
|
|
275
|
+
shape,
|
|
276
|
+
target: path(el),
|
|
277
|
+
label: label.slice(0, 48),
|
|
278
|
+
kind: kindOf(el),
|
|
279
|
+
// No text means the fix is not an underline: an icon or image control
|
|
280
|
+
// needs a border, ring, or shape change instead. Worth carrying into the
|
|
281
|
+
// report so the advice is not wrong for a logo link.
|
|
282
|
+
graphic: !label,
|
|
283
|
+
classes: stateClasses(el),
|
|
284
|
+
occurrences: 1,
|
|
285
|
+
};
|
|
286
|
+
shapes.set(shape, entry);
|
|
287
|
+
controls.push(entry);
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
window.__swsStates = {
|
|
291
|
+
controls,
|
|
292
|
+
skipped,
|
|
293
|
+
at: (idx) => document.querySelector(`[data-sws-state="${idx}"]`),
|
|
294
|
+
fingerprint: (idx) => {
|
|
295
|
+
const el = window.__swsStates.at(idx);
|
|
296
|
+
return el ? fingerprint(el) : null;
|
|
297
|
+
},
|
|
298
|
+
// Read back which pseudo-classes actually applied at read time. This is the
|
|
299
|
+
// guard that keeps the check honest: a "no cue" verdict is only reported
|
|
300
|
+
// when the browser confirms the state was really active, and a control that
|
|
301
|
+
// is hovered while being measured for focus is reported as inconclusive
|
|
302
|
+
// rather than as a finding.
|
|
303
|
+
stateOf: (idx) => {
|
|
304
|
+
const el = window.__swsStates.at(idx);
|
|
305
|
+
if (!el) return null;
|
|
306
|
+
const m = (s) => { try { return el.matches(s); } catch { return false; } };
|
|
307
|
+
return { hover: m(':hover'), focus: m(':focus'), focusVisible: m(':focus-visible') };
|
|
308
|
+
},
|
|
309
|
+
activeIdx: () => {
|
|
310
|
+
const a = document.activeElement;
|
|
311
|
+
if (!a || a === document.body || a === document.documentElement) return null;
|
|
312
|
+
const own = a.getAttribute('data-sws-state');
|
|
313
|
+
if (own !== null) return Number(own);
|
|
314
|
+
// Focus landed inside a control we tagged (a link wrapping a span, say).
|
|
315
|
+
const owner = a.closest('[data-sws-state]');
|
|
316
|
+
return owner ? Number(owner.getAttribute('data-sws-state')) : -1;
|
|
317
|
+
},
|
|
318
|
+
blur: () => document.activeElement && document.activeElement.blur && document.activeElement.blur(),
|
|
319
|
+
};
|
|
320
|
+
|
|
321
|
+
return { controls, skipped };
|
|
322
|
+
}
|
|
323
|
+
|
|
324
|
+
// --------------------------------------------------------------------------
|
|
325
|
+
// one route
|
|
326
|
+
// --------------------------------------------------------------------------
|
|
327
|
+
|
|
328
|
+
async function measureRoute({ page, origin, route }) {
|
|
329
|
+
const resp = await page.goto(`${origin}${route}`, { waitUntil: 'load', timeout: 30_000 });
|
|
330
|
+
if (!resp || !resp.ok()) throw new Error(`HTTP ${resp ? resp.status() : 'no response'}`);
|
|
331
|
+
|
|
332
|
+
// Same two steps, same order, and the same reasons as the axe runner: entry
|
|
333
|
+
// motion holds below-fold content at `opacity: 0` until it is scrolled into
|
|
334
|
+
// view, and an unsettled animation is read mid-fade. An `opacity: 0` control
|
|
335
|
+
// is skipped as invisible, so without the scroll this check would silently
|
|
336
|
+
// measure only what is above the fold.
|
|
337
|
+
await revealAll(page);
|
|
338
|
+
await settleAnimations(page);
|
|
339
|
+
|
|
340
|
+
const { controls, skipped } = await page.evaluate(installProbe, {
|
|
341
|
+
watch: WATCH, selector: CONTROL_SELECTOR, maxShapes: MAX_SHAPES,
|
|
342
|
+
pseudoExtra: ['width', 'height'],
|
|
343
|
+
});
|
|
344
|
+
|
|
345
|
+
const rests = new Map();
|
|
346
|
+
for (const c of controls) {
|
|
347
|
+
const rest = await page.evaluate((i) => window.__swsStates.fingerprint(i), c.idx);
|
|
348
|
+
if (rest) rests.set(c.idx, rest);
|
|
349
|
+
}
|
|
350
|
+
|
|
351
|
+
// ---- focus, by actually pressing Tab ------------------------------------
|
|
352
|
+
//
|
|
353
|
+
// Before the mouse has moved, so nothing is hovered and the two states
|
|
354
|
+
// cannot be confused. Chromium only matches `:focus-visible` for
|
|
355
|
+
// keyboard-driven focus, which is why this is a Tab loop and not a series of
|
|
356
|
+
// `el.focus()` calls.
|
|
357
|
+
const focus = new Map();
|
|
358
|
+
let reached = 0;
|
|
359
|
+
await page.evaluate(() => { window.__swsStates.blur(); window.scrollTo(0, 0); });
|
|
360
|
+
let first = null;
|
|
361
|
+
for (let press = 0; press < MAX_TAB; press++) {
|
|
362
|
+
await page.keyboard.press('Tab');
|
|
363
|
+
const idx = await page.evaluate(() => window.__swsStates.activeIdx());
|
|
364
|
+
if (idx === null) break; // focus left the document: end of the order
|
|
365
|
+
if (idx === -1) continue; // focusable, but not a control we track
|
|
366
|
+
reached += 1;
|
|
367
|
+
first ??= idx;
|
|
368
|
+
// Landing on the first control again means the Tab order wrapped.
|
|
369
|
+
if (focus.has(idx)) { if (idx === first && press > 0) break; continue; }
|
|
370
|
+
await settleAnimations(page, STATE_SETTLE_MS);
|
|
371
|
+
const [fp, st] = await Promise.all([
|
|
372
|
+
page.evaluate((i) => window.__swsStates.fingerprint(i), idx),
|
|
373
|
+
page.evaluate((i) => window.__swsStates.stateOf(i), idx),
|
|
374
|
+
]);
|
|
375
|
+
focus.set(idx, { fp, st });
|
|
376
|
+
}
|
|
377
|
+
|
|
378
|
+
// ---- hover, by actually moving the mouse -------------------------------
|
|
379
|
+
const hover = new Map();
|
|
380
|
+
await page.evaluate(() => window.__swsStates.blur());
|
|
381
|
+
for (const c of controls) {
|
|
382
|
+
try {
|
|
383
|
+
await page.hover(`[data-sws-state="${c.idx}"]`, { timeout: 2000 });
|
|
384
|
+
} catch (err) {
|
|
385
|
+
// Covered by a sticky header, moving under the pointer, or not reachable
|
|
386
|
+
// by a mouse at all -- the visually hidden skip link is the standard
|
|
387
|
+
// example, and it is the one control on this project's own site that
|
|
388
|
+
// lands here. Not a finding either way: it is unmeasured, and it says so.
|
|
389
|
+
hover.set(c.idx, { fp: null, st: null, why: err.message.split('\n')[0].slice(0, 120) });
|
|
390
|
+
continue;
|
|
391
|
+
}
|
|
392
|
+
await settleAnimations(page, STATE_SETTLE_MS);
|
|
393
|
+
const [fp, st] = await Promise.all([
|
|
394
|
+
page.evaluate((i) => window.__swsStates.fingerprint(i), c.idx),
|
|
395
|
+
page.evaluate((i) => window.__swsStates.stateOf(i), c.idx),
|
|
396
|
+
]);
|
|
397
|
+
hover.set(c.idx, { fp, st });
|
|
398
|
+
}
|
|
399
|
+
|
|
400
|
+
// ---- verdicts -----------------------------------------------------------
|
|
401
|
+
const findings = [];
|
|
402
|
+
const counts = {
|
|
403
|
+
controls: controls.length,
|
|
404
|
+
instances: controls.reduce((a, c) => a + c.occurrences, 0),
|
|
405
|
+
focusReached: focus.size,
|
|
406
|
+
hoverColourOnly: 0,
|
|
407
|
+
focusColourOnly: 0,
|
|
408
|
+
focusNoChange: 0,
|
|
409
|
+
inconclusive: 0,
|
|
410
|
+
};
|
|
411
|
+
|
|
412
|
+
// Every measured shape, with its verdict, whether or not it is a finding.
|
|
413
|
+
// The findings list alone is not auditable: "one hover problem" gives no way
|
|
414
|
+
// to tell a clean control from one the mouse never reached, and that
|
|
415
|
+
// distinction is the whole difference between a pass and an unknown.
|
|
416
|
+
const inventory = [];
|
|
417
|
+
|
|
418
|
+
const record = (c, state, verdict, d) => {
|
|
419
|
+
findings.push({
|
|
420
|
+
state,
|
|
421
|
+
verdict,
|
|
422
|
+
target: c.target,
|
|
423
|
+
label: c.label,
|
|
424
|
+
kind: c.kind,
|
|
425
|
+
graphic: c.graphic,
|
|
426
|
+
// Only the classes for the state being reported. A hover finding that
|
|
427
|
+
// lists four `focus-visible:` utilities alongside the one hover utility
|
|
428
|
+
// buries the fix in the noise, and the terminal line was 300 characters.
|
|
429
|
+
classes: c.classes.filter((cls) => (state === 'hover'
|
|
430
|
+
? /(?:^|:)(?:hover|hocus|group-hover)[:-]/.test(cls)
|
|
431
|
+
: /(?:^|:)(?:focus|focus-visible|hocus|group-focus)[:-]/.test(cls))).slice(0, 3),
|
|
432
|
+
occurrences: c.occurrences,
|
|
433
|
+
changed: verdict === 'colour-only' ? d.colours.slice(0, 4) : [],
|
|
434
|
+
});
|
|
435
|
+
};
|
|
436
|
+
|
|
437
|
+
for (const c of controls) {
|
|
438
|
+
const entry = {
|
|
439
|
+
target: c.target, label: c.label, kind: c.kind, occurrences: c.occurrences,
|
|
440
|
+
classes: c.classes, hover: 'unmeasured', focus: 'unreached',
|
|
441
|
+
};
|
|
442
|
+
inventory.push(entry);
|
|
443
|
+
|
|
444
|
+
const rest = rests.get(c.idx);
|
|
445
|
+
if (!rest) { counts.inconclusive += 1; continue; }
|
|
446
|
+
|
|
447
|
+
const h = hover.get(c.idx);
|
|
448
|
+
if (h?.fp && h.st?.hover && !h.st.focusVisible) {
|
|
449
|
+
const d = diffState(rest, h.fp);
|
|
450
|
+
// No hover state at all is not a finding here. Hover feedback is not
|
|
451
|
+
// required by WCAG; what is required is that when it exists, the cue is
|
|
452
|
+
// not colour alone -- and for a control identified by colour, that a cue
|
|
453
|
+
// exists on hover AND focus. The focus half is checked below, and it is
|
|
454
|
+
// the half that is actually mandatory.
|
|
455
|
+
entry.hover = d.verdict;
|
|
456
|
+
// Which cue was found, capped. Without this the inventory says "cue" and
|
|
457
|
+
// gives a reviewer no way to tell a real underline from an artefact of
|
|
458
|
+
// the measurement, which is the one thing that would let this check fool
|
|
459
|
+
// itself into a false pass.
|
|
460
|
+
entry.hoverCue = d.cues.slice(0, 3);
|
|
461
|
+
if (d.verdict === 'colour-only') { counts.hoverColourOnly += 1; record(c, 'hover', 'colour-only', d); }
|
|
462
|
+
} else {
|
|
463
|
+
// The mouse could not be put over it, or `:hover` did not apply when it
|
|
464
|
+
// was read. Unmeasured, not clean.
|
|
465
|
+
counts.inconclusive += 1;
|
|
466
|
+
entry.hover = 'unmeasured';
|
|
467
|
+
if (h?.why) entry.hoverWhy = h.why;
|
|
468
|
+
}
|
|
469
|
+
|
|
470
|
+
const f = focus.get(c.idx);
|
|
471
|
+
if (f?.fp && f.st?.focus) {
|
|
472
|
+
const d = diffState(rest, f.fp);
|
|
473
|
+
entry.focus = d.verdict;
|
|
474
|
+
entry.focusCue = d.cues.slice(0, 3);
|
|
475
|
+
entry.focusVisible = f.st.focusVisible === true;
|
|
476
|
+
if (d.verdict === 'colour-only') { counts.focusColourOnly += 1; record(c, 'focus', 'colour-only', d); }
|
|
477
|
+
else if (d.verdict === 'no-change') { counts.focusNoChange += 1; record(c, 'focus', 'no-change', d); }
|
|
478
|
+
}
|
|
479
|
+
}
|
|
480
|
+
|
|
481
|
+
return {
|
|
482
|
+
route, controls: controls.length, shapesSkipped: skipped,
|
|
483
|
+
counts, findings, inventory, focusReached: reached,
|
|
484
|
+
};
|
|
485
|
+
}
|
|
486
|
+
|
|
487
|
+
// --------------------------------------------------------------------------
|
|
488
|
+
// the run
|
|
489
|
+
// --------------------------------------------------------------------------
|
|
490
|
+
|
|
491
|
+
/**
|
|
492
|
+
* Measure hover and focus state on every built route. Always resolves; never
|
|
493
|
+
* throws. `status` is `ok` only when every route was really measured.
|
|
494
|
+
*/
|
|
495
|
+
export async function runStates({ root, dist, html }) {
|
|
496
|
+
const started = new Date().toISOString();
|
|
497
|
+
const bail = (status, detail) => writeResults(root, RESULTS_PATH, {
|
|
498
|
+
schema: SCHEMA, status, detail, generated: started, routes: [],
|
|
499
|
+
});
|
|
500
|
+
|
|
501
|
+
if (!dist || !existsSync(dist)) return bail('no-build', 'no build output found; run the build first');
|
|
502
|
+
if (!html.length) {
|
|
503
|
+
return bail('no-build', `no HTML found in ${relative(root, dist) || dist}; run the build first`);
|
|
504
|
+
}
|
|
505
|
+
|
|
506
|
+
const { browser, playwrightPkg, error } = await launchChromium(root);
|
|
507
|
+
if (error) {
|
|
508
|
+
return bail(/Playwright package/.test(error) ? 'deps-missing' : 'browser-unavailable', error);
|
|
509
|
+
}
|
|
510
|
+
|
|
511
|
+
const srv = await serve(dist);
|
|
512
|
+
const routes = [];
|
|
513
|
+
|
|
514
|
+
try {
|
|
515
|
+
const context = await browser.newContext();
|
|
516
|
+
for (const file of html) {
|
|
517
|
+
const route = routeFor(dist, file);
|
|
518
|
+
const page = await context.newPage();
|
|
519
|
+
try {
|
|
520
|
+
const r = await measureRoute({ page, origin: srv.origin, route });
|
|
521
|
+
routes.push({ ...r, file: relative(root, file), status: 'ok' });
|
|
522
|
+
} catch (err) {
|
|
523
|
+
routes.push({ route, file: relative(root, file), status: 'error', error: err.message });
|
|
524
|
+
} finally {
|
|
525
|
+
await page.close();
|
|
526
|
+
}
|
|
527
|
+
}
|
|
528
|
+
} finally {
|
|
529
|
+
await browser.close().catch(() => {});
|
|
530
|
+
await srv.stop();
|
|
531
|
+
}
|
|
532
|
+
|
|
533
|
+
const failed = routes.filter((r) => r.status !== 'ok');
|
|
534
|
+
const sum = (k) => routes.reduce((a, r) => a + (r.counts?.[k] ?? 0), 0);
|
|
535
|
+
|
|
536
|
+
return writeResults(root, RESULTS_PATH, {
|
|
537
|
+
schema: SCHEMA,
|
|
538
|
+
status: failed.length === 0 ? 'ok' : (failed.length === routes.length ? 'error' : 'partial'),
|
|
539
|
+
detail: failed.length ? `${failed.length} of ${routes.length} route(s) could not be measured` : undefined,
|
|
540
|
+
generated: started,
|
|
541
|
+
finished: new Date().toISOString(),
|
|
542
|
+
distMtime: newestMtime(dist),
|
|
543
|
+
criteria: ['WCAG 2.1 SC 1.4.1 (G183)', 'WCAG 2.1 SC 2.4.7'],
|
|
544
|
+
versions: { playwright: playwrightPkg, browser: browser.version() },
|
|
545
|
+
totals: {
|
|
546
|
+
routes: routes.length,
|
|
547
|
+
controls: sum('controls'),
|
|
548
|
+
instances: sum('instances'),
|
|
549
|
+
hoverColourOnly: sum('hoverColourOnly'),
|
|
550
|
+
focusColourOnly: sum('focusColourOnly'),
|
|
551
|
+
focusNoChange: sum('focusNoChange'),
|
|
552
|
+
inconclusive: sum('inconclusive'),
|
|
553
|
+
},
|
|
554
|
+
routes,
|
|
555
|
+
});
|
|
556
|
+
}
|