Skip to content

Commit 7040e04

Browse files
author
Adrián Bolonio
authored
Merge branch 'master' into accessibility/issues/1430/migrate-a11y-rubocop-rules-no-redundant-image-alt
2 parents 19eb191 + 3399237 commit 7040e04

File tree

5 files changed

+91
-1
lines changed

5 files changed

+91
-1
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ inherit_gem:
1919
rubocop-github:
2020
- config/default.yml
2121
- config/rails.yml
22+
- config/accessibility.yml
2223
```
2324
2425
### Legacy usage
@@ -33,4 +34,4 @@ If you are using a rubocop version < 1.0.0, you can use rubocop-github version
3334

3435
## The Cops
3536

36-
All cops are located under [`lib/rubocop/cop/github`](lib/rubocop/cop/github), and contain examples/documentation.
37+
All cops are located under [`lib/rubocop/cop/github`](lib/rubocop/cop/github) and [`lib/rubocop/cop/github/accessibility`](lib/rubocop/cop/github/accessibility), and contain examples/documentation.

config/accessibility.yml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
require:
2+
- rubocop/cop/github/accessibility
3+
4+
GitHub/Accessibility/ImageHasAlt:
5+
Enabled: true
6+
StyleGuide: https://github.com/github/rubocop-github/blob/master/guides/image-has-alt.md

guides/image-has-alt.md

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# GitHub/Accessibility/ImageHasAlt
2+
3+
## Rule Details
4+
5+
Images should have an alt prop with meaningful text or an empty string for decorative images.
6+
7+
## Resources
8+
9+
- [W3C WAI Images Tutorial](https://www.w3.org/WAI/tutorials/images/)
10+
- [Primer: Alternative text for images](https://primer.style/design/accessibility/alternative-text-for-images)
11+
12+
## Examples
13+
### **Incorrect** code for this rule 👎
14+
15+
```erb
16+
<%= image_tag "spinners/octocat-spinner-16px.gif", size: "12x12" %>
17+
```
18+
19+
### **Correct** code for this rule 👍
20+
21+
```erb
22+
<!-- good -->
23+
<%= image_tag "spinners/octocat-spinner-16px.gif", size: "12x12", alt: "GitHub Logo spinner" %>
24+
```
25+
26+
```erb
27+
<!-- also good -->
28+
<%= image_tag "spinners/octocat-spinner-16px.gif", size: "12x12", alt: "" %>
29+
```
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# frozen_string_literal: true
2+
3+
require "rubocop"
4+
5+
module RuboCop
6+
module Cop
7+
module GitHub
8+
module Accessibility
9+
class ImageHasAlt < Base
10+
MSG = "Images should have an alt prop with meaningful text or an empty string for decorative images"
11+
12+
def_node_search :has_alt_attribute?, "(sym :alt)"
13+
14+
def on_send(node)
15+
receiver, method_name, _= *node
16+
17+
if receiver.nil? && method_name == :image_tag
18+
alt = has_alt_attribute?(node)
19+
add_offense(node.loc.selector) if alt.nil?
20+
end
21+
end
22+
end
23+
end
24+
end
25+
end
26+
end
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# frozen_string_literal: true
2+
3+
require_relative "./cop_test"
4+
require "minitest/autorun"
5+
require "rubocop/cop/github/accessibility/image_has_alt"
6+
7+
class TestImageHasAlt < CopTest
8+
def cop_class
9+
RuboCop::Cop::GitHub::Accessibility::ImageHasAlt
10+
end
11+
12+
def test_image_has_alt_offense
13+
offenses = erb_investigate cop, <<-ERB, "app/views/products/index.html.erb"
14+
<%= image_tag "spinners/octocat-spinner-16px.gif", size: "12x12" %>
15+
ERB
16+
17+
assert_equal 1, offenses.count
18+
assert_equal "Images should have an alt prop with meaningful text or an empty string for decorative images", offenses[0].message
19+
end
20+
21+
def test_image_has_alt_no_offense
22+
offenses = erb_investigate cop, <<-ERB, "app/views/products/index.html.erb"
23+
<%= image_tag "spinners/octocat-spinner-16px.gif", size: "12x12", alt: "GitHub Logo spinner" %>
24+
ERB
25+
26+
assert_equal 0, offenses.count
27+
end
28+
end

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy