Skip to content

Commit

Permalink
fix(aria/allowed-attr): work with standards object (#2360)
Browse files Browse the repository at this point in the history
* fix(allowed-attr): work with standards object

* const
  • Loading branch information
straker committed Jul 13, 2020
1 parent 240b528 commit 40397f5
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 66 deletions.
23 changes: 17 additions & 6 deletions lib/commons/aria/allowed-attr.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import lookupTable from './lookup-table';
import standards from '../../standards';
import getGlobalAriaAttrs from '../standards/get-global-aria-attrs';

/**
* Get allowed attributes for a given role
Expand All @@ -9,12 +10,22 @@ import lookupTable from './lookup-table';
* @return {Array}
*/
function allowedAttr(role) {
const roles = lookupTable.role[role];
const attr = (roles && roles.attributes && roles.attributes.allowed) || [];
const requiredAttr =
(roles && roles.attributes && roles.attributes.required) || [];
const roleDef = standards.ariaRoles[role];
const attrs = [...getGlobalAriaAttrs()];

return attr.concat(lookupTable.globalAttributes).concat(requiredAttr);
if (!roleDef) {
return attrs;
}

if (roleDef.allowedAttrs) {
attrs.push(...roleDef.allowedAttrs);
}

if (roleDef.requiredAttrs) {
attrs.push(...roleDef.requiredAttrs);
}

return attrs;
}

export default allowedAttr;
11 changes: 10 additions & 1 deletion lib/commons/standards/get-global-aria-attrs.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,22 @@
import cache from '../../core/base/cache';
import standards from '../../standards';

/**
* Return a list of global aria attributes.
* @return {String[]} List of all global aria attributes
*/
function getGlobalAriaAttrs() {
return Object.keys(standards.ariaAttrs).filter(attrName => {
if (cache.get('globalAriaAttrs')) {
return cache.get('globalAriaAttrs');
}

const globalAttrs = Object.keys(standards.ariaAttrs).filter(attrName => {
return standards.ariaAttrs[attrName].global;
});

cache.set('globalAriaAttrs', globalAttrs);

return globalAttrs;
}

export default getGlobalAriaAttrs;
52 changes: 52 additions & 0 deletions test/commons/aria/allowed-attr.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
describe('aria.allowedAttr', function() {
'use strict';

var globalAttrs;
before(function() {
axe._load({});
globalAttrs = axe.commons.standards.getGlobalAriaAttrs();
});

afterEach(function() {
axe.reset();
});

it('should returned the attributes property for the proper role', function() {
axe.configure({
standards: {
ariaRoles: {
cats: {
allowedAttrs: ['hello']
}
}
}
});

assert.deepEqual(
axe.commons.aria.allowedAttr('cats'),
globalAttrs.concat(['hello'])
);
});

it('should also check required attributes', function() {
axe.configure({
standards: {
ariaRoles: {
cats: {
requiredAttrs: ['hello'],
allowedAttrs: ['ok']
}
}
}
});

assert.deepEqual(
axe.commons.aria.allowedAttr('cats'),
globalAttrs.concat(['ok', 'hello'])
);
});

it('should return an array with globally allowed attributes', function() {
assert.deepEqual(axe.commons.aria.allowedAttr('cats'), globalAttrs);
});
});
54 changes: 0 additions & 54 deletions test/commons/aria/attributes.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,60 +29,6 @@ describe('aria.requiredAttr', function() {
});
});

describe('aria.allowedAttr', function() {
'use strict';

var orig;
var origGlobals;
beforeEach(function() {
orig = axe.commons.aria.lookupTable.role;
origGlobals = axe.commons.aria.lookupTable.globalAttributes;
});

afterEach(function() {
axe.commons.aria.lookupTable.role = orig;
axe.commons.aria.lookupTable.globalAttributes = origGlobals;
});

it('should returned the attributes property for the proper role', function() {
axe.commons.aria.lookupTable.globalAttributes = ['world'];
axe.commons.aria.lookupTable.role = {
cats: {
attributes: {
allowed: ['hello']
}
}
};

assert.deepEqual(axe.commons.aria.allowedAttr('cats'), ['hello', 'world']);
});

it('should also check required attributes', function() {
axe.commons.aria.lookupTable.globalAttributes = ['world'];
axe.commons.aria.lookupTable.role = {
cats: {
attributes: {
required: ['hello'],
allowed: ['ok']
}
}
};

assert.deepEqual(axe.commons.aria.allowedAttr('cats'), [
'ok',
'world',
'hello'
]);
});

it('should return an array with globally allowed attributes', function() {
axe.commons.aria.lookupTable.globalAttributes = ['world'];
axe.commons.aria.lookupTable.role = {};

assert.deepEqual(axe.commons.aria.allowedAttr('cats'), ['world']);
});
});

describe('aria.validateAttr', function() {
'use strict';

Expand Down
5 changes: 0 additions & 5 deletions test/integration/rules/aria-allowed-attr/passes.html
Original file line number Diff line number Diff line change
Expand Up @@ -938,7 +938,6 @@
<div
role="menuitemradio"
id="pass35"
aria-selected="value"
aria-posinset="value"
aria-setsize="value"
aria-checked="value"
Expand Down Expand Up @@ -1078,7 +1077,6 @@
<div
role="radio"
id="pass40"
aria-selected="value"
aria-posinset="value"
aria-setsize="value"
aria-checked="value"
Expand Down Expand Up @@ -1193,8 +1191,6 @@
<div
role="rowgroup"
id="pass44"
aria-activedescendant="value"
aria-expanded="value"
aria-atomic="value"
aria-busy="value"
aria-controls="value"
Expand Down Expand Up @@ -1310,7 +1306,6 @@
<div
role="separator"
id="pass48"
aria-expanded="value"
aria-orientation="value"
aria-atomic="value"
aria-busy="value"
Expand Down

0 comments on commit 40397f5

Please sign in to comment.