Skip to content

Commit

Permalink
fix: check invaildrole fails only when all roles are invalid (#2075)
Browse files Browse the repository at this point in the history
  • Loading branch information
jeeyyy authored and straker committed Mar 6, 2020
1 parent 60a6cb1 commit 989b317
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 10 deletions.
21 changes: 12 additions & 9 deletions lib/checks/aria/invalidrole.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
const invalidRoles = axe.utils
.tokenList(virtualNode.attr('role'))
.filter(role => {
return !axe.commons.aria.isValidRole(role, {
allowAbstract: true
});
});
const { tokenList } = axe.utils;
const { aria } = axe.commons;

if (invalidRoles.length > 0) {
this.data(invalidRoles);
const allRoles = tokenList(virtualNode.attr('role'));
const allInvalid = allRoles.every(
role => !aria.isValidRole(role, { allowAbstract: true })
);

/**
* Only fail when all the roles are invalid
*/
if (allInvalid) {
this.data(allRoles);
return true;
}

Expand Down
16 changes: 15 additions & 1 deletion test/checks/shared/invalidrole.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,10 +83,24 @@ describe('invalidrole', function() {
assert.isNull(checkContext._data);
});

it('should return true if applied to at least one nonsensical role', function() {
it('should return false if atleast one role is valid', function() {
var virtualNode = queryFixture(
'<div id="target" role="alert button foo bar">Contents</div>'
);
assert.isFalse(
checks.invalidrole.evaluate.call(
checkContext,
virtualNode.actualNode,
null,
virtualNode
)
);
});

it('should return true if all roles are invalid', function() {
var virtualNode = queryFixture(
'<div id="target" role="foo bar">Contents</div>'
);
assert.isTrue(
checks.invalidrole.evaluate.call(
checkContext,
Expand Down

0 comments on commit 989b317

Please sign in to comment.