From 40397f560f0bf0bcd7d80b98c4c02857cf6f2584 Mon Sep 17 00:00:00 2001 From: Steven Lambert <2433219+straker@users.noreply.github.com> Date: Mon, 13 Jul 2020 08:41:38 -0600 Subject: [PATCH] fix(aria/allowed-attr): work with standards object (#2360) * fix(allowed-attr): work with standards object * const --- lib/commons/aria/allowed-attr.js | 23 +++++--- .../standards/get-global-aria-attrs.js | 11 +++- test/commons/aria/allowed-attr.js | 52 ++++++++++++++++++ test/commons/aria/attributes.js | 54 ------------------- .../rules/aria-allowed-attr/passes.html | 5 -- 5 files changed, 79 insertions(+), 66 deletions(-) create mode 100644 test/commons/aria/allowed-attr.js diff --git a/lib/commons/aria/allowed-attr.js b/lib/commons/aria/allowed-attr.js index d768d7a548..e710554eaa 100644 --- a/lib/commons/aria/allowed-attr.js +++ b/lib/commons/aria/allowed-attr.js @@ -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 @@ -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; diff --git a/lib/commons/standards/get-global-aria-attrs.js b/lib/commons/standards/get-global-aria-attrs.js index 67e67b16be..51af69aa08 100644 --- a/lib/commons/standards/get-global-aria-attrs.js +++ b/lib/commons/standards/get-global-aria-attrs.js @@ -1,3 +1,4 @@ +import cache from '../../core/base/cache'; import standards from '../../standards'; /** @@ -5,9 +6,17 @@ import standards from '../../standards'; * @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; diff --git a/test/commons/aria/allowed-attr.js b/test/commons/aria/allowed-attr.js new file mode 100644 index 0000000000..ec207e9011 --- /dev/null +++ b/test/commons/aria/allowed-attr.js @@ -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); + }); +}); diff --git a/test/commons/aria/attributes.js b/test/commons/aria/attributes.js index 191178eb34..9dde9e7ddf 100644 --- a/test/commons/aria/attributes.js +++ b/test/commons/aria/attributes.js @@ -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'; diff --git a/test/integration/rules/aria-allowed-attr/passes.html b/test/integration/rules/aria-allowed-attr/passes.html index 675944a48b..506e4056b6 100644 --- a/test/integration/rules/aria-allowed-attr/passes.html +++ b/test/integration/rules/aria-allowed-attr/passes.html @@ -938,7 +938,6 @@