|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +const { isNativeElement } = require('../utils/is-native-element'); |
| 4 | +const { getStaticAttrValue } = require('../utils/static-attr-value'); |
| 5 | + |
| 6 | +function findAttr(node, name) { |
| 7 | + return node.attributes?.find((a) => a.name === name); |
| 8 | +} |
| 9 | + |
| 10 | +// Returns the statically-known string value of a named attribute, or |
| 11 | +// `undefined` when the attribute is absent or its value is dynamic. |
| 12 | +function getTextAttrValue(node, name) { |
| 13 | + const attr = findAttr(node, name); |
| 14 | + if (!attr) { |
| 15 | + return undefined; |
| 16 | + } |
| 17 | + return getStaticAttrValue(attr.value); |
| 18 | +} |
| 19 | + |
| 20 | +// Per WAI-ARIA 1.2 §6.6 + aria-hidden value table, a missing or empty-string |
| 21 | +// aria-hidden resolves to the default `undefined` — NOT `true`. So only an |
| 22 | +// explicit `"true"` (ASCII case-insensitive per HTML enumerated-attribute |
| 23 | +// rules) hides the element. Mustache boolean-literal `{{true}}` and |
| 24 | +// string-literal `{{"true"}}` also qualify. |
| 25 | +function isAriaHiddenTrue(node) { |
| 26 | + const value = findAttr(node, 'aria-hidden')?.value; |
| 27 | + if (!value) { |
| 28 | + return false; |
| 29 | + } |
| 30 | + if (value.type === 'GlimmerTextNode') { |
| 31 | + return value.chars.trim().toLowerCase() === 'true'; |
| 32 | + } |
| 33 | + if (value.type === 'GlimmerMustacheStatement' && value.path) { |
| 34 | + if (value.path.type === 'GlimmerBooleanLiteral') { |
| 35 | + return value.path.value === true; |
| 36 | + } |
| 37 | + if (value.path.type === 'GlimmerStringLiteral') { |
| 38 | + return value.path.value.trim().toLowerCase() === 'true'; |
| 39 | + } |
| 40 | + } |
| 41 | + return false; |
| 42 | +} |
| 43 | + |
| 44 | +// Tags with an unconditional default focusable UI (sequentially focusable per |
| 45 | +// HTML §6.6.3 "focusable area" + widget roles per HTML-AAM). |
| 46 | +// NOTE: <label> is HTML-interactive-content (§3.2.5.2.7) but NOT keyboard- |
| 47 | +// focusable by default — clicks on a label forward to its associated control, |
| 48 | +// but the label itself isn't in the tab order. So it's excluded here even |
| 49 | +// though `isHtmlInteractiveContent` would return true for it. |
| 50 | +const UNCONDITIONAL_FOCUSABLE_TAGS = new Set([ |
| 51 | + 'button', |
| 52 | + 'select', |
| 53 | + 'textarea', |
| 54 | + 'iframe', |
| 55 | + 'embed', |
| 56 | + 'summary', |
| 57 | + 'details', |
| 58 | + 'option', |
| 59 | + 'datalist', |
| 60 | +]); |
| 61 | + |
| 62 | +// Form-control tags whose `disabled` attribute removes them from the tab order |
| 63 | +// (HTML §4.10.18.5 "disabled" + HTML §6.6.3 "focusable area"). |
| 64 | +const DISABLEABLE_TAGS = new Set(['button', 'input', 'select', 'textarea', 'fieldset']); |
| 65 | + |
| 66 | +function isDisabledFormControl(node, tag) { |
| 67 | + if (!DISABLEABLE_TAGS.has(tag)) { |
| 68 | + return false; |
| 69 | + } |
| 70 | + return Boolean(findAttr(node, 'disabled')); |
| 71 | +} |
| 72 | + |
| 73 | +// Narrow rule-local "keyboard-focusable" check. Intentionally distinct from |
| 74 | +// `isHtmlInteractiveContent` (HTML content-model) — we want the sequential- |
| 75 | +// focus + programmatic-focus axis only. See WAI-ARIA "focusable" definition |
| 76 | +// and HTML §6.6.3. |
| 77 | +function isKeyboardFocusable(node, getTextAttrValueFn) { |
| 78 | + const rawTag = node?.tag; |
| 79 | + if (typeof rawTag !== 'string' || rawTag.length === 0) { |
| 80 | + return false; |
| 81 | + } |
| 82 | + const tag = rawTag.toLowerCase(); |
| 83 | + |
| 84 | + // Disabled form controls are not focusable. |
| 85 | + if (isDisabledFormControl(node, tag)) { |
| 86 | + return false; |
| 87 | + } |
| 88 | + |
| 89 | + // Any tabindex (including "-1") makes the element at least programmatically |
| 90 | + // focusable — still a keyboard-trap risk under aria-hidden. |
| 91 | + if (findAttr(node, 'tabindex')) { |
| 92 | + return true; |
| 93 | + } |
| 94 | + |
| 95 | + // contenteditable (truthy) makes the element focusable. |
| 96 | + const contentEditable = getTextAttrValueFn(node, 'contenteditable'); |
| 97 | + if (contentEditable !== undefined && contentEditable !== null) { |
| 98 | + const normalized = contentEditable.trim().toLowerCase(); |
| 99 | + // per HTML spec, "", "true", and "plaintext-only" all enable editing. |
| 100 | + if (normalized === '' || normalized === 'true' || normalized === 'plaintext-only') { |
| 101 | + return true; |
| 102 | + } |
| 103 | + } |
| 104 | + |
| 105 | + if (UNCONDITIONAL_FOCUSABLE_TAGS.has(tag)) { |
| 106 | + return true; |
| 107 | + } |
| 108 | + |
| 109 | + if (tag === 'input') { |
| 110 | + const type = getTextAttrValueFn(node, 'type'); |
| 111 | + return type === undefined || type === null || type.trim().toLowerCase() !== 'hidden'; |
| 112 | + } |
| 113 | + |
| 114 | + if (tag === 'a' || tag === 'area') { |
| 115 | + return Boolean(findAttr(node, 'href')); |
| 116 | + } |
| 117 | + |
| 118 | + if (tag === 'img') { |
| 119 | + return Boolean(findAttr(node, 'usemap')); |
| 120 | + } |
| 121 | + |
| 122 | + if (tag === 'audio' || tag === 'video') { |
| 123 | + return Boolean(findAttr(node, 'controls')); |
| 124 | + } |
| 125 | + |
| 126 | + return false; |
| 127 | +} |
| 128 | + |
| 129 | +// A focusable descendant of an aria-hidden="true" ancestor can still receive |
| 130 | +// focus (aria-hidden does not remove elements from the tab order), so the |
| 131 | +// ancestor hides AT-visible content that remains keyboard-reachable — a |
| 132 | +// keyboard trap. This rule targets the anti-pattern flagged by axe's |
| 133 | +// `aria-hidden-focus` check and by jsx-a11y's `no-aria-hidden-on-focusable`. |
| 134 | +// WAI-ARIA 1.2 says authors SHOULD NOT put aria-hidden on focusable content |
| 135 | +// (the spec normatively warns against this in the aria-hidden authoring note). |
| 136 | +function hasFocusableDescendant(node, sourceCode) { |
| 137 | + const children = node.children; |
| 138 | + if (!children || children.length === 0) { |
| 139 | + return false; |
| 140 | + } |
| 141 | + for (const child of children) { |
| 142 | + if (child.type !== 'GlimmerElementNode') { |
| 143 | + // Skip TextNode, GlimmerMustacheStatement (dynamic content), yield |
| 144 | + // expressions, and anything else whose rendered element we can't inspect. |
| 145 | + continue; |
| 146 | + } |
| 147 | + if (!isNativeElement(child, sourceCode)) { |
| 148 | + // Component / dynamic / shadowed tag — opaque. Don't recurse. |
| 149 | + continue; |
| 150 | + } |
| 151 | + if (isKeyboardFocusable(child, getTextAttrValue)) { |
| 152 | + return true; |
| 153 | + } |
| 154 | + if (hasFocusableDescendant(child, sourceCode)) { |
| 155 | + return true; |
| 156 | + } |
| 157 | + } |
| 158 | + return false; |
| 159 | +} |
| 160 | + |
| 161 | +/** @type {import('eslint').Rule.RuleModule} */ |
| 162 | +module.exports = { |
| 163 | + meta: { |
| 164 | + type: 'problem', |
| 165 | + docs: { |
| 166 | + description: 'disallow aria-hidden="true" on focusable elements', |
| 167 | + category: 'Accessibility', |
| 168 | + url: 'https://github.com/ember-cli/eslint-plugin-ember/tree/master/docs/rules/template-no-aria-hidden-on-focusable.md', |
| 169 | + templateMode: 'both', |
| 170 | + }, |
| 171 | + fixable: null, |
| 172 | + schema: [], |
| 173 | + messages: { |
| 174 | + noAriaHiddenOnFocusable: |
| 175 | + 'aria-hidden="true" must not be set on focusable elements — it creates a keyboard trap (element reachable via Tab but hidden from assistive tech).', |
| 176 | + noAriaHiddenOnAncestorOfFocusable: |
| 177 | + 'aria-hidden="true" must not be set on an element that contains focusable descendants — the descendants remain keyboard-reachable but are hidden from assistive tech.', |
| 178 | + }, |
| 179 | + }, |
| 180 | + |
| 181 | + create(context) { |
| 182 | + const sourceCode = context.sourceCode ?? context.getSourceCode(); |
| 183 | + return { |
| 184 | + GlimmerElementNode(node) { |
| 185 | + if (!isAriaHiddenTrue(node)) { |
| 186 | + return; |
| 187 | + } |
| 188 | + if (!isNativeElement(node, sourceCode)) { |
| 189 | + return; |
| 190 | + } |
| 191 | + if (isKeyboardFocusable(node, getTextAttrValue)) { |
| 192 | + context.report({ node, messageId: 'noAriaHiddenOnFocusable' }); |
| 193 | + return; |
| 194 | + } |
| 195 | + if (hasFocusableDescendant(node, sourceCode)) { |
| 196 | + context.report({ node, messageId: 'noAriaHiddenOnAncestorOfFocusable' }); |
| 197 | + } |
| 198 | + }, |
| 199 | + }; |
| 200 | + }, |
| 201 | +}; |
0 commit comments