@ngbracket/a11y-devtools 0.12.0 → 0.12.2

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 CHANGED
@@ -3,6 +3,44 @@
3
3
  All notable changes to `@ngbracket/a11y-devtools` are documented here.
4
4
  This project adheres to [Semantic Versioning](https://semver.org/).
5
5
 
6
+ ## 0.12.2
7
+
8
+ ### Changed
9
+
10
+ - **Every `ngbr/*` finding links to its own explanation.** `helpUrl` now points to
11
+ the rule's page in the docs (for example
12
+ `https://ngbracket.com/tools/a11y-devtools/docs/click-without-key`). Each page
13
+ explains what the message means, who it affects, exactly what triggers it, how
14
+ to fix it and how to check it yourself, and links the WCAG or APG reference. It
15
+ used to link straight to the WCAG page.
16
+ - **Clearer messages.** Each now starts with what's wrong for the user, in plain
17
+ words. `ngbr/click-without-key` no longer calls itself a "possible keyboard
18
+ trap": a keyboard trap means you can't Tab away, and this rule is about a
19
+ control you can reach but may not be able to activate. `ngbr/focus-trap` now
20
+ starts "Keyboard trap:", which is what it finds.
21
+
22
+ ## 0.12.1
23
+
24
+ ### Fixed
25
+
26
+ - **Production builds really drop the devtools now.** axe-core was never loaded
27
+ in production, but the devtools' own code (overlay, pill, menu) still ended up
28
+ in the app's main bundle when the provider was called unconditionally or behind
29
+ a runtime `isDevMode()` check: +28.5 KB raw (+10.4 KB gzipped) in 0.12.0 on the
30
+ admin example. The provider now checks `ngDevMode`, which the Angular CLI sets
31
+ to `false` in production builds, so the whole implementation is tree-shaken:
32
+ +0.2 KB raw (+0.05 KB gzipped). A new test bundles a production-style build
33
+ and checks the overlay, pill, menu and axe-core are all absent.
34
+ - **Rescans after top-layer changes Angular doesn't see.** Pressing Escape
35
+ closes a native `<dialog>` without any Angular event, so the page kept the
36
+ findings from while the modal was open until something else changed. The
37
+ devtools now also rescan when a dialog closes or a popover opens or closes. A
38
+ scan requested while one is running now runs once that one finishes, instead
39
+ of being dropped.
40
+ - **Keyboard findings skip the page behind an open modal `<dialog>`.** The
41
+ browser makes it inert, and axe already skips it, so the keyboard checks no
42
+ longer report `ngbr/unreachable-control` and `ngbr/click-without-key` there.
43
+
6
44
  ## 0.12.0
7
45
 
8
46
  ### Added
@@ -34821,10 +34821,7 @@
34821
34821
  "treeitem"
34822
34822
  ]);
34823
34823
  var KEY_EVENTS = ["keydown", "keyup", "keypress"];
34824
- var UNDERSTANDING = "https://www.w3.org/WAI/WCAG22/Understanding";
34825
- var KEYBOARD_URL = `${UNDERSTANDING}/keyboard.html`;
34826
- var FOCUS_ORDER_URL = `${UNDERSTANDING}/focus-order.html`;
34827
- var DIALOG_MODAL_URL = "https://www.w3.org/WAI/ARIA/apg/patterns/dialog-modal/";
34824
+ var RULE_DOCS = "https://ngbracket.com/tools/a11y-devtools/docs";
34828
34825
  function shortSelector(element) {
34829
34826
  const tag = element.tagName.toLowerCase();
34830
34827
  if (element.id)
@@ -34854,7 +34851,11 @@
34854
34851
  html: shortHtml(element)
34855
34852
  };
34856
34853
  };
