-
Notifications
You must be signed in to change notification settings - Fork 8
rule: no-aria-hidden-on-focusable-elements #55
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 all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
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
35 changes: 35 additions & 0 deletions
35
docs/rules/accessibility/no-aria-hidden-on-focusable-counter.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,35 @@ | ||
| # No aria-hidden on focusable counter | ||
|
|
||
| ## Rule Details | ||
|
|
||
| Elements that are focusable should never have `aria-hidden="true"` set. | ||
|
|
||
| `aria-hidden="true"` hides elements from assistive technologies. `aria-hidden="true"` should only be used to hide non-interactive content such as decorative elements or redundant text. If a focusable element has `aria-hidden="true"`, it can cause confusion amongst assistive technology users who may be able to reach the element but not receive information about it. | ||
|
|
||
| ### Resources | ||
|
|
||
| - [Accessibility insights: aria-hidden-focus](https://accessibilityinsights.io/info-examples/web/aria-hidden-focus/) | ||
| - [Deque: aria-hidden elements do not contain focusable elements](https://dequeuniversity.com/rules/axe/html/4.4/aria-hidden-focus) | ||
| - [W3: Element with aria-hidden has no content in sequential focus navigation](https://www.w3.org/WAI/standards-guidelines/act/rules/6cfa84/proposed/) | ||
|
|
||
| ## Examples | ||
|
|
||
| ### **Incorrect** code for this rule 👎 | ||
|
|
||
| ```erb | ||
| <button aria-hidden="true">Submit</button> | ||
| ``` | ||
|
|
||
| ```erb | ||
| <div role="menuitem" aria-hidden="true" tabindex="0"></div> | ||
| ``` | ||
|
|
||
| ### **Correct** code for this rule 👍 | ||
|
|
||
| ```erb | ||
| <button>Submit</button> | ||
| ``` | ||
|
|
||
| ```erb | ||
| <div role="menuitem" aria-hidden="true" tabindex="-1"></div> | ||
| ``` |
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
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
41 changes: 41 additions & 0 deletions
41
lib/erblint-github/linters/github/accessibility/no_aria_hidden_on_focusable_counter.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,41 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| require_relative "../../custom_helpers" | ||
|
|
||
| module ERBLint | ||
| module Linters | ||
| module GitHub | ||
| module Accessibility | ||
| class NoAriaHiddenOnFocusableCounter < Linter | ||
| include ERBLint::Linters::CustomHelpers | ||
| include LinterRegistry | ||
|
|
||
| MESSAGE = "Elements that are focusable should not have `aria-hidden='true' because it will cause confusion for assistive technology users." | ||
|
|
||
| def run(processed_source) | ||
| tags(processed_source).each do |tag| | ||
| aria_hidden = possible_attribute_values(tag, "aria-hidden") | ||
| generate_offense(self.class, processed_source, tag) if aria_hidden.include?("true") && focusable?(tag) | ||
| end | ||
|
|
||
| counter_correct?(processed_source) | ||
| end | ||
|
|
||
| def autocorrect(processed_source, offense) | ||
| return unless offense.context | ||
|
|
||
| lambda do |corrector| | ||
| if processed_source.file_content.include?("erblint:counter #{simple_class_name}") | ||
| # update the counter if exists | ||
| corrector.replace(offense.source_range, offense.context) | ||
| else | ||
| # add comment with counter if none | ||
| corrector.insert_before(processed_source.source_buffer.source_range, "#{offense.context}\n") | ||
| end | ||
| end | ||
| end | ||
| end | ||
| end | ||
| end | ||
| end | ||
| end | ||
58 changes: 58 additions & 0 deletions
58
test/linters/accessibility/no_aria_hidden_on_focusable_counter_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,58 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| require "test_helper" | ||
|
|
||
| class NoAriaHiddenOnFocusableCounterTest < LinterTestCase | ||
| def linter_class | ||
| ERBLint::Linters::GitHub::Accessibility::NoAriaHiddenOnFocusableCounter | ||
| end | ||
|
|
||
| def test_does_not_warn_if_link_does_not_have_aria_hidden | ||
| @file = "<a href='github.com'>GitHub</a>" | ||
| @linter.run(processed_source) | ||
|
|
||
| assert_empty @linter.offenses | ||
| end | ||
|
|
||
| def test_does_not_consider_aria_hidden_as_aria_hidden_true | ||
| # aria-hidden is not the same as aria-hidden="true". Not ideal code. | ||
| @file = "<a aria-hidden href='github.com'>GitHub</a>" | ||
| @linter.run(processed_source) | ||
|
|
||
| assert_empty @linter.offenses | ||
| end | ||
|
|
||
| def test_does_not_warn_if_link_has_aria_hidden_false | ||
| @file = "<a aria-hidden='false' href='github.com'>GitHub</a>" | ||
| @linter.run(processed_source) | ||
|
|
||
| assert_empty @linter.offenses | ||
| end | ||
|
|
||
| def test_does_not_warn_when_link_has_aria_hidden_true_and_is_not_focusable | ||
| @file = "<a aria-hidden='true' tabindex='-1' href='github.com'>GitHub</a>" | ||
| @linter.run(processed_source) | ||
|
|
||
| assert_empty @linter.offenses | ||
| end | ||
|
|
||
| def test_warns_when_element_has_aria_hidden_true_and_not_tab_focusable | ||
| @file = "<div role='button' tabindex='0' aria-hidden='true'>GitHub</a>" | ||
| @linter.run(processed_source) | ||
| refute_empty @linter.offenses | ||
| end | ||
|
|
||
| def test_warns_when_link_has_aria_hidden_true | ||
| @file = "<a aria-hidden='true' href='github.com'>GitHub</a>" | ||
| @linter.run(processed_source) | ||
|
|
||
| refute_empty @linter.offenses | ||
| end | ||
|
|
||
| def test_warns_when_element_has_aria_hidden_true_and_is_tab_focusable | ||
| @file = "<div role='list' aria-hidden='true' tabindex='0'></div>" | ||
| @linter.run(processed_source) | ||
|
|
||
| refute_empty @linter.offenses | ||
| end | ||
| end |
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.