@ngbracket/a11y-devtools 0.15.1 → 0.15.3

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,36 @@
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.3
7
+
8
+ ### Fixed
9
+
10
+ - **`ngbr/unreachable-control` no longer flags mouse shortcuts inside a
11
+ keyboard-operated widget.** A click-only element with no role of its own is
12
+ skipped when it's part of something the keyboard already operates: a
13
+ composite-widget item Tab or arrow keys reach (a tree's expand/collapse arrow
14
+ inside its treeitem, which ←/→ expand), or the nearest Tab-reachable ancestor
15
+ that handles keys itself (the bars, slices and points of a focusable chart
16
+ that moves between them with arrow keys). A page-wide key listener doesn't
17
+ count, and a click target inside a focusable ancestor with no key handling is
18
+ still reported. Found on our own docs (dev build): 83 false positives → 0.
19
+
20
+ ## 0.15.2
21
+
22
+ ### Fixed
23
+
24
+ - **`ngbr/unreachable-control` no longer flags items that arrow keys reach.**
25
+ In a composite widget (a tab list, toolbar, menu, menubar, radio group,
26
+ listbox, tree, treegrid or grid), Tab reaches the widget once and arrow keys
27
+ move between its items, so the other items are correctly out of the tab
28
+ order. They were reported as serious. An item is now skipped when the widget
29
+ (or a combobox that controls it) uses `aria-activedescendant`, or when the
30
+ item has a `tabindex` and Tab can get into the widget: one item is tabbable,
31
+ the widget is, or another element controls it (a menu button). A widget Tab
32
+ can't enter at all, and an item script can't focus, are still reported.
33
+ Found on our own docs: 9 false positives across the listbox, tree and
34
+ selectable-list pages.
35
+
6
36
  ## 0.15.1
7
37
 
8
38
  ### Fixed
@@ -34653,6 +34653,9 @@
34653
34653
  function hasFocusableDescendant(element) {
34654
34654
  return element.querySelector(FOCUSABLE_SELECTOR) !== null;
34655
34655
  }
34656
+ function hasTabbableDescendant(element, isVisible = defaultIsVisible) {
34657
+ return [...element.querySelectorAll(FOCUSABLE_SELECTOR)].some((el) => isTabbable(el, isVisible));
34658
+ }
34656
34659
  function nativeTabIndex(element) {
34657
34660
  return isNativelyFocusable(element) ? 0 : -1;
34658
34661
  }
@@ -34831,6 +34834,33 @@
34831
34834
  "treeitem"
34832
34835
  ]);
34833
34836
  var KEY_EVENTS = ["keydown", "keyup", "keypress"];
34837
+ var COMPOSITE_SELECTOR = ["tablist", "toolbar", "menu", "menubar", "radiogroup", "listbox", "tree", "treegrid", "grid"].map((role) => `[role="${role}"]`).join(",");
34838
+ function reachedWithinWidget(element, isVisible) {
34839
+ const widget = element.parentElement?.closest(COMPOSITE_SELECTOR);
34840
+ if (!widget)
34841
+ return false;
34842
+ 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
+ if ([widget, ...controllers].some((el) => el.hasAttribute("aria-activedescendant")))
34844
+ return true;
34845
+ const tabindex = element.getAttribute("tabindex");
34846
+ if (tabindex === null || Number.isNaN(Number.parseInt(tabindex, 10)))
34847
+ return false;
34848
+ return isTabbable(widget, isVisible) || controllers.length > 0 || hasTabbableDescendant(widget, isVisible);
34849
+ }
34850
+ var ITEM_SELECTOR = [...INTERACTIVE_ROLES].map((role) => `[role="${role}"]`).join(",");
34851
+ function partOfKeyboardOperatedWidget(element, isVisible) {
34852
+ const item = element.parentElement?.closest(ITEM_SELECTOR);
34853
+ if (item?.parentElement?.closest(COMPOSITE_SELECTOR)) {
34854
+ if (isTabbable(item, isVisible) || reachedWithinWidget(item, isVisible))
34855
+ return true;
34856
+ }
34857
+ for (let el = element.parentElement; el; el = el.parentElement) {
34858
+ if (!isTabbable(el, isVisible))
34859
+ continue;
34860
+ return resolveListenerEvents(el).some((e) => KEY_EVENTS.includes(e));
34861
+ }
34862
+ return false;
34863
+ }
34834
34864
  var RULE_DOCS = "https://ngbracket.com/tools/a11y-devtools/docs";
