|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +/** |
| 4 | + * Native-interactive HTML element classification, shared across rules that need to |
| 5 | + * ask "does this HTML tag natively expose interactive UI to keyboard / AT users?". |
| 6 | + * |
| 7 | + * The set is hand-curated rather than derived from a single authority because |
| 8 | + * aria-query, axobject-query, HTML-AAM, WAI-ARIA, and browser reality disagree on |
| 9 | + * several rows. Decision rationale is documented per-tag: |
| 10 | + * |
| 11 | + * | Element | Behavior | Rationale | |
| 12 | + * |-------------------------------------------------|----------------------|-----------| |
| 13 | + * | button, select, textarea, iframe, embed, | Interactive | aria-query/axobject-query widget + universally-accepted | |
| 14 | + * | summary, details | | | |
| 15 | + * | input (except type=hidden) | Interactive | Same as above, minus hidden | |
| 16 | + * | option, datalist | Interactive | aria-query roles option/listbox; axobject widget; HTML-AAM | |
| 17 | + * | a[href], area[href] | Interactive (cond.) | HTML-AAM: anchor interactivity requires href | |
| 18 | + * | audio[controls], video[controls] | Interactive | Browsers only render focusable UI with `controls` | |
| 19 | + * | audio, video (no controls) | NOT interactive | No keyboard semantics without controls; browsers agree | |
| 20 | + * | object | Interactive | axobject-query EmbeddedObjectRole | |
| 21 | + * | canvas | Interactive | axobject-query CanvasRole widget; bias toward no-FP | |
| 22 | + * | input[type=hidden] | NOT interactive | HTML spec: no UI, no focus, no AT exposure | |
| 23 | + * | menuitem | NOT interactive | Deprecated; no longer rendered in Chrome/Edge/Safari/FF | |
| 24 | + * | label | NOT interactive | axobject-query LabelRole is structure, not widget | |
| 25 | + */ |
| 26 | + |
| 27 | +// Unconditionally-interactive HTML tags (no attribute dependencies). |
| 28 | +const UNCONDITIONAL_INTERACTIVE_TAGS = new Set([ |
| 29 | + 'button', |
| 30 | + 'select', |
| 31 | + 'textarea', |
| 32 | + 'iframe', |
| 33 | + 'embed', |
| 34 | + 'summary', |
| 35 | + 'details', |
| 36 | + 'option', |
| 37 | + 'datalist', |
| 38 | + 'object', |
| 39 | + 'canvas', |
| 40 | +]); |
| 41 | + |
| 42 | +/** |
| 43 | + * Determine whether a Glimmer element node represents a natively-interactive |
| 44 | + * HTML element. |
| 45 | + * |
| 46 | + * @param {object} node Glimmer `ElementNode` (has a string `tag`). |
| 47 | + * @param {Function} getTextAttrValue Helper (node, attrName) -> string | undefined |
| 48 | + * that returns the text value of a static |
| 49 | + * attribute, or undefined for dynamic / missing. |
| 50 | + * @returns {boolean} True if the element is natively interactive. |
| 51 | + */ |
| 52 | +function isNativeInteractive(node, getTextAttrValue) { |
| 53 | + const rawTag = node && node.tag; |
| 54 | + if (typeof rawTag !== 'string' || rawTag.length === 0) { |
| 55 | + return false; |
| 56 | + } |
| 57 | + const tag = rawTag.toLowerCase(); |
| 58 | + |
| 59 | + // Unconditional interactive tags. |
| 60 | + if (UNCONDITIONAL_INTERACTIVE_TAGS.has(tag)) { |
| 61 | + return true; |
| 62 | + } |
| 63 | + |
| 64 | + // <input> is interactive unless type="hidden" (HTML spec: no UI/focus/AT exposure). |
| 65 | + if (tag === 'input') { |
| 66 | + const type = getTextAttrValue(node, 'type'); |
| 67 | + return type !== 'hidden'; |
| 68 | + } |
| 69 | + |
| 70 | + // <a> and <area> are interactive only when an href is present (HTML-AAM). |
| 71 | + if (tag === 'a' || tag === 'area') { |
| 72 | + return hasAttribute(node, 'href'); |
| 73 | + } |
| 74 | + |
| 75 | + // <audio>/<video> are only interactive when `controls` is present. |
| 76 | + if (tag === 'audio' || tag === 'video') { |
| 77 | + return hasAttribute(node, 'controls'); |
| 78 | + } |
| 79 | + |
| 80 | + return false; |
| 81 | +} |
| 82 | + |
| 83 | +function hasAttribute(node, name) { |
| 84 | + return Boolean(node.attributes && node.attributes.some((a) => a.name === name)); |
| 85 | +} |
| 86 | + |
| 87 | +module.exports = { |
| 88 | + isNativeInteractive, |
| 89 | +}; |
0 commit comments