Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 6 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -178,11 +178,12 @@ rules in templates can be disabled with eslint directives with mustache or html

### Accessibility

| Name | Description | 💼 | 🔧 | 💡 |
| :------------------------------------------------------------------------------- | :-------------------------------------- | :- | :- | :- |
| [template-link-href-attributes](docs/rules/template-link-href-attributes.md) | require href attribute on link elements | | | |
| [template-no-abstract-roles](docs/rules/template-no-abstract-roles.md) | disallow abstract ARIA roles | | | |
| [template-no-accesskey-attribute](docs/rules/template-no-accesskey-attribute.md) | disallow accesskey attribute | | 🔧 | |
| Name                                  | Description | 💼 | 🔧 | 💡 |
| :------------------------------------------------------------------------------------------- | :------------------------------------------------------------------------------- | :- | :- | :- |
| [template-link-href-attributes](docs/rules/template-link-href-attributes.md) | require href attribute on link elements | | | |
| [template-no-abstract-roles](docs/rules/template-no-abstract-roles.md) | disallow abstract ARIA roles | | | |
| [template-no-accesskey-attribute](docs/rules/template-no-accesskey-attribute.md) | disallow accesskey attribute | | 🔧 | |
| [template-no-aria-unsupported-elements](docs/rules/template-no-aria-unsupported-elements.md) | disallow ARIA roles, states, and properties on elements that do not support them | | | |

### Best Practices

Expand Down
61 changes: 61 additions & 0 deletions docs/rules/template-no-aria-unsupported-elements.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
# ember/template-no-aria-unsupported-elements

<!-- end auto-generated rule header -->

Disallows using ARIA roles, states, and properties on elements that do not support them.

Certain HTML elements do not support ARIA roles, states, and properties. This rule helps ensure that ARIA attributes are only used on elements that support them, improving accessibility.

## Rule Details

This rule disallows ARIA attributes on elements that do not support them, including:

- `meta`
- `html`
- `script`
- `style`
- `title`
- `base`
- `head`
- `link`

## Examples

Examples of **incorrect** code for this rule:

```gjs
<template>
<meta role="button" />
</template>
```

```gjs
<template>
<script aria-label="Analytics"></script>
</template>
```

```gjs
<template>
<style role="presentation"></style>
</template>
```

Examples of **correct** code for this rule:

```gjs
<template>
<div role="button" aria-label="Submit"></div>
</template>
```

```gjs
<template>
<button aria-pressed="true">Toggle</button>
</template>
```

## References

- [WAI-ARIA in HTML](https://www.w3.org/TR/html-aria/)
- [ember-template-lint no-aria-unsupported-elements](https://github.com/ember-template-lint/ember-template-lint/blob/master/docs/rule/no-aria-unsupported-elements.md)
58 changes: 58 additions & 0 deletions lib/rules/template-no-aria-unsupported-elements.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/** @type {import('eslint').Rule.RuleModule} */
module.exports = {
meta: {
type: 'problem',
docs: {
description:
'disallow ARIA roles, states, and properties on elements that do not support them',
category: 'Accessibility',
strictGjs: true,
strictGts: true,
url: 'https://github.com/ember-cli/eslint-plugin-ember/tree/master/docs/rules/template-no-aria-unsupported-elements.md',
},
fixable: null,
schema: [],
messages: {
unsupported:
'ARIA attribute "{{attribute}}" is not supported on <{{element}}> elements. Consider using a different element.',
},
},

create(context) {
// Elements that don't support ARIA
const ELEMENTS_WITHOUT_ARIA_SUPPORT = new Set([
'meta',
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

seems fine tho, it's correct afaict

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we update the documentation to match then?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

'html',
'script',
'style',
'title',
'base',
'head',
'link',
]);

return {
GlimmerElementNode(node) {
if (ELEMENTS_WITHOUT_ARIA_SUPPORT.has(node.tag)) {
const ariaAttributes = node.attributes?.filter(
(attr) =>
attr.type === 'GlimmerAttrNode' &&
attr.name &&
(attr.name.startsWith('aria-') || attr.name === 'role')
);

for (const attr of ariaAttributes || []) {
context.report({
node: attr,
messageId: 'unsupported',
data: {
attribute: attr.name,
element: node.tag,
},
});
}
}
},
};
},
};
33 changes: 33 additions & 0 deletions tests/lib/rules/template-no-aria-unsupported-elements.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
const rule = require('../../../lib/rules/template-no-aria-unsupported-elements');
const RuleTester = require('eslint').RuleTester;

const ruleTester = new RuleTester({
parser: require.resolve('ember-eslint-parser'),
parserOptions: { ecmaVersion: 2022, sourceType: 'module' },
});

ruleTester.run('template-no-aria-unsupported-elements', rule, {
valid: [
'<template><div role="button" aria-label="Submit"></div></template>',
'<template><button aria-pressed="true">Toggle</button></template>',
'<template><input aria-label="Username"></template>',
],

invalid: [
{
code: '<template><meta role="button"></template>',
output: null,
errors: [{ messageId: 'unsupported' }],
},
{
code: '<template><script aria-label="Script"></script></template>',
output: null,
errors: [{ messageId: 'unsupported' }],
},
{
code: '<template><style role="presentation"></style></template>',
output: null,
errors: [{ messageId: 'unsupported' }],
},
],
});
Loading