forked from ember-cli/eslint-plugin-ember
-
Notifications
You must be signed in to change notification settings - Fork 0
feat: add template-click-events-have-key-events #17
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
johanrd
wants to merge
19
commits into
master
Choose a base branch
from
feat/template-click-events-have-key-events
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
9e8e45e
Prepare Release v13.1.4 using 'release-plan'
github-actions[bot] 0cf348b
Merge pull request #2750 from ember-cli/release-preview
NullVoxPopuli 148a2a3
feat: add template-require-input-type — reject invalid input types; o…
johanrd 675427a
Merge pull request #2763 from johanrd/html-validate/template-require-…
NullVoxPopuli 493f154
Update parser
NullVoxPopuli 4ae4688
Merge pull request #2766 from ember-cli/nvp/update-parser
NullVoxPopuli e5baa77
Prepare Release v13.2.0 using 'release-plan'
github-actions[bot] c52084a
Merge pull request #2765 from ember-cli/release-preview
NullVoxPopuli 6a8d7b7
docs: add glimmer-attribute-behavior reference
johanrd 6052bae
docs: cite ember-source 6.12, drop separate Glimmer version
johanrd 8c4c540
docs(glimmer-attribute-behavior): rewrite from verified empirical data
johanrd e0a8d17
fix(docs): use querySelector to satisfy unicorn/prefer-query-selector
johanrd 1b384a7
docs(glimmer-attribute-behavior): add "Reading attribute values in ru…
johanrd bb1ebb3
feat(utils): add glimmer-attr-presence — encodes the verified renderi…
johanrd bfb839d
fix(glimmer-attr-presence): correctness fixes + adopt property-inform…
johanrd a1873f0
fix(html-interactive-content): use classifyAttribute for controls (cr…
johanrd 38cb75c
feat: add template-click-events-have-key-events
johanrd ae35a86
fix(template-click-events-have-key-events): drop misleading 'Visible'…
johanrd 03ddced
test: regression fixtures for controls={{false}}/{{null}} + concat (h…
johanrd File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,22 +1,22 @@ | ||
| { | ||
| "solution": { | ||
| "eslint-plugin-ember": { | ||
| "impact": "patch", | ||
| "oldVersion": "13.1.2", | ||
| "newVersion": "13.1.3", | ||
| "impact": "minor", | ||
| "oldVersion": "13.1.4", | ||
| "newVersion": "13.2.0", | ||
| "tagName": "latest", | ||
| "constraints": [ | ||
| { | ||
| "impact": "patch", | ||
| "reason": "Appears in changelog section :bug: Bug Fix" | ||
| "impact": "minor", | ||
| "reason": "Appears in changelog section :rocket: Enhancement" | ||
| }, | ||
| { | ||
| "impact": "patch", | ||
| "reason": "Appears in changelog section :house: Internal" | ||
| "reason": "Appears in changelog section :bug: Bug Fix" | ||
| } | ||
| ], | ||
| "pkgJSONPath": "./package.json" | ||
| } | ||
| }, | ||
| "description": "## Release (2026-04-25)\n\n* eslint-plugin-ember 13.1.3 (patch)\n\n#### :bug: Bug Fix\n* `eslint-plugin-ember`\n * [#2730](https://github.com/ember-cli/eslint-plugin-ember/pull/2730) BUGFIX: template-require-valid-alt-text — reject empty-string aria-label/labelledby/alt on <input type=image>, <object>, <area> ([@johanrd](https://github.com/johanrd))\n * [#2729](https://github.com/ember-cli/eslint-plugin-ember/pull/2729) BUGFIX: template-no-invalid-role — support DPUB/Graphics-ARIA and role-fallback lists ([@johanrd](https://github.com/johanrd))\n * [#2726](https://github.com/ember-cli/eslint-plugin-ember/pull/2726) BUGFIX: template-no-unsupported-role-attributes — honor aria-query attribute constraints ([@johanrd](https://github.com/johanrd))\n * [#2727](https://github.com/ember-cli/eslint-plugin-ember/pull/2727) BUGFIX: template-no-redundant-role — case-insensitive match + <select>→combobox ([@johanrd](https://github.com/johanrd))\n\n#### :house: Internal\n* `eslint-plugin-ember`\n * [#2746](https://github.com/ember-cli/eslint-plugin-ember/pull/2746) refactor: extract isNativeElement util (fix component-vs-HTML-tag misclassification) ([@johanrd](https://github.com/johanrd))\n\n#### Committers: 1\n- [@johanrd](https://github.com/johanrd)\n" | ||
| "description": "## Release (2026-04-28)\n\n* eslint-plugin-ember 13.2.0 (minor)\n\n#### :rocket: Enhancement\n* `eslint-plugin-ember`\n * [#2763](https://github.com/ember-cli/eslint-plugin-ember/pull/2763) feat: add template-require-input-type ([@johanrd](https://github.com/johanrd))\n\n#### :bug: Bug Fix\n* `eslint-plugin-ember`\n * [#2766](https://github.com/ember-cli/eslint-plugin-ember/pull/2766) Update parser ([@NullVoxPopuli](https://github.com/NullVoxPopuli))\n\n#### Committers: 2\n- [@NullVoxPopuli](https://github.com/NullVoxPopuli)\n- [@johanrd](https://github.com/johanrd)\n" | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,55 @@ | ||
| # ember/template-click-events-have-key-events | ||
|
|
||
| <!-- end auto-generated rule header --> | ||
|
|
||
| Enforce that a clickable non-interactive element has at least one keyboard event listener. | ||
|
|
||
| When a `{{on "click" …}}` modifier is attached to a non-interactive DOM element (e.g. `<div>`, `<span>`, `<a>` without `href`), keyboard-only users can't reach the handler through the keyboard path alone. Adding `{{on "keydown" …}}`, `{{on "keyup" …}}`, or `{{on "keypress" …}}` — along with appropriate `role`/`tabindex` to make the element focusable — restores keyboard parity. | ||
|
|
||
| ## On "normative basis" | ||
|
|
||
| [WCAG 2.1 SC 2.1.1 Keyboard (Level A)](https://www.w3.org/WAI/WCAG21/Understanding/keyboard) requires all functionality to be operable via the keyboard. The click-without-keydown shape is the canonical violation in practice. However, [Understanding 2.1.1](https://www.w3.org/WAI/WCAG21/Understanding/keyboard) explicitly notes that "a custom button only reacting to Enter" still satisfies the SC — so the click+keydown pairing isn't literally spec-mandated. It's a peer-plugin convention (jsx-a11y, vuejs-accessibility, lit-a11y, @angular-eslint/template all use it) as the strongest static-analysis proxy for SC 2.1.1 violations. This rule follows the convention. | ||
|
|
||
| This rule is complementary to [`ember/template-no-invalid-interactive`](./template-no-invalid-interactive.md): | ||
|
|
||
| - `template-no-invalid-interactive` takes a stricter stance: don't use DOM event modifiers on non-interactive elements at all. Steers authors toward native interactive elements. | ||
| - `template-click-events-have-key-events` is permissive: if you _do_ use `{{on "click" …}}` on a non-interactive element, at least pair it with a keyboard listener. | ||
|
|
||
| Enable one, the other, or both depending on your project's stance. | ||
|
|
||
| ## Examples | ||
|
|
||
| This rule **forbids** the following: | ||
|
|
||
| ```gjs | ||
| <template> | ||
| <div {{on "click" this.onClick}}></div> | ||
| <span {{on "click" this.onClick}}>text</span> | ||
| <a {{on "click" this.onClick}}>Not a link (missing href)</a> | ||
| </template> | ||
| ``` | ||
|
|
||
| This rule **allows** the following: | ||
|
|
||
| ```gjs | ||
| <template> | ||
| {{! Interactive elements — keyboard is built in }} | ||
| <button {{on "click" this.onClick}}>Toggle</button> | ||
| <a href="/x" {{on "click" this.track}}>Link</a> | ||
|
|
||
| {{! Non-interactive element with both click and keyboard handlers }} | ||
| <div {{on "click" this.onClick}} {{on "keydown" this.onKey}}></div> | ||
|
|
||
| {{! Hidden from assistive tech }} | ||
| <div aria-hidden="true" {{on "click" this.noop}}></div> | ||
|
|
||
| {{! Role="presentation" opts out of the widget contract }} | ||
| <div role="presentation" {{on "click" this.noop}}></div> | ||
| </template> | ||
| ``` | ||
|
|
||
| ## References | ||
|
|
||
| - [WAI-ARIA Authoring Practices — Keyboard Interaction](https://www.w3.org/WAI/ARIA/apg/practices/keyboard-interface/) | ||
| - [MDN — Keyboard-navigable JavaScript widgets](https://developer.mozilla.org/en-US/docs/Web/Accessibility/Keyboard-navigable_JavaScript_widgets) | ||
| - [`click-events-have-key-events` — eslint-plugin-jsx-a11y](https://github.com/jsx-eslint/eslint-plugin-jsx-a11y/blob/main/docs/rules/click-events-have-key-events.md) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,66 @@ | ||
| # ember/template-require-input-type | ||
|
|
||
| 🔧 This rule is automatically fixable by the [`--fix` CLI option](https://eslint.org/docs/latest/user-guide/command-line-interface#--fix). | ||
|
|
||
| <!-- end auto-generated rule header --> | ||
|
|
||
| This rule rejects `<input type="...">` values that are not one of the input | ||
| types defined by the HTML spec, and (optionally) requires every `<input>` to | ||
| declare a `type` attribute. | ||
|
|
||
| An invalid value like `<input type="foo">` silently falls back to the Text | ||
| state — the browser reports no error, but the author's intent (validation, | ||
| inputmode hint, platform keyboard) is lost. That's a genuine silent-failure | ||
| class, which this rule always flags and auto-fixes to `type="text"`. | ||
|
|
||
| A missing `type` attribute (`<input />`) is _spec-compliant_ — the | ||
| missing-value default is the Text state — so flagging it is a style / | ||
| consistency choice, not a correctness one. Opt in with `requireExplicit: true` | ||
| if your team wants parity with `template-require-button-type`. | ||
|
|
||
| ## Examples | ||
|
|
||
| This rule **forbids** the following (always): | ||
|
|
||
| ```hbs | ||
| <input type='' /> | ||
| <input type='foo' /> | ||
| <input type='TEXTY' /> | ||
| ``` | ||
|
|
||
| With `requireExplicit: true` the rule **also forbids**: | ||
|
|
||
| ```hbs | ||
| <input /> | ||
| <input name='email' /> | ||
| ``` | ||
|
|
||
| This rule **allows** the following: | ||
|
|
||
| ```hbs | ||
| <input type='text' /> | ||
| <input type='email' /> | ||
| <input type='checkbox' /> | ||
| <input type={{this.inputType}} /> | ||
| ``` | ||
|
|
||
| Dynamic values such as `type={{this.inputType}}` are not flagged at lint time. | ||
|
|
||
| ## Configuration | ||
|
|
||
| - `requireExplicit` (`boolean`, default `false`): when true, also flag | ||
| `<input>` elements that have no `type` attribute. Auto-fix inserts | ||
| `type="text"`. | ||
|
|
||
| ```js | ||
| module.exports = { | ||
| rules: { | ||
| 'ember/template-require-input-type': ['error', { requireExplicit: true }], | ||
| }, | ||
| }; | ||
| ``` | ||
|
|
||
| ## References | ||
|
|
||
| - [HTML spec — the input element](https://html.spec.whatwg.org/multipage/input.html#the-input-element) | ||
| - Adapted from [`html-validate`'s `no-implicit-input-type`](https://html-validate.org/rules/no-implicit-input-type.html) (MIT). |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,164 @@ | ||
| const { dom } = require('aria-query'); | ||
| const { isNativeElement } = require('../utils/is-native-element'); | ||
| const { isHtmlInteractiveContent } = require('../utils/html-interactive-content'); | ||
| const { getStaticAttrValue } = require('../utils/static-attr-value'); | ||
|
|
||
| const KEYBOARD_EVENT_NAMES = new Set(['keydown', 'keyup', 'keypress']); | ||
|
|
||
| function findAttr(node, name) { | ||
| return node.attributes?.find((a) => a.name === name); | ||
| } | ||
|
|
||
| function getAttrTextValue(attr) { | ||
| if (attr?.value?.type === 'GlimmerTextNode') { | ||
| return attr.value.chars; | ||
| } | ||
| return undefined; | ||
| } | ||
|
|
||
| // Adapter matching the `isHtmlInteractiveContent` util's expected signature: | ||
| // `(node, attrName) -> string | undefined` for static attribute text values. | ||
| function getTextAttrValue(node, attrName) { | ||
| return getAttrTextValue(findAttr(node, attrName)); | ||
| } | ||
|
|
||
| function isHiddenFromScreenReader(node) { | ||
| const ariaHidden = findAttr(node, 'aria-hidden'); | ||
| if (ariaHidden) { | ||
| // WAI-ARIA 1.2: aria-hidden is NOT a boolean HTML attribute. Only the | ||
| // string value "true" hides the element. A valueless attribute or an | ||
| // empty string is invalid and must NOT be treated as hiding the element. | ||
| // | ||
| // getStaticAttrValue resolves GlimmerTextNode, GlimmerMustacheStatement | ||
| // with a literal path (boolean/string), and GlimmerConcatStatement whose | ||
| // parts are all static — covering aria-hidden="TRUE", aria-hidden={{true}}, | ||
| // aria-hidden={{"true"}}, and aria-hidden="{{true}}". Dynamic expressions | ||
| // return undefined and are intentionally not treated as hidden. | ||
| const resolved = getStaticAttrValue(ariaHidden.value); | ||
| if (resolved !== undefined && resolved.trim().toLowerCase() === 'true') { | ||
| return true; | ||
| } | ||
| } | ||
| if (findAttr(node, 'hidden')) { | ||
| return true; | ||
| } | ||
| return false; | ||
| } | ||
|
|
||
| function hasPresentationRole(node) { | ||
| const role = getAttrTextValue(findAttr(node, 'role')); | ||
| if (!role) { | ||
| return false; | ||
| } | ||
| return role | ||
| .trim() | ||
| .toLowerCase() | ||
| .split(/\s+/u) | ||
| .some((token) => token === 'presentation' || token === 'none'); | ||
| } | ||
|
|
||
| function getOnModifierEventName(modifier) { | ||
| if (modifier.type !== 'GlimmerElementModifierStatement') { | ||
| return undefined; | ||
| } | ||
| if (modifier.path?.type !== 'GlimmerPathExpression' || modifier.path.original !== 'on') { | ||
| return undefined; | ||
| } | ||
| const firstParam = modifier.params?.[0]; | ||
| if (firstParam?.type === 'GlimmerStringLiteral') { | ||
| return firstParam.value; | ||
| } | ||
| return undefined; | ||
| } | ||
|
|
||
| function modifierEvents(node) { | ||
| const events = new Set(); | ||
| for (const modifier of node.modifiers || []) { | ||
| const eventName = getOnModifierEventName(modifier); | ||
| if (eventName) { | ||
| events.add(eventName); | ||
| } | ||
| } | ||
| return events; | ||
| } | ||
|
|
||
| /** @type {import('eslint').Rule.RuleModule} */ | ||
| module.exports = { | ||
| meta: { | ||
| type: 'problem', | ||
| docs: { | ||
| description: | ||
| 'require a clickable non-interactive element to have at least one keyboard event listener', | ||
| category: 'Accessibility', | ||
| url: 'https://github.com/ember-cli/eslint-plugin-ember/tree/master/docs/rules/template-click-events-have-key-events.md', | ||
| templateMode: 'both', | ||
| }, | ||
| fixable: null, | ||
| schema: [], | ||
| messages: { | ||
| needsKeyEvent: | ||
| 'Non-interactive elements with click handlers must have at least one keyboard listener (keydown/keyup/keypress).', | ||
| }, | ||
| }, | ||
|
|
||
| create(context) { | ||
| const sourceCode = context.sourceCode ?? context.getSourceCode(); | ||
| return { | ||
| GlimmerElementNode(node) { | ||
| if (!node.tag) { | ||
| return; | ||
| } | ||
|
|
||
| // Skip component invocations (PascalCase, named-arg, this-path, dot-path, named-block) | ||
| // and scope-shadowed tag names. | ||
| if (!isNativeElement(node, sourceCode)) { | ||
| return; | ||
| } | ||
|
|
||
| // Skip tags aria-query doesn't recognize as DOM elements (e.g. hyphenated | ||
| // custom elements like `<my-widget>`). | ||
| if (!dom.has(node.tag)) { | ||
| return; | ||
| } | ||
|
|
||
| const events = modifierEvents(node); | ||
| if (!events.has('click')) { | ||
| return; | ||
| } | ||
|
|
||
| if (isHiddenFromScreenReader(node) || hasPresentationRole(node)) { | ||
| return; | ||
| } | ||
|
|
||
| if (isHtmlInteractiveContent(node, getTextAttrValue)) { | ||
| return; | ||
| } | ||
|
johanrd marked this conversation as resolved.
|
||
|
|
||
| // Elements outside HTML §3.2.5.2.7 that are nonetheless ARIA widgets | ||
| // or conventionally interactive surfaces — click-without-key on them | ||
| // isn't what this rule targets. The HTML-content-model util covers | ||
| // the spec-normative list; these are explicit exemptions for elements | ||
| // that do not qualify as interactive content under the HTML spec but | ||
| // are carved out here due to their ARIA roles or browser-native | ||
| // behavior (see `html-interactive-content.js` docstring for context). | ||
| // - <canvas>: drawing/game surface (axobject-query: CanvasRole). | ||
| // - <option>: ARIA role="option" (widget), but not keyboard-activatable | ||
| // as a standalone element — exempted as a special case. | ||
| // - <datalist>: ARIA role="listbox" (widget), same rationale as <option>. | ||
| const lowerTag = node.tag.toLowerCase(); | ||
| if (lowerTag === 'canvas' || lowerTag === 'option' || lowerTag === 'datalist') { | ||
| return; | ||
| } | ||
|
johanrd marked this conversation as resolved.
johanrd marked this conversation as resolved.
|
||
|
|
||
| // If any of the keyboard events is already handled, we're done. | ||
| for (const event of events) { | ||
| if (KEYBOARD_EVENT_NAMES.has(event)) { | ||
| return; | ||
| } | ||
| } | ||
|
|
||
| context.report({ node, messageId: 'needsKeyEvent' }); | ||
| }, | ||
| }; | ||
| }, | ||
| }; | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.