Repository navigation
Expand file tree
/
Copy pathindex.js
More file actions
99 lines (77 loc) · 2.45 KB
/
Copy pathindex.js
File metadata and controls
99 lines (77 loc) · 2.45 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
module.exports = function(el) {
var basicTabbables = [];
var orderedTabbables = [];
// A node is "available" if
// - it's computed style
var isUnavailable = createIsUnavailable();
var candidateSelectors = [
'input',
'select',
'a[href]',
'textarea',
'button',
'[tabindex]',
];
var candidates = el.querySelectorAll(candidateSelectors);
var candidate, candidateIndex;
for (var i = 0, l = candidates.length; i < l; i++) {
candidate = candidates[i];
candidateIndex = parseInt(candidate.getAttribute('tabindex'), 10) || candidate.tabIndex;
if (
candidateIndex < 0
|| (candidate.tagName === 'INPUT' && candidate.type === 'hidden')
|| candidate.disabled
|| isUnavailable(candidate)
) {
continue;
}
if (candidateIndex === 0) {
basicTabbables.push(candidate);
} else {
orderedTabbables.push({
tabIndex: candidateIndex,
node: candidate,
});
}
}
var tabbableNodes = orderedTabbables
.sort(function(a, b) {
return a.tabIndex - b.tabIndex;
})
.map(function(a) {
return a.node
});
Array.prototype.push.apply(tabbableNodes, basicTabbables);
return tabbableNodes;
}
function createIsUnavailable() {
// Node cache must be refreshed on every check, in case
// the content of the element has changed
var isOffCache = [];
// "off" means `display: none;`, as opposed to "hidden",
// which means `visibility: hidden;`. getComputedStyle
// accurately reflects visiblity in context but not
// "off" state, so we need to recursively check parents.
function isOff(node, nodeComputedStyle) {
if (node === document.documentElement) return false;
// Find the cached node (Array.prototype.find not available in IE9)
for (var i = 0, length = isOffCache.length; i < length; i++) {
if (isOffCache[i][0] === node) return isOffCache[i][1];
}
nodeComputedStyle = nodeComputedStyle || window.getComputedStyle(node);
var result = false;
if (nodeComputedStyle.display === 'none') {
result = true;
} else if (node.parentNode) {
result = isOff(node.parentNode);
}
isOffCache.push([node, result]);
return result;
}
return function isUnavailable(node) {
if (node === document.documentElement) return false;
var computedStyle = window.getComputedStyle(node);
if (isOff(node, computedStyle)) return true;
return computedStyle.visibility === 'hidden';
}
}