-
Notifications
You must be signed in to change notification settings - Fork 8
Add image-has-alt and better documentation of rules
#3
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
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,25 @@ | ||
| # Image Has Alt | ||
|
|
||
| ## Rule Details | ||
|
|
||
| `<img>` should have an alt prop with meaningful text or an empty string for decorative images. | ||
|
|
||
| Learn more at [W3C WAI Images Tutorial](https://www.w3.org/WAI/tutorials/images/). | ||
|
|
||
| 👎 Examples of **incorrect** code for this rule: | ||
|
|
||
| ```erb | ||
| <img src="logo.png"> | ||
| ``` | ||
|
|
||
| 👍 Examples of **correct** code for this rule: | ||
|
|
||
| ```erb | ||
| <!-- good --> | ||
| <img alt="" src="logo.png" > | ||
| ``` | ||
|
|
||
| ```erb | ||
| <!-- also good --> | ||
| <a href='https://github.com/'><img alt="GitHub homepage" src="logo.png" ></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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| # No redundant image alt | ||
|
|
||
| ## Rule Details | ||
|
|
||
| `<img>` alt prop should not contain `image` or `picture` as screen readers already announce the element as an image. | ||
|
|
||
| Learn more at [W3C WAI Images Tutorial](https://www.w3.org/WAI/tutorials/images/). | ||
|
|
||
| 👎 Examples of **incorrect** code for this rule: | ||
|
|
||
| ```erb | ||
| <img alt="picture of Mona Lisa" src="monalisa.png"> | ||
| ``` | ||
|
|
||
| 👍 Examples of **correct** code for this rule: | ||
|
|
||
| ```erb | ||
| <!-- good --> | ||
| <img alt="Mona Lisa" src="monalisa.png"> | ||
| ``` | ||
|
|
||
|
|
||
| ```erb | ||
| <!-- also good --> | ||
| <img alt="The original painting of Mona Lisa hangs on the wall of Louvre museum" src="monalisa.png"> | ||
| ``` |
31 changes: 31 additions & 0 deletions
31
lib/erblint-github/linters/github/accessibility/image_has_alt.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,31 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| require_relative "../../custom_helpers" | ||
|
|
||
| module ERBLint | ||
| module Linters | ||
| module GitHub | ||
| module Accessibility | ||
| class ImageHasAlt < Linter | ||
| include ERBLint::Linters::CustomHelpers | ||
| include LinterRegistry | ||
|
|
||
| MESSAGE = "<img> should have an alt prop with meaningful text or an empty string for decorative images" | ||
|
|
||
| def run(processed_source) | ||
| tags(processed_source).each do |tag| | ||
| next if tag.name != "img" | ||
| next if tag.closing? | ||
|
|
||
| alt = possible_attribute_values(tag, "alt") | ||
|
|
||
| generate_offense(self.class, processed_source, tag) if alt.empty? | ||
| end | ||
|
|
||
| rule_disabled?(processed_source) | ||
| 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| require "test_helper" | ||
|
|
||
| class ImageHasAltTest < LinterTestCase | ||
| def linter_class | ||
| ERBLint::Linters::GitHub::Accessibility::ImageHasAlt | ||
| end | ||
|
|
||
| def test_warns_if_image_has_no_alt_attribute | ||
| @file = "<img></img>" | ||
| @linter.run(processed_source) | ||
|
|
||
| refute_empty @linter.offenses | ||
| end | ||
|
|
||
| def test_does_not_warn_if_image_has_alt_attribute_set_to_empty_string | ||
| @file = "<img alt=''></img>" | ||
| @linter.run(processed_source) | ||
|
|
||
| assert_empty @linter.offenses | ||
| end | ||
|
|
||
| def test_does_not_warn_if_image_has_alt_attribute_set_to_string | ||
| @file = "<img alt='monalisa'></img>" | ||
| @linter.run(processed_source) | ||
|
|
||
| assert_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.
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.
Shouldn't be
mustinstead ofshould? (same for all instances). Other than that, looks good to me :)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.
mustdoes come off a bit stronger but I think both are ok :)