-
Notifications
You must be signed in to change notification settings - Fork 8
Port over counter implementation and introduce linter against title attribute #19
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
ea9d80d
add counter support and title counter
khiga8 17dd622
introduce counter test
khiga8 fa7ddfe
introduce rule tests
khiga8 e4e8c85
Update docs/rules/accessibility/no-title-attribute-counter.md
khiga8 7591c1b
Update docs/rules/accessibility/no-title-attribute-counter.md
khiga8 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
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,39 @@ | ||
| # No title attribute counter | ||
|
|
||
| ## Rule Details | ||
|
|
||
| The `title` attribute is strongly discouraged. The only exception is on an `<iframe>` element. It is hardly useful and cannot be accessed by multiple groups of users including keyboard-only users and mobile users. | ||
|
|
||
| The `title` attribute is commonly seen set on links, matching the link text. This is redundant and unnecessary so it can be simply be removed. | ||
|
|
||
| If you are considering the`title` attribute to provide supplementary description, consider whether the text in question can be persisted in the design. Alternatively, if it's important to display supplementary text that is hidden by default, consider using an **accessible** tooltip implementation that uses the `aria-labelledby` or `aria-describedby` semantics. Even so, proceed with caution: tooltips should only be used on interactive elements like links or buttons. | ||
|
|
||
| ### Should I use the `title` attribute to provide an accessible name for an `<svg>`? | ||
|
|
||
| Use a `<title>` element instead of the `title` attribute, or an `aria-label`. | ||
|
|
||
| ### Resources | ||
|
|
||
| - [TPGI: Using the HTML title attribute ](https://www.tpgi.com/using-the-html-title-attribute/) | ||
| - [The Trials and Tribulations of the Title Attribute](https://www.24a11y.com/2017/the-trials-and-tribulations-of-the-title-attribute/) | ||
|
|
||
| ### 👎 Examples of **incorrect** code for this rule: | ||
|
|
||
| ```erb | ||
| <a title="A home for all developers" href="github.com">GitHub</a> | ||
| ``` | ||
|
|
||
| ```erb | ||
| <a href="/" title="github.com">GitHub</a> | ||
| ``` | ||
|
|
||
| ### 👍 Examples of **correct** code for this rule: | ||
|
|
||
| ```erb | ||
| <a href="github.com" aria-describedby="description">GitHub</a> | ||
| <p id="description" class="tooltip js-tooltip">A home for all developers</p> | ||
| ``` | ||
|
|
||
| ```erb | ||
| <a href="github.com">GitHub</a> | ||
| ``` | ||
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
44 changes: 44 additions & 0 deletions
44
lib/erblint-github/linters/github/accessibility/no_title_attribute_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,44 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| require_relative "../../custom_helpers" | ||
|
|
||
| module ERBLint | ||
| module Linters | ||
| module GitHub | ||
| module Accessibility | ||
| class NoTitleAttributeCounter < Linter | ||
| include ERBLint::Linters::CustomHelpers | ||
| include LinterRegistry | ||
|
|
||
| MESSAGE = "The title attribute should never be used unless for an `<iframe>` as it is inaccessible for several groups of users." | ||
|
|
||
| def run(processed_source) | ||
| tags(processed_source).each do |tag| | ||
| next if tag.name == "iframe" | ||
| next if tag.closing? | ||
|
|
||
| title = possible_attribute_values(tag, "title") | ||
| generate_offense(self.class, processed_source, tag) if title.present? | ||
| 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 |
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
59 changes: 59 additions & 0 deletions
59
test/linters/accessibility/no_title_attribute_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,59 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| require "test_helper" | ||
|
|
||
| class NoTitleAttributeCounterTest < LinterTestCase | ||
| def linter_class | ||
| ERBLint::Linters::GitHub::Accessibility::NoTitleAttributeCounter | ||
| end | ||
|
|
||
| def test_warns_if_element_sets_title_and_has_no_counter_comment | ||
| @file = "<img title='octopus'></img>" | ||
| @linter.run(processed_source) | ||
|
|
||
| assert_equal(2, @linter.offenses.count) | ||
| error_messages = @linter.offenses.map(&:message).sort | ||
| assert_match(/If you must, add <%# erblint:counter GitHub::Accessibility::NoTitleAttributeCounter 1 %> to bypass this check./, error_messages.first) | ||
| assert_match(/The title attribute should never be used unless for an `<iframe>` as it is inaccessible for several groups of users./, error_messages.last) | ||
| end | ||
|
|
||
| def test_does_not_warns_if_element_sets_title_and_has_correct_counter_comment | ||
| @file = <<~ERB | ||
| <%# erblint:counter GitHub::Accessibility::NoTitleAttributeCounter 1 %> | ||
| <a href="/" title="bad">some website</a> | ||
| ERB | ||
| @linter.run(processed_source) | ||
|
|
||
| assert_equal 0, @linter.offenses.count | ||
| end | ||
|
|
||
| def test_does_not_warn_if_iframe_sets_title | ||
| @file = "<iframe title='Introduction video'></iframe>" | ||
| @linter.run(processed_source) | ||
|
|
||
| assert_empty @linter.offenses | ||
| end | ||
|
|
||
| def test_does_not_autocorrect_when_ignores_are_correct | ||
| @file = <<~ERB | ||
| <%# erblint:counter GitHub::Accessibility::NoTitleAttributeCounter 1 %> | ||
| <a href="/" title="bad">some website</a> | ||
| ERB | ||
|
|
||
| assert_equal @file, corrected_content | ||
| end | ||
|
|
||
| def test_does_autocorrect_when_ignores_are_not_correct | ||
| @file = <<~ERB | ||
| <%# erblint:counter GitHub::Accessibility::NoTitleAttributeCounter 3 %> | ||
| <a href="/" title="bad">some website</a> | ||
| ERB | ||
| refute_equal @file, corrected_content | ||
|
|
||
| expected_content = <<~ERB | ||
| <%# erblint:counter GitHub::Accessibility::NoTitleAttributeCounter 1 %> | ||
| <a href="/" title="bad">some website</a> | ||
| ERB | ||
| assert_equal expected_content, corrected_content | ||
| 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.