Skip to content

Commit d9c1e9c

Browse files
committed
Migrate iframe title rule
1 parent 7e064cd commit d9c1e9c

File tree

4 files changed

+96
-0
lines changed

4 files changed

+96
-0
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)
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+
```
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)