-
-
Notifications
You must be signed in to change notification settings - Fork 29
[Rule Request]: Prohibiting use of aria-hidden and role='presentation' on focusable elements. #1169
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
vhoyer
merged 8 commits into
vue-a11y:main
from
mauryapari:no-presentation-role-or-aria-hidden-on-foucusable-elements
Jul 17, 2024
Merged
Changes from 7 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
7e6d8e4
Added rule for prohibiting use of aria-hidden and role=presentaion on…
mauryapari 05a3cab
Refactored code into seprate files and utility file
mauryapari 0b9a565
Added EOL for files with prettier errors
mauryapari b3049b1
Removed nested ifs
mauryapari 5885723
Fixed EOL errors
mauryapari 1c2dded
Added more lines
mauryapari 492cf15
Fixed prettier issues
mauryapari 37d4811
Removed commented code
mauryapari 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 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,79 @@ | ||
# no-aria-hidden-on-focusbable | ||
|
||
Enforce that `aria-hidden="true"` is not set on focusable elements or parent of focusable elements. | ||
|
||
`aria-hidden="true"` can be used to hide purely decorative content from screen reader users. An element with `aria-hidden="true"` that can also be reached by keyboard can lead to confusion or unexpected behavior for screen reader users. Avoid using `aria-hidden="true"` on focusable elements. | ||
|
||
See more in [WAI-ARIA Use in HTML](https://www.w3.org/TR/using-aria/#fourth). | ||
|
||
### ✔ Succeed | ||
|
||
```vue | ||
<template> | ||
<button>Press Me</button> | ||
</template> | ||
``` | ||
|
||
```vue | ||
<template> | ||
<div aria-hidden="true"><button tabindex="-1">Submit</button></div> | ||
</template> | ||
``` | ||
|
||
```vue | ||
<template> | ||
<div aria-hidden='true'><span>Some text</div></div> | ||
</template> | ||
``` | ||
|
||
```vue | ||
<template> | ||
<button tabindex="-1" aria-hidden="true">Press</button> | ||
</template> | ||
``` | ||
|
||
```vue | ||
<template> | ||
<div aria-hidden="true"><a href="#" tabindex="-1">Link</a></div> | ||
</template> | ||
``` | ||
|
||
```vue | ||
<template> | ||
<div aria-hidden="true"><span>Some text</span></div> | ||
</template> | ||
``` | ||
|
||
### ❌ Fail | ||
|
||
```vue | ||
<template> | ||
<button aria-hidden="true">press me</button> | ||
</template> | ||
``` | ||
|
||
```vue | ||
<template> | ||
<button aria-hidden="true">press me</button> | ||
</template> | ||
``` | ||
|
||
```vue | ||
<template> | ||
<a href="#" aria-hidden="true">press me</a> | ||
</template> | ||
``` | ||
|
||
```vue | ||
<template> | ||
<div aria-hidden="true"> | ||
<button>press me</button> | ||
</div> | ||
</template> | ||
``` | ||
|
||
```vue | ||
<template> | ||
<span tabindex="0" aria-hidden="true"><em>Icon</em></span> | ||
</template> | ||
``` |
This file contains 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,79 @@ | ||
# no-role-presentaion-on-focusbable | ||
|
||
Enforce that `role="presentation"` is not set on focusable elements or parent of focusbale elements. | ||
|
||
`role="presentation` can be used to hide purely decorative content from screen reader users. An element with `role="presentation"` that can also be reached by keyboard can lead to confusion or unexpected behavior for screen reader users. Avoid using `role="presentation"` on focusable elements. | ||
|
||
See more in [WAI-ARIA Use in HTML](https://www.w3.org/TR/using-aria/#fourth). | ||
|
||
### ✔ Succeed | ||
|
||
```vue | ||
<template> | ||
<button>Press Me</button> | ||
</template> | ||
``` | ||
|
||
```vue | ||
<template> | ||
<div role="presentation"><button tabindex="-1">Submit</button></div> | ||
</template> | ||
``` | ||
|
||
```vue | ||
<template> | ||
<div role="presentation"><span>Some text</div></div> | ||
</template> | ||
``` | ||
|
||
```vue | ||
<template> | ||
<button tabindex="-1" role="presentation">Press</button> | ||
</template> | ||
``` | ||
|
||
```vue | ||
<template> | ||
<div role="presentation"><a href="#" tabindex="-1">Link</a></div> | ||
</template> | ||
``` | ||
|
||
```vue | ||
<template> | ||
<div role="presentation"><span>Some text</span></div> | ||
</template> | ||
``` | ||
|
||
### ❌ Fail | ||
|
||
```vue | ||
<template> | ||
<button role="presentation">press me</button> | ||
</template> | ||
``` | ||
|
||
```vue | ||
<template> | ||
<button role="presentation">press me</button> | ||
</template> | ||
``` | ||
|
||
```vue | ||
<template> | ||
<a href="#" role="presentation">press me</a> | ||
</template> | ||
``` | ||
|
||
```vue | ||
<template> | ||
<div role="presentation"> | ||
<button>press me</button> | ||
</div> | ||
</template> | ||
``` | ||
|
||
```vue | ||
<template> | ||
<span tabindex="0" role="presentation"><em>Icon</em></span> | ||
</template> | ||
``` |
This file contains 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,31 @@ | ||
import rule from "../no-aria-hidden-on-focusable"; | ||
import makeRuleTester from "./makeRuleTester"; | ||
|
||
makeRuleTester("no-presentation-role-or-aria-hidden-on-focusable", rule, { | ||
valid: [ | ||
"<button>Submit</button>", | ||
"<div aria-hidden='true'><button tabindex='-1'>Some text</button></div>", | ||
"<div><button>Submit</button></div>", | ||
"<a href='#' tabindex='-1'>link</a>", | ||
"<button tabindex='-1' aria-hidden='true'>Press</button>", | ||
"<div aria-hidden='true'><a href='#' tabindex='-1'>Link</a></div>" | ||
], | ||
invalid: [ | ||
{ | ||
code: "<div aria-hidden='true'><button>Submit</button></div>", | ||
errors: [{ messageId: "default" }] | ||
}, | ||
{ | ||
code: "<button type='button' aria-hidden='true'>Submit</button>", | ||
errors: [{ messageId: "default" }] | ||
}, | ||
{ | ||
code: "<a href='#' aria-hidden='true'>Link</a>", | ||
errors: [{ messageId: "default" }] | ||
}, | ||
{ | ||
code: "<span tabindex='0' aria-hidden='true'><em>Icon</em></span>", | ||
errors: [{ messageId: "default" }] | ||
} | ||
] | ||
}); |
31 changes: 31 additions & 0 deletions
31
src/rules/__tests__/no-role-presentation-on-focusable.test.ts
This file contains 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,31 @@ | ||
import rule from "../no-role-presentation-on-focusable"; | ||
import makeRuleTester from "./makeRuleTester"; | ||
|
||
makeRuleTester("no-role-presentation-role-on-focusable", rule, { | ||
valid: [ | ||
"<button>Submit</button>", | ||
"<div role='presentation'><button tabindex='-1'>Some text</button></div>", | ||
"<div><button>Submit</button></div>", | ||
"<a href='#' tabindex='-1'>link</a>", | ||
"<button tabindex='-1' role='presentation'>Press</button>", | ||
"<div role='presentation'><a href='#' tabindex='-1'>Link</a></div>" | ||
], | ||
invalid: [ | ||
{ | ||
code: "<div role='presentation'><button>Submit</button></div>", | ||
errors: [{ messageId: "default" }] | ||
}, | ||
{ | ||
code: "<button type='button' role='presentation'>Submit</button>", | ||
errors: [{ messageId: "default" }] | ||
}, | ||
{ | ||
code: "<a href='#' role='presentation'>Link</a>", | ||
errors: [{ messageId: "default" }] | ||
}, | ||
{ | ||
code: "<span tabindex='0' role='presentation'><em>Icon</em></span>", | ||
errors: [{ messageId: "default" }] | ||
} | ||
] | ||
}); |
This file contains 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,37 @@ | ||
import type { Rule } from "eslint"; | ||
|
||
import { | ||
defineTemplateBodyVisitor, | ||
getElementAttributeValue, | ||
makeDocsURL | ||
} from "../utils"; | ||
import hasFocusableElements from "../utils/hasFocusableElement"; | ||
|
||
const rule: Rule.RuleModule = { | ||
meta: { | ||
type: "problem", | ||
docs: { | ||
url: makeDocsURL("no-aria-hidden-on-focusable") | ||
}, | ||
messages: { | ||
default: | ||
"Focusable/Interactive elements must not have an aria-hidden attribute." | ||
}, | ||
schema: [] | ||
}, | ||
create(context) { | ||
return defineTemplateBodyVisitor(context, { | ||
VElement(node) { | ||
const hasAriaHidden = getElementAttributeValue(node, "aria-hidden"); | ||
if (hasAriaHidden && hasFocusableElements(node)) { | ||
context.report({ | ||
node: node as any, | ||
messageId: "default" | ||
}); | ||
} | ||
} | ||
}); | ||
} | ||
}; | ||
|
||
export default rule; |
This file contains 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,39 @@ | ||
import type { Rule } from "eslint"; | ||
// import type { AST } from "vue-eslint-parser"; | ||
|
||
import { | ||
defineTemplateBodyVisitor, | ||
getElementAttributeValue, | ||
makeDocsURL | ||
} from "../utils"; | ||
import hasFocusableElements from "../utils/hasFocusableElement"; | ||
|
||
const rule: Rule.RuleModule = { | ||
meta: { | ||
type: "problem", | ||
docs: { | ||
url: makeDocsURL("no-role-presentation-on-focusable") | ||
}, | ||
messages: { | ||
default: | ||
"Focusable/Interactive elements must not have a presentation role attribute." | ||
}, | ||
schema: [] | ||
}, | ||
create(context) { | ||
return defineTemplateBodyVisitor(context, { | ||
VElement(node) { | ||
const hasRolePresentation = | ||
getElementAttributeValue(node, "role") === "presentation"; | ||
if (hasRolePresentation && hasFocusableElements(node)) { | ||
context.report({ | ||
node: node as any, | ||
messageId: "default" | ||
}); | ||
} | ||
} | ||
}); | ||
} | ||
}; | ||
|
||
export default rule; |
This file contains 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 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,21 @@ | ||
import type { AST } from "vue-eslint-parser"; | ||
import getElementAttributeValue from "./getElementAttributeValue"; | ||
import isInteractiveElement from "./isInteractiveElement"; | ||
|
||
function hasFocusableElements(node: AST.VElement): boolean { | ||
const tabindex = getElementAttributeValue(node, "tabindex"); | ||
|
||
if (isInteractiveElement(node)) { | ||
return tabindex !== "-1"; | ||
} | ||
|
||
if (tabindex !== null && tabindex !== "-1") { | ||
return true; | ||
} | ||
|
||
return node.children.some( | ||
(child) => child.type === "VElement" && hasFocusableElements(child) | ||
); | ||
} | ||
|
||
export default hasFocusableElements; |
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.
it would be better if we removed the comments, no?
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.
done