-
-
Notifications
You must be signed in to change notification settings - Fork 211
Extract rule: template-no-heading-inside-button #2437
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
Merged
NullVoxPopuli
merged 1 commit into
ember-cli:master
from
NullVoxPopuli:nvp/template-lint-extract-rule-template-no-heading-inside-button
Feb 25, 2026
Merged
Changes from all commits
Commits
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
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
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,32 @@ | ||
| # ember/template-no-heading-inside-button | ||
|
|
||
| <!-- end auto-generated rule header --> | ||
|
|
||
| Assistive technology allows users to browse a page by heading elements (`<h1>` - `<h6>`). However, if those heading elements are nested inside of button elements, they will automatically be marked as presentational by browsers. Any HTML element where ["children presentational" is true](https://w3c.github.io/aria/#button) should be coerced by the browser to be presentational, and therefore not included in the accessibility tree. | ||
|
|
||
| As such, nesting a heading element inside of a button element will cause failures for WCAG requirement 1.3.1, Info and Relationships, because the heading has lost semantic meaning. | ||
|
|
||
| This rule checks `<button>` elements to see if they contain heading (`<h1>` - `<h6>`) elements, and gives an error message if they are found. | ||
|
|
||
| ## Examples | ||
|
|
||
| This rule **forbids** the following: | ||
|
|
||
| ```gjs | ||
| <template><button><h1>Some Text</h1></button></template> | ||
| ``` | ||
|
|
||
| This rule **allows** the following: | ||
|
|
||
| ```gjs | ||
| <template><button><span>Button Text</span></button></template> | ||
| ``` | ||
|
|
||
| ## Migration | ||
|
|
||
| - Replace `<h1>` - `<h6>` elements inside of `<button>` elements with classes that reflect the desired styling. | ||
|
|
||
| ## References | ||
|
|
||
| - <https://www.w3.org/WAI/WCAG21/Understanding/info-and-relationships.html> | ||
| - <https://w3c.github.io/aria/#button> |
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,47 @@ | ||
| const HEADING_ELEMENTS = new Set(['h1', 'h2', 'h3', 'h4', 'h5', 'h6']); | ||
|
|
||
| function hasButtonParent(node) { | ||
| let parent = node.parent; | ||
| while (parent) { | ||
| if (parent.type === 'GlimmerElementNode') { | ||
| // Check if it's a button element | ||
| if (parent.tag === 'button') { | ||
| return true; | ||
| } | ||
| // Check if it has role="button" | ||
| const roleAttr = parent.attributes?.find((a) => a.name === 'role'); | ||
| if (roleAttr?.value?.type === 'GlimmerTextNode' && roleAttr.value.chars === 'button') { | ||
| return true; | ||
| } | ||
| } | ||
| parent = parent.parent; | ||
| } | ||
| return false; | ||
| } | ||
|
|
||
| /** @type {import('eslint').Rule.RuleModule} */ | ||
| module.exports = { | ||
| meta: { | ||
| type: 'problem', | ||
| docs: { | ||
| description: 'disallow heading elements inside button elements', | ||
| category: 'Accessibility', | ||
| strictGjs: true, | ||
| strictGts: true, | ||
| url: 'https://github.com/ember-cli/eslint-plugin-ember/tree/master/docs/rules/template-no-heading-inside-button.md', | ||
| }, | ||
| schema: [], | ||
| messages: { | ||
| noHeading: 'Buttons should not contain heading elements', | ||
| }, | ||
| }, | ||
| create(context) { | ||
| return { | ||
| GlimmerElementNode(node) { | ||
| if (HEADING_ELEMENTS.has(node.tag) && hasButtonParent(node)) { | ||
| context.report({ node, messageId: 'noHeading' }); | ||
| } | ||
| }, | ||
| }; | ||
| }, | ||
| }; |
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,77 @@ | ||
| const rule = require('../../../lib/rules/template-no-heading-inside-button'); | ||
| const RuleTester = require('eslint').RuleTester; | ||
|
|
||
| const ruleTester = new RuleTester({ | ||
| parser: require.resolve('ember-eslint-parser'), | ||
| parserOptions: { ecmaVersion: 2022, sourceType: 'module' }, | ||
| }); | ||
|
|
||
| ruleTester.run('template-no-heading-inside-button', rule, { | ||
| valid: [ | ||
| '<template><button>Click me</button></template>', | ||
| '<template><h1>Title</h1></template>', | ||
| '<template><div><h2>Heading</h2></div></template>', | ||
|
|
||
| // Test cases ported from ember-template-lint | ||
| '<template><button>Show More</button></template>', | ||
| '<template><button><span>thumbs-up emoji</span>Show More</button></template>', | ||
| '<template><button><div>Show More</div></button></template>', | ||
| '<template><div>Showing that it is not a button</div></template>', | ||
| '<template><div><h1>Page Title in a div is fine</h1></div></template>', | ||
| '<template><h1>Page Title</h1></template>', | ||
| ], | ||
| invalid: [ | ||
| { | ||
| code: '<template><button><h1>Bad</h1></button></template>', | ||
| output: null, | ||
| errors: [{ messageId: 'noHeading' }], | ||
| }, | ||
| { | ||
| code: '<template><div role="button"><h2>Bad</h2></div></template>', | ||
| output: null, | ||
| errors: [{ messageId: 'noHeading' }], | ||
| }, | ||
|
|
||
| // Test cases ported from ember-template-lint | ||
| { | ||
| code: '<template><button><h1>Page Title</h1></button></template>', | ||
| output: null, | ||
| errors: [{ messageId: 'noHeading' }], | ||
| }, | ||
| { | ||
| code: '<template><button><h2>Heading Title</h2></button></template>', | ||
| output: null, | ||
| errors: [{ messageId: 'noHeading' }], | ||
| }, | ||
| { | ||
| code: '<template><button><h3>Heading Title</h3></button></template>', | ||
| output: null, | ||
| errors: [{ messageId: 'noHeading' }], | ||
| }, | ||
| { | ||
| code: '<template><button><h4>Heading Title</h4></button></template>', | ||
| output: null, | ||
| errors: [{ messageId: 'noHeading' }], | ||
| }, | ||
| { | ||
| code: '<template><button><h5>Heading Title</h5></button></template>', | ||
| output: null, | ||
| errors: [{ messageId: 'noHeading' }], | ||
| }, | ||
| { | ||
| code: '<template><button><div><h1>Heading Title</h1></div></button></template>', | ||
| output: null, | ||
| errors: [{ messageId: 'noHeading' }], | ||
| }, | ||
| { | ||
| code: '<template><button><h6>Heading Title</h6></button></template>', | ||
| output: null, | ||
| errors: [{ messageId: 'noHeading' }], | ||
| }, | ||
| { | ||
| code: '<template><div role="button"><h6>Heading in a div with a role of button</h6></div></template>', | ||
| output: null, | ||
| errors: [{ messageId: 'noHeading' }], | ||
| }, | ||
| ], | ||
| }); | ||
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
more weird comment