@ngbracket/a11y-devtools 0.15.1 → 0.15.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,22 @@
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.2
7
+
8
+ ### Fixed
9
+
10
+ - **`ngbr/unreachable-control` no longer flags items that arrow keys reach.**
11
+ In a composite widget (a tab list, toolbar, menu, menubar, radio group,
12
+ listbox, tree, treegrid or grid), Tab reaches the widget once and arrow keys
13
+ move between its items, so the other items are correctly out of the tab
14
+ order. They were reported as serious. An item is now skipped when the widget
15
+ (or a combobox that controls it) uses `aria-activedescendant`, or when the
16
+ item has a `tabindex` and Tab can get into the widget: one item is tabbable,
17
+ the widget is, or another element controls it (a menu button). A widget Tab
18
+ can't enter at all, and an item script can't focus, are still reported.
19
+ Found on our own docs: 9 false positives across the listbox, tree and
20
+ selectable-list pages.
21
+
6
22
  ## 0.15.1
7
23
 
8
24
  ### 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,19 @@
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
+ }
34834
34850
  var RULE_DOCS = "https://ngbracket.com/tools/a11y-devtools/docs";
34835
34851
  function shortSelector(element) {
34836
34852
  const tag = element.tagName.toLowerCase();
@@ -34880,6 +34896,8 @@
34880
34896
  continue;
34881
34897
  const focusable = isTabbable(element, isVisible);
34882
34898
  if (!focusable) {
34899
+ if (interactiveByRole && reachedWithinWidget(element, isVisible))
34900
+ continue;
34883
34901
  const reason = interactiveByRole ? `has role="${role}"` : "has a click handler";
34884
34902
  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
34903
  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,40 @@ 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
+ }
38
72
  /**
39
73
  * Each rule's page: what the message means, who it affects, exactly what
40
74
  * triggers it, how to fix and check it, and the WCAG/APG reference.
@@ -109,6 +143,8 @@ export function scanKeyboard(root = document, options = {}) {
109
143
  continue;
110
144
  const focusable = isTabbable(element, isVisible);
111
145
  if (!focusable) {
146
+ if (interactiveByRole && reachedWithinWidget(element, isVisible))
147
+ continue;
112
148
  const reason = interactiveByRole ? `has role="${role}"` : 'has a click handler';
113
149
  findings.push(make(element, 'ngbr/unreachable-control', 'serious', `Keyboard users can't reach this control: it ${reason}, but it isn't a native ` +
114
150
  `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.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",