Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(aria-allowed-attr): error when generic elements use aria-label and aria-labelledy #2766

Merged
merged 9 commits into from
Mar 5, 2021
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 2 additions & 5 deletions lib/checks/aria/aria-prohibited-attr-evaluate.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,9 @@ import standards from '../../standards';
*/
function ariaProhibitedAttrEvaluate(node, options, virtualNode) {
const prohibited = [];
const role = getRole(virtualNode);

if (!role) {
return false;
}

// From Scott O'Hara on asking about HTML elements with no role mapping: no mapping = generic
const role = getRole(virtualNode) || 'generic';
straker marked this conversation as resolved.
Show resolved Hide resolved
const attrs = virtualNode.attrNames;
const prohibitedAttrs = standards.ariaRoles[role].prohibitedAttrs;

Expand Down
4 changes: 4 additions & 0 deletions lib/standards/aria-roles.js
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,10 @@ const ariaRoles = {
allowedAttrs: ['aria-expanded'],
superclassRole: ['landmark']
},
generic: {
type: 'structure',
prohibitedAttrs: ['aria-label', 'aria-labelledby']
straker marked this conversation as resolved.
Show resolved Hide resolved
},
grid: {
type: 'composite',
requiredOwned: ['rowgroup', 'row'],
Expand Down
13 changes: 10 additions & 3 deletions test/checks/aria/aria-prohibited-attr.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
describe.only('aria-prohibited-attr', function() {
describe('aria-prohibited-attr', function() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Huh, odd.

'use strict';

var checkContext = axe.testUtils.MockCheckContext();
Expand Down Expand Up @@ -30,16 +30,23 @@ describe.only('aria-prohibited-attr', function() {
]);
});

it('should return true if element has no role', function() {
var params = checkSetup(
'<div id="target" aria-label="foo" aria-labelledby="foo">Contents</div>'
);
assert.isTrue(checkEvaluate.apply(checkContext, params));
});

it('should return false if all attributes are allowed', function() {
var params = checkSetup(
'<div id="target" role="button" aria-label="foo" aria-labelledby="foo">Contents</div>'
);
assert.isFalse(checkEvaluate.apply(checkContext, params));
});

it('should return false if element has no role', function() {
it('should return false if no prohibited attributes are used', function() {
var params = checkSetup(
'<div id="target" aria-label="foo" aria-labelledby="foo">Contents</div>'
'<div id="target" role="code" aria-selected="true">Contents</div>'
);
assert.isFalse(checkEvaluate.apply(checkContext, params));
});
Expand Down
65 changes: 19 additions & 46 deletions test/commons/standards/get-aria-roles-by-type.js
Original file line number Diff line number Diff line change
@@ -1,59 +1,32 @@
describe('standards.getAriaRolesByType', function() {
var getAriaRolesByType = axe.commons.standards.getAriaRolesByType;

before(function() {
axe._load({});
});
it('should return a list of role names by type', function() {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I got tired of updating this test when adding new roles. This should allow us to not have it change so often.

// first remove all role types
var roleNames = Object.keys(axe._audit.standards.ariaRoles);
var ariaRoles = {};
for (var i = 0; i < roleNames.length; i++) {
ariaRoles[roleNames[i]] = { type: 'off' };
}

after(function() {
axe.reset();
});
// then turn on a few specific roles
ariaRoles.article = { type: 'structure' };
ariaRoles.blockquote = { type: 'structure' };
ariaRoles.caption = { type: 'structure' };
ariaRoles.cell = { type: 'structure' };

axe.configure({
standards: {
ariaRoles: ariaRoles
}
});

it('should return a list of role names by type', function() {
// Source: https://www.w3.org/TR/wai-aria-1.1/#document_structure_roles
var structureRoles = getAriaRolesByType('structure');
assert.deepEqual(structureRoles, [
'article',
'blockquote',
'caption',
'cell',
'code',
'columnheader',
'definition',
'deletion',
'directory',
'document',
'emphasis',
'feed',
'figure',
'group',
'heading',
'img',
'insertion',
'list',
'listitem',
'math',
'meter',
'none',
'note',
'paragraph',
'presentation',
'row',
'rowgroup',
'rowheader',
'separator',
'strong',
'subscript',
'superscript',
'table',
'term',
'text',
'time',
'toolbar',
'tooltip',
'graphics-document',
'graphics-object',
'graphics-symbol'
'cell'
]);
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,55 +2,32 @@ describe('standards.getAriaRolesSupportingNameFromContent', function() {
var getAriaRolesSupportingNameFromContent =
axe.commons.standards.getAriaRolesSupportingNameFromContent;

before(function() {
axe._load({});
});
it('should return a list of role names which are named from content', function() {
// first remove all namedFromContent
var roleNames = Object.keys(axe._audit.standards.ariaRoles);
var ariaRoles = {};
for (var i = 0; i < roleNames.length; i++) {
ariaRoles[roleNames[i]] = { nameFromContent: false };
}

after(function() {
axe.reset();
});
// then turn on a few specific roles
ariaRoles.button = { nameFromContent: true };
ariaRoles.cell = { nameFromContent: true };
ariaRoles.checkbox = { nameFromContent: true };
ariaRoles.columnheader = { nameFromContent: true };

axe.configure({
standards: {
ariaRoles: ariaRoles
}
});

it('should return a list of role names which are named from content', function() {
// Source: https://www.w3.org/TR/wai-aria-1.1/#namefromcontent
// Source: https://www.w3.org/TR/dpub-aria-1.0/
// Note: we have added roles in our spec. also note that
// although "tree" is listed as supporting name from content
// it's role definition does not list contents in the name from
// section (it was removed from the list in WAI ARIA 1.2)
var contentRoles = getAriaRolesSupportingNameFromContent();
assert.deepEqual(contentRoles, [
'button',
'cell',
'checkbox',
'columnheader',
'directory',
'figure',
'gridcell',
'heading',
'link',
'listitem',
'menuitem',
'menuitemcheckbox',
'menuitemradio',
'option',
'radio',
'row',
'rowgroup',
'rowheader',
'section',
'sectionhead',
'switch',
'tab',
'table',
'term',
'text',
'tooltip',
'treeitem',
'doc-backlink',
'doc-biblioref',
'doc-glossref',
'doc-noteref',
'graphics-object'
'columnheader'
]);
});

Expand Down
2 changes: 2 additions & 0 deletions test/integration/rules/aria-allowed-attr/failures.html
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,6 @@
<div role="subscript" aria-labelledby="value" id="fail20">fail</div>
<div role="superscript" aria-label="value" id="fail21">fail</div>
<div role="superscript" aria-labelledby="value" id="fail22">fail</div>
<div aria-label="value" id="fail23">fail</div>
<div aria-labelledby="value" id="fail24">fail</div>
<!-- technically presentation and none roles do not allow aria-label and aria-labelledby, but since those are global attributes the presentation role conflict will not resolve the roles to none or presentation -->
4 changes: 3 additions & 1 deletion test/integration/rules/aria-allowed-attr/failures.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
["#fail19"],
["#fail20"],
["#fail21"],
["#fail22"]
["#fail22"],
["#fail23"],
["#fail24"]
]
}