Skip to content

Commit 8226a4b

Browse files
authored
Merge pull request #14 from github/kh-add_iframe_title_rule
Add iframe has title rule
2 parents 7e064cd + 4482f18 commit 8226a4b

File tree

8 files changed

+104
-8
lines changed

8 files changed

+104
-8
lines changed

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ require "erblint-github/linters"
2525
linters:
2626
GitHub::Accessibility::AvoidBothDisabledAndAriaDisabled:
2727
enabled: true
28+
GitHub::Accessibility::IframeHasTitle:
29+
enabled: true
2830
GitHub::Accessibility::ImageHasAlt:
2931
enabled: true
3032
GitHub::Accessibility::NoAriaLabelMisuse:
@@ -36,6 +38,7 @@ linters:
3638
## Rules
3739
3840
- [GitHub::Accessibility::AvoidBothDisabledAndAriaDisabled](./docs/rules/accessibility/avoid-both-disabled-and-aria-disabled.md)
41+
- [GitHub::Accessibility::IframeHasTitle](./docs/rules/accessibility/iframe-has-title.md)
3942
- [GitHub::Accessibility::ImageHasAlt](./docs/rules/accessibility/image-has-alt.md)
4043
- [GitHub::Accessibility::NoAriaLabelMisuse](./docs/rules/accessibility/no-aria-label-misuse.md)
4144
- [GitHub::Accessibility::NoRedundantImageAlt](./docs/rules/accessibility/no-redundant-image-alt.md)

docs/rules/accessibility/avoid-both-disabled-and-aria-disabled.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,14 @@ HTML elements with `disabled` are ignored when a screen reader uses tab navigati
99

1010
This linter will raise when both `aria-disabled` and `disabled` are set on HTML elements that natively support `disabled` including `button`, `fieldset`, `input`, `optgroup`, `option`, `select`, and `textarea`.
1111

12-
👎 Examples of **incorrect** code for this rule:
12+
### 👎 Examples of **incorrect** code for this rule:
1313

1414
```erb
1515
<button aria-disabled="true" disabled="true">
1616
<input aria-disabled="true" disabled="true">
1717
```
1818

19-
👍 Examples of **correct** code for this rule:
19+
### 👍 Examples of **correct** code for this rule:
2020

2121
```erb
2222
<button disabled="true">
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# Iframe has title
2+
3+
## Rule Details
4+
5+
`<iframe>` should have a unique title attribute that identifies the content. The title will help screen reader users determine whether to explore the frame in detail. If an `<iframe>` contains no meaningful content, hide it by setting `aria-hidden="true"`.
6+
7+
### 👎 Examples of **incorrect** code for this rule:
8+
9+
```erb
10+
<iframe src="../welcome-video"></iframe>
11+
```
12+
13+
### 👍 Examples of **correct** code for this rule:
14+
15+
```erb
16+
<!-- good -->
17+
<iframe src="../welcome-video" title="Welcome to GitHub Video" ></iframe>
18+
```
19+
20+
```erb
21+
<!-- also good -->
22+
<iframe aria-hidden="true">
23+
<!-- Meaningless JavaScript code -->
24+
</iframe>
25+
```

docs/rules/accessibility/image-has-alt.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,13 @@
66

77
Learn more at [W3C WAI Images Tutorial](https://www.w3.org/WAI/tutorials/images/).
88

9-
👎 Examples of **incorrect** code for this rule:
9+
### 👎 Examples of **incorrect** code for this rule:
1010

1111
```erb
1212
<img src="logo.png">
1313
```
1414

15-
👍 Examples of **correct** code for this rule:
15+
### 👍 Examples of **correct** code for this rule:
1616

1717
```erb
1818
<!-- good -->

docs/rules/accessibility/no-aria-label-misuse.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ Also check out the following resources:
1212
- [w3c/aria Consider prohibiting author naming certain roles #833](https://github.com/w3c/aria/issues/833)
1313
- [Not so short note on aria-label usage - Big Table Edition](https://html5accessibility.com/stuff/2020/11/07/not-so-short-note-on-aria-label-usage-big-table-edition/)
1414

15-
👎 Examples of **incorrect** code for this rule:
15+
### 👎 Examples of **incorrect** code for this rule:
1616

1717
```erb
1818
<span aria-label="This does something">Hello</span>
@@ -26,7 +26,7 @@ Also check out the following resources:
2626
<h1 aria-label="This will override the content">Page title</h1>
2727
```
2828

29-
👍 Examples of **correct** code for this rule:
29+
### 👍 Examples of **correct** code for this rule:
3030

3131
```erb
3232
<span>Hello</span>

docs/rules/accessibility/no-redundant-image-alt.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,13 @@
66

77
Learn more at [W3C WAI Images Tutorial](https://www.w3.org/WAI/tutorials/images/).
88

9-
👎 Examples of **incorrect** code for this rule:
9+
### 👎 Examples of **incorrect** code for this rule:
1010

1111
```erb
1212
<img alt="picture of Mona Lisa" src="monalisa.png">
1313
```
1414

15-
👍 Examples of **correct** code for this rule:
15+
### 👍 Examples of **correct** code for this rule:
1616

1717
```erb
1818
<!-- good -->
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_relative "../../custom_helpers"
4+
5+
module ERBLint
6+
module Linters
7+
module GitHub
8+
module Accessibility
9+
class IframeHasTitle < Linter
10+
include ERBLint::Linters::CustomHelpers
11+
include LinterRegistry
12+
13+
MESSAGE = "<iframe> with meaningful content should have a title attribute that identifies the content."\
14+
" If <iframe> has no meaningful content, hide it from assistive technology with `aria-hidden='true'`."\
15+
16+
def run(processed_source)
17+
tags(processed_source).each do |tag|
18+
next if tag.name != "iframe"
19+
next if tag.closing?
20+
21+
title = possible_attribute_values(tag, "title")
22+
23+
generate_offense(self.class, processed_source, tag) if title.empty? && !aria_hidden?(tag)
24+
end
25+
26+
rule_disabled?(processed_source)
27+
end
28+
29+
private
30+
31+
def aria_hidden?(tag)
32+
tag.attributes["aria-hidden"]&.value&.present?
33+
end
34+
end
35+
end
36+
end
37+
end
38+
end
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# frozen_string_literal: true
2+
3+
require "test_helper"
4+
5+
class IframeHasTitle < LinterTestCase
6+
def linter_class
7+
ERBLint::Linters::GitHub::Accessibility::IframeHasTitle
8+
end
9+
10+
def test_warns_if_iframe_has_no_title
11+
@file = "<iframe alt='image of an octopus'></iframe>"
12+
@linter.run(processed_source)
13+
14+
refute_empty @linter.offenses
15+
end
16+
17+
def test_does_not_warn_if_iframe_has_aria_hidden_to_true
18+
@file = "<iframe aria-hidden='true'></iframe>"
19+
@linter.run(processed_source)
20+
21+
assert_empty @linter.offenses
22+
end
23+
24+
def test_does_not_warn_if_iframe_has_title_set_to_string
25+
@file = "<iframe title='Video tutorial of GitHub Actions'></iframe>"
26+
@linter.run(processed_source)
27+
28+
assert_empty @linter.offenses
29+
end
30+
end

0 commit comments

Comments
 (0)