Skip to content

Commit 3fc0b67

Browse files
author
Adrián Bolonio
authored
Merge branch 'master' into accessibility/issues/1430/migrate-a11y-rubocop-rules-link-href
2 parents 295968e + 3e11dc9 commit 3fc0b67

8 files changed

Lines changed: 181 additions & 1 deletion

File tree

config/accessibility.yml

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,10 @@ GitHub/Accessibility/ImageHasAlt:
66
StyleGuide: https://github.com/github/rubocop-github/blob/master/guides/image-has-alt.md
77
GitHub/Accessibility/LinkHasHref:
88
Enabled: true
9-
StyleGuide: https://github.com/github/rubocop-github/blob/master/guides/link-has-href.md
9+
StyleGuide: https://github.com/github/rubocop-github/blob/master/guides/link-has-href.md
10+
GitHub/Accessibility/NoPositiveTabindex:
11+
Enabled: true
12+
StyleGuide: https://github.com/github/rubocop-github/blob/master/guides/no-positive-tabindex.md
13+
GitHub/Accessibility/NoRedundantImageAlt:
14+
Enabled: true
15+
StyleGuide: https://github.com/github/rubocop-github/blob/master/guides/no-redundant-image-alt.md

guides/no-positive-tabindex.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# GitHub/Accessibility/NoPositiveTabindex
2+
3+
## Rule Details
4+
5+
Positive tabindex is error-prone and often inaccessible.
6+
7+
## Resources
8+
9+
- [F44: Failure of Success Criterion 2.4.3 due to using tabindex to create a tab order that does not preserve meaning and operability](https://www.w3.org/TR/WCAG20-TECHS/F44.html)
10+
- [Deque University: Avoid Using Tabindex with Positive Numbers](https://dequeuniversity.com/tips/tabindex-positive-numbers)
11+
12+
## Examples
13+
### **Incorrect** code for this rule 👎
14+
15+
```erb
16+
<%= button_tag "Continue", :tabindex => 3 %>
17+
```
18+
19+
### **Correct** code for this rule 👍
20+
21+
```erb
22+
<!-- good -->
23+
<%= button_tag "Continue", :tabindex => -1 %>
24+
```

guides/no-redundant-image-alt.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# GitHub/Accessibility/NoRedundantImageAlt
2+
3+
## Rule Details
4+
5+
Alt prop should not contain `image` or `picture` as screen readers already announce the element as an image
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 "cat.gif", size: "12x12", alt: "Picture of a cat" %>
17+
```
18+
19+
### **Correct** code for this rule 👍
20+
21+
```erb
22+
<!-- good -->
23+
<%= image_tag "cat.gif", size: "12x12", alt: "A black cat" %>
24+
```
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# frozen_string_literal: true
2+
3+
require "rubocop"
4+
5+
module RuboCop
6+
module Cop
7+
module GitHub
8+
module Accessibility
9+
class NoPositiveTabindex < Base
10+
MSG = "Positive tabindex is error-prone and often inaccessible."
11+
12+
def on_send(node)
13+
receiver, method_name, *args = *node
14+
if receiver.nil?
15+
args.select do |arg|
16+
arg.type == :hash
17+
end.each do |hash|
18+
hash.each_pair do |key, value|
19+
next if key.type == :dsym
20+
next unless key.respond_to?(:value)
21+
if key.value == :tabindex && value.source.to_i > 0
22+
add_offense(hash)
23+
end
24+
end
25+
end
26+
end
27+
end
28+
end
29+
end
30+
end
31+
end
32+
end
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# frozen_string_literal: true
2+
3+
require "rubocop"
4+
5+
module RuboCop
6+
module Cop
7+
module GitHub
8+
module Accessibility
9+
class NoRedundantImageAlt < Base
10+
MSG = "Alt prop should not contain `image` or `picture` as screen readers already announce the element as an image"
11+
REDUNDANT_ALT_WORDS = %w(image picture)
12+
13+
def_node_search :redundant_alt?, "(pair (sym :alt) (str #contains_redundant_alt_text?))"
14+
15+
def on_send(node)
16+
receiver, method_name, _= *node
17+
18+
if receiver.nil? && method_name == :image_tag
19+
if redundant_alt?(node)
20+
add_offense(node.loc.selector)
21+
end
22+
end
23+
end
24+
25+
private
26+
27+
def contains_redundant_alt_text?(string)
28+
return false if string.empty?
29+
30+
if (string.downcase.split & REDUNDANT_ALT_WORDS).any?
31+
return true
32+
end
33+
end
34+
end
35+
end
36+
end
37+
end
38+
end

test/test_no_positive_tabindex.rb

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/no_positive_tabindex"
6+
7+
class NoPositiveTabindex < CopTest
8+
def cop_class
9+
RuboCop::Cop::GitHub::Accessibility::NoPositiveTabindex
10+
end
11+
12+
def test_no_positive_tabindex_alt_offense
13+
offenses = erb_investigate cop, <<-ERB, "app/views/products/index.html.erb"
14+
<%= button_tag "Continue", :tabindex => 3 %>
15+
ERB
16+
17+
assert_equal 1, offenses.count
18+
assert_equal "Positive tabindex is error-prone and often inaccessible.", offenses[0].message
19+
end
20+
21+
def test_no_positive_tabindex_alt_no_offense
22+
offenses = erb_investigate cop, <<-ERB, "app/views/products/index.html.erb"
23+
<%= button_tag "Continue", :tabindex => -1 %>
24+
ERB
25+
26+
assert_equal 0, offenses.count
27+
end
28+
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/no_redundant_image_alt"
6+
7+
class NoRedundantImageAlt < CopTest
8+
def cop_class
9+
RuboCop::Cop::GitHub::Accessibility::NoRedundantImageAlt
10+
end
11+
12+
def test_no_redundant_image_alt_offense
13+
offenses = erb_investigate cop, <<-ERB, "app/views/products/index.html.erb"
14+
<%= image_tag "cat.gif", size: "12x12", alt: "Picture of a cat" %>
15+
ERB
16+
17+
assert_equal 1, offenses.count
18+
assert_equal "Alt prop should not contain `image` or `picture` as screen readers already announce the element as an image", offenses[0].message
19+
end
20+
21+
def test_no_redundant_image_alt_no_offense
22+
offenses = erb_investigate cop, <<-ERB, "app/views/products/index.html.erb"
23+
<%= image_tag "cat.gif", size: "12x12", alt: "A black cat" %>
24+
ERB
25+
26+
assert_equal 0, offenses.count
27+
end
28+
end

0 commit comments

Comments
 (0)