Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Don't require redundant role for <img alt="" /> in require-valid-alt-text rule #2954

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/rule/require-valid-alt-text.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ This rule **forbids** the following:

### `<img>`

An `<img>` must have the `alt` attribute. It must have either meaningful text, or be an empty string. If it is an empty string, the `<img>` element tag must also have `role="presentation"` or `role="none"`.
An `<img>` must have the `alt` attribute. It must have either meaningful text, or be an empty string.

The content of an `alt` attribute is used to calculate the machine-readable label of an element, whereas the text content is used to produce a label for the element. For this reason, adding a label to an icon can produce a confusing or duplicated label on a control that already has appropriate text content.

Expand Down
12 changes: 0 additions & 12 deletions lib/rules/require-valid-alt-text.js
Original file line number Diff line number Diff line change
Expand Up @@ -120,18 +120,6 @@ export default class RequireValidAltText extends Rule {
normalizedAltValue = null;
}

if (normalizedAltValue === '') {
if (['presentation', 'none'].includes(roleValue)) {
return;
}
this.logNode({
message:
'If the `alt` attribute is present and the value is an empty string, `role="presentation"` or `role="none"` must be present',
node,
});
return;
}

if (normalizedAltValue !== null) {
if (/^\d+$/g.test(normalizedAltValue)) {
this.logNode({
Expand Down
49 changes: 7 additions & 42 deletions test/unit/rules/require-valid-alt-text-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,16 @@ generateRuleTests({
'<img alt="some-alt-name">',
'<img alt="name {{picture}}">',
'<img alt="{{picture}}">',
'<img alt="">',
'<img alt="" src="zoey.jpg">',
'<img alt="" role="none">',
'<img alt="" role="presentation">',

'<img alt>',
'<img alt role="none">',
'<img alt role="presentation">',
'<img alt src="zoey.jpg">',

// Valid words containing redundant words.
'<img alt="logout">',
'<img alt="photography">',
Expand Down Expand Up @@ -96,48 +103,6 @@ generateRuleTests({
`);
},
},
{
template: '<img alt="" src="zoey.jpg">',

verifyResults(results) {
expect(results).toMatchInlineSnapshot(`
[
{
"column": 0,
"endColumn": 27,
"endLine": 1,
"filePath": "layout.hbs",
"line": 1,
"message": "If the \`alt\` attribute is present and the value is an empty string, \`role=\\"presentation\\"\` or \`role=\\"none\\"\` must be present",
"rule": "require-valid-alt-text",
"severity": 2,
"source": "<img alt=\\"\\" src=\\"zoey.jpg\\">",
},
]
`);
},
},
{
template: '<img alt src="zoey.jpg">',

verifyResults(results) {
expect(results).toMatchInlineSnapshot(`
[
{
"column": 0,
"endColumn": 24,
"endLine": 1,
"filePath": "layout.hbs",
"line": 1,
"message": "If the \`alt\` attribute is present and the value is an empty string, \`role=\\"presentation\\"\` or \`role=\\"none\\"\` must be present",
"rule": "require-valid-alt-text",
"severity": 2,
"source": "<img alt src=\\"zoey.jpg\\">",
},
]
`);
},
},
{
template: '<img alt="path/to/zoey.jpg" src="path/to/zoey.jpg">',
verifyResults(results) {
Expand Down