-
Notifications
You must be signed in to change notification settings - Fork 779
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(image-alt): require alt text or empty strings
The image-alt rule now checks for space characters, and fails if they are present. Empty alt attributes are still fine, and text content is still fine. This fixes cases where ATs do not skip decorative images because of the space characters in the alt attribute. Closes #1174
- Loading branch information
Marcy Sutton
committed
Nov 27, 2018
1 parent
26fa49a
commit 37962e2
Showing
6 changed files
with
54 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
let nn = node.nodeName.toLowerCase(); | ||
let validSetup = node.hasAttribute('alt') && (nn === 'img' || nn === 'input' || nn === 'area'); | ||
let validAttrValue = /^\s+$/.test(node.getAttribute('alt')); | ||
return ( | ||
validSetup && validAttrValue | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
{ | ||
"id": "alt-empty-space", | ||
"evaluate": "alt-empty-space.js", | ||
"metadata": { | ||
"impact": "critical", | ||
"messages": { | ||
"pass": "Element has a valid alt attribute value", | ||
"fail": "Element has an alt attribute containing only a space character" | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -21,5 +21,7 @@ | |
"role-presentation", | ||
"role-none" | ||
], | ||
"none": [] | ||
"none": [ | ||
"alt-empty-space" | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
describe('alt-empty-space', function() { | ||
'use strict'; | ||
|
||
var checkSetup = axe.testUtils.checkSetup; | ||
var checkContext = axe.testUtils.MockCheckContext(); | ||
var check = checks['alt-empty-space']; | ||
|
||
afterEach(function() { | ||
checkContext.reset(); | ||
}); | ||
|
||
it('should return true if alt contains a space character', function() { | ||
var params = checkSetup('<img id="target" alt=" " />'); | ||
assert.isTrue(check.evaluate.apply(checkContext, params)); | ||
}); | ||
|
||
it('should return true if alt contains a non-breaking space character', function() { | ||
var params = checkSetup('<img id="target" alt=" " />'); | ||
assert.isTrue(check.evaluate.apply(checkContext, params)); | ||
}); | ||
|
||
it('should return false if alt attribute is empty', function() { | ||
var params = checkSetup('<img id="target" alt="" />') | ||
assert.isFalse(check.evaluate.apply(checkContext, params)); | ||
}); | ||
|
||
it('should return false if alt attribute has a proper text value', function() { | ||
var params = checkSetup('<img id="target" alt="text content" />') | ||
assert.isFalse(check.evaluate.apply(checkContext, params)); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters