This repository has been archived by the owner on Sep 5, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3.4k
/
aria.js
330 lines (301 loc) · 8.36 KB
/
aria.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
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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
/**
* @ngdoc module
* @name material.core.aria
* @description
* Aria Expectations for AngularJS Material components.
*/
angular
.module('material.core')
.provider('$mdAria', MdAriaProvider);
/**
* @ngdoc service
* @name $mdAriaProvider
* @module material.core.aria
*
* @description
*
* Modify options of the `$mdAria` service, which will be used by most of the AngularJS Material
* components.
*
* You are able to disable `$mdAria` warnings, by using the following markup.
*
* <hljs lang="js">
* app.config(function($mdAriaProvider) {
* // Globally disables all ARIA warnings.
* $mdAriaProvider.disableWarnings();
* });
* </hljs>
*
*/
function MdAriaProvider() {
var config = {
/** Whether we should show ARIA warnings in the console if labels are missing on the element */
showWarnings: true
};
return {
disableWarnings: disableWarnings,
$get: function($$rAF, $log, $window, $interpolate) {
return MdAriaService.apply(config, arguments);
}
};
/**
* @ngdoc method
* @name $mdAriaProvider#disableWarnings
* @description Disables all ARIA warnings generated by AngularJS Material.
*/
function disableWarnings() {
config.showWarnings = false;
}
}
/*
* @ngInject
*/
function MdAriaService($$rAF, $log, $window, $interpolate) {
// Load the showWarnings option from the current context and store it inside of a scope variable,
// because the context will be probably lost in some function calls.
var showWarnings = this.showWarnings;
return {
expect: expect,
expectAsync: expectAsync,
expectWithText: expectWithText,
expectWithoutText: expectWithoutText,
getText: getText,
hasAriaLabel: hasAriaLabel,
parentHasAriaLabel: parentHasAriaLabel
};
/**
* Check if expected attribute has been specified on the target element or child
* @param {string|JQLite} element
* @param {string} attrName
* @param {string=} defaultValue What to set the attr to if no value is found
*/
function expect(element, attrName, defaultValue) {
var node = angular.element(element)[0] || element;
// if node exists and neither it nor its children have the attribute
if (node &&
((!node.hasAttribute(attrName) ||
node.getAttribute(attrName).length === 0) &&
!childHasAttribute(node, attrName))) {
defaultValue = angular.isString(defaultValue) ? defaultValue.trim() : '';
if (defaultValue.length) {
element.attr(attrName, defaultValue);
} else if (showWarnings) {
$log.warn('ARIA: Attribute "', attrName, '", required for accessibility, is missing on node:', node);
}
}
}
function expectAsync(element, attrName, defaultValueGetter) {
// Problem: when retrieving the element's contents synchronously to find the label,
// the text may not be defined yet in the case of a binding.
// There is a higher chance that a binding will be defined if we wait one frame.
$$rAF(function() {
expect(element, attrName, defaultValueGetter());
});
}
function expectWithText(element, attrName) {
var content = getText(element) || "";
var hasBinding = content.indexOf($interpolate.startSymbol()) > -1;
if (hasBinding) {
expectAsync(element, attrName, function() {
return getText(element);
});
} else {
expect(element, attrName, content);
}
}
function expectWithoutText(element, attrName) {
var content = getText(element);
var hasBinding = content.indexOf($interpolate.startSymbol()) > -1;
if (!hasBinding && !content) {
expect(element, attrName, content);
}
}
/**
* @param {Element|JQLite} element
* @returns {string}
*/
function getText(element) {
element = element[0] || element;
var walker = document.createTreeWalker(element, NodeFilter.SHOW_TEXT, null, false);
var text = '';
var node;
while (node = walker.nextNode()) {
if (!isAriaHiddenNode(node)) {
text += node.textContent;
}
}
return text.trim() || '';
/**
* @param {Node} node
* @returns {boolean}
*/
function isAriaHiddenNode(node) {
while (node.parentNode && (node = node.parentNode) !== element) {
if (node.getAttribute && node.getAttribute('aria-hidden') === 'true') {
return true;
}
}
}
}
function childHasAttribute(node, attrName) {
var hasChildren = node.hasChildNodes(),
hasAttr = false;
function isHidden(el) {
var style = el.currentStyle ? el.currentStyle : $window.getComputedStyle(el);
return (style.display === 'none');
}
if (hasChildren) {
var children = node.childNodes;
for (var i=0; i < children.length; i++) {
var child = children[i];
if (child.nodeType === 1 && child.hasAttribute(attrName)) {
if (!isHidden(child)) {
hasAttr = true;
}
}
}
}
return hasAttr;
}
/**
* Check if expected element has aria label attribute
* @param element
*/
function hasAriaLabel(element) {
var node = angular.element(element)[0] || element;
/* Check if compatible node type (ie: not HTML Document node) */
if (!node.hasAttribute) {
return false;
}
/* Check label or description attributes */
return node.hasAttribute('aria-label') || node.hasAttribute('aria-labelledby') || node.hasAttribute('aria-describedby');
}
/**
* Check if expected element's parent has aria label attribute and has valid role and tagName
* @param {string|JQLite|Node & ParentNode} element
* @param {number=} level Number of levels deep search should be performed
*/
function parentHasAriaLabel(element, level) {
level = level || 1;
var node = angular.element(element)[0] || element;
if (!node.parentNode) {
return false;
}
if (performCheck(node.parentNode)) {
return true;
}
level--;
if (level) {
return parentHasAriaLabel(node.parentNode, level);
}
return false;
function performCheck(parentNode) {
if (!hasAriaLabel(parentNode)) {
return false;
}
/* Perform role block-list check */
if (parentNode.hasAttribute('role')) {
switch (parentNode.getAttribute('role').toLowerCase()) {
case 'command':
case 'definition':
case 'directory':
case 'grid':
case 'list':
case 'listitem':
case 'log':
case 'marquee':
case 'menu':
case 'menubar':
case 'note':
case 'presentation':
case 'separator':
case 'scrollbar':
case 'status':
case 'tablist':
return false;
}
}
/* Perform tagName block-list check */
switch (parentNode.tagName.toLowerCase()) {
case 'abbr':
case 'acronym':
case 'address':
case 'applet':
case 'audio':
case 'b':
case 'bdi':
case 'bdo':
case 'big':
case 'blockquote':
case 'br':
case 'canvas':
case 'caption':
case 'center':
case 'cite':
case 'code':
case 'col':
case 'data':
case 'dd':
case 'del':
case 'dfn':
case 'dir':
case 'div':
case 'dl':
case 'em':
case 'embed':
case 'fieldset':
case 'figcaption':
case 'font':
case 'h1':
case 'h2':
case 'h3':
case 'h4':
case 'h5':
case 'h6':
case 'hgroup':
case 'html':
case 'i':
case 'ins':
case 'isindex':
case 'kbd':
case 'keygen':
case 'label':
case 'legend':
case 'li':
case 'map':
case 'mark':
case 'menu':
case 'object':
case 'ol':
case 'output':
case 'pre':
case 'presentation':
case 'q':
case 'rt':
case 'ruby':
case 'samp':
case 'small':
case 'source':
case 'span':
case 'status':
case 'strike':
case 'strong':
case 'sub':
case 'sup':
case 'svg':
case 'tbody':
case 'td':
case 'th':
case 'thead':
case 'time':
case 'tr':
case 'track':
case 'tt':
case 'ul':
case 'var':
return false;
}
return true;
}
}
}