-
-
Notifications
You must be signed in to change notification settings - Fork 107
Linter: Implement a11y-no-visually-hidden-interactive-elements rule
#1671
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,56 @@ | ||
| # Linter Rule: No visually hidden interactive elements | ||
|
|
||
| **Rule:** `a11y-no-visually-hidden-interactive-elements` | ||
|
|
||
| ## Description | ||
|
|
||
| Enforce that interactive elements are not visually hidden using classes like `sr-only`. | ||
|
|
||
| ## Rationale | ||
|
|
||
| Visually hiding interactive elements can be confusing to sighted keyboard users as it appears their focus has been lost when they navigate to the hidden element. | ||
|
|
||
| Note: `input` elements are not flagged at this time as some visually hidden inputs might cause false positives (e.g. file inputs). | ||
|
|
||
| ## Examples | ||
|
|
||
| ### ✅ Good | ||
|
|
||
| ```erb | ||
| <h2 class="sr-only">Welcome to GitHub</h2> | ||
| ``` | ||
|
|
||
| ```erb | ||
| <span class="sr-only">Visually hidden text</span> | ||
| ``` | ||
|
|
||
| ```erb | ||
| <button class="btn">Submit</button> | ||
| ``` | ||
|
|
||
| ### 🚫 Bad | ||
|
|
||
| ```erb | ||
| <button class="sr-only">Submit</button> | ||
| ``` | ||
|
|
||
| ```erb | ||
| <a class="sr-only" href="/about">About</a> | ||
| ``` | ||
|
|
||
| ```erb | ||
| <summary class="sr-only">Details</summary> | ||
| ``` | ||
|
|
||
| ```erb | ||
| <select class="sr-only"><option>A</option></select> | ||
| ``` | ||
|
|
||
| ```erb | ||
| <textarea class="sr-only"></textarea> | ||
| ``` | ||
|
|
||
| ## References | ||
|
|
||
| - [erblint-github: NoVisuallyHiddenInteractiveElements](https://github.com/github/erblint-github/blob/main/lib/erblint-github/linters/github/accessibility/no_visually_hidden_interactive_elements.rb) | ||
| - [erblint-github docs](https://github.com/github/erblint-github/blob/main/docs/rules/accessibility/no-visually-hidden-interactive-elements.md) | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,59 @@ | ||
| import { type ParseResult, type ParserOptions, type HTMLElementNode, getTagLocalName, getStaticAttributeValue } from "@herb-tools/core" | ||
|
|
||
| import { BaseRuleVisitor } from "./rule-utils.js" | ||
| import { ParserRule } from "../types.js" | ||
| import type { UnboundLintOffense, LintContext, FullRuleConfig } from "../types.js" | ||
|
|
||
| const INTERACTIVE_ELEMENTS = ["a", "button", "summary", "select", "option", "textarea"] | ||
|
|
||
| const VISUALLY_HIDDEN_CLASSES = ["sr-only"] | ||
| const VISUALLY_HIDDEN_UNDO_CLASSES = ["not-sr-only", "focus:not-sr-only", "focus-within:not-sr-only"] | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Since the rule already works on Tailwind's conventions, it might be better to handle its focus variants more completely. Instead of an explicit list, matching the semantics (any focus-triggered const VISUALLY_SHOWN_ON_FOCUS = /(?:^|:)(?:group-)?focus(?:-visible|-within)?:not-sr-only$/
const isUndoClass = (cls: string) => cls === "not-sr-only" || VISUALLY_SHOWN_ON_FOCUS.test(cls)And then |
||
|
|
||
| class NoVisuallyHiddenInteractiveElementsVisitor extends BaseRuleVisitor { | ||
| visitHTMLElementNode(node: HTMLElementNode): void { | ||
| const tagName = getTagLocalName(node) | ||
|
|
||
| if (tagName && INTERACTIVE_ELEMENTS.includes(tagName)) { | ||
| const classValue = getStaticAttributeValue(node, "class") | ||
|
|
||
| if (classValue) { | ||
| const classes = classValue.split(/\s+/) | ||
|
|
||
| if (VISUALLY_HIDDEN_CLASSES.some((cls) => classes.includes(cls)) && !VISUALLY_HIDDEN_UNDO_CLASSES.some((cls) => classes.includes(cls))) { | ||
| this.addOffense( | ||
| "Avoid visually hiding interactive elements. Visually hiding interactive elements can be confusing to sighted keyboard users as it appears their focus has been lost when they navigate to the hidden element.", | ||
| node.tag_name!.location, | ||
| ) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| super.visitHTMLElementNode(node) | ||
| } | ||
| } | ||
|
|
||
| export class A11yNoVisuallyHiddenInteractiveElementsRule extends ParserRule { | ||
| static ruleName = "a11y-no-visually-hidden-interactive-elements" | ||
| static introducedIn = this.version("0.9.4") | ||
|
|
||
| get defaultConfig(): FullRuleConfig { | ||
| return { | ||
| enabled: false, | ||
| severity: "warning", | ||
| } | ||
| } | ||
|
|
||
| get parserOptions(): Partial<ParserOptions> { | ||
| return { | ||
| action_view_helpers: true, | ||
| } | ||
| } | ||
|
|
||
| check(result: ParseResult, context?: Partial<LintContext>): UnboundLintOffense[] { | ||
| const visitor = new NoVisuallyHiddenInteractiveElementsVisitor(this.ruleName, context) | ||
|
|
||
| visitor.visit(result.value) | ||
|
|
||
| return visitor.offenses | ||
| } | ||
| } | ||
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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.