getRole.js
6.66 KB
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
"use strict";
exports.__esModule = true;
exports.default = getRole;
var _util = require("./util");
function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }
function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; }
function _iterableToArrayLimit(arr, i) { if (typeof Symbol === "undefined" || !(Symbol.iterator in Object(arr))) return; var _arr = []; var _n = true; var _d = false; var _e = undefined; try { for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"] != null) _i["return"](); } finally { if (_d) throw _e; } } return _arr; }
function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
var localNameToRoleMappings = {
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"
};
var prohibitedAttributes = {
caption: new Set(["aria-label", "aria-labelledby"]),
code: new Set(["aria-label", "aria-labelledby"]),
deletion: new Set(["aria-label", "aria-labelledby"]),
emphasis: new Set(["aria-label", "aria-labelledby"]),
generic: new Set(["aria-label", "aria-labelledby", "aria-roledescription"]),
insertion: new Set(["aria-label", "aria-labelledby"]),
paragraph: new Set(["aria-label", "aria-labelledby"]),
presentation: new Set(["aria-label", "aria-labelledby"]),
strong: new Set(["aria-label", "aria-labelledby"]),
subscript: new Set(["aria-label", "aria-labelledby"]),
superscript: new Set(["aria-label", "aria-labelledby"])
};
/**
*
* @param element
* @param role The role used for this element. This is specified to control whether you want to use the implicit or explicit role.
*/
function hasGlobalAriaAttributes(element, role) {
// https://rawgit.com/w3c/aria/stable/#global_states
// commented attributes are deprecated
return ["aria-atomic", "aria-busy", "aria-controls", "aria-current", "aria-describedby", "aria-details", // "disabled",
"aria-dropeffect", // "errormessage",
"aria-flowto", "aria-grabbed", // "haspopup",
"aria-hidden", // "invalid",
"aria-keyshortcuts", "aria-label", "aria-labelledby", "aria-live", "aria-owns", "aria-relevant", "aria-roledescription"].some(function (attributeName) {
var _prohibitedAttributes;
return element.hasAttribute(attributeName) && !((_prohibitedAttributes = prohibitedAttributes[role]) === null || _prohibitedAttributes === void 0 ? void 0 : _prohibitedAttributes.has(attributeName));
});
}
function ignorePresentationalRole(element, implicitRole) {
// https://rawgit.com/w3c/aria/stable/#conflict_resolution_presentation_none
return hasGlobalAriaAttributes(element, implicitRole);
}
function getRole(element) {
var explicitRole = getExplicitRole(element);
if (explicitRole === null || explicitRole === "presentation") {
var implicitRole = getImplicitRole(element);
if (explicitRole !== "presentation" || ignorePresentationalRole(element, implicitRole || "")) {
return implicitRole;
}
}
return explicitRole;
}
function getImplicitRole(element) {
var mappedByTag = localNameToRoleMappings[(0, _util.getLocalName)(element)];
if (mappedByTag !== undefined) {
return mappedByTag;
}
switch ((0, _util.getLocalName)(element)) {
case "a":
case "area":
case "link":
if (element.hasAttribute("href")) {
return "link";
}
break;
case "img":
if (element.getAttribute("alt") === "" && !ignorePresentationalRole(element, "img")) {
return "presentation";
}
return "img";
case "input":
{
var _ref = element,
type = _ref.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.size > 1) {
return "listbox";
}
return "combobox";
}
return null;
}
function getExplicitRole(element) {
if (element.hasAttribute("role")) {
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion -- safe due to hasAttribute check
var _trim$split = element.getAttribute("role").trim().split(" "),
_trim$split2 = _slicedToArray(_trim$split, 1),
explicitRole = _trim$split2[0];
if (explicitRole !== undefined && explicitRole.length > 0) {
return explicitRole;
}
}
return null;
}
//# sourceMappingURL=getRole.js.map