-
Notifications
You must be signed in to change notification settings - Fork 28
/
a11y.js
207 lines (188 loc) · 6.07 KB
/
a11y.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
/**
* Accessibility utility functions (keyboard, tab stops, etc.).
* @module delite/a11y
* */
define([], function () {
var a11y = /** @lends module:delite/a11y */ {
/**
* Returns true if Element is visible.
* @param {Element} elem - The Element.
* @returns {boolean}
* @private
*/
_isElementShown: function (elem) {
var s = getComputedStyle(elem);
return s.visibility !== "hidden"
&& s.visibility !== "collapsed"
&& s.display !== "none"
&& elem.type !== "hidden";
},
/**
* Tests if element is tab-navigable even without an explicit tabIndex setting
* @param {Element} elem - The Element.
* @returns {boolean}
*/
hasDefaultTabStop: function (elem) {
// No explicit tabIndex setting, need to investigate node type
switch (elem.nodeName.toLowerCase()) {
case "a":
// An <a> w/out a tabIndex is only navigable if it has an href
return elem.hasAttribute("href");
case "area":
case "button":
case "input":
case "object":
case "select":
case "textarea":
// These are navigable by default
return true;
case "iframe":
var contentDocument = elem.contentDocument;
// If it's a cross-domain <iframe> then contentDocument is null for security. Just return false.
if (!contentDocument) {
return false;
}
// Otherwise, if it's an editor <iframe> then it's tab navigable.
if ("designMode" in contentDocument && contentDocument.designMode === "on") {
return true;
}
var body = contentDocument.body;
return body && (body.contentEditable === "true" ||
(body.firstChild && body.firstChild.contentEditable === "true"));
default:
return elem.contentEditable === "true";
}
},
/**
* Returns effective tabIndex of an element, either a number, or undefined if element isn't focusable.
* @param {Element} elem - The Element.
* @returns {number|undefined}
*/
effectiveTabIndex: function (elem) {
if (elem.disabled) {
return undefined;
} else if (elem.hasAttribute("tabIndex")) {
// Explicit tab index setting
return +elem.getAttribute("tabIndex");// + to convert string --> number
} else {
// No explicit tabIndex setting, so depends on node type
return a11y.hasDefaultTabStop(elem) ? 0 : undefined;
}
},
/**
* Tests if an element is tab-navigable.
* @param {Element} elem - The Element.
* @returns {boolean}
*/
isTabNavigable: function (elem) {
return a11y.effectiveTabIndex(elem) >= 0;
},
/**
* Tests if an element is focusable by tabbing to it, or clicking it with the mouse.
* @param {Element} elem - The Element.
* @returns {boolean}
*/
isFocusable: function (elem) {
return a11y.effectiveTabIndex(elem) >= -1;
},
/**
* Return array of tab-navigable descendants of the specified root node,
* in the order that they would be navigated by the tab key.
*
* @param {Element} root - The Element.
* @returns [{Element}]
*/
getTabNavigable: function (root) {
var elements = [], radioButtonByName = {};
function radioName (node) {
// If this element is part of a radio button group, return the name for that group.
return node && node.tagName.toLowerCase() === "input" &&
node.type && node.type.toLowerCase() === "radio" &&
node.name && node.name.toLowerCase();
}
var shown = a11y._isElementShown, effectiveTabIndex = a11y.effectiveTabIndex;
function walkTree (/*Element*/ parent) {
for (var child = parent.firstElementChild; child; child = child.nextElementSibling) {
// Skip hidden DOM trees.
if (!shown(child)) {
continue;
}
// If node is tab-navigable then add to elements[].
var tabIndex = effectiveTabIndex(child);
if (tabIndex >= 0) {
var rn = radioName(child);
if (rn) {
// Only register one radio button (for a given group) as tab-navigable.
// Note: assumes that all radio buttons in the same group have the same tabindex.
if (!(rn in radioButtonByName)) {
// First radio button seen with this name. Register it, and add it to
// elements[] array.
radioButtonByName[rn] = {
tabIndex: tabIndex,
position: elements.length,
element: child
};
elements.push(radioButtonByName[rn]);
} else if (child.checked) {
// This radio button is selected, so it overrides the radio button already added to
// elements[].
radioButtonByName[rn].element = child;
} else {
// Ignore this radio button since it's not the first one with seen (with this name),
// and it's not selected.
}
} else {
// Not a radio button, so just add it to elements[];
elements.push({
tabIndex: tabIndex,
position: elements.length,
element: child
});
}
}
// Search child nodes.
if (child.nodeName.toUpperCase() !== "SELECT") {
walkTree(child);
}
}
}
if (shown(root)) {
walkTree(root);
}
var sortedElements = elements.sort(function (a, b) {
// Tab should go to positive tabindexes before tabindex=0, so convert 0 to Infinity.
return (a.tabIndex || Infinity) - (b.tabIndex || Infinity) || a.position - b.position;
}).map(function (info) {
return info.element;
});
return sortedElements;
},
/**
* Finds the descendant of the specified root node that is first in the tabbing order.
* @param {string|Element} root
* @param {Document} [doc]
* @returns {Element}
*/
getFirstInTabbingOrder: function (root, doc) {
if (typeof root === "string") {
root = (doc || document).getElementById(root);
}
var elems = a11y.getTabNavigable(root);
return elems[0];
},
/**
* Finds the descendant of the specified root node that is last in the tabbing order.
* @param {string|Element} root
* @param {Document} [doc]
* @returns {Element}
*/
getLastInTabbingOrder: function (root, doc) {
if (typeof root === "string") {
root = (doc || document).getElementById(root);
}
var elems = a11y.getTabNavigable(root);
return elems[elems.length - 1];
}
};
return a11y;
});