Skip to content

Commit 94d782c

Browse files
authored
Merge pull request #64 from github/kh-fix-landmark-logic
Fix and rename LandmarkHasLabel rule to NavigationHasLabel
2 parents a14e7c3 + 0a0e22a commit 94d782c

File tree

7 files changed

+116
-131
lines changed

7 files changed

+116
-131
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ linters:
3535
enabled: true
3636
GitHub::Accessibility::ImageHasAlt:
3737
enabled: true
38-
GitHub::Accessibility::LandmarkHasLabel:
38+
GitHub::Accessibility::NavigationHasLabel:
3939
enabled: true
4040
GitHub::Accessibility::LinkHasHref:
4141
enabled: true
@@ -61,7 +61,7 @@ linters:
6161
- [GitHub::Accessibility::AvoidBothDisabledAndAriaDisabled](./docs/rules/accessibility/avoid-both-disabled-and-aria-disabled.md)
6262
- [GitHub::Accessibility::AvoidGenericLinkText](./docs/rules/accessibility/avoid-generic-link-text.md)
6363
- [GitHub::Accessibility::DisabledAttribute](./docs/rules/accessibility/disabled-attribute.md)
64-
- [GitHub::Accessibility::LandmarkHasLabel](./docs/rules/accessibility/landmark-has-label.md)
64+
- [GitHub::Accessibility::NavigationHasLabel](./docs/rules/accessibility/navigation-has-label.md)
6565
- [GitHub::Accessibility::LinkHasHref](./docs/rules/accessibility/link-has-href.md)
6666
- [GitHub::Accessibility::NestedInteractiveElements](./docs/rules/accessibility/nested-interactive-elements.md)
6767
- [GitHub::Accessibility::IframeHasTitle](./docs/rules/accessibility/iframe-has-title.md)

docs/rules/accessibility/landmark-has-label.md

Lines changed: 0 additions & 28 deletions
This file was deleted.
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# Navigation Has Label
2+
3+
## Rule Details
4+
5+
This rule enforces that a navigation landmark (a `<nav>` or a `role="navigation"`) has an accessible name. This rule is helpful to enforce for sites (like GitHub) where multiple navigation is common.
6+
7+
The navigation landmark element should have an `aria-label` attribute, or `aria-labelledby` to distinguish it from other elements.
8+
9+
## Resources
10+
11+
- [ARIA Landmarks Example](https://www.w3.org/WAI/ARIA/apg/example-index/landmarks/index.html)
12+
13+
## Examples
14+
### **Incorrect** code for this rule 👎
15+
16+
```erb
17+
<!-- incorrect -->
18+
<nav>
19+
<h1>This is a text</h1>
20+
</nav>
21+
```
22+
23+
### **Correct** code for this rule 👍
24+
25+
```erb
26+
<!-- correct -->
27+
<nav aria-labelledby="title_id"t>
28+
<h1 id="title_id">This is a text</h1>
29+
</nav>
30+
```

lib/erblint-github/linters/github/accessibility/landmark_has_label.rb

Lines changed: 0 additions & 68 deletions
This file was deleted.
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
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 NavigationHasLabel < Linter
10+
include ERBLint::Linters::CustomHelpers
11+
include LinterRegistry
12+
13+
MESSAGE = "The navigation landmark should have a unique accessible name via `aria-label` or `aria-labelledby`. Remember that the name does not need to include `navigation` or `nav` since it will already be announced."
14+
15+
def run(processed_source)
16+
tags(processed_source).each do |tag|
17+
next if tag.closing?
18+
next unless possible_attribute_values(tag, "role").include?("navigation") || tag.name == "nav"
19+
if possible_attribute_values(tag, "aria-label").empty? && possible_attribute_values(tag, "aria-labelledby").empty?
20+
message = MESSAGE
21+
if tag.name != "nav"
22+
message += "Additionally, you can safely drop the `role='navigation'` and replace it with the native HTML `nav` element."
23+
end
24+
generate_offense(self.class, processed_source, tag, message)
25+
end
26+
27+
end
28+
end
29+
end
30+
end
31+
end
32+
end
33+
end

test/linters/accessibility/landmark_has_label_test.rb

Lines changed: 0 additions & 33 deletions
This file was deleted.
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
# frozen_string_literal: true
2+
3+
require "test_helper"
4+
5+
class NavigationHasLabelTest < LinterTestCase
6+
def linter_class
7+
ERBLint::Linters::GitHub::Accessibility::NavigationHasLabel
8+
end
9+
10+
def test_warns_if_navigation_landmark_has_no_label
11+
@file = <<~ERB
12+
<nav>
13+
</nav>
14+
ERB
15+
@linter.run(processed_source)
16+
17+
assert_equal(1, @linter.offenses.count)
18+
assert_match(/The navigation landmark should have a unique accessible name via `aria-label` or `aria-labelledby`./, @linter.offenses.first.message)
19+
end
20+
21+
def test_warns_if_navigation_role_landmark_has_no_label
22+
@file = <<~ERB
23+
<div role="navigation">
24+
</div>
25+
ERB
26+
@linter.run(processed_source)
27+
28+
assert_equal(1, @linter.offenses.count)
29+
assert_match(/The navigation landmark should have a unique accessible name via `aria-label` or `aria-labelledby`./, @linter.offenses.first.message)
30+
end
31+
32+
def test_does_not_warn_if_navigation_landmark_has_aria_labelled_by
33+
@file = <<~ERB
34+
<nav aria-labelledby="title_id"t>
35+
</nav>
36+
ERB
37+
@linter.run(processed_source)
38+
39+
assert_empty @linter.offenses
40+
end
41+
42+
def test_does_not_warn_if_navigation_landmark_has_aria_label
43+
@file = <<~ERB
44+
<nav aria-label="Repos"t>
45+
</nav>
46+
ERB
47+
@linter.run(processed_source)
48+
49+
assert_empty @linter.offenses
50+
end
51+
end

0 commit comments

Comments
 (0)