Skip to content

Commit

Permalink
fix: allows all roles on img tag with no alt attribute
Browse files Browse the repository at this point in the history
  • Loading branch information
AutoSponge committed Oct 9, 2019
1 parent b634c34 commit 929085a
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 5 deletions.
18 changes: 14 additions & 4 deletions lib/commons/aria/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2243,6 +2243,12 @@ lookupTable.elementsAllowedAnyRole = [
href: isNull
}
},
{
nodeName: 'img',
attributes: {
alt: isNull
}
},
{
nodeName: [
'abbr',
Expand Down Expand Up @@ -2310,11 +2316,15 @@ lookupTable.evaluateRoleForElement = {
}
return out;
},
IMG: ({ node, out }) => {
if (node.alt) {
return !out;
IMG: ({ node, role, out }) => {
switch (node.alt) {
case null:
return out;
case '':
return role === 'presentation' || role === 'none';
default:
return role !== 'presentation' && role !== 'none';
}
return out;
},
INPUT: ({ node, role, out }) => {
switch (node.type) {
Expand Down
39 changes: 38 additions & 1 deletion test/checks/aria/aria-allowed-role.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,14 +87,51 @@ describe('aria-allowed-role', function() {
);
});

it('returns false when img has no alt', function() {
it('returns true when img has no alt', function() {
var node = document.createElement('img');
node.setAttribute('role', 'presentation');
fixture.appendChild(node);
assert.isTrue(
checks['aria-allowed-role'].evaluate.call(checkContext, node)
);
assert.deepEqual(checkContext._data, null);
node.setAttribute('role', 'none');
assert.isTrue(
checks['aria-allowed-role'].evaluate.call(checkContext, node)
);
assert.deepEqual(checkContext._data, null);
});

it('returns true when img has empty alt', function() {
var node = document.createElement('img');
node.setAttribute('alt', '');
node.setAttribute('role', 'presentation');
fixture.appendChild(node);
assert.isTrue(
checks['aria-allowed-role'].evaluate.call(checkContext, node)
);
assert.deepEqual(checkContext._data, null);
node.setAttribute('role', 'none');
assert.isTrue(
checks['aria-allowed-role'].evaluate.call(checkContext, node)
);
assert.deepEqual(checkContext._data, null);
});

it('returns false when img has alt', function() {
var node = document.createElement('img');
node.setAttribute('alt', 'not empty');
node.setAttribute('role', 'presentation');
fixture.appendChild(node);
assert.isFalse(
checks['aria-allowed-role'].evaluate.call(checkContext, node)
);
assert.deepEqual(checkContext._data, ['presentation']);
node.setAttribute('role', 'none');
assert.isFalse(
checks['aria-allowed-role'].evaluate.call(checkContext, node)
);
assert.deepEqual(checkContext._data, ['none']);
});

it('returns true when input of type image and no role', function() {
Expand Down
24 changes: 24 additions & 0 deletions test/commons/aria/is-aria-role-allowed-on-element.js
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,7 @@ describe('aria.isAriaRoleAllowedOnElement', function() {
}
};
var node = document.createElement('img');
node.setAttribute('alt', '');
node.setAttribute('role', 'presentation');
var actual = axe.commons.aria.isAriaRoleAllowedOnElement(
node,
Expand All @@ -258,6 +259,29 @@ describe('aria.isAriaRoleAllowedOnElement', function() {
assert.isFalse(actual);
});

it('returns true, ensure evaluateRoleForElement in lookupTable is invoked', function() {
var overrideInvoked = false;
axe.commons.aria.lookupTable.evaluateRoleForElement = {
IMG: function(options) {
overrideInvoked = true;
assert.isDefined(options.node);
assert.equal(options.node.nodeName.toUpperCase(), 'IMG');
assert.isBoolean(options.out);
return false;
}
};
var node = document.createElement('img');
node.setAttribute('role', 'presentation');
var presentationAllowed = axe.commons.aria.isAriaRoleAllowedOnElement(
node,
'presentation'
);
var noneAllowed = axe.commons.aria.isAriaRoleAllowedOnElement(node, 'none');
assert.isFalse(overrideInvoked);
assert.isTrue(presentationAllowed);
assert.isTrue(noneAllowed);
});

it('returns false if element with role MENU type context', function() {
var overrideInvoked = false;
axe.commons.aria.lookupTable.evaluateRoleForElement = {
Expand Down

0 comments on commit 929085a

Please sign in to comment.