34854
+ const doc = root instanceof Document ? root : root.ownerDocument;
34855
+ const modal = doc ? blockingModalDialog(doc, options.isModal) : null;
34857
34856
  for (const element of root.querySelectorAll("*")) {
34857
+ if (modal && !modal.contains(element))
34858
+ continue;
34858
34859
  if (isNativelyFocusable(element))
34859
34860
  continue;
34860
34861
  const role = element.getAttribute("role")?.toLowerCase() ?? "";
@@ -34868,21 +34869,21 @@
34868
34869
  const focusable = isTabbable(element, isVisible);
34869
34870
  if (!focusable) {
34870
34871
  const reason = interactiveByRole ? `has role="${role}"` : "has a click handler";
34871
- findings.push(make(element, "ngbr/unreachable-control", "serious", `Interactive element isn't reachable by keyboard \u2014 it ${reason} but is not a native control and has no tabindex >= 0, so keyboard and screen-reader users can't focus it. Add tabindex="0" (and a role, if missing), or use a <button>/<a>.`, KEYBOARD_URL));
34872
+ findings.push(make(element, "ngbr/unreachable-control", "serious", `Keyboard users can't reach this control: it ${reason}, but it isn't a native control and has no tabindex="0", so Tab skips it. Use a <button> or <a href>, or add tabindex="0" and Enter/Space handling.`, `${RULE_DOCS}/unreachable-control`));
34872
34873
  continue;
34873
34874
  }
34874
34875
  const hasKey = events.some((e) => KEY_EVENTS.includes(e));
34875
34876
  if (hasClick && !hasKey) {
34876
- findings.push(make(element, "ngbr/click-without-key", "moderate", `Possible keyboard trap: this element has a (click) handler but no keyboard handler (keydown/keyup) and isn't a native button/link, so Enter/Space may not activate it. Verify by hand, then add a key handler or use a <button>.`, KEYBOARD_URL));
34877
+ findings.push(make(element, "ngbr/click-without-key", "moderate", `Keyboard users may not be able to activate this: Tab reaches it, but it only listens for click, and Enter and Space don't fire click on a non-native element. Use a <button>, or handle Enter and Space too. Heuristic \u2014 verify manually.`, `${RULE_DOCS}/click-without-key`));
34877
34878
  }
34878
34879
  }
34879
- for (const modal of findUncontainedModals(root, { frameworkPrefixes: prefixes, isVisible })) {
34880
- findings.push(make(modal.element, "ngbr/modal-focus-not-contained", "moderate", `Possible missing focus trap: this element has aria-modal="true", but ${modal.outsideCount} tabbable element(s) outside it are still reachable, so a keyboard user can Tab out of the modal to the page behind. Mark the background inert or trap focus within the dialog. Heuristic \u2014 verify manually.`, DIALOG_MODAL_URL));
34880
+ for (const modal2 of findUncontainedModals(root, { frameworkPrefixes: prefixes, isVisible })) {
34881
+ findings.push(make(modal2.element, "ngbr/modal-focus-not-contained", "moderate", `Keyboard users can Tab out of this modal: it has aria-modal="true", but ${modal2.outsideCount} control(s) outside it can still be reached with Tab. Open it with <dialog>.showModal(), make the rest of the page inert, or trap focus. Heuristic \u2014 verify manually.`, `${RULE_DOCS}/modal-focus-not-contained`));
34881
34882
  }
34882
- const stops = tabSequence(root, { frameworkPrefixes: prefixes, isVisible });
34883
+ const stops = tabSequence(root, { frameworkPrefixes: prefixes, isVisible, isModal: options.isModal });
34883
34884
  for (const jump of visualOrderJumps(stops)) {
34884
34885
  const stop = stops[jump];
34885
- findings.push(make(stop.element, "ngbr/tab-order-mismatch", "moderate", `Possible tab-order mismatch: Tab reaches this control (stop ${stop.order}) after one that sits below or to the right of it, so focus jumps against the visual reading order. Heuristic \u2014 verify the order makes sense for a sighted keyboard user.`, FOCUS_ORDER_URL));
34886
+ findings.push(make(stop.element, "ngbr/tab-order-mismatch", "moderate", `Tab order jumps backwards here: Tab reaches this control (stop ${stop.order}) after one that sits below it or to its right, so focus moves against the reading order. Heuristic \u2014 check the order makes sense to a sighted keyboard user.`, `${RULE_DOCS}/tab-order-mismatch`));
34886
34887
  }
34887
34888
  return findings;
34888
34889
  }
@@ -34912,7 +34913,7 @@
34912
34913
  }
34913
34914
  return { kind: "pending" };
34914
34915
  }
