@ngbracket/a11y-devtools 0.15.2 → 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,46 @@
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
+
32
+ ## 0.15.3
33
+
34
+ ### Fixed
35
+
36
+ - **`ngbr/unreachable-control` no longer flags mouse shortcuts inside a
37
+ keyboard-operated widget.** A click-only element with no role of its own is
38
+ skipped when it's part of something the keyboard already operates: a
39
+ composite-widget item Tab or arrow keys reach (a tree's expand/collapse arrow
40
+ inside its treeitem, which ←/→ expand), or the nearest Tab-reachable ancestor
41
+ that handles keys itself (the bars, slices and points of a focusable chart
42
+ that moves between them with arrow keys). A page-wide key listener doesn't
43
+ count, and a click target inside a focusable ancestor with no key handling is
44
+ still reported. Found on our own docs (dev build): 83 false positives → 0.
45
+
6
46
  ## 0.15.2
7
47
 
8
48
  ### 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,18 +34834,65 @@
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
+ }
34882
+ }
34883
+ var ITEM_SELECTOR = [...INTERACTIVE_ROLES].map((role) => `[role="${role}"]`).join(",");
34884
+ function partOfKeyboardOperatedWidget(element, isVisible, keyCache) {
34885
+ const item = element.parentElement?.closest(ITEM_SELECTOR);
34886
+ if (item?.parentElement?.closest(COMPOSITE_SELECTOR)) {
34887
+ if (isTabbable(item, isVisible) || reachedWithinWidget(item, isVisible, keyCache))
34888
+ return true;
34889
+ }
34890
+ for (let el = element.parentElement; el; el = el.parentElement) {
34891
+ if (!isTabbable(el, isVisible))
34892
+ continue;
34893
+ return hasKeyEvent(resolveListenerEvents(el));
34894
+ }
34895
+ return false;
34849
34896
  }
34850
34897
  var RULE_DOCS = "https://ngbracket.com/tools/a11y-devtools/docs";
34851
34898
  function shortSelector(element) {
@@ -34863,6 +34910,7 @@
34863
34910
  const prefixes = options.frameworkPrefixes ?? DEFAULT_FRAMEWORK_PREFIXES;
34864
34911
  const isVisible = options.isVisible;
34865
34912
  const findings = [];
34913
+ const keyCache = /* @__PURE__ */ new Map();
34866
34914
  const make = (element, id, impact, help, helpUrl) => {
34867
34915
  const componentPath = resolveComponentPath(element);
34868
34916
  return {
@@ -34896,13 +34944,20 @@
34896
34944
  continue;
34897
34945
  const focusable = isTabbable(element, isVisible);
34898
34946
  if (!focusable) {
34899
- 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")
34900
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
+ }
34901
34956
  const reason = interactiveByRole ? `has role="${role}"` : "has a click handler";
34902
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`));
34903
34958
  continue;
34904
34959
  }
34905
- const hasKey = events.some((e) => KEY_EVENTS.includes(e));
34960
+ const hasKey = hasKeyEvent(events);
34906
34961
  if (hasClick && !hasKey) {
34907
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`));
34908
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,78 @@ 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
+ }
126
+ }
127
+ const ITEM_SELECTOR = [...INTERACTIVE_ROLES].map((role) => `[role="${role}"]`).join(',');
128
+ /**
129
+ * True when `element` is a click-only part of something the keyboard already
130
+ * operates, so the click is a mouse shortcut, not a control of its own:
131
+ * - a composite-widget item that the keyboard can reach, such as a tree's
132
+ * expand/collapse arrow inside its treeitem (←/→ on the treeitem); or
133
+ * - the nearest Tab-reachable ancestor that handles keys itself, such as a
134
+ * chart's bars inside a focusable chart that moves between them with arrow
135
+ * keys. `<body>` is never tabbable, so a page-wide shortcut listener doesn't
136
+ * count.
137
+ */
138
+ function partOfKeyboardOperatedWidget(element, isVisible, keyCache) {
139
+ const item = element.parentElement?.closest(ITEM_SELECTOR);
140
+ if (item?.parentElement?.closest(COMPOSITE_SELECTOR)) {
141
+ // 'no-keys' too: the item itself is reported, so its parts needn't be.
142
+ if (isTabbable(item, isVisible) || reachedWithinWidget(item, isVisible, keyCache))
143
+ return true;
144
+ }
145
+ for (let el = element.parentElement; el; el = el.parentElement) {
146
+ if (!isTabbable(el, isVisible))
147
+ continue;
148
+ return hasKeyEvent(resolveListenerEvents(el));
149
+ }
150
+ return false;
71
151
  }
72
152
  /**
73
153
  * Each rule's page: what the message means, who it affects, exactly what
@@ -101,6 +181,7 @@ export function scanKeyboard(root = document, options = {}) {
101
181
  const prefixes = options.frameworkPrefixes ?? DEFAULT_FRAMEWORK_PREFIXES;
102
182
  const isVisible = options.isVisible;
103
183
  const findings = [];
184
+ const keyCache = new Map();
104
185
  const make = (element, id, impact, help, helpUrl) => {
105
186
  const componentPath = resolveComponentPath(element);
106
187
  return {
@@ -143,8 +224,20 @@ export function scanKeyboard(root = document, options = {}) {
143
224
  continue;
144
225
  const focusable = isTabbable(element, isVisible);
145
226
  if (!focusable) {
146
- 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`));
147
239
  continue;
240
+ }
148
241
  const reason = interactiveByRole ? `has role="${role}"` : 'has a click handler';
149
242
  findings.push(make(element, 'ngbr/unreachable-control', 'serious', `Keyboard users can't reach this control: it ${reason}, but it isn't a native ` +
150
243
  `control and has no tabindex="0", so Tab skips it. Use a <button> or <a href>, ` +
@@ -153,7 +246,7 @@ export function scanKeyboard(root = document, options = {}) {
153
246
  }
154
247
  // Focusable, but a bare (click) never fires on Enter/Space the way a native
155
248
  // button does — so a keyboard user can reach it and still not activate it.
156
- const hasKey = events.some((e) => KEY_EVENTS.includes(e));
249
+ const hasKey = hasKeyEvent(events);
157
250
  if (hasClick && !hasKey) {
158
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 ` +
159
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.2",
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",