@ngbracket/a11y-devtools 0.5.0 → 0.7.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 +46 -0
- package/README.md +59 -4
- package/bin/ngbr-a11y-report.mjs +5 -0
- package/dist/attribution.d.ts +22 -0
- package/dist/attribution.js +26 -0
- package/dist/index.d.ts +5 -2
- package/dist/index.js +4 -1
- package/dist/inpage.global.js +378 -8
- package/dist/keyboard/accname.d.ts +42 -0
- package/dist/keyboard/accname.js +131 -0
- package/dist/keyboard/keyboard-scan.d.ts +14 -0
- package/dist/keyboard/keyboard-scan.js +117 -0
- package/dist/keyboard/tab-sequence.d.ts +58 -0
- package/dist/keyboard/tab-sequence.js +178 -0
- package/dist/overlay.d.ts +22 -0
- package/dist/overlay.js +157 -1
- package/dist/provider.d.ts +8 -0
- package/dist/provider.js +36 -4
- package/dist/report/headless.d.ts +7 -0
- package/dist/report/headless.js +2 -2
- package/dist/runner.d.ts +6 -0
- package/dist/runner.js +1 -0
- package/dist/scan.d.ts +7 -0
- package/dist/scan.js +6 -0
- package/package.json +1 -1
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The keyboard layer, part B: findings axe can't produce, because they depend on
|
|
3
|
+
* runtime state axe never sees — which elements actually have a DOM `click`
|
|
4
|
+
* listener (read from Angular's `window.ng.getListeners`) and whether the tab
|
|
5
|
+
* path matches the visual order. These are emitted as {@link A11yFinding}s with
|
|
6
|
+
* synthetic `ngbr/*` rule ids, so they flow through the same grouped console
|
|
7
|
+
* output, overlay, and report as the axe violations — attributed to the owning
|
|
8
|
+
* component like everything else.
|
|
9
|
+
*
|
|
10
|
+
* Honesty guardrail: these are *heuristics*, not axe rules. `click-without-key`
|
|
11
|
+
* and `tab-order-mismatch` are flagged as candidates to verify by hand, never as
|
|
12
|
+
* confirmed failures.
|
|
13
|
+
*/
|
|
14
|
+
import { appComponentFromPath, DEFAULT_FRAMEWORK_PREFIXES, resolveComponentPath, resolveDirectiveNames, resolveListenerEvents, } from '../attribution.js';
|
|
15
|
+
import { isNativelyFocusable, isTabbable, tabSequence, visualOrderJumps, } from './tab-sequence.js';
|
|
16
|
+
/** WAI-ARIA roles that make a non-native element behave as an interactive control. */
|
|
17
|
+
const INTERACTIVE_ROLES = new Set([
|
|
18
|
+
'button',
|
|
19
|
+
'link',
|
|
20
|
+
'menuitem',
|
|
21
|
+
'menuitemcheckbox',
|
|
22
|
+
'menuitemradio',
|
|
23
|
+
'tab',
|
|
24
|
+
'switch',
|
|
25
|
+
'checkbox',
|
|
26
|
+
'radio',
|
|
27
|
+
'option',
|
|
28
|
+
'slider',
|
|
29
|
+
'spinbutton',
|
|
30
|
+
'combobox',
|
|
31
|
+
'textbox',
|
|
32
|
+
'searchbox',
|
|
33
|
+
'treeitem',
|
|
34
|
+
]);
|
|
35
|
+
const KEY_EVENTS = ['keydown', 'keyup', 'keypress'];
|
|
36
|
+
const UNDERSTANDING = 'https://www.w3.org/WAI/WCAG22/Understanding';
|
|
37
|
+
const KEYBOARD_URL = `${UNDERSTANDING}/keyboard.html`;
|
|
38
|
+
const FOCUS_ORDER_URL = `${UNDERSTANDING}/focus-order.html`;
|
|
39
|
+
/** A short, human CSS-ish selector for our own findings (not an axe target). */
|
|
40
|
+
function shortSelector(element) {
|
|
41
|
+
const tag = element.tagName.toLowerCase();
|
|
42
|
+
if (element.id)
|
|
43
|
+
return `${tag}#${element.id}`;
|
|
44
|
+
const classes = (element.getAttribute('class') ?? '')
|
|
45
|
+
.trim()
|
|
46
|
+
.split(/\s+/)
|
|
47
|
+
.filter(Boolean)
|
|
48
|
+
.slice(0, 2);
|
|
49
|
+
return classes.length ? `${tag}.${classes.join('.')}` : tag;
|
|
50
|
+
}
|
|
51
|
+
function shortHtml(element) {
|
|
52
|
+
const html = element.outerHTML ?? '';
|
|
53
|
+
return html.length > 160 ? `${html.slice(0, 159)}…` : html;
|
|
54
|
+
}
|
|
55
|
+
/**
|
|
56
|
+
* Scan `root` for keyboard-operability problems and return them as findings.
|
|
57
|
+
* Runs entirely in the page (needs the live `window.ng` for listeners and
|
|
58
|
+
* attribution), so it's a no-op — empty result — in production where that global
|
|
59
|
+
* is absent.
|
|
60
|
+
*/
|
|
61
|
+
export function scanKeyboard(root = document, options = {}) {
|
|
62
|
+
const prefixes = options.frameworkPrefixes ?? DEFAULT_FRAMEWORK_PREFIXES;
|
|
63
|
+
const isVisible = options.isVisible;
|
|
64
|
+
const findings = [];
|
|
65
|
+
const make = (element, id, impact, help, helpUrl) => {
|
|
66
|
+
const componentPath = resolveComponentPath(element);
|
|
67
|
+
return {
|
|
68
|
+
id,
|
|
69
|
+
impact,
|
|
70
|
+
help,
|
|
71
|
+
helpUrl,
|
|
72
|
+
component: appComponentFromPath(componentPath, prefixes),
|
|
73
|
+
componentPath,
|
|
74
|
+
directives: resolveDirectiveNames(element),
|
|
75
|
+
target: shortSelector(element),
|
|
76
|
+
html: shortHtml(element),
|
|
77
|
+
};
|
|
78
|
+
};
|
|
79
|
+
for (const element of root.querySelectorAll('*')) {
|
|
80
|
+
// Native controls are keyboard-reachable and activate on Enter/Space on their
|
|
81
|
+
// own, so they can't be the subject of either finding — skip them outright.
|
|
82
|
+
if (isNativelyFocusable(element))
|
|
83
|
+
continue;
|
|
84
|
+
const role = element.getAttribute('role')?.toLowerCase() ?? '';
|
|
85
|
+
const events = resolveListenerEvents(element);
|
|
86
|
+
const hasClick = events.includes('click');
|
|
87
|
+
const interactiveByRole = INTERACTIVE_ROLES.has(role);
|
|
88
|
+
if (!hasClick && !interactiveByRole)
|
|
89
|
+
continue; // nothing marks this as interactive
|
|
90
|
+
const focusable = isTabbable(element, isVisible);
|
|
91
|
+
if (!focusable) {
|
|
92
|
+
const reason = interactiveByRole ? `has role="${role}"` : 'has a click handler';
|
|
93
|
+
findings.push(make(element, 'ngbr/unreachable-control', 'serious', `Interactive element isn't reachable by keyboard — it ${reason} but is not a ` +
|
|
94
|
+
`native control and has no tabindex >= 0, so keyboard and screen-reader users ` +
|
|
95
|
+
`can't focus it. Add tabindex="0" (and a role, if missing), or use a <button>/<a>.`, KEYBOARD_URL));
|
|
96
|
+
continue; // don't also report a weaker click-without-key on the same node
|
|
97
|
+
}
|
|
98
|
+
// Focusable, but a bare (click) never fires on Enter/Space the way a native
|
|
99
|
+
// button does — so a keyboard user can reach it and still not activate it.
|
|
100
|
+
const hasKey = events.some((e) => KEY_EVENTS.includes(e));
|
|
101
|
+
if (hasClick && !hasKey) {
|
|
102
|
+
findings.push(make(element, 'ngbr/click-without-key', 'moderate', `Possible keyboard trap: this element has a (click) handler but no keyboard ` +
|
|
103
|
+
`handler (keydown/keyup) and isn't a native button/link, so Enter/Space may not ` +
|
|
104
|
+
`activate it. Verify by hand, then add a key handler or use a <button>.`, KEYBOARD_URL));
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
// Visual-vs-tab-order mismatch: a heuristic over the resolved sequence. Needs
|
|
108
|
+
// real layout, so it no-ops where getBoundingClientRect returns zeros (jsdom).
|
|
109
|
+
const stops = tabSequence(root, { frameworkPrefixes: prefixes, isVisible });
|
|
110
|
+
for (const jump of visualOrderJumps(stops)) {
|
|
111
|
+
const stop = stops[jump];
|
|
112
|
+
findings.push(make(stop.element, 'ngbr/tab-order-mismatch', 'moderate', `Possible tab-order mismatch: Tab reaches this control (stop ${stop.order}) after ` +
|
|
113
|
+
`one that sits below or to the right of it, so focus jumps against the visual ` +
|
|
114
|
+
`reading order. Heuristic — verify the order makes sense for a sighted keyboard user.`, FOCUS_ORDER_URL));
|
|
115
|
+
}
|
|
116
|
+
return findings;
|
|
117
|
+
}
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
/** One element in the resolved tab sequence, with its owning component. */
|
|
2
|
+
export interface TabStop {
|
|
3
|
+
element: Element;
|
|
4
|
+
/** 1-based position in the resolved tab order. */
|
|
5
|
+
order: number;
|
|
6
|
+
/** Resolved tabindex: 0 for a native default or explicit `tabindex="0"`, else the positive value. */
|
|
7
|
+
tabindex: number;
|
|
8
|
+
/** True when an explicit positive `tabindex` puts this stop out of DOM order. */
|
|
9
|
+
positive: boolean;
|
|
10
|
+
/** Nearest app-owned component (walks past UI primitives); null in prod. */
|
|
11
|
+
component: string | null;
|
|
12
|
+
/** Owning components from the node up to the root, nearest first; [] in prod. */
|
|
13
|
+
componentPath: string[];
|
|
14
|
+
}
|
|
15
|
+
export interface TabSequenceOptions {
|
|
16
|
+
/** UI-primitive prefixes to walk past during attribution; see the scan options. */
|
|
17
|
+
frameworkPrefixes?: readonly string[];
|
|
18
|
+
/**
|
|
19
|
+
* Visibility predicate override — mainly for tests, where jsdom reports no
|
|
20
|
+
* layout. Defaults to a computed-style + `inert`/`hidden`/`<details>` check.
|
|
21
|
+
*/
|
|
22
|
+
isVisible?: (element: Element) => boolean;
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* True when the element takes focus without an explicit `tabindex` — a link with
|
|
26
|
+
* an href, a form control, a `<summary>`, an editing host, and the embedded
|
|
27
|
+
* document elements. Disabled/hidden state is handled separately.
|
|
28
|
+
*/
|
|
29
|
+
export declare function isNativelyFocusable(element: Element): boolean;
|
|
30
|
+
/**
|
|
31
|
+
* The tabindex the browser will use: the parsed attribute when present and
|
|
32
|
+
* numeric, otherwise the native default. A non-numeric `tabindex` (e.g.
|
|
33
|
+
* `tabindex="foo"`) is ignored by browsers, so it falls back to the native value.
|
|
34
|
+
*/
|
|
35
|
+
export declare function resolvedTabIndex(element: Element): number;
|
|
36
|
+
/**
|
|
37
|
+
* True when `element` participates in sequential keyboard navigation: it's
|
|
38
|
+
* focusable, has a resolved `tabindex >= 0`, and is neither disabled nor hidden.
|
|
39
|
+
* `isVisible` overrides only the layout (computed-style) part of the check.
|
|
40
|
+
*/
|
|
41
|
+
export declare function isTabbable(element: Element, isVisible?: (el: Element) => boolean): boolean;
|
|
42
|
+
/**
|
|
43
|
+
* The resolved tab order within `root`: positive-tabindex stops first (ascending,
|
|
44
|
+
* ties in DOM order), then `tabindex=0`/native stops in DOM order — the order a
|
|
45
|
+
* browser actually moves focus. Each stop carries its owning component.
|
|
46
|
+
*/
|
|
47
|
+
export declare function tabSequence(root?: ParentNode, options?: TabSequenceOptions): TabStop[];
|
|
48
|
+
/**
|
|
49
|
+
* Indices (0-based, into the sequence) where the tab path jumps *backward against
|
|
50
|
+
* reading order* — a stop that sits clearly above, or well to the left on the
|
|
51
|
+
* same row, of the stop before it. A heuristic signal that DOM/tabindex order
|
|
52
|
+
* has drifted from the visual order a sighted keyboard user expects; always
|
|
53
|
+
* "verify manually". `getRect` is injectable for tests.
|
|
54
|
+
*/
|
|
55
|
+
export declare function visualOrderJumps(stops: readonly TabStop[], getRect?: (el: Element) => {
|
|
56
|
+
top: number;
|
|
57
|
+
left: number;
|
|
58
|
+
}, rowThreshold?: number): number[];
|
|
@@ -0,0 +1,178 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The keyboard layer, part A: compute the order in which Tab moves through a
|
|
3
|
+
* page. This is the ~2/3 of accessibility axe can't test — keyboard operability
|
|
4
|
+
* — and, like the axe findings, every stop is attributed to the component that
|
|
5
|
+
* rendered it.
|
|
6
|
+
*
|
|
7
|
+
* The tabbable set and ordering mirror the browser's own algorithm (and the
|
|
8
|
+
* well-known `tabbable` element definition), implemented here with no runtime
|
|
9
|
+
* dependency: elements with a positive `tabindex` come first in ascending order
|
|
10
|
+
* (the anti-pattern that hijacks focus order), then everything with `tabindex=0`
|
|
11
|
+
* or a native default in DOM order.
|
|
12
|
+
*/
|
|
13
|
+
import { appComponentFromPath, DEFAULT_FRAMEWORK_PREFIXES, resolveComponentPath, } from '../attribution.js';
|
|
14
|
+
/** Elements that can hold focus and thus may appear in the tab order. */
|
|
15
|
+
const FOCUSABLE_SELECTOR = [
|
|
16
|
+
'a[href]',
|
|
17
|
+
'area[href]',
|
|
18
|
+
'button',
|
|
19
|
+
'input',
|
|
20
|
+
'select',
|
|
21
|
+
'textarea',
|
|
22
|
+
'audio[controls]',
|
|
23
|
+
'video[controls]',
|
|
24
|
+
'summary',
|
|
25
|
+
'iframe',
|
|
26
|
+
'object',
|
|
27
|
+
'embed',
|
|
28
|
+
'[contenteditable]',
|
|
29
|
+
'[tabindex]',
|
|
30
|
+
].join(',');
|
|
31
|
+
/** A native default of 0 (focusable without a tabindex attribute), else -1. */
|
|
32
|
+
function nativeTabIndex(element) {
|
|
33
|
+
return isNativelyFocusable(element) ? 0 : -1;
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* True when the element takes focus without an explicit `tabindex` — a link with
|
|
37
|
+
* an href, a form control, a `<summary>`, an editing host, and the embedded
|
|
38
|
+
* document elements. Disabled/hidden state is handled separately.
|
|
39
|
+
*/
|
|
40
|
+
export function isNativelyFocusable(element) {
|
|
41
|
+
const tag = element.tagName.toLowerCase();
|
|
42
|
+
switch (tag) {
|
|
43
|
+
case 'a':
|
|
44
|
+
case 'area':
|
|
45
|
+
return element.hasAttribute('href');
|
|
46
|
+
case 'button':
|
|
47
|
+
case 'select':
|
|
48
|
+
case 'textarea':
|
|
49
|
+
return true;
|
|
50
|
+
case 'input':
|
|
51
|
+
return element.getAttribute('type')?.toLowerCase() !== 'hidden';
|
|
52
|
+
case 'summary':
|
|
53
|
+
return true;
|
|
54
|
+
case 'iframe':
|
|
55
|
+
case 'object':
|
|
56
|
+
case 'embed':
|
|
57
|
+
return true;
|
|
58
|
+
case 'audio':
|
|
59
|
+
case 'video':
|
|
60
|
+
return element.hasAttribute('controls');
|
|
61
|
+
}
|
|
62
|
+
const editable = element.getAttribute('contenteditable');
|
|
63
|
+
return editable !== null && editable !== 'false';
|
|
64
|
+
}
|
|
65
|
+
/**
|
|
66
|
+
* The tabindex the browser will use: the parsed attribute when present and
|
|
67
|
+
* numeric, otherwise the native default. A non-numeric `tabindex` (e.g.
|
|
68
|
+
* `tabindex="foo"`) is ignored by browsers, so it falls back to the native value.
|
|
69
|
+
*/
|
|
70
|
+
export function resolvedTabIndex(element) {
|
|
71
|
+
const attr = element.getAttribute('tabindex');
|
|
72
|
+
if (attr === null)
|
|
73
|
+
return nativeTabIndex(element);
|
|
74
|
+
const parsed = Number.parseInt(attr, 10);
|
|
75
|
+
return Number.isNaN(parsed) ? nativeTabIndex(element) : parsed;
|
|
76
|
+
}
|
|
77
|
+
/** True when `element` (or an ancestor) is disabled via a control or `<fieldset disabled>`. */
|
|
78
|
+
function isDisabled(element) {
|
|
79
|
+
if ('disabled' in element && element.disabled)
|
|
80
|
+
return true;
|
|
81
|
+
// A disabled fieldset disables its controls (except those inside its first legend).
|
|
82
|
+
return element.closest('fieldset[disabled]') !== null && element.closest('legend') === null;
|
|
83
|
+
}
|
|
84
|
+
/**
|
|
85
|
+
* Structurally hidden — removed from the tab order by markup, independent of any
|
|
86
|
+
* layout: inside an `[inert]` or `[hidden]` subtree, or non-summary content of a
|
|
87
|
+
* closed `<details>`. Kept separate from the layout check so the injectable
|
|
88
|
+
* visibility predicate only ever overrides computed-style visibility.
|
|
89
|
+
*/
|
|
90
|
+
function isStructurallyHidden(element) {
|
|
91
|
+
if (element.closest('[inert]') || element.closest('[hidden]'))
|
|
92
|
+
return true;
|
|
93
|
+
const closedDetails = element.closest('details:not([open])');
|
|
94
|
+
return closedDetails !== null && element.closest('summary') === null && element !== closedDetails;
|
|
95
|
+
}
|
|
96
|
+
/** Layout-aware default visibility: computed `display`/`visibility` up the tree. */
|
|
97
|
+
function defaultIsVisible(element) {
|
|
98
|
+
const view = element.ownerDocument?.defaultView;
|
|
99
|
+
if (!view?.getComputedStyle)
|
|
100
|
+
return true; // no layout engine (e.g. jsdom) — assume visible
|
|
101
|
+
for (let node = element; node; node = node.parentElement) {
|
|
102
|
+
const style = view.getComputedStyle(node);
|
|
103
|
+
if (style.display === 'none')
|
|
104
|
+
return false;
|
|
105
|
+
if (style.visibility === 'hidden' || style.visibility === 'collapse')
|
|
106
|
+
return false;
|
|
107
|
+
}
|
|
108
|
+
return true;
|
|
109
|
+
}
|
|
110
|
+
/**
|
|
111
|
+
* True when `element` participates in sequential keyboard navigation: it's
|
|
112
|
+
* focusable, has a resolved `tabindex >= 0`, and is neither disabled nor hidden.
|
|
113
|
+
* `isVisible` overrides only the layout (computed-style) part of the check.
|
|
114
|
+
*/
|
|
115
|
+
export function isTabbable(element, isVisible = defaultIsVisible) {
|
|
116
|
+
if (resolvedTabIndex(element) < 0)
|
|
117
|
+
return false;
|
|
118
|
+
if (isDisabled(element))
|
|
119
|
+
return false;
|
|
120
|
+
if (isStructurallyHidden(element))
|
|
121
|
+
return false;
|
|
122
|
+
// An element matched only by `[contenteditable]` with the value "false" isn't focusable.
|
|
123
|
+
const editable = element.getAttribute('contenteditable');
|
|
124
|
+
if (editable === 'false' && !isNativelyFocusable(element) && !element.hasAttribute('tabindex')) {
|
|
125
|
+
return false;
|
|
126
|
+
}
|
|
127
|
+
return isVisible(element);
|
|
128
|
+
}
|
|
129
|
+
/**
|
|
130
|
+
* The resolved tab order within `root`: positive-tabindex stops first (ascending,
|
|
131
|
+
* ties in DOM order), then `tabindex=0`/native stops in DOM order — the order a
|
|
132
|
+
* browser actually moves focus. Each stop carries its owning component.
|
|
133
|
+
*/
|
|
134
|
+
export function tabSequence(root = document, options = {}) {
|
|
135
|
+
const isVisible = options.isVisible ?? defaultIsVisible;
|
|
136
|
+
const prefixes = options.frameworkPrefixes ?? DEFAULT_FRAMEWORK_PREFIXES;
|
|
137
|
+
const candidates = [...root.querySelectorAll(FOCUSABLE_SELECTOR)].filter((el) => isTabbable(el, isVisible));
|
|
138
|
+
const withMeta = candidates.map((element, domIndex) => ({
|
|
139
|
+
element,
|
|
140
|
+
domIndex,
|
|
141
|
+
tabindex: resolvedTabIndex(element),
|
|
142
|
+
positive: /^\d+$/.test(element.getAttribute('tabindex') ?? '') && resolvedTabIndex(element) > 0,
|
|
143
|
+
}));
|
|
144
|
+
const positives = withMeta
|
|
145
|
+
.filter((m) => m.tabindex > 0)
|
|
146
|
+
.sort((a, b) => a.tabindex - b.tabindex || a.domIndex - b.domIndex);
|
|
147
|
+
const zeros = withMeta.filter((m) => m.tabindex === 0); // already in DOM order
|
|
148
|
+
return [...positives, ...zeros].map((m, i) => {
|
|
149
|
+
const componentPath = resolveComponentPath(m.element);
|
|
150
|
+
return {
|
|
151
|
+
element: m.element,
|
|
152
|
+
order: i + 1,
|
|
153
|
+
tabindex: m.tabindex,
|
|
154
|
+
positive: m.positive,
|
|
155
|
+
component: appComponentFromPath(componentPath, prefixes),
|
|
156
|
+
componentPath,
|
|
157
|
+
};
|
|
158
|
+
});
|
|
159
|
+
}
|
|
160
|
+
/**
|
|
161
|
+
* Indices (0-based, into the sequence) where the tab path jumps *backward against
|
|
162
|
+
* reading order* — a stop that sits clearly above, or well to the left on the
|
|
163
|
+
* same row, of the stop before it. A heuristic signal that DOM/tabindex order
|
|
164
|
+
* has drifted from the visual order a sighted keyboard user expects; always
|
|
165
|
+
* "verify manually". `getRect` is injectable for tests.
|
|
166
|
+
*/
|
|
167
|
+
export function visualOrderJumps(stops, getRect = (el) => el.getBoundingClientRect(), rowThreshold = 8) {
|
|
168
|
+
const jumps = [];
|
|
169
|
+
for (let i = 1; i < stops.length; i++) {
|
|
170
|
+
const prev = getRect(stops[i - 1].element);
|
|
171
|
+
const curr = getRect(stops[i].element);
|
|
172
|
+
const movedUp = curr.top < prev.top - rowThreshold;
|
|
173
|
+
const sameRowMovedLeft = Math.abs(curr.top - prev.top) <= rowThreshold && curr.left < prev.left - rowThreshold;
|
|
174
|
+
if (movedUp || sameRowMovedLeft)
|
|
175
|
+
jumps.push(i);
|
|
176
|
+
}
|
|
177
|
+
return jumps;
|
|
178
|
+
}
|
package/dist/overlay.d.ts
CHANGED
|
@@ -1,4 +1,6 @@
|
|
|
1
1
|
import type { A11yFinding } from './scan.js';
|
|
2
|
+
import type { TabStop } from './keyboard/tab-sequence.js';
|
|
3
|
+
import type { AxDescription } from './keyboard/accname.js';
|
|
2
4
|
/**
|
|
3
5
|
* Attribute marking the overlay's own DOM. `scan()` excludes anything under it
|
|
4
6
|
* so the tool never reports violations against its own highlights.
|
|
@@ -6,11 +8,31 @@ import type { A11yFinding } from './scan.js';
|
|
|
6
8
|
export declare const OVERLAY_ATTR = "data-ngb-a11y-overlay";
|
|
7
9
|
/** Selector form of {@link OVERLAY_ATTR}, passed to axe as an exclude. */
|
|
8
10
|
export declare const OVERLAY_EXCLUDE_SELECTOR = "[data-ngb-a11y-overlay]";
|
|
11
|
+
/** The computed accessibility-tree info shown in the focus-follow panel. */
|
|
12
|
+
export interface AxPanelData extends AxDescription {
|
|
13
|
+
/** Owning component of the focused element, or null. */
|
|
14
|
+
component: string | null;
|
|
15
|
+
/** Tag name of the focused element, for context (e.g. `div`, `button`). */
|
|
16
|
+
tag: string;
|
|
17
|
+
}
|
|
9
18
|
export interface A11yOverlay {
|
|
10
19
|
/** Draw a highlight over each finding's node, replacing the previous set. */
|
|
11
20
|
render(findings: A11yFinding[]): void;
|
|
21
|
+
/**
|
|
22
|
+
* Draw the tab order as numbered badges plus a connector path, on a layer
|
|
23
|
+
* independent of the findings highlights. Replaces the previous tab-order set.
|
|
24
|
+
*/
|
|
25
|
+
renderTabOrder(stops: TabStop[]): void;
|
|
26
|
+
/**
|
|
27
|
+
* Show the accessibility-tree preview panel for the focused element, or hide it
|
|
28
|
+
* when passed `null`. Deliberately framed as a *computed approximation* — see
|
|
29
|
+
* the panel header — never as verbatim screen-reader output.
|
|
30
|
+
*/
|
|
31
|
+
renderAxPanel(data: AxPanelData | null): void;
|
|
12
32
|
/** Remove all highlights but keep the overlay live. */
|
|
13
33
|
clear(): void;
|
|
34
|
+
/** Remove the tab-order layer but keep the overlay live. */
|
|
35
|
+
clearTabOrder(): void;
|
|
14
36
|
/** Tear down: remove the container and detach scroll/resize listeners. */
|
|
15
37
|
destroy(): void;
|
|
16
38
|
}
|
package/dist/overlay.js
CHANGED
|
@@ -1,3 +1,7 @@
|
|
|
1
|
+
const SVG_NS = 'http://www.w3.org/2000/svg';
|
|
2
|
+
/** Tab-order path + badge colours: normal teal, warning orange for positive tabindex. */
|
|
3
|
+
const TAB_ORDER_COLOR = '#0b8f8f';
|
|
4
|
+
const TAB_ORDER_WARN_COLOR = '#e8710a';
|
|
1
5
|
/**
|
|
2
6
|
* Attribute marking the overlay's own DOM. `scan()` excludes anything under it
|
|
3
7
|
* so the tool never reports violations against its own highlights.
|
|
@@ -35,12 +39,65 @@ export function createOverlay(options = {}) {
|
|
|
35
39
|
zIndex: '2147483646',
|
|
36
40
|
});
|
|
37
41
|
doc.body.appendChild(container);
|
|
42
|
+
// The tab-order connector lives on its own SVG layer under the badges, so the
|
|
43
|
+
// findings highlights and the tab-order path render and clear independently.
|
|
44
|
+
const svg = doc.createElementNS(SVG_NS, 'svg');
|
|
45
|
+
svg.setAttribute(OVERLAY_ATTR, '');
|
|
46
|
+
Object.assign(svg.style, {
|
|
47
|
+
position: 'fixed',
|
|
48
|
+
inset: '0',
|
|
49
|
+
width: '100%',
|
|
50
|
+
height: '100%',
|
|
51
|
+
pointerEvents: 'none',
|
|
52
|
+
overflow: 'visible',
|
|
53
|
+
});
|
|
54
|
+
const connector = doc.createElementNS(SVG_NS, 'polyline');
|
|
55
|
+
connector.setAttribute('fill', 'none');
|
|
56
|
+
connector.setAttribute('stroke', TAB_ORDER_COLOR);
|
|
57
|
+
connector.setAttribute('stroke-width', '2');
|
|
58
|
+
connector.setAttribute('stroke-dasharray', '4 3');
|
|
59
|
+
connector.setAttribute('opacity', '0.7');
|
|
60
|
+
svg.appendChild(connector);
|
|
61
|
+
container.appendChild(svg); // under the badges, which are appended later
|
|
62
|
+
// The accessibility-tree preview panel: a fixed card that follows focus. Hidden
|
|
63
|
+
// until renderAxPanel is called with data. Marked with OVERLAY_ATTR so scans
|
|
64
|
+
// never flag the tool's own UI.
|
|
65
|
+
const axPanel = doc.createElement('div');
|
|
66
|
+
axPanel.setAttribute(OVERLAY_ATTR, '');
|
|
67
|
+
Object.assign(axPanel.style, {
|
|
68
|
+
position: 'fixed',
|
|
69
|
+
right: '12px',
|
|
70
|
+
bottom: '12px',
|
|
71
|
+
maxWidth: '320px',
|
|
72
|
+
padding: '8px 10px',
|
|
73
|
+
borderRadius: '6px',
|
|
74
|
+
background: 'rgba(20,22,28,0.94)',
|
|
75
|
+
color: '#f4f4f5',
|
|
76
|
+
font: '12px/1.5 ui-sans-serif, system-ui, sans-serif',
|
|
77
|
+
boxShadow: '0 2px 12px rgba(0,0,0,0.4)',
|
|
78
|
+
pointerEvents: 'none',
|
|
79
|
+
zIndex: '2147483647',
|
|
80
|
+
display: 'none',
|
|
81
|
+
});
|
|
82
|
+
container.appendChild(axPanel);
|
|
38
83
|
let highlights = [];
|
|
84
|
+
let tabBadges = [];
|
|
39
85
|
const reposition = () => {
|
|
40
86
|
for (const { target, box } of highlights) {
|
|
41
87
|
positionBox(box, target);
|
|
42
88
|
}
|
|
89
|
+
repositionTabOrder();
|
|
43
90
|
};
|
|
91
|
+
function repositionTabOrder() {
|
|
92
|
+
const points = [];
|
|
93
|
+
for (const { target, badge } of tabBadges) {
|
|
94
|
+
const rect = target.getBoundingClientRect();
|
|
95
|
+
badge.style.top = `${rect.top}px`;
|
|
96
|
+
badge.style.left = `${rect.left}px`;
|
|
97
|
+
points.push(`${rect.left + rect.width / 2},${rect.top + rect.height / 2}`);
|
|
98
|
+
}
|
|
99
|
+
connector.setAttribute('points', points.join(' '));
|
|
100
|
+
}
|
|
44
101
|
// rAF-throttle so a stream of scroll events collapses to one layout read.
|
|
45
102
|
const raf = doc.defaultView?.requestAnimationFrame?.bind(doc.defaultView);
|
|
46
103
|
let scheduled = false;
|
|
@@ -78,13 +135,112 @@ export function createOverlay(options = {}) {
|
|
|
78
135
|
highlights.push({ finding, target, box });
|
|
79
136
|
}
|
|
80
137
|
}
|
|
138
|
+
function clearTabOrder() {
|
|
139
|
+
for (const { badge } of tabBadges)
|
|
140
|
+
badge.remove();
|
|
141
|
+
tabBadges = [];
|
|
142
|
+
connector.setAttribute('points', '');
|
|
143
|
+
}
|
|
144
|
+
function renderTabOrder(stops) {
|
|
145
|
+
clearTabOrder();
|
|
146
|
+
for (const stop of stops) {
|
|
147
|
+
const target = stop.element;
|
|
148
|
+
if (!target.isConnected)
|
|
149
|
+
continue; // node gone since the sequence was computed
|
|
150
|
+
const badge = buildBadge(doc, stop);
|
|
151
|
+
container.appendChild(badge);
|
|
152
|
+
tabBadges.push({ stop, target, badge });
|
|
153
|
+
}
|
|
154
|
+
repositionTabOrder();
|
|
155
|
+
}
|
|
156
|
+
function renderAxPanel(data) {
|
|
157
|
+
if (!data) {
|
|
158
|
+
axPanel.style.display = 'none';
|
|
159
|
+
axPanel.replaceChildren();
|
|
160
|
+
return;
|
|
161
|
+
}
|
|
162
|
+
axPanel.replaceChildren(...buildAxPanelContent(doc, data));
|
|
163
|
+
axPanel.style.display = 'block';
|
|
164
|
+
}
|
|
81
165
|
function destroy() {
|
|
82
166
|
clear();
|
|
167
|
+
clearTabOrder();
|
|
168
|
+
renderAxPanel(null);
|
|
83
169
|
win?.removeEventListener('scroll', onViewportChange, { capture: true });
|
|
84
170
|
win?.removeEventListener('resize', onViewportChange);
|
|
85
171
|
container.remove();
|
|
86
172
|
}
|
|
87
|
-
return { render, clear, destroy };
|
|
173
|
+
return { render, renderTabOrder, renderAxPanel, clear, clearTabOrder, destroy };
|
|
174
|
+
}
|
|
175
|
+
/** The rows of the accessibility-tree preview panel, honesty header first. */
|
|
176
|
+
function buildAxPanelContent(doc, data) {
|
|
177
|
+
const nodes = [];
|
|
178
|
+
const header = doc.createElement('div');
|
|
179
|
+
header.textContent = 'Accessibility-tree preview — computed approximation';
|
|
180
|
+
Object.assign(header.style, {
|
|
181
|
+
fontWeight: '700',
|
|
182
|
+
fontSize: '10px',
|
|
183
|
+
letterSpacing: '0.02em',
|
|
184
|
+
textTransform: 'uppercase',
|
|
185
|
+
color: '#a9b0bd',
|
|
186
|
+
marginBottom: '4px',
|
|
187
|
+
});
|
|
188
|
+
header.title = 'A computed name/role/state — an approximation of what assistive tech announces, not any one screen reader.';
|
|
189
|
+
nodes.push(header);
|
|
190
|
+
const row = (label, value, muted = false) => {
|
|
191
|
+
const div = doc.createElement('div');
|
|
192
|
+
const key = doc.createElement('span');
|
|
193
|
+
key.textContent = `${label}: `;
|
|
194
|
+
key.style.color = '#a9b0bd';
|
|
195
|
+
const val = doc.createElement('span');
|
|
196
|
+
val.textContent = value;
|
|
197
|
+
if (muted)
|
|
198
|
+
val.style.color = '#e8710a'; // draw the eye to a missing name
|
|
199
|
+
div.append(key, val);
|
|
200
|
+
return div;
|
|
201
|
+
};
|
|
202
|
+
nodes.push(row('role', data.role ?? `(none — <${data.tag}>)`));
|
|
203
|
+
nodes.push(data.name
|
|
204
|
+
? row('name', `"${data.name}"`)
|
|
205
|
+
: row('name', '(no accessible name)', true));
|
|
206
|
+
if (data.description)
|
|
207
|
+
nodes.push(row('description', `"${data.description}"`));
|
|
208
|
+
if (data.states.length)
|
|
209
|
+
nodes.push(row('states', data.states.join(', ')));
|
|
210
|
+
if (data.component)
|
|
211
|
+
nodes.push(row('component', data.component));
|
|
212
|
+
return nodes;
|
|
213
|
+
}
|
|
214
|
+
/**
|
|
215
|
+
* A numbered badge marking one tab stop, anchored at the target's top-left
|
|
216
|
+
* corner. A positive-tabindex stop is coloured as a warning, since an explicit
|
|
217
|
+
* positive tabindex hijacks the natural order.
|
|
218
|
+
*/
|
|
219
|
+
function buildBadge(doc, stop) {
|
|
220
|
+
const color = stop.positive ? TAB_ORDER_WARN_COLOR : TAB_ORDER_COLOR;
|
|
221
|
+
const badge = doc.createElement('div');
|
|
222
|
+
badge.setAttribute('data-ngb-tab-order', String(stop.order));
|
|
223
|
+
const owner = stop.component ? ` · ${stop.component}` : '';
|
|
224
|
+
const ti = stop.positive ? ` · tabindex=${stop.tabindex} (hijacks order)` : '';
|
|
225
|
+
badge.title = `Tab stop ${stop.order}${owner}${ti}`;
|
|
226
|
+
badge.textContent = String(stop.order);
|
|
227
|
+
Object.assign(badge.style, {
|
|
228
|
+
position: 'fixed',
|
|
229
|
+
transform: 'translate(-50%, -50%)',
|
|
230
|
+
minWidth: '16px',
|
|
231
|
+
height: '16px',
|
|
232
|
+
padding: '0 3px',
|
|
233
|
+
boxSizing: 'border-box',
|
|
234
|
+
borderRadius: '8px',
|
|
235
|
+
font: '10px/16px ui-monospace, monospace',
|
|
236
|
+
fontWeight: '700',
|
|
237
|
+
textAlign: 'center',
|
|
238
|
+
color: '#fff',
|
|
239
|
+
background: color,
|
|
240
|
+
border: '1px solid rgba(255,255,255,0.85)',
|
|
241
|
+
pointerEvents: 'none',
|
|
242
|
+
});
|
|
243
|
+
return badge;
|
|
88
244
|
}
|
|
89
245
|
/** Resolve a finding's target selector to a node, tolerating a bad selector. */
|
|
90
246
|
function resolveTarget(doc, target) {
|
package/dist/provider.d.ts
CHANGED
|
@@ -26,6 +26,14 @@ export interface A11yDevtoolsOptions {
|
|
|
26
26
|
* when auditing a component library's own code.
|
|
27
27
|
*/
|
|
28
28
|
frameworkPrefixes?: readonly string[];
|
|
29
|
+
/**
|
|
30
|
+
* Turn on the **keyboard layer** — the ~2/3 of accessibility axe can't test.
|
|
31
|
+
* Adds heuristic `ngbr/*` findings (keyboard-unreachable controls,
|
|
32
|
+
* click-without-keyboard handlers, tab-order mismatches) to the report, and,
|
|
33
|
+
* when `overlay` is on, draws the numbered tab-order path over the page.
|
|
34
|
+
* Default false.
|
|
35
|
+
*/
|
|
36
|
+
keyboard?: boolean;
|
|
29
37
|
}
|
|
30
38
|
/**
|
|
31
39
|
* Dev-only in-app accessibility auditing. Rescans whenever the application
|
package/dist/provider.js
CHANGED
|
@@ -1,7 +1,10 @@
|
|
|
1
1
|
import { ApplicationRef, DestroyRef, inject, isDevMode, makeEnvironmentProviders, provideEnvironmentInitializer, } from '@angular/core';
|
|
2
2
|
import { debounceTime, filter } from 'rxjs';
|
|
3
3
|
import { runA11yScan } from './runner.js';
|
|
4
|
-
import { createOverlay } from './overlay.js';
|
|
4
|
+
import { createOverlay, OVERLAY_ATTR } from './overlay.js';
|
|
5
|
+
import { tabSequence } from './keyboard/tab-sequence.js';
|
|
6
|
+
import { describeElement } from './keyboard/accname.js';
|
|
7
|
+
import { resolveOwningComponentName } from './attribution.js';
|
|
5
8
|
/**
|
|
6
9
|
* Dev-only in-app accessibility auditing. Rescans whenever the application
|
|
7
10
|
* settles (zoneless-aware, via `ApplicationRef.isStable`) and reports each
|
|
@@ -15,7 +18,7 @@ export function provideA11yDevtools(options = {}) {
|
|
|
15
18
|
if (!isDevMode()) {
|
|
16
19
|
return makeEnvironmentProviders([]);
|
|
17
20
|
}
|
|
18
|
-
const { root, log = true, logger, overlay = false, debounceMs = 500, tags, frameworkPrefixes } = options;
|
|
21
|
+
const { root, log = true, logger, overlay = false, debounceMs = 500, tags, frameworkPrefixes, keyboard = false, } = options;
|
|
19
22
|
const axe = tags
|
|
20
23
|
? { runOnly: { type: 'tag', values: tags } }
|
|
21
24
|
: undefined;
|
|
@@ -24,6 +27,26 @@ export function provideA11yDevtools(options = {}) {
|
|
|
24
27
|
const appRef = inject(ApplicationRef);
|
|
25
28
|
const destroyRef = inject(DestroyRef);
|
|
26
29
|
const overlayView = overlay ? createOverlay() : undefined;
|
|
30
|
+
// Keyboard & Focus Mode, part C: a focus-follow accessibility-tree preview.
|
|
31
|
+
// As the user Tabs, show the focused control's computed role/name/state —
|
|
32
|
+
// the ~2/3 axe can't test — attributed to its component. Only meaningful
|
|
33
|
+
// with the visual overlay on.
|
|
34
|
+
let onFocusIn;
|
|
35
|
+
if (overlayView && keyboard) {
|
|
36
|
+
onFocusIn = (event) => {
|
|
37
|
+
const el = event.target;
|
|
38
|
+
if (!(el instanceof Element) || el.closest(`[${OVERLAY_ATTR}]`))
|
|
39
|
+
return; // skip our own UI
|
|
40
|
+
describeElement(el)
|
|
41
|
+
.then((desc) => overlayView.renderAxPanel({
|
|
42
|
+
...desc,
|
|
43
|
+
component: resolveOwningComponentName(el, frameworkPrefixes),
|
|
44
|
+
tag: el.tagName.toLowerCase(),
|
|
45
|
+
}))
|
|
46
|
+
.catch(() => undefined);
|
|
47
|
+
};
|
|
48
|
+
document.addEventListener('focusin', onFocusIn, true);
|
|
49
|
+
}
|
|
27
50
|
let scanning = false;
|
|
28
51
|
const subscription = appRef.isStable
|
|
29
52
|
.pipe(filter((stable) => stable), debounceTime(debounceMs))
|
|
@@ -31,8 +54,15 @@ export function provideA11yDevtools(options = {}) {
|
|
|
31
54
|
if (scanning)
|
|
32
55
|
return; // don't stack rescans while one is in flight
|
|
33
56
|
scanning = true;
|
|
34
|
-
runA11yScan(root?.(), { log, logger, axe, frameworkPrefixes })
|
|
35
|
-
.then((findings) =>
|
|
57
|
+
runA11yScan(root?.(), { log, logger, axe, frameworkPrefixes, keyboard })
|
|
58
|
+
.then((findings) => {
|
|
59
|
+
overlayView?.render(findings);
|
|
60
|
+
// Draw the tab-order path as its own overlay layer (independent of
|
|
61
|
+
// the findings highlights) when the keyboard layer is on.
|
|
62
|
+
if (overlayView && keyboard) {
|
|
63
|
+
overlayView.renderTabOrder(tabSequence(root?.() ?? document, { frameworkPrefixes }));
|
|
64
|
+
}
|
|
65
|
+
})
|
|
36
66
|
.catch(() => undefined)
|
|
37
67
|
.finally(() => {
|
|
38
68
|
scanning = false;
|
|
@@ -40,6 +70,8 @@ export function provideA11yDevtools(options = {}) {
|
|
|
40
70
|
});
|
|
41
71
|
destroyRef.onDestroy(() => {
|
|
42
72
|
subscription.unsubscribe();
|
|
73
|
+
if (onFocusIn)
|
|
74
|
+
document.removeEventListener('focusin', onFocusIn, true);
|
|
43
75
|
overlayView?.destroy();
|
|
44
76
|
});
|
|
45
77
|
}),
|