34915
- var NO_KEYBOARD_TRAP_URL = "https://www.w3.org/WAI/WCAG22/Understanding/no-keyboard-trap.html";
34916
+ var FOCUS_TRAP_DOCS = "https://ngbracket.com/tools/a11y-devtools/docs/focus-trap";
34916
34917
  function deepActiveElement(doc) {
34917
34918
  let active = doc.activeElement;
34918
34919
  while (active?.shadowRoot?.activeElement)
@@ -34974,8 +34975,8 @@
34974
34975
  return {
34975
34976
  id: "ngbr/focus-trap",
34976
34977
  impact: shiftTabEscapes ? "moderate" : "serious",
34977
- help: `Possible keyboard trap: ${where}${reach}.${shift} If this widget deliberately keeps Tab (e.g. a code editor), it must tell users how to leave (such as Escape); otherwise let Tab move focus on. Found by real Tab presses \u2014 verify manually.`,
34978
- helpUrl: NO_KEYBOARD_TRAP_URL,
34978
+ help: `Keyboard trap: ${where}${reach}.${shift} If this widget deliberately keeps Tab (e.g. a code editor), it must tell users how to leave (such as Escape); otherwise let Tab move focus on. Found by real Tab presses \u2014 verify manually.`,
34979
+ helpUrl: FOCUS_TRAP_DOCS,
34979
34980
  component: appComponentFromPath(componentPath, frameworkPrefixes),
34980
34981
  componentPath,
34981
34982
  directives: resolveDirectiveNames(owner),
@@ -56,7 +56,8 @@ export function detectTabTrap(observations, collapsible = new Set()) {
56
56
  }
57
57
  return { kind: 'pending' };
58
58
  }
59
- const NO_KEYBOARD_TRAP_URL = 'https://www.w3.org/WAI/WCAG22/Understanding/no-keyboard-trap.html';
59
+ /** The rule's page: what the message means, how to fix and check it, and WCAG 2.1.2. */
60
+ const FOCUS_TRAP_DOCS = 'https://ngbracket.com/tools/a11y-devtools/docs/focus-trap';
60
61
  /** The element that really has focus, through open shadow roots. */
61
62
  function deepActiveElement(doc) {
62
63
  let active = doc.activeElement;
@@ -132,10 +133,10 @@ export function createFocusWalkProbe(doc = document, frameworkPrefixes = DEFAULT
132
133
  return {
133
134
  id: 'ngbr/focus-trap',
134
135
  impact: shiftTabEscapes ? 'moderate' : 'serious',
135
- help: `Possible keyboard trap: ${where}${reach}.${shift} If this widget deliberately keeps ` +
136
+ help: `Keyboard trap: ${where}${reach}.${shift} If this widget deliberately keeps ` +
136
137
  `Tab (e.g. a code editor), it must tell users how to leave (such as Escape); otherwise ` +
137
138
  `let Tab move focus on. Found by real Tab presses — verify manually.`,
138
- helpUrl: NO_KEYBOARD_TRAP_URL,
139
+ helpUrl: FOCUS_TRAP_DOCS,
139
140
  component: appComponentFromPath(componentPath, frameworkPrefixes),
140
141
  componentPath,
141
142
  directives: resolveDirectiveNames(owner),
@@ -4,7 +4,14 @@ export interface KeyboardScanOptions {
4
4
  frameworkPrefixes?: readonly string[];
5
5
  /** Visibility predicate override (tests); see {@link tabSequence}. */
6
6
  isVisible?: (element: Element) => boolean;
7
+ /** Modal-dialog check override (tests; jsdom has no `:modal`). See {@link tabSequence}. */
8
+ isModal?: (element: Element) => boolean;
7
9
  }
10
+ /**
11
+ * Each rule's page: what the message means, who it affects, exactly what
12
+ * triggers it, how to fix and check it, and the WCAG/APG reference.
13
+ */
14
+ export declare const RULE_DOCS = "https://ngbracket.com/tools/a11y-devtools/docs";
8
15
  /** A short, human CSS-ish selector for our own findings (not an axe target). */
9
16
  export declare function shortSelector(element: Element): string;
10
17
  /** Truncated outerHTML for a finding's `html`. */
@@ -14,6 +14,7 @@
14
14
  import { appComponentFromPath, DEFAULT_FRAMEWORK_PREFIXES, resolveComponentPath, resolveDirectiveNames, resolveListenerEvents, } from '../attribution.js';
15
15
  import { hasFocusableDescendant, isNativelyFocusable, isTabbable, tabSequence, visualOrderJumps, } from './tab-sequence.js';
16
16
  import { findUncontainedModals } from './focus-trap.js';
17
+ import { blockingModalDialog } from '../top-layer.js';
17
18
  /** WAI-ARIA roles that make a non-native element behave as an interactive control. */
18
19
  const INTERACTIVE_ROLES = new Set([
19
20
  'button',
@@ -34,10 +35,11 @@ const INTERACTIVE_ROLES = new Set([
34
35
  'treeitem',
35
36
  ]);
36
37
  const KEY_EVENTS = ['keydown', 'keyup', 'keypress'];
37
- const UNDERSTANDING = 'https://www.w3.org/WAI/WCAG22/Understanding';
38
- const KEYBOARD_URL = `${UNDERSTANDING}/keyboard.html`;
39
- const FOCUS_ORDER_URL = `${UNDERSTANDING}/focus-order.html`;
40
- const DIALOG_MODAL_URL = 'https://www.w3.org/WAI/ARIA/apg/patterns/dialog-modal/';
38
+ /**
39
+ * Each rule's page: what the message means, who it affects, exactly what
40
+ * triggers it, how to fix and check it, and the WCAG/APG reference.
41
+ */
42
+ export const RULE_DOCS = 'https://ngbracket.com/tools/a11y-devtools/docs';
41
43
  /** A short, human CSS-ish selector for our own findings (not an axe target). */
42
44
  export function shortSelector(element) {
43
45
  const tag = element.tagName.toLowerCase();
@@ -79,7 +81,13 @@ export function scanKeyboard(root = document, options = {}) {
79
81
  html: shortHtml(element),
80
82
  };
81
83
  };
84
+ // While a modal <dialog> is open the page behind it is inert: not reachable,
85
+ // and not something to report on (axe skips it too). Only check the modal.
86
+ const doc = root instanceof Document ? root : root.ownerDocument;
87
+ const modal = doc ? blockingModalDialog(doc, options.isModal) : null;
82
88
  for (const element of root.querySelectorAll('*')) {
89
+ if (modal && !modal.contains(element))
90
+ continue;
83
91
  // Native controls are keyboard-reachable and activate on Enter/Space on their
84
92
  // own, so they can't be the subject of either finding — skip them outright.
85
93
  if (isNativelyFocusable(element))
@@ -98,37 +106,37 @@ export function scanKeyboard(root = document, options = {}) {
98
106
  const focusable = isTabbable(element, isVisible);
99
107
  if (!focusable) {
100
108
  const reason = interactiveByRole ? `has role="${role}"` : 'has a click handler';
101
- findings.push(make(element, 'ngbr/unreachable-control', 'serious', `Interactive element isn't reachable by keyboard — it ${reason} but is not a ` +
102
- `native control and has no tabindex >= 0, so keyboard and screen-reader users ` +
103
- `can't focus it. Add tabindex="0" (and a role, if missing), or use a <button>/<a>.`, KEYBOARD_URL));
109
+ findings.push(make(element, 'ngbr/unreachable-control', 'serious', `Keyboard users can't reach this control: it ${reason}, but it isn't a native ` +
110
+ `control and has no tabindex="0", so Tab skips it. Use a <button> or <a href>, ` +
111
+ `or add tabindex="0" and Enter/Space handling.`, `${RULE_DOCS}/unreachable-control`));
104
112
  continue; // don't also report a weaker click-without-key on the same node
105
113
  }
106
114
  // Focusable, but a bare (click) never fires on Enter/Space the way a native
107
115
  // button does — so a keyboard user can reach it and still not activate it.
108
116
  const hasKey = events.some((e) => KEY_EVENTS.includes(e));
109
117
  if (hasClick && !hasKey) {
110
- findings.push(make(element, 'ngbr/click-without-key', 'moderate', `Possible keyboard trap: this element has a (click) handler but no keyboard ` +
111
- `handler (keydown/keyup) and isn't a native button/link, so Enter/Space may not ` +
112
- `activate it. Verify by hand, then add a key handler or use a <button>.`, KEYBOARD_URL));
118
+ findings.push(make(element, 'ngbr/click-without-key', 'moderate', `Keyboard users may not be able to activate this: Tab reaches it, but it only ` +
119
+ `listens for click, and Enter and Space don't fire click on a non-native element. ` +
120
+ `Use a <button>, or handle Enter and Space too. Heuristic — verify manually.`, `${RULE_DOCS}/click-without-key`));
113
121
  }
114
122
  }
115
123
  // Missing focus trap (M3): an open aria-modal whose focus isn't contained.
116
124
  // Reuses the tab-order machinery (inert/hidden already excluded), so a modal
117
125
  // that correctly inerts the background is not flagged.
118
126
  for (const modal of findUncontainedModals(root, { frameworkPrefixes: prefixes, isVisible })) {
119
- findings.push(make(modal.element, 'ngbr/modal-focus-not-contained', 'moderate', `Possible missing focus trap: this element has aria-modal="true", but ` +
120
- `${modal.outsideCount} tabbable element(s) outside it are still reachable, so a keyboard ` +
121
- `user can Tab out of the modal to the page behind. Mark the background inert or trap ` +
122
- `focus within the dialog. Heuristic — verify manually.`, DIALOG_MODAL_URL));
127
+ findings.push(make(modal.element, 'ngbr/modal-focus-not-contained', 'moderate', `Keyboard users can Tab out of this modal: it has aria-modal="true", but ` +
128
+ `${modal.outsideCount} control(s) outside it can still be reached with Tab. Open it ` +
129
+ `with <dialog>.showModal(), make the rest of the page inert, or trap focus. ` +
130
+ `Heuristic — verify manually.`, `${RULE_DOCS}/modal-focus-not-contained`));
123
131
  }
124
132
  // Visual-vs-tab-order mismatch: a heuristic over the resolved sequence. Needs
125
133
  // real layout, so it no-ops where getBoundingClientRect returns zeros (jsdom).
126
- const stops = tabSequence(root, { frameworkPrefixes: prefixes, isVisible });
134
+ const stops = tabSequence(root, { frameworkPrefixes: prefixes, isVisible, isModal: options.isModal });
127
135
  for (const jump of visualOrderJumps(stops)) {
128
136
  const stop = stops[jump];
129
- findings.push(make(stop.element, 'ngbr/tab-order-mismatch', 'moderate', `Possible tab-order mismatch: Tab reaches this control (stop ${stop.order}) after ` +
130
- `one that sits below or to the right of it, so focus jumps against the visual ` +
131
- `reading order. Heuristic — verify the order makes sense for a sighted keyboard user.`, FOCUS_ORDER_URL));
137
+ findings.push(make(stop.element, 'ngbr/tab-order-mismatch', 'moderate', `Tab order jumps backwards here: Tab reaches this control (stop ${stop.order}) after ` +
138
+ `one that sits below it or to its right, so focus moves against the reading order. ` +
139
+ `Heuristic — check the order makes sense to a sighted keyboard user.`, `${RULE_DOCS}/tab-order-mismatch`));
132
140
  }
133
141
  return findings;
134
142
  }
@@ -70,8 +70,9 @@ export interface A11yDevtoolsOptions {
70
70
  * settles (zoneless-aware, via `ApplicationRef.isStable`) and reports each
71
71
  * violation against the component that rendered it.
72
72
  *
73
- * In production this is a no-op and axe-core is never loaded, so it carries no
74
- * runtime weight. For a hard guarantee, include the provider only in your dev
75
- * bootstrap config.
73
+ * In production this is a no-op and axe-core is never loaded. The Angular CLI
74
+ * replaces `ngDevMode` with `false` in production builds, so the check below is
75
+ * constant and the bundler drops the whole implementation — about 0.3 KB is
76
+ * left, even when the provider is called unconditionally.
76
77
  */
77
78
  export declare function provideA11yDevtools(options?: A11yDevtoolsOptions): EnvironmentProviders;
package/dist/provider.js CHANGED
@@ -12,11 +12,22 @@ import { readStoredSettings, resolveSettings, visibleFindings, writeStoredSettin
12
12
  * settles (zoneless-aware, via `ApplicationRef.isStable`) and reports each
13
13
  * violation against the component that rendered it.
14
14
  *
15
- * In production this is a no-op and axe-core is never loaded, so it carries no
16
- * runtime weight. For a hard guarantee, include the provider only in your dev
17
- * bootstrap config.
15
+ * In production this is a no-op and axe-core is never loaded. The Angular CLI
16
+ * replaces `ngDevMode` with `false` in production builds, so the check below is
17
+ * constant and the bundler drops the whole implementation — about 0.3 KB is
18
+ * left, even when the provider is called unconditionally.
18
19
  */
19
20
  export function provideA11yDevtools(options = {}) {
21
+ // In an Angular production build `ngDevMode` is the constant `false`, so this
22
+ // folds to the empty branch and `devtoolsProviders` (overlay, pill, menu,
23
+ // scanning) is never referenced and gets tree-shaken. Keep it a ternary:
24
+ // bundlers fold a constant conditional expression before tree-shaking, but not
25
+ // an `if (…) return` followed by more code.
26
+ return typeof ngDevMode !== 'undefined' && !ngDevMode
27
+ ? makeEnvironmentProviders([])
28
+ : devtoolsProviders(options);
29
+ }
30
+ function devtoolsProviders(options) {
20
31
  if (!isDevMode()) {
21
32
  return makeEnvironmentProviders([]);
22
33
  }
@@ -150,9 +161,14 @@ export function provideA11yDevtools(options = {}) {
150
161
  document.addEventListener('focusin', onFocusIn, true);
151
162
  }
152
163
  let scanning = false;
164
+ let rescanQueued = false;
153
165
  function scanNow() {
154
- if (!enabled || scanning)
155
- return; // don't stack rescans while one is in flight
166
+ if (!enabled)
167
+ return;
168
+ if (scanning) {
169
+ rescanQueued = true; // don't stack scans; run one more when this finishes
170
+ return;
171
+ }
156
172
  scanning = true;
157
173
  runA11yScan(root?.(), { log, logger, axe, frameworkPrefixes, keyboard })
158
174
  .then((findings) => {
@@ -167,8 +183,26 @@ export function provideA11yDevtools(options = {}) {
167
183
  .catch(() => undefined)
168
184
  .finally(() => {
169
185
  scanning = false;
186
+ if (rescanQueued) {
187
+ rescanQueued = false;
188
+ scanNow();
189
+ }
170
190
  });
171
191
  }
192
+ // Some top-layer changes happen outside Angular, so the app never becomes
193
+ // "unstable" and no rescan follows: Escape closing a native <dialog>, or a
194
+ // popover toggled by the browser. Rescan after those too (our own layers'
195
+ // toggles excepted).
196
+ let topLayerTimer;
197
+ const onTopLayerChange = (event) => {
198
+ const target = event.target;
199
+ if (target instanceof Element && target.hasAttribute(OVERLAY_ATTR))
200
+ return;
201
+ clearTimeout(topLayerTimer);
202
+ topLayerTimer = setTimeout(scanNow, debounceMs);
203
+ };
204
+ document.addEventListener('close', onTopLayerChange, true);
205
+ document.addEventListener('toggle', onTopLayerChange, true);
172
206
  const subscription = appRef.isStable
173
207
  .pipe(filter((stable) => stable), debounceTime(debounceMs))
174
208
  .subscribe(scanNow);
@@ -178,6 +212,9 @@ export function provideA11yDevtools(options = {}) {
178
212
  document.removeEventListener('focusin', onFocusIn, true);
179
213
  if (onKeyDown)
180
214
  document.removeEventListener('keydown', onKeyDown, true);
215
+ document.removeEventListener('close', onTopLayerChange, true);
216
+ document.removeEventListener('toggle', onTopLayerChange, true);
217
+ clearTimeout(topLayerTimer);
181
218
  pillView?.destroy();
182
219
  overlayView?.destroy();
183
220
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ngbracket/a11y-devtools",
3
- "version": "0.12.0",
3
+ "version": "0.12.2",
4
4
  "description": "Dev-only in-app accessibility auditing for Angular that maps each axe violation back to the component that rendered it — the attribution React overlay tools can't do.",
5
5
  "license": "MIT",
6
6
  "author": "Duncan Faulkner",