Skip to content

Commit 40c0b2b

Browse files
committed
Move map constructing code into a function
1 parent aaca700 commit 40c0b2b

File tree

1 file changed

+30
-19
lines changed

1 file changed

+30
-19
lines changed

lib/utils/get-role.js

Lines changed: 30 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -3,27 +3,38 @@ const {elementRoles} = require('aria-query')
33
const {getElementType} = require('./get-element-type')
44
const ObjectMap = require('./object-map')
55

6-
// Clean-up `elementRoles` from `aria-query`.
7-
const elementRolesMap = new ObjectMap()
8-
for (const [key, value] of elementRoles.entries()) {
9-
// - Remove empty `attributes` key
10-
if (!key.attributes || key.attributes?.length === 0) {
11-
delete key.attributes
6+
const elementRolesMap = cleanElementRolesMap()
7+
8+
/*
9+
Returns an element roles map which uses `aria-query`'s elementRoles as the foundation.
10+
We additionally clean the data so we're able to fetch a role using a key we construct based on the node we're looking at.
11+
In a few scenarios, we stray from the roles returned by `aria-query` and hard code the mapping.
12+
*/
13+
function cleanElementRolesMap() {
14+
const rolesMap = new ObjectMap()
15+
16+
for (const [key, value] of elementRoles.entries()) {
17+
// - Remove empty `attributes` key
18+
if (!key.attributes || key.attributes?.length === 0) {
19+
delete key.attributes
20+
}
21+
rolesMap.set(key, value)
1222
}
13-
elementRolesMap.set(key, value)
14-
}
15-
// Remove insufficiently-disambiguated `menuitem` entry
16-
elementRolesMap.delete({name: 'menuitem'})
17-
// Disambiguate `menuitem` and `menu` roles by `type`
18-
elementRolesMap.set({name: 'menuitem', attributes: [{name: 'type', value: 'command'}]}, ['menuitem'])
19-
elementRolesMap.set({name: 'menuitem', attributes: [{name: 'type', value: 'radio'}]}, ['menuitemradio'])
20-
elementRolesMap.set({name: 'menuitem', attributes: [{name: 'type', value: 'toolbar'}]}, ['toolbar'])
21-
elementRolesMap.set({name: 'menu', attributes: [{name: 'type', value: 'toolbar'}]}, ['toolbar'])
23+
// Remove insufficiently-disambiguated `menuitem` entry
24+
rolesMap.delete({name: 'menuitem'})
25+
// Disambiguate `menuitem` and `menu` roles by `type`
26+
rolesMap.set({name: 'menuitem', attributes: [{name: 'type', value: 'command'}]}, ['menuitem'])
27+
rolesMap.set({name: 'menuitem', attributes: [{name: 'type', value: 'radio'}]}, ['menuitemradio'])
28+
rolesMap.set({name: 'menuitem', attributes: [{name: 'type', value: 'toolbar'}]}, ['toolbar'])
29+
rolesMap.set({name: 'menu', attributes: [{name: 'type', value: 'toolbar'}]}, ['toolbar'])
2230

23-
/* These have constraints defined in aria-query's `elementRoles` which depend on knowledge of ancestor roles which we cant accurately determine in a linter context.
24-
However, we benefit more from assuming the role, than assuming it's generic or undefined so we opt to hard code the mapping */
25-
elementRolesMap.set({name: 'aside'}, ['complementary']) // `aside` still maps to `complementary` in https://www.w3.org/TR/html-aria/#docconformance.
26-
elementRolesMap.set({name: 'li'}, ['listitem']) // `li` can be generic if it's not within a list but we would never want to render `li` outside of a list.
31+
/* These have constraints defined in aria-query's `elementRoles` which depend on knowledge of ancestor roles which we cant accurately determine in a linter context.
32+
However, we benefit more from assuming the role, than assuming it's generic or undefined so we opt to hard code the mapping */
33+
rolesMap.set({name: 'aside'}, ['complementary']) // `aside` still maps to `complementary` in https://www.w3.org/TR/html-aria/#docconformance.
34+
rolesMap.set({name: 'li'}, ['listitem']) // `li` can be generic if it's not within a list but we would never want to render `li` outside of a list.
35+
36+
return rolesMap
37+
}
2738

2839
/*
2940
Determine role of an element, based on its name and attributes.

0 commit comments

Comments
 (0)