Skip to content

Commit

Permalink
fix: ignore empty, whitespace or undefined role for rule `ari… (#2077)
Browse files Browse the repository at this point in the history
  • Loading branch information
jeeyyy authored and straker committed Mar 6, 2020
1 parent 989b317 commit dbd3c02
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 0 deletions.
1 change: 1 addition & 0 deletions lib/rules/aria-roles.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"id": "aria-roles",
"selector": "[role]",
"matches": "no-empty-role-matches.js",
"tags": ["cat.aria", "wcag2a", "wcag412"],
"metadata": {
"description": "Ensures all elements with a role attribute use a valid value",
Expand Down
9 changes: 9 additions & 0 deletions lib/rules/no-empty-role-matches.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
if (!virtualNode.hasAttr('role')) {
return false;
}

if (!virtualNode.attr('role').trim()) {
return false;
}

return true;
6 changes: 6 additions & 0 deletions test/integration/rules/aria-roles/aria-roles.html
Original file line number Diff line number Diff line change
Expand Up @@ -129,3 +129,9 @@
<!-- fallback roles -->
<div role="button alert" id="fail15">fail</div>
</div>

<!-- inapplicable -->
<div id="inapplicable1">no role attribute</div>
<div id="inapplicable2" role>role not defined</div>
<div id="inapplicable3" role="">empty role</div>
<div id="inapplicable4" role=" ">role has only whitespace</div>
63 changes: 63 additions & 0 deletions test/rule-matches/no-empty-role-matches.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
describe('no-role-empty-matches', function() {
'use strict';

var fixture = document.getElementById('fixture');
var queryFixture = axe.testUtils.queryFixture;
var rule;

beforeEach(function() {
rule = axe._audit.rules.find(function(rule) {
return rule.id === 'aria-roles';
});
});

afterEach(function() {
fixture.innerHTML = '';
});

it('returns false when element does not have `role` attribute', function() {
var vNode = queryFixture('<div id="target">Some Content</div>');
var actual = rule.matches(vNode.actualNode, vNode);
assert.isFalse(actual);
});

it('returns false when element has role attribute has no value', function() {
var vNode = queryFixture('<div role id="target">Some Content</div>');
var actual = rule.matches(vNode.actualNode, vNode);
assert.isFalse(actual);
});

it('returns false when element has empty role attribute', function() {
var vNode = queryFixture('<div role="" id="target">Some Content</div>');
var actual = rule.matches(vNode.actualNode, vNode);
assert.isFalse(actual);
});

it('returns false when element has role attribute consisting of only whitespace', function() {
var vNode = queryFixture('<div role=" " id="target">Some Content</div>');
var actual = rule.matches(vNode.actualNode, vNode);
assert.isFalse(actual);
});

it('returns true when element has role attribute', function() {
var vNode = queryFixture(
'<div role="button" id="target">Some Content</div>'
);
var actual = rule.matches(vNode.actualNode, vNode);
assert.isTrue(actual);
});

it('returns true when element has multiple roles', function() {
var vNode = queryFixture(
'<div role="button link" id="target">Some Content</div>'
);
var actual = rule.matches(vNode.actualNode, vNode);
assert.isTrue(actual);
});

it('returns true when element has invalid role', function() {
var vNode = queryFixture('<div role="xyz" id="target">Some Content</div>');
var actual = rule.matches(vNode.actualNode, vNode);
assert.isTrue(actual);
});
});

0 comments on commit dbd3c02

Please sign in to comment.