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
7 changes: 4 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -176,9 +176,10 @@ 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 | | | |
| Name | Description | 💼 | 🔧 | 💡 |
| :------------------------------------------------------------------------------- | :-------------------------------------- | :- | :- | :- |
| [template-link-href-attributes](docs/rules/template-link-href-attributes.md) | require href attribute on link elements | | | |
| [template-no-accesskey-attribute](docs/rules/template-no-accesskey-attribute.md) | disallow accesskey attribute | | 🔧 | |

### Best Practices

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

🔧 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 -->

Disallows the use of `accesskey` attribute on elements.

The `accesskey` attribute creates inconsistencies between keyboard shortcuts and keyboard commands used by screen readers and keyboard-only users, causing accessibility issues.

## Examples

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

```gjs
<template>
<button accesskey="s">Save</button>
</template>
```

```gjs
<template>
<a href="#" accesskey="h">Home</a>
</template>
```

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

```gjs
<template>
<button>Save</button>
</template>
```

```gjs
<template>
<a href="#">Home</a>
</template>
```

## References

- [eslint-plugin-ember no-accesskey-attribute](https://github.com/eslint-plugin-ember/eslint-plugin-ember/blob/master/docs/rule/no-accesskey-attribute.md)
- [WebAIM: Keyboard Accessibility - Accesskey](https://webaim.org/techniques/keyboard/accesskey)
48 changes: 48 additions & 0 deletions lib/rules/template-no-accesskey-attribute.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/** @type {import('eslint').Rule.RuleModule} */
module.exports = {
meta: {
type: 'suggestion',
docs: {
description: 'disallow accesskey attribute',
category: 'Accessibility',
strictGjs: true,
strictGts: true,
url: 'https://github.com/ember-cli/eslint-plugin-ember/tree/master/docs/rules/template-no-accesskey-attribute.md',
},
fixable: 'code',
schema: [],
messages: {
noAccesskey:
'No access key attribute allowed. Inconsistencies between keyboard shortcuts and keyboard commands used by screenreader and keyboard only users create accessibility complications.',
},
},

create(context) {
return {
GlimmerElementNode(node) {
const accesskeyAttr = node.attributes?.find((attr) => attr.name === 'accesskey');

if (accesskeyAttr) {
context.report({
node: accesskeyAttr,
messageId: 'noAccesskey',
fix(fixer) {
// Remove the attribute including preceding whitespace
const sourceCode = context.sourceCode;
const text = sourceCode.getText();
const attrStart = accesskeyAttr.range[0];
const attrEnd = accesskeyAttr.range[1];

let removeStart = attrStart;
while (removeStart > 0 && /\s/.test(text[removeStart - 1])) {
removeStart--;
}

return fixer.removeRange([removeStart, attrEnd]);
},
});
}
},
};
},
};
57 changes: 57 additions & 0 deletions tests/lib/rules/template-no-accesskey-attribute.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
//------------------------------------------------------------------------------
// Requirements
//------------------------------------------------------------------------------

const rule = require('../../../lib/rules/template-no-accesskey-attribute');
const RuleTester = require('eslint').RuleTester;

//------------------------------------------------------------------------------
// Tests
//------------------------------------------------------------------------------

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

ruleTester.run('template-no-accesskey-attribute', rule, {
valid: [
`<template>
<button>Click me</button>
</template>`,
`<template>
<div class="button">Content</div>
</template>`,
],

invalid: [
{
code: `<template>
<button accesskey="s">Save</button>
</template>`,
output: `<template>
<button>Save</button>
</template>`,
errors: [
{
message:
'No access key attribute allowed. Inconsistencies between keyboard shortcuts and keyboard commands used by screenreader and keyboard only users create accessibility complications.',
type: 'GlimmerAttrNode',
},
],
},
{
code: `<template>
<a href="#" accesskey="h">Home</a>
</template>`,
output: `<template>
<a href="#">Home</a>
</template>`,
errors: [
{
type: 'GlimmerAttrNode',
},
],
},
],
});
Loading