File tree Expand file tree Collapse file tree 5 files changed +120
-1
lines changed
lib/erblint-github/linters/github/accessibility
test/linters/accessibility Expand file tree Collapse file tree 5 files changed +120
-1
lines changed Original file line number Diff line number Diff line change @@ -23,13 +23,20 @@ require "erblint-github/linters"
2323``` yaml
2424---
2525linters :
26+ GitHub::Accessibility::ImageHasAlt :
27+ enabled : true
2628 GitHub::Accessibility::NoRedundantImageAlt :
2729 enabled : true
2830` ` `
2931
32+ ### Rules
33+
34+ - [GitHub::Accessibility::NoRedundantImageAlt](./docs/rules/accessibility/no-redundant-image-alt.md)
35+ - [GitHub::Accessibility::ImageHasAlt](./docs/rules/accessibility/image-has-alt.md)
36+
3037## Testing
3138
3239` ` `
3340bundle install
34- bundle exec rake test
41+ bundle exec rake
3542```
Original file line number Diff line number Diff line change 1+ # Image Has Alt
2+
3+ ## Rule Details
4+
5+ ` <img> ` should have an alt prop with meaningful text or an empty string for decorative images.
6+
7+ Learn more at [ W3C WAI Images Tutorial] ( https://www.w3.org/WAI/tutorials/images/ ) .
8+
9+ 👎 Examples of ** incorrect** code for this rule:
10+
11+ ``` erb
12+ <img src="logo.png">
13+ ```
14+
15+ 👍 Examples of ** correct** code for this rule:
16+
17+ ``` erb
18+ <!-- good -->
19+ <img alt="" src="logo.png" >
20+ ```
21+
22+ ``` erb
23+ <!-- also good -->
24+ <a href='https://github.com/'><img alt="GitHub homepage" src="logo.png" ></a>
25+ ```
Original file line number Diff line number Diff line change 1+ # No redundant image alt
2+
3+ ## Rule Details
4+
5+ ` <img> ` alt prop should not contain ` image ` or ` picture ` as screen readers already announce the element as an image.
6+
7+ Learn more at [ W3C WAI Images Tutorial] ( https://www.w3.org/WAI/tutorials/images/ ) .
8+
9+ 👎 Examples of ** incorrect** code for this rule:
10+
11+ ``` erb
12+ <img alt="picture of Mona Lisa" src="monalisa.png">
13+ ```
14+
15+ 👍 Examples of ** correct** code for this rule:
16+
17+ ``` erb
18+ <!-- good -->
19+ <img alt="Mona Lisa" src="monalisa.png">
20+ ```
21+
22+
23+ ``` erb
24+ <!-- also good -->
25+ <img alt="The original painting of Mona Lisa hangs on the wall of Louvre museum" src="monalisa.png">
26+ ```
Original file line number Diff line number Diff line change 1+ # frozen_string_literal: true
2+
3+ require_relative "../../custom_helpers"
4+
5+ module ERBLint
6+ module Linters
7+ module GitHub
8+ module Accessibility
9+ class ImageHasAlt < Linter
10+ include ERBLint ::Linters ::CustomHelpers
11+ include LinterRegistry
12+
13+ MESSAGE = "<img> should have an alt prop with meaningful text or an empty string for decorative images"
14+
15+ def run ( processed_source )
16+ tags ( processed_source ) . each do |tag |
17+ next if tag . name != "img"
18+ next if tag . closing?
19+
20+ alt = possible_attribute_values ( tag , "alt" )
21+
22+ generate_offense ( self . class , processed_source , tag ) if alt . empty?
23+ end
24+
25+ rule_disabled? ( processed_source )
26+ end
27+ end
28+ end
29+ end
30+ end
31+ end
Original file line number Diff line number Diff line change 1+ # frozen_string_literal: true
2+
3+ require "test_helper"
4+
5+ class ImageHasAltTest < LinterTestCase
6+ def linter_class
7+ ERBLint ::Linters ::GitHub ::Accessibility ::ImageHasAlt
8+ end
9+
10+ def test_warns_if_image_has_no_alt_attribute
11+ @file = "<img></img>"
12+ @linter . run ( processed_source )
13+
14+ refute_empty @linter . offenses
15+ end
16+
17+ def test_does_not_warn_if_image_has_alt_attribute_set_to_empty_string
18+ @file = "<img alt=''></img>"
19+ @linter . run ( processed_source )
20+
21+ assert_empty @linter . offenses
22+ end
23+
24+ def test_does_not_warn_if_image_has_alt_attribute_set_to_string
25+ @file = "<img alt='monalisa'></img>"
26+ @linter . run ( processed_source )
27+
28+ assert_empty @linter . offenses
29+ end
30+ end
You can’t perform that action at this time.
0 commit comments