-
Notifications
You must be signed in to change notification settings - Fork 7
Create rule: no-visually-hidden-interactive-element #89
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
Merged
Changes from 2 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
ab3e1c9
wip
kendallgassner 33aed06
add docs
kendallgassner 5dcd3d0
Update docs/rules/accessibility/no-visually-hidden-interactive-elemen…
kendallgassner 5eedaa2
lint
kendallgassner d88c7f0
Merge branch 'add_visually_hidden_interactive_rule' of https://github…
kendallgassner 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
36 changes: 36 additions & 0 deletions
36
docs/rules/accessibility/no-visually-hidden-interactive-elements.md
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,36 @@ | ||
| # No visually hidden interactive elements | ||
|
|
||
| ## Rule Details | ||
|
|
||
| This rule guards against visually hiding interactive elements. If a sighted keyboard user navigates to an interactive element that is visually hidden they might become confused and assume that keyboard focus has been lost. | ||
|
|
||
| Note: we are not guarding against visually hidden `input` elements at this time. Some visually hidden inputs might cause a false positive (e.g. some file inputs). | ||
|
|
||
| ### Why do we visually hide content? | ||
|
|
||
| Visually hiding content can be useful when you want to provide information specifically to screen reader users or other assistive technology users while keeping content hidden from sighted users. | ||
|
|
||
| Applying the following css will visually hide content while still making it accessible to screen reader users. | ||
|
|
||
| ```css | ||
| clip-path: inset(50%); | ||
| height: 1px; | ||
| overflow: hidden; | ||
| position: absolute; | ||
| white-space: nowrap; | ||
| width: 1px; | ||
| ``` | ||
|
|
||
| 👎 Examples of **incorrect** code for this rule: | ||
|
|
||
| ```jsx | ||
| <h2 className="sr-only">Welcome to GitHub</h2> | ||
| ``` | ||
|
|
||
| 👍 Examples of **correct** code for this rule: | ||
|
|
||
| ```jsx | ||
| <h2 className="sr-only">Welcome to GitHub</h2> | ||
|
Contributor
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. These are the same example 🤔 |
||
| ``` | ||
|
|
||
| ## Version | ||
33 changes: 33 additions & 0 deletions
33
lib/erblint-github/linters/github/accessibility/no_visually_hidden_interactive_elements.rb
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,33 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| require_relative "../../custom_helpers" | ||
|
|
||
| module ERBLint | ||
| module Linters | ||
| module GitHub | ||
| module Accessibility | ||
| class NoVisuallyHiddenInteractiveElements < Linter | ||
| include ERBLint::Linters::CustomHelpers | ||
| include LinterRegistry | ||
| INTERACTIVE_ELEMENTS = %w[a button summary select option textarea].freeze | ||
|
|
||
| MESSAGE = "Avoid visually hidding 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" | ||
|
|
||
| def run(processed_source) | ||
| visually_hidden = false | ||
|
|
||
| tags(processed_source).each do |tag| | ||
| next if tag.closing? | ||
| classes = possible_attribute_values(tag, "class") | ||
| visually_hidden = true if classes.include?("sr-only") | ||
| next unless classes.include?("sr-only") || visually_hidden | ||
| if INTERACTIVE_ELEMENTS.include?(tag.name) | ||
| generate_offense(self.class, processed_source, tag) | ||
| end | ||
| end | ||
| end | ||
| end | ||
| end | ||
| end | ||
| end | ||
| end |
49 changes: 49 additions & 0 deletions
49
test/linters/accessibility/no_visually_hidden_interactive_elements_test.rb
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,49 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| require "test_helper" | ||
|
|
||
| class NoVisuallyHiddenInteractiveElements < LinterTestCase | ||
| def linter_class | ||
| ERBLint::Linters::GitHub::Accessibility::NoVisuallyHiddenInteractiveElements | ||
| end | ||
|
|
||
| def test_warns_if_element_is_interactive_and_visually_hidden | ||
| @file = "<button class='sr-only'>Text</button>" | ||
| @linter.run(processed_source) | ||
|
|
||
| assert_equal(1, @linter.offenses.count) | ||
| error_messages = @linter.offenses.map(&:message).sort | ||
| assert_match(/Avoid visually hidding 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/, error_messages.last) | ||
| end | ||
|
|
||
| def test_does_not_warn_if_element_is_not_interactive_and_visually_hidden | ||
| @file = "<div class='sr-only'>Text</div>" | ||
| @linter.run(processed_source) | ||
|
|
||
| assert_empty @linter.offenses | ||
| end | ||
|
|
||
|
|
||
| def test_does_not_warn_if_element_is_interactive_and_not_visually_hidden | ||
| @file = "<button class='other'>Submit</button>" | ||
| @linter.run(processed_source) | ||
|
|
||
| assert_empty @linter.offenses | ||
| end | ||
|
|
||
| def test_does_not_warn_if_element_is_interactive_and_shown_on_focus | ||
| @file = "<a class='other show-on-focus'>skip to main content</a>" | ||
| @linter.run(processed_source) | ||
|
|
||
| assert_empty @linter.offenses | ||
| end | ||
|
|
||
| def test_warn_if_element_is_interactive_in_a_visually_hidden_parent | ||
| @file = "<div class='sr-only'><button>Submit</button></div>" | ||
| @linter.run(processed_source) | ||
|
|
||
| assert_equal(1, @linter.offenses.count) | ||
| error_messages = @linter.offenses.map(&:message).sort | ||
| assert_match(/Avoid visually hidding 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/, error_messages.last) | ||
| end | ||
| end |
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
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.
Uh oh!
There was an error while loading. Please reload this page.