Skip to content

Commit

Permalink
feat(image-alt): require alt text or empty strings (#1260)
Browse files Browse the repository at this point in the history
* 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

* chore: include updated build files

(there are no actual changes)

* chore: rename to alt-space-value, focus on images

* fix: PR feedback
  • Loading branch information
marcysutton authored and WilcoFiers committed Dec 7, 2018
1 parent 9930bb9 commit e24cea9
Show file tree
Hide file tree
Showing 7 changed files with 58 additions and 17 deletions.
2 changes: 2 additions & 0 deletions lib/checks/label/alt-space-value.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
const validAttrValue = /^\s+$/.test(node.getAttribute('alt'));
return node.hasAttribute('alt') && validAttrValue;
11 changes: 11 additions & 0 deletions lib/checks/label/alt-space-value.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"id": "alt-space-value",
"evaluate": "alt-space-value.js",
"metadata": {
"impact": "critical",
"messages": {
"pass": "Element has a valid alt attribute value",
"fail": "Element has an alt attribute containing only a space character, which is not ignored by all screen readers"
}
}
}
2 changes: 1 addition & 1 deletion lib/rules/image-alt.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,5 @@
"role-presentation",
"role-none"
],
"none": []
"none": ["alt-space-value"]
}
25 changes: 10 additions & 15 deletions lib/rules/landmark-complementary-is-top-level.json
Original file line number Diff line number Diff line change
@@ -1,17 +1,12 @@
{
"id": "landmark-complementary-is-top-level",
"selector": "aside:not([role]), [role=complementary]",
"tags": [
"cat.semantics",
"best-practice"
],
"metadata": {
"description": "Ensures the complementary landmark or aside is at top level",
"help": "Aside must not be contained in another landmark"
},
"all": [],
"any": [
"landmark-is-top-level"
],
"none": []
"id": "landmark-complementary-is-top-level",
"selector": "aside:not([role]), [role=complementary]",
"tags": ["cat.semantics", "best-practice"],
"metadata": {
"description": "Ensures the complementary landmark or aside is at top level",
"help": "Aside must not be contained in another landmark"
},
"all": [],
"any": ["landmark-is-top-level"],
"none": []
}
31 changes: 31 additions & 0 deletions test/checks/label/alt-space-value.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
describe('alt-space-value', function() {
'use strict';

var checkSetup = axe.testUtils.checkSetup;
var checkContext = axe.testUtils.MockCheckContext();
var check = checks['alt-space-value'];

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="&nbsp;" />');
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));
});
});
1 change: 1 addition & 0 deletions test/integration/rules/image-alt/image-alt.html
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,6 @@
<img src="img.jpg" id="pass7" role="none">
<img src="img.jpg" id="pass8" aria-labelledby="ninjamonkeys">
<div role="img" alt="blah" id="violation6"></div>
<img src="img.jpg" id="violation7" alt=" " />
<div role="img" aria-label="blah" id="pass9"></div>
<svg role="img" id="ignore1"><title>SVG Title</title></svg>
3 changes: 2 additions & 1 deletion test/integration/rules/image-alt/image-alt.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
["#violation3"],
["#violation4"],
["#violation5"],
["#violation6"]
["#violation6"],
["#violation7"]
],
"passes": [
["#pass1"],
Expand Down

0 comments on commit e24cea9

Please sign in to comment.