forked from facebook/react
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDOMAccessibilityRoles.js
140 lines (130 loc) · 3.13 KB
/
DOMAccessibilityRoles.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
/**
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @flow
*/
// Below code forked from dom-accessibility-api
const tagToRoleMappings = {
ARTICLE: 'article',
ASIDE: 'complementary',
BODY: 'document',
BUTTON: 'button',
DATALIST: 'listbox',
DD: 'definition',
DETAILS: 'group',
DIALOG: 'dialog',
DT: 'term',
FIELDSET: 'group',
FIGURE: 'figure',
// WARNING: Only with an accessible name
FORM: 'form',
FOOTER: 'contentinfo',
H1: 'heading',
H2: 'heading',
H3: 'heading',
H4: 'heading',
H5: 'heading',
H6: 'heading',
HEADER: 'banner',
HR: 'separator',
LEGEND: 'legend',
LI: 'listitem',
MATH: 'math',
MAIN: 'main',
MENU: 'list',
NAV: 'navigation',
OL: 'list',
OPTGROUP: 'group',
// WARNING: Only in certain context
OPTION: 'option',
OUTPUT: 'status',
PROGRESS: 'progressbar',
// WARNING: Only with an accessible name
SECTION: 'region',
SUMMARY: 'button',
TABLE: 'table',
TBODY: 'rowgroup',
TEXTAREA: 'textbox',
TFOOT: 'rowgroup',
// WARNING: Only in certain context
TD: 'cell',
TH: 'columnheader',
THEAD: 'rowgroup',
TR: 'row',
UL: 'list',
};
function getImplicitRole(element: Element): string | null {
// $FlowFixMe[invalid-computed-prop]
const mappedByTag = tagToRoleMappings[element.tagName];
if (mappedByTag !== undefined) {
return mappedByTag;
}
switch (element.tagName) {
case 'A':
case 'AREA':
case 'LINK':
if (element.hasAttribute('href')) {
return 'link';
}
break;
case 'IMG':
if ((element.getAttribute('alt') || '').length > 0) {
return 'img';
}
break;
case 'INPUT': {
const type = (element: any).type;
switch (type) {
case 'button':
case 'image':
case 'reset':
case 'submit':
return 'button';
case 'checkbox':
case 'radio':
return type;
case 'range':
return 'slider';
case 'email':
case 'tel':
case 'text':
case 'url':
if (element.hasAttribute('list')) {
return 'combobox';
}
return 'textbox';
case 'search':
if (element.hasAttribute('list')) {
return 'combobox';
}
return 'searchbox';
default:
return null;
}
}
case 'SELECT':
if (element.hasAttribute('multiple') || (element: any).size > 1) {
return 'listbox';
}
return 'combobox';
}
return null;
}
function getExplicitRoles(element: Element): Array<string> | null {
const role = element.getAttribute('role');
if (role) {
return role.trim().split(' ');
}
return null;
}
// https://w3c.github.io/html-aria/#document-conformance-requirements-for-use-of-aria-attributes-in-html
export function hasRole(element: Element, role: string): boolean {
const explicitRoles = getExplicitRoles(element);
if (explicitRoles !== null && explicitRoles.indexOf(role) >= 0) {
return true;
}
return role === getImplicitRole(element);
}