34835
34865
  function shortSelector(element) {
34836
34866
  const tag = element.tagName.toLowerCase();
@@ -34878,8 +34908,12 @@
34878
34908
  continue;
34879
34909
  if (isHidden(element, isVisible))
34880
34910
  continue;
34911
+ if (!interactiveByRole && partOfKeyboardOperatedWidget(element, isVisible))
34912
+ continue;
34881
34913
  const focusable = isTabbable(element, isVisible);
34882
34914
  if (!focusable) {
34915
+ if (interactiveByRole && reachedWithinWidget(element, isVisible))
34916
+ continue;
34883
34917
  const reason = interactiveByRole ? `has role="${role}"` : "has a click handler";
34884
34918
  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`));
34885
34919
  continue;
@@ -12,7 +12,7 @@
12
12
  * confirmed failures.
13
13
  */
14
14
  import { appComponentFromPath, DEFAULT_FRAMEWORK_PREFIXES, resolveComponentPath, resolveDirectiveNames, resolveListenerEvents, } from '../attribution.js';
15
- import { hasFocusableDescendant, isHidden, isNativelyFocusable, isTabbable, tabSequence, visualOrderJumps, } from './tab-sequence.js';
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';
18
18
  /** WAI-ARIA roles that make a non-native element behave as an interactive control. */
@@ -35,6 +35,64 @@ const INTERACTIVE_ROLES = new Set([
35
35
  'treeitem',
36
36
  ]);
37
37
  const KEY_EVENTS = ['keydown', 'keyup', 'keypress'];
38
+ /**
39
+ * Composite widgets: Tab reaches the widget once, and arrow keys move between
40
+ * its items (WAI-ARIA APG "Keyboard navigation inside components").
41
+ */
42
+ const COMPOSITE_SELECTOR = ['tablist', 'toolbar', 'menu', 'menubar', 'radiogroup', 'listbox', 'tree', 'treegrid', 'grid']
43
+ .map((role) => `[role="${role}"]`)
44
+ .join(',');
45
+ /**
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.
55
+ */
56
+ function reachedWithinWidget(element, isVisible) {
57
+ const widget = element.parentElement?.closest(COMPOSITE_SELECTOR);
58
+ if (!widget)
59
+ return false;
60
+ const controllers = widget.id
61
+ ? [...(widget.ownerDocument ?? document).querySelectorAll('[aria-controls], [aria-owns]')].filter((el) => `${el.getAttribute('aria-controls') ?? ''} ${el.getAttribute('aria-owns') ?? ''}`
62
+ .split(/\s+/)
63
+ .includes(widget.id))
64
+ : [];
65
+ if ([widget, ...controllers].some((el) => el.hasAttribute('aria-activedescendant')))
66
+ return true;
67
+ const tabindex = element.getAttribute('tabindex');
68
+ if (tabindex === null || Number.isNaN(Number.parseInt(tabindex, 10)))
69
+ return false;
70
+ return isTabbable(widget, isVisible) || controllers.length > 0 || hasTabbableDescendant(widget, isVisible);
71
+ }
72
+ const ITEM_SELECTOR = [...INTERACTIVE_ROLES].map((role) => `[role="${role}"]`).join(',');
73
+ /**
74
+ * True when `element` is a click-only part of something the keyboard already
75
+ * operates, so the click is a mouse shortcut, not a control of its own:
76
+ * - a composite-widget item that the keyboard can reach, such as a tree's
77
+ * expand/collapse arrow inside its treeitem (←/→ on the treeitem); or
78
+ * - the nearest Tab-reachable ancestor that handles keys itself, such as a
79
+ * chart's bars inside a focusable chart that moves between them with arrow
80
+ * keys. `<body>` is never tabbable, so a page-wide shortcut listener doesn't
81
+ * count.
82
+ */
83
+ function partOfKeyboardOperatedWidget(element, isVisible) {
84
+ const item = element.parentElement?.closest(ITEM_SELECTOR);
85
+ if (item?.parentElement?.closest(COMPOSITE_SELECTOR)) {
86
+ if (isTabbable(item, isVisible) || reachedWithinWidget(item, isVisible))
87
+ return true;
88
+ }
89
+ for (let el = element.parentElement; el; el = el.parentElement) {
90
+ if (!isTabbable(el, isVisible))
91
+ continue;
92
+ return resolveListenerEvents(el).some((e) => KEY_EVENTS.includes(e));
93
+ }
94
+ return false;
95
+ }
38
96
  /**
39
97
  * Each rule's page: what the message means, who it affects, exactly what
40
98
  * triggers it, how to fix and check it, and the WCAG/APG reference.
@@ -107,8 +165,12 @@ export function scanKeyboard(root = document, options = {}) {
107
165
  // it isn't shown. Check it when it is.
108
166
  if (isHidden(element, isVisible))
109
167
  continue;
168
+ if (!interactiveByRole && partOfKeyboardOperatedWidget(element, isVisible))
169
+ continue;
110
170
  const focusable = isTabbable(element, isVisible);
111
171
  if (!focusable) {
172
+ if (interactiveByRole && reachedWithinWidget(element, isVisible))
173
+ continue;
112
174
  const reason = interactiveByRole ? `has role="${role}"` : 'has a click handler';
113
175
  findings.push(make(element, 'ngbr/unreachable-control', 'serious', `Keyboard users can't reach this control: it ${reason}, but it isn't a native ` +
114
176
  `control and has no tabindex="0", so Tab skips it. Use a <button> or <a href>, ` +
@@ -45,6 +45,8 @@ export declare function isFocusSentinel(el: Element): boolean;
45
45
  * event delegation — handling clicks on the controls inside — not a fake button.
46
46
  */
47
47
  export declare function hasFocusableDescendant(element: Element): boolean;
48
+ /** True when something inside `element` is reachable with Tab. */
49
+ export declare function hasTabbableDescendant(element: Element, isVisible?: (el: Element) => boolean): boolean;
48
50
  /**
49
51
  * True when the element takes focus without an explicit `tabindex` — a link with
50
52
  * an href, a form control, a `<summary>`, an editing host, and the embedded
@@ -71,6 +71,10 @@ const FOCUSABLE_SELECTOR = [
71
71
  export function hasFocusableDescendant(element) {
72
72
  return element.querySelector(FOCUSABLE_SELECTOR) !== null;
73
73
  }
74
+ /** True when something inside `element` is reachable with Tab. */
75
+ export function hasTabbableDescendant(element, isVisible = defaultIsVisible) {
76
+ return [...element.querySelectorAll(FOCUSABLE_SELECTOR)].some((el) => isTabbable(el, isVisible));
77
+ }
74
78
  /** A native default of 0 (focusable without a tabindex attribute), else -1. */
75
79
  function nativeTabIndex(element) {
76
80
  return isNativelyFocusable(element) ? 0 : -1;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ngbracket/a11y-devtools",
3
- "version": "0.15.1",
3
+ "version": "0.15.3",
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",