Skip to content

Commit 8fb6419

Browse files
committed
Delete constraints, and hard-code roles we benefit from enforcing despite constraints
1 parent b2ed30e commit 8fb6419

File tree

1 file changed

+11
-7
lines changed

1 file changed

+11
-7
lines changed

lib/utils/get-role.js

+11-7
Original file line numberDiff line numberDiff line change
@@ -3,27 +3,32 @@ 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`
6+
// Clean-up `elementRoles` from `aria-query`.
77
const elementRolesMap = new ObjectMap()
88
for (const [key, value] of elementRoles.entries()) {
9-
// - Remove unused `constraints` key
10-
delete key.constraints
119
// - Remove empty `attributes` key
1210
if (!key.attributes || key.attributes?.length === 0) {
1311
delete key.attributes
1412
}
1513
elementRolesMap.set(key, value)
1614
}
17-
// - Remove insufficiently-disambiguated `menuitem` entry
15+
// Remove insufficiently-disambiguated `menuitem` entry
1816
elementRolesMap.delete({name: 'menuitem'})
19-
// - Disambiguate `menuitem` and `menu` roles by `type`
17+
// Disambiguate `menuitem` and `menu` roles by `type`
2018
elementRolesMap.set({name: 'menuitem', attributes: [{name: 'type', value: 'command'}]}, ['menuitem'])
2119
elementRolesMap.set({name: 'menuitem', attributes: [{name: 'type', value: 'radio'}]}, ['menuitemradio'])
2220
elementRolesMap.set({name: 'menuitem', attributes: [{name: 'type', value: 'toolbar'}]}, ['toolbar'])
2321
elementRolesMap.set({name: 'menu', attributes: [{name: 'type', value: 'toolbar'}]}, ['toolbar'])
2422

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.
27+
2528
/*
2629
Determine role of an element, based on its name and attributes.
30+
We construct a key and look up the element's role in `elementRolesMap`.
31+
If there is no match, we return undefined.
2732
*/
2833
function getRole(context, node) {
2934
// Early return if role is explicitly set
@@ -48,8 +53,7 @@ function getRole(context, node) {
4853
'scope',
4954
'name',
5055
]) {
51-
if ((prop === 'aria-labelledby' || prop === 'aria-label') && !['section', 'aside', 'form'].includes(key.name))
52-
continue
56+
if ((prop === 'aria-labelledby' || prop === 'aria-label') && !['section', 'form'].includes(key.name)) continue
5357
if (prop === 'name' && key.name !== 'form') continue
5458
if (prop === 'href' && key.name !== 'a' && key.name !== 'area') continue
5559
if (prop === 'alt' && key.name !== 'img') continue

0 commit comments

Comments
 (0)