@ngbracket/a11y-devtools 0.15.3 → 0.15.4

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,32 @@
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.15.4
7
+
8
+ ### Fixed
9
+
10
+ - **`ngbr/unreachable-control` checks that arrow keys are handled before
11
+ skipping a roving-tabindex item.** 0.15.2 skipped an item with
12
+ `tabindex="-1"` whenever Tab could get into its composite widget, so a half-built
13
+ tab list (the first tab at `tabindex="0"`, the rest at `-1`, no arrow-key
14
+ handling) passed silently. The item is now skipped only when something handles
15
+ keys: the widget, anything inside it, an element that controls it, or an
16
+ element around it up to and including the nearest component's host (so an app
17
+ shell's shortcut listener doesn't count). When nothing does, it's reported as
18
+ moderate, to verify by hand. Without Angular's dev-mode debug API (a
19
+ production build), listeners can't be read, so these items are still skipped.
20
+ - **Key listeners bound with modifiers now count as key handling.** Angular
21
+ reports `(keydown.enter)` as `keydown.enter`, which wasn't recognised, so
22
+ `ngbr/click-without-key` flagged a control with `(click)`, `(keydown.enter)`
23
+ and `(keydown.space)`: the fix its own docs recommend. The same applies to
24
+ `(keydown.arrowRight)` and friends in the checks above.
25
+ - **The mouse-shortcut skip no longer hides `ngbr/click-without-key`.** 0.15.3's
26
+ skip for click-only parts of a keyboard-operated widget ran before the
27
+ Tab-reachability check, so a click-only element Tab *does* reach, inside such a
28
+ widget, got no finding at all. The skip now applies only to elements Tab can't
29
+ reach.
30
+ - `--from` with `--serve` no longer also names `--base` in its error message.
31
+
6
32
  ## 0.15.3
7
33
 
8
34
  ### Fixed
@@ -111,7 +111,6 @@ function parseArgs(argv) {
111
111
  }
112
112
 
113
113
  const opts = parseArgs(process.argv.slice(2));
114
- if (opts.serve && !opts.base) opts.base = 'http://localhost:4200';
115
114
 
116
115
  // Options that only mean something when scanning — refused with --from, rather
117
116
  // than silently ignored.
@@ -131,6 +130,9 @@ if (opts.from && !opts.help) {
131
130
  }
132
131
  }
133
132
 
133
+ // After the --from check, so --serve alone isn't reported as --base too.
134
+ if (opts.serve && !opts.base) opts.base = 'http://localhost:4200';
135
+
134
136
  if (opts.help || (!opts.from && (!opts.base || opts.routes.length === 0))) {
135
137
  process.stderr.write(USAGE);
136
138
  process.exit(opts.help ? 0 : 1);
@@ -34834,30 +34834,63 @@
34834
34834
  "treeitem"
34835
34835
  ]);
34836
34836
  var KEY_EVENTS = ["keydown", "keyup", "keypress"];
34837
+ function hasKeyEvent(events) {
34838
+ return events.some((e) => KEY_EVENTS.includes(e.split(".")[0]));
34839
+ }
34837
34840
  var COMPOSITE_SELECTOR = ["tablist", "toolbar", "menu", "menubar", "radiogroup", "listbox", "tree", "treegrid", "grid"].map((role) => `[role="${role}"]`).join(",");
34838
- function reachedWithinWidget(element, isVisible) {
34841
+ function reachedWithinWidget(element, isVisible, keyCache = /* @__PURE__ */ new Map()) {
34839
34842
  const widget = element.parentElement?.closest(COMPOSITE_SELECTOR);
34840
34843
  if (!widget)
34841
34844
  return false;
34842
34845
  const controllers = widget.id ? [...(widget.ownerDocument ?? document).querySelectorAll("[aria-controls], [aria-owns]")].filter((el) => `${el.getAttribute("aria-controls") ?? ""} ${el.getAttribute("aria-owns") ?? ""}`.split(/\s+/).includes(widget.id)) : [];
34843
34846
  if ([widget, ...controllers].some((el) => el.hasAttribute("aria-activedescendant")))
34844
- return true;
34847
+ return "reached";
34845
34848
  const tabindex = element.getAttribute("tabindex");
34846
34849
  if (tabindex === null || Number.isNaN(Number.parseInt(tabindex, 10)))
34847
34850
  return false;
34848
- return isTabbable(widget, isVisible) || controllers.length > 0 || hasTabbableDescendant(widget, isVisible);
34851
+ const enterable = isTabbable(widget, isVisible) || controllers.length > 0 || hasTabbableDescendant(widget, isVisible);
34852
+ if (!enterable)
34853
+ return false;
34854
+ if (!listenersVisible())
34855
+ return "reached";
34856
+ let keys = keyCache.get(widget);
34857
+ if (keys === void 0)
34858
+ keyCache.set(widget, keys = handlesKeys(widget, controllers));
34859
+ return keys ? "reached" : "no-keys";
34860
+ }
34861
+ function listenersVisible() {
34862
+ return typeof ngDebug()?.getListeners === "function";
34863
+ }
34864
+ function handlesKeys(widget, controllers) {
34865
+ const ng = ngDebug();
34866
+ const body = widget.ownerDocument?.body;
34867
+ const ancestors = [];
34868
+ for (let el = widget.parentElement; el && el !== body; el = el.parentElement) {
34869
+ ancestors.push(el);
34870
+ if (isComponentHost(el, ng))
34871
+ break;
34872
+ }
34873
+ const keyed = (el) => hasKeyEvent(resolveListenerEvents(el));
34874
+ return [widget, ...controllers, ...ancestors].some(keyed) || [...widget.querySelectorAll("*")].some(keyed);
34875
+ }
34876
+ function isComponentHost(el, ng) {
34877
+ try {
34878
+ return ng?.getComponent(el) != null;
34879
+ } catch {
34880
+ return false;
34881
+ }
34849
34882
  }
34850
34883
  var ITEM_SELECTOR = [...INTERACTIVE_ROLES].map((role) => `[role="${role}"]`).join(",");
34851
- function partOfKeyboardOperatedWidget(element, isVisible) {
34884
+ function partOfKeyboardOperatedWidget(element, isVisible, keyCache) {
34852
34885
  const item = element.parentElement?.closest(ITEM_SELECTOR);
34853
34886
  if (item?.parentElement?.closest(COMPOSITE_SELECTOR)) {
34854
- if (isTabbable(item, isVisible) || reachedWithinWidget(item, isVisible))
34887
+ if (isTabbable(item, isVisible) || reachedWithinWidget(item, isVisible, keyCache))
34855
34888
  return true;
34856
34889
  }
34857
34890
  for (let el = element.parentElement; el; el = el.parentElement) {
34858
34891
  if (!isTabbable(el, isVisible))
34859
34892
  continue;
34860
- return resolveListenerEvents(el).some((e) => KEY_EVENTS.includes(e));
34893
+ return hasKeyEvent(resolveListenerEvents(el));
34861
34894
  }
34862
34895
  return false;
34863
34896
  }
@@ -34877,6 +34910,7 @@
34877
34910
  const prefixes = options.frameworkPrefixes ?? DEFAULT_FRAMEWORK_PREFIXES;
34878
34911
  const isVisible = options.isVisible;
34879
34912
  const findings = [];
34913
+ const keyCache = /* @__PURE__ */ new Map();
34880
34914
  const make = (element, id, impact, help, helpUrl) => {
34881
34915
  const componentPath = resolveComponentPath(element);
34882
34916
  return {
@@ -34908,17 +34942,22 @@
34908
34942
  continue;
34909
34943
  if (isHidden(element, isVisible))
34910
34944
  continue;
34911
- if (!interactiveByRole && partOfKeyboardOperatedWidget(element, isVisible))
34912
- continue;
34913
34945
  const focusable = isTabbable(element, isVisible);
34914
34946
  if (!focusable) {
34915
- if (interactiveByRole && reachedWithinWidget(element, isVisible))
34947
+ if (!interactiveByRole && partOfKeyboardOperatedWidget(element, isVisible, keyCache))
34948
+ continue;
34949
+ const within = interactiveByRole ? reachedWithinWidget(element, isVisible, keyCache) : false;
34950
+ if (within === "reached")
34916
34951
  continue;
34952
+ if (within === "no-keys") {
34953
+ findings.push(make(element, "ngbr/unreachable-control", "moderate", `Keyboard users may not be able to reach this item: it has role="${role}" and is out of the tab order, like a roving-tabindex item, but nothing in or around its widget handles keys, so arrow keys may not move to it. Handle the arrow keys, or use aria-activedescendant. Heuristic \u2014 verify manually.`, `${RULE_DOCS}/unreachable-control`));
34954
+ continue;
34955
+ }
34917
34956
  const reason = interactiveByRole ? `has role="${role}"` : "has a click handler";
34918
34957
  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`));
34919
34958
  continue;
34920
34959
  }
34921
- const hasKey = events.some((e) => KEY_EVENTS.includes(e));
34960
+ const hasKey = hasKeyEvent(events);
34922
34961
  if (hasClick && !hasKey) {
34923
34962
  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`));
34924
34963
  }
@@ -11,7 +11,7 @@
11
11
  * and `tab-order-mismatch` are flagged as candidates to verify by hand, never as
12
12
  * confirmed failures.
13
13
  */
14
- import { appComponentFromPath, DEFAULT_FRAMEWORK_PREFIXES, resolveComponentPath, resolveDirectiveNames, resolveListenerEvents, } from '../attribution.js';
14
+ import { appComponentFromPath, DEFAULT_FRAMEWORK_PREFIXES, resolveComponentPath, resolveDirectiveNames, resolveListenerEvents, ngDebug, } from '../attribution.js';
15
15
  import { hasFocusableDescendant, hasTabbableDescendant, isHidden, isNativelyFocusable, isTabbable, tabSequence, visualOrderJumps, } from './tab-sequence.js';
16
16
  import { findUncontainedModals } from './focus-trap.js';
17
17
  import { blockingModalDialog } from '../top-layer.js';
@@ -35,6 +35,14 @@ const INTERACTIVE_ROLES = new Set([
35
35
  'treeitem',
36
36
  ]);
37
37
  const KEY_EVENTS = ['keydown', 'keyup', 'keypress'];
38
+ /**
39
+ * True when `events` includes a key event. Angular reports a listener by the name
40
+ * in the template, key modifiers and all (`(keydown.enter)` → `keydown.enter`),
41
+ * so match on the event before the first dot.
42
+ */
43
+ function hasKeyEvent(events) {
44
+ return events.some((e) => KEY_EVENTS.includes(e.split('.')[0]));
45
+ }
38
46
  /**
39
47
  * Composite widgets: Tab reaches the widget once, and arrow keys move between
40
48
  * its items (WAI-ARIA APG "Keyboard navigation inside components").
@@ -43,17 +51,22 @@ const COMPOSITE_SELECTOR = ['tablist', 'toolbar', 'menu', 'menubar', 'radiogroup
43
51
  .map((role) => `[role="${role}"]`)
44
52
  .join(',');
45
53
  /**
46
- * True when `element` is an item that its composite widget reaches with arrow
47
- * keys, so Tab skipping it is correct:
48
- * - **aria-activedescendant**: focus stays on the widget (or on a combobox that
49
- * controls it) and points at the item; or
50
- * - **roving tabindex**: the item has a tabindex (usually -1), so script can
51
- * focus it, and Tab can get into the widget: one of its items is tabbable,
52
- * the widget itself is, or another element controls it (a menu or listbox
53
- * popup, which gets focus when it opens).
54
- * An item with neither, or a widget Tab can't enter at all, is still reported.
54
+ * How a composite widget's item is reached, when Tab skipping it may be correct:
55
+ * - `'reached'`: the keyboard reaches it within the widget —
56
+ * **aria-activedescendant** (focus stays on the widget, or on a combobox that
57
+ * controls it, and points at the item), or **roving tabindex** (the item has a
58
+ * tabindex, usually -1, so script can focus it; Tab can get into the widget —
59
+ * one of its items is tabbable, the widget itself is, or another element
60
+ * controls it, like a menu button — and something handles keys: see
61
+ * {@link handlesKeys}).
62
+ * - `'no-keys'`: set up for roving tabindex, but no key handling was found, so
63
+ * arrow keys may not move to it — the classic half-built tab list.
64
+ * - `false`: not a composite item, or one Tab can't get into / script can't focus.
65
+ *
66
+ * Key listeners are read through Angular's dev-mode debug API. Without it (a
67
+ * production build) nothing can be seen, so roving tabindex counts as reached.
55
68
  */
56
- function reachedWithinWidget(element, isVisible) {
69
+ function reachedWithinWidget(element, isVisible, keyCache = new Map()) {
57
70
  const widget = element.parentElement?.closest(COMPOSITE_SELECTOR);
58
71
  if (!widget)
59
72
  return false;
@@ -63,11 +76,53 @@ function reachedWithinWidget(element, isVisible) {
63
76
  .includes(widget.id))
64
77
  : [];
65
78
  if ([widget, ...controllers].some((el) => el.hasAttribute('aria-activedescendant')))
66
- return true;
79
+ return 'reached';
67
80
  const tabindex = element.getAttribute('tabindex');
68
81
  if (tabindex === null || Number.isNaN(Number.parseInt(tabindex, 10)))
69
82
  return false;
70
- return isTabbable(widget, isVisible) || controllers.length > 0 || hasTabbableDescendant(widget, isVisible);
83
+ const enterable = isTabbable(widget, isVisible) || controllers.length > 0 || hasTabbableDescendant(widget, isVisible);
84
+ if (!enterable)
85
+ return false;
86
+ if (!listenersVisible())
87
+ return 'reached';
88
+ // Same widget, same answer: work it out once per scan, not once per item.
89
+ let keys = keyCache.get(widget);
90
+ if (keys === undefined)
91
+ keyCache.set(widget, (keys = handlesKeys(widget, controllers)));
92
+ return keys ? 'reached' : 'no-keys';
93
+ }
94
+ /** True when Angular's debug API can tell us which listeners an element has. */
95
+ function listenersVisible() {
96
+ return typeof ngDebug()?.getListeners === 'function';
97
+ }
98
+ /**
99
+ * True when something that could move focus between `widget`'s items listens
100
+ * for keys: the widget, an element that controls it, an ancestor up to and
101
+ * including the nearest component host (Angular Material, for one, puts a tab
102
+ * list's keydown on a wrapper around the `role="tablist"`), or anything inside
103
+ * the widget. The walk stops at that host so an app shell's shortcut listener
104
+ * doesn't vouch for every widget below it.
105
+ */
106
+ function handlesKeys(widget, controllers) {
107
+ const ng = ngDebug();
108
+ const body = widget.ownerDocument?.body;
109
+ const ancestors = [];
110
+ for (let el = widget.parentElement; el && el !== body; el = el.parentElement) {
111
+ ancestors.push(el);
112
+ if (isComponentHost(el, ng))
113
+ break;
114
+ }
115
+ const keyed = (el) => hasKeyEvent(resolveListenerEvents(el));
116
+ // Cheapest first; the widget's contents (every cell of a grid) last.
117
+ return [widget, ...controllers, ...ancestors].some(keyed) || [...widget.querySelectorAll('*')].some(keyed);
118
+ }
119
+ function isComponentHost(el, ng) {
120
+ try {
121
+ return ng?.getComponent(el) != null;
122
+ }
123
+ catch {
124
+ return false;
125
+ }
71
126
  }
72
127
  const ITEM_SELECTOR = [...INTERACTIVE_ROLES].map((role) => `[role="${role}"]`).join(',');
73
128
  /**
@@ -80,16 +135,17 @@ const ITEM_SELECTOR = [...INTERACTIVE_ROLES].map((role) => `[role="${role}"]`).j
80
135
  * keys. `<body>` is never tabbable, so a page-wide shortcut listener doesn't
81
136
  * count.
82
137
  */
83
- function partOfKeyboardOperatedWidget(element, isVisible) {
138
+ function partOfKeyboardOperatedWidget(element, isVisible, keyCache) {
84
139
  const item = element.parentElement?.closest(ITEM_SELECTOR);
85
140
  if (item?.parentElement?.closest(COMPOSITE_SELECTOR)) {
86
- if (isTabbable(item, isVisible) || reachedWithinWidget(item, isVisible))
141
+ // 'no-keys' too: the item itself is reported, so its parts needn't be.
142
+ if (isTabbable(item, isVisible) || reachedWithinWidget(item, isVisible, keyCache))
87
143
  return true;
88
144
  }
89
145
  for (let el = element.parentElement; el; el = el.parentElement) {
90
146
  if (!isTabbable(el, isVisible))
91
147
  continue;
92
- return resolveListenerEvents(el).some((e) => KEY_EVENTS.includes(e));
148
+ return hasKeyEvent(resolveListenerEvents(el));
93
149
  }
94
150
  return false;
95
151
  }
@@ -125,6 +181,7 @@ export function scanKeyboard(root = document, options = {}) {
125
181
  const prefixes = options.frameworkPrefixes ?? DEFAULT_FRAMEWORK_PREFIXES;
126
182
  const isVisible = options.isVisible;
127
183
  const findings = [];
184
+ const keyCache = new Map();
128
185
  const make = (element, id, impact, help, helpUrl) => {
129
186
  const componentPath = resolveComponentPath(element);
130
187
  return {
@@ -165,12 +222,22 @@ export function scanKeyboard(root = document, options = {}) {
165
222
  // it isn't shown. Check it when it is.
166
223
  if (isHidden(element, isVisible))
167
224
  continue;
168
- if (!interactiveByRole && partOfKeyboardOperatedWidget(element, isVisible))
169
- continue;
170
225
  const focusable = isTabbable(element, isVisible);
171
226
  if (!focusable) {
172
- if (interactiveByRole && reachedWithinWidget(element, isVisible))
227
+ // A click-only part of something the keyboard operates is a mouse shortcut.
228
+ // Only for unreachable elements: one Tab reaches is still checked below.
229
+ if (!interactiveByRole && partOfKeyboardOperatedWidget(element, isVisible, keyCache))
230
+ continue;
231
+ const within = interactiveByRole ? reachedWithinWidget(element, isVisible, keyCache) : false;
232
+ if (within === 'reached')
233
+ continue;
234
+ if (within === 'no-keys') {
235
+ findings.push(make(element, 'ngbr/unreachable-control', 'moderate', `Keyboard users may not be able to reach this item: it has role="${role}" and is ` +
236
+ `out of the tab order, like a roving-tabindex item, but nothing in or around its ` +
237
+ `widget handles keys, so arrow keys may not move to it. Handle the arrow keys, or ` +
238
+ `use aria-activedescendant. Heuristic — verify manually.`, `${RULE_DOCS}/unreachable-control`));
173
239
  continue;
240
+ }
174
241
  const reason = interactiveByRole ? `has role="${role}"` : 'has a click handler';
175
242
  findings.push(make(element, 'ngbr/unreachable-control', 'serious', `Keyboard users can't reach this control: it ${reason}, but it isn't a native ` +
176
243
  `control and has no tabindex="0", so Tab skips it. Use a <button> or <a href>, ` +
@@ -179,7 +246,7 @@ export function scanKeyboard(root = document, options = {}) {
179
246
  }
180
247
  // Focusable, but a bare (click) never fires on Enter/Space the way a native
181
248
  // button does — so a keyboard user can reach it and still not activate it.
182
- const hasKey = events.some((e) => KEY_EVENTS.includes(e));
249
+ const hasKey = hasKeyEvent(events);
183
250
  if (hasClick && !hasKey) {
184
251
  findings.push(make(element, 'ngbr/click-without-key', 'moderate', `Keyboard users may not be able to activate this: Tab reaches it, but it only ` +
185
252
  `listens for click, and Enter and Space don't fire click on a non-native element. ` +
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ngbracket/a11y-devtools",
3
- "version": "0.15.3",
3
+ "version": "0.15.4",
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",