forked from focus-trap/tabbable
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
544 lines (494 loc) · 19 KB
/
index.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
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
const candidateSelectors = [
'input',
'select',
'textarea',
'a[href]',
'button',
'[tabindex]:not(slot)',
'audio[controls]',
'video[controls]',
'[contenteditable]:not([contenteditable="false"])',
'details>summary:first-of-type',
'details',
];
const candidateSelector = /* #__PURE__ */ candidateSelectors.join(',');
const NoElement = typeof Element === 'undefined';
const matches = NoElement
? function () {}
: Element.prototype.matches ||
Element.prototype.msMatchesSelector ||
Element.prototype.webkitMatchesSelector;
const getRootNode =
!NoElement && Element.prototype.getRootNode
? (element) => element.getRootNode()
: (element) => element.ownerDocument;
/**
* @param {Element} el container to check in
* @param {boolean} includeContainer add container to check
* @param {(node: Element) => boolean} filter filter candidates
* @returns {Element[]}
*/
const getCandidates = function (el, includeContainer, filter) {
let candidates = Array.prototype.slice.apply(
el.querySelectorAll(candidateSelector)
);
if (includeContainer && matches.call(el, candidateSelector)) {
candidates.unshift(el);
}
candidates = candidates.filter(filter);
return candidates;
};
/**
* @callback GetShadowRoot
* @param {Element} element to check for shadow root
* @returns {ShadowRoot|boolean} ShadowRoot if available or boolean indicating if a shadowRoot is attached but not available.
*/
/**
* @callback ShadowRootFilter
* @param {Element} shadowHostNode the element which contains shadow content
* @returns {boolean} true if a shadow root could potentially contain valid candidates.
*/
/**
* @typedef {Object} CandidatesScope
* @property {Element} scope contains inner candidates
* @property {Element[]} candidates
*/
/**
* @typedef {Object} IterativeOptions
* @property {GetShadowRoot|boolean} getShadowRoot true if shadow support is enabled; falsy if not;
* if a function, implies shadow support is enabled and either returns the shadow root of an element
* or a boolean stating if it has an undisclosed shadow root
* @property {(node: Element) => boolean} filter filter candidates
* @property {boolean} flatten if true then result will flatten any CandidatesScope into the returned list
* @property {ShadowRootFilter} shadowRootFilter filter shadow roots;
*/
/**
* @param {Element[]} elements list of element containers to match candidates from
* @param {boolean} includeContainer add container list to check
* @param {IterativeOptions} options
* @returns {Array.<Element|CandidatesScope>}
*/
const getCandidatesIteratively = function (
elements,
includeContainer,
options
) {
const candidates = [];
const elementsToCheck = Array.from(elements);
while (elementsToCheck.length) {
const element = elementsToCheck.shift();
if (element.tagName === 'SLOT') {
// add shadow dom slot scope (slot itself cannot be focusable)
const assigned = element.assignedElements();
const content = assigned.length ? assigned : element.children;
const nestedCandidates = getCandidatesIteratively(content, true, options);
if (options.flatten) {
candidates.push(...nestedCandidates);
} else {
candidates.push({
scope: element,
candidates: nestedCandidates,
});
}
} else {
// check candidate element
const validCandidate = matches.call(element, candidateSelector);
if (
validCandidate &&
options.filter(element) &&
(includeContainer || !elements.includes(element))
) {
candidates.push(element);
}
// iterate over shadow content if possible
const shadowRoot =
element.shadowRoot ||
// check for an undisclosed shadow
(typeof options.getShadowRoot === 'function' &&
options.getShadowRoot(element));
const validShadowRoot =
!options.shadowRootFilter || options.shadowRootFilter(element);
if (shadowRoot && validShadowRoot) {
// add shadow dom scope IIF a shadow root node was given; otherwise, an undisclosed
// shadow exists, so look at light dom children as fallback BUT create a scope for any
// child candidates found because they're likely slotted elements (elements that are
// children of the web component element (which has the shadow), in the light dom, but
// slotted somewhere _inside_ the undisclosed shadow) -- the scope is created below,
// _after_ we return from this recursive call
const nestedCandidates = getCandidatesIteratively(
shadowRoot === true ? element.children : shadowRoot.children,
true,
options
);
if (options.flatten) {
candidates.push(...nestedCandidates);
} else {
candidates.push({
scope: element,
candidates: nestedCandidates,
});
}
} else {
// there's not shadow so just dig into the element's (light dom) children
// __without__ giving the element special scope treatment
elementsToCheck.unshift(...element.children);
}
}
}
return candidates;
};
const getTabindex = function (node, isScope) {
if (node.tabIndex < 0) {
// in Chrome, <details/>, <audio controls/> and <video controls/> elements get a default
// `tabIndex` of -1 when the 'tabindex' attribute isn't specified in the DOM,
// yet they are still part of the regular tab order; in FF, they get a default
// `tabIndex` of 0; since Chrome still puts those elements in the regular tab
// order, consider their tab index to be 0.
// Also browsers do not return `tabIndex` correctly for contentEditable nodes;
// so if they don't have a tabindex attribute specifically set, assume it's 0.
//
// isScope is positive for custom element with shadow root or slot that by default
// have tabIndex -1, but need to be sorted by document order in order for their
// content to be inserted in the correct position
if (
(isScope ||
/^(AUDIO|VIDEO|DETAILS)$/.test(node.tagName) ||
node.isContentEditable) &&
isNaN(parseInt(node.getAttribute('tabindex'), 10))
) {
return 0;
}
}
return node.tabIndex;
};
const sortOrderedTabbables = function (a, b) {
return a.tabIndex === b.tabIndex
? a.documentOrder - b.documentOrder
: a.tabIndex - b.tabIndex;
};
const isInput = function (node) {
return node.tagName === 'INPUT';
};
const isHiddenInput = function (node) {
return isInput(node) && node.type === 'hidden';
};
const isDetailsWithSummary = function (node) {
const r =
node.tagName === 'DETAILS' &&
Array.prototype.slice
.apply(node.children)
.some((child) => child.tagName === 'SUMMARY');
return r;
};
const getCheckedRadio = function (nodes, form) {
for (let i = 0; i < nodes.length; i++) {
if (nodes[i].checked && nodes[i].form === form) {
return nodes[i];
}
}
};
const isTabbableRadio = function (node) {
if (!node.name) {
return true;
}
const radioScope = node.form || getRootNode(node);
const queryRadios = function (name) {
return radioScope.querySelectorAll(
'input[type="radio"][name="' + name + '"]'
);
};
let radioSet;
if (
typeof window !== 'undefined' &&
typeof window.CSS !== 'undefined' &&
typeof window.CSS.escape === 'function'
) {
radioSet = queryRadios(window.CSS.escape(node.name));
} else {
try {
radioSet = queryRadios(node.name);
} catch (err) {
// eslint-disable-next-line no-console
console.error(
'Looks like you have a radio button with a name attribute containing invalid CSS selector characters and need the CSS.escape polyfill: %s',
err.message
);
return false;
}
}
const checked = getCheckedRadio(radioSet, node.form);
return !checked || checked === node;
};
const isRadio = function (node) {
return isInput(node) && node.type === 'radio';
};
const isNonTabbableRadio = function (node) {
return isRadio(node) && !isTabbableRadio(node);
};
const isZeroArea = function (node) {
const { width, height } = node.getBoundingClientRect();
return width === 0 && height === 0;
};
const isHidden = function (node, { displayCheck, getShadowRoot }) {
// NOTE: visibility will be `undefined` if node is detached from the document
// (see notes about this further down), which means we will consider it visible
// (this is legacy behavior from a very long way back)
// NOTE: we check this regardless of `displayCheck="none"` because this is a
// _visibility_ check, not a _display_ check
if (getComputedStyle(node).visibility === 'hidden') {
return true;
}
const isDirectSummary = matches.call(node, 'details>summary:first-of-type');
const nodeUnderDetails = isDirectSummary ? node.parentElement : node;
if (matches.call(nodeUnderDetails, 'details:not([open]) *')) {
return true;
}
// The root node is the shadow root if the node is in a shadow DOM; some document otherwise
// (but NOT _the_ document; see second 'If' comment below for more).
// If rootNode is shadow root, it'll have a host, which is the element to which the shadow
// is attached, and the one we need to check if it's in the document or not (because the
// shadow, and all nodes it contains, is never considered in the document since shadows
// behave like self-contained DOMs; but if the shadow's HOST, which is part of the document,
// is hidden, or is not in the document itself but is detached, it will affect the shadow's
// visibility, including all the nodes it contains). The host could be any normal node,
// or a custom element (i.e. web component). Either way, that's the one that is considered
// part of the document, not the shadow root, nor any of its children (i.e. the node being
// tested).
// If rootNode is not a shadow root, it won't have a host, and so rootNode should be the
// document (per the docs) and while it's a Document-type object, that document does not
// appear to be the same as the node's `ownerDocument` for some reason, so it's safer
// to ignore the rootNode at this point, and use `node.ownerDocument`. Otherwise,
// using `rootNode.contains(node)` will _always_ be true we'll get false-positives when
// node is actually detached.
const nodeRootHost = getRootNode(node).host;
const nodeIsAttached =
nodeRootHost?.ownerDocument.contains(nodeRootHost) ||
node.ownerDocument.contains(node);
if (!displayCheck || displayCheck === 'full') {
if (typeof getShadowRoot === 'function') {
// figure out if we should consider the node to be in an undisclosed shadow and use the
// 'non-zero-area' fallback
const originalNode = node;
while (node) {
const parentElement = node.parentElement;
const rootNode = getRootNode(node);
if (
parentElement &&
!parentElement.shadowRoot &&
getShadowRoot(parentElement) === true // check if there's an undisclosed shadow
) {
// node has an undisclosed shadow which means we can only treat it as a black box, so we
// fall back to a non-zero-area test
return isZeroArea(node);
} else if (node.assignedSlot) {
// iterate up slot
node = node.assignedSlot;
} else if (!parentElement && rootNode !== node.ownerDocument) {
// cross shadow boundary
node = rootNode.host;
} else {
// iterate up normal dom
node = parentElement;
}
}
node = originalNode;
}
// else, `getShadowRoot` might be true, but all that does is enable shadow DOM support
// (i.e. it does not also presume that all nodes might have undisclosed shadows); or
// it might be a falsy value, which means shadow DOM support is disabled
// Since we didn't find it sitting in an undisclosed shadow (or shadows are disabled)
// now we can just test to see if it would normally be visible or not, provided it's
// attached to the main document.
// NOTE: We must consider case where node is inside a shadow DOM and given directly to
// `isTabbable()` or `isFocusable()` -- regardless of `getShadowRoot` option setting.
if (nodeIsAttached) {
// this works wherever the node is: if there's at least one client rect, it's
// somehow displayed; it also covers the CSS 'display: contents' case where the
// node itself is hidden in place of its contents; and there's no need to search
// up the hierarchy either
return !node.getClientRects().length;
}
// Else, the node isn't attached to the document, which means the `getClientRects()`
// API will __always__ return zero rects (this can happen, for example, if React
// is used to render nodes onto a detached tree, as confirmed in this thread:
// https://github.com/facebook/react/issues/9117#issuecomment-284228870)
//
// It also means that even window.getComputedStyle(node).display will return `undefined`
// because styles are only computed for nodes that are in the document.
//
// NOTE: THIS HAS BEEN THE CASE FOR YEARS. It is not new, nor is it caused by tabbable
// somehow. Though it was never stated officially, anyone who has ever used tabbable
// APIs on nodes in detached containers has actually implicitly used tabbable in what
// was later (as of v5.2.0 on Apr 9, 2021) called `displayCheck="none"` mode -- essentially
// considering __everything__ to be visible because of the innability to determine styles.
} else if (displayCheck === 'non-zero-area') {
// NOTE: Even though this tests that the node's client rect is non-zero to determine
// whether it's displayed, and that a detached node will __always__ have a zero-area
// client rect, we don't special-case for whether the node is attached or not. In
// this mode, we do want to consider nodes that have a zero area to be hidden at all
// times, and that includes attached or not.
return isZeroArea(node);
}
// visible, as far as we can tell, or per current `displayCheck` mode
return false;
};
// form fields (nested) inside a disabled fieldset are not focusable/tabbable
// unless they are in the _first_ <legend> element of the top-most disabled
// fieldset
const isDisabledFromFieldset = function (node) {
if (/^(INPUT|BUTTON|SELECT|TEXTAREA)$/.test(node.tagName)) {
let parentNode = node.parentElement;
// check if `node` is contained in a disabled <fieldset>
while (parentNode) {
if (parentNode.tagName === 'FIELDSET' && parentNode.disabled) {
// look for the first <legend> among the children of the disabled <fieldset>
for (let i = 0; i < parentNode.children.length; i++) {
const child = parentNode.children.item(i);
// when the first <legend> (in document order) is found
if (child.tagName === 'LEGEND') {
// if its parent <fieldset> is not nested in another disabled <fieldset>,
// return whether `node` is a descendant of its first <legend>
return matches.call(parentNode, 'fieldset[disabled] *')
? true
: !child.contains(node);
}
}
// the disabled <fieldset> containing `node` has no <legend>
return true;
}
parentNode = parentNode.parentElement;
}
}
// else, node's tabbable/focusable state should not be affected by a fieldset's
// enabled/disabled state
return false;
};
const isNodeMatchingSelectorFocusable = function (options, node) {
if (
node.disabled ||
isHiddenInput(node) ||
isHidden(node, options) ||
// For a details element with a summary, the summary element gets the focus
isDetailsWithSummary(node) ||
isDisabledFromFieldset(node)
) {
return false;
}
return true;
};
const isNodeMatchingSelectorTabbable = function (options, node) {
if (
isNonTabbableRadio(node) ||
getTabindex(node) < 0 ||
!isNodeMatchingSelectorFocusable(options, node)
) {
return false;
}
return true;
};
const isValidShadowRootTabbable = function (shadowHostNode) {
const tabIndex = parseInt(shadowHostNode.getAttribute('tabindex'), 10);
if (isNaN(tabIndex) || tabIndex >= 0) {
return true;
}
// If a custom element has an explicit negative tabindex,
// browsers will not allow tab targeting said element's children.
return false;
};
/**
* @param {Array.<Element|CandidatesScope>} candidates
* @returns Element[]
*/
const sortByOrder = function (candidates) {
const regularTabbables = [];
const orderedTabbables = [];
candidates.forEach(function (item, i) {
const isScope = !!item.scope;
const element = isScope ? item.scope : item;
const candidateTabindex = getTabindex(element, isScope);
const elements = isScope ? sortByOrder(item.candidates) : element;
if (candidateTabindex === 0) {
isScope
? regularTabbables.push(...elements)
: regularTabbables.push(element);
} else {
orderedTabbables.push({
documentOrder: i,
tabIndex: candidateTabindex,
item: item,
isScope: isScope,
content: elements,
});
}
});
return orderedTabbables
.sort(sortOrderedTabbables)
.reduce((acc, sortable) => {
sortable.isScope
? acc.push(...sortable.content)
: acc.push(sortable.content);
return acc;
}, [])
.concat(regularTabbables);
};
const tabbable = function (el, options) {
options = options || {};
let candidates;
if (options.getShadowRoot) {
candidates = getCandidatesIteratively([el], options.includeContainer, {
filter: isNodeMatchingSelectorTabbable.bind(null, options),
flatten: false,
getShadowRoot: options.getShadowRoot,
shadowRootFilter: isValidShadowRootTabbable,
});
} else {
candidates = getCandidates(
el,
options.includeContainer,
isNodeMatchingSelectorTabbable.bind(null, options)
);
}
return sortByOrder(candidates);
};
const focusable = function (el, options) {
options = options || {};
let candidates;
if (options.getShadowRoot) {
candidates = getCandidatesIteratively([el], options.includeContainer, {
filter: isNodeMatchingSelectorFocusable.bind(null, options),
flatten: true,
getShadowRoot: options.getShadowRoot,
});
} else {
candidates = getCandidates(
el,
options.includeContainer,
isNodeMatchingSelectorFocusable.bind(null, options)
);
}
return candidates;
};
const isTabbable = function (node, options) {
options = options || {};
if (!node) {
throw new Error('No node provided');
}
if (matches.call(node, candidateSelector) === false) {
return false;
}
return isNodeMatchingSelectorTabbable(options, node);
};
const focusableCandidateSelector = /* #__PURE__ */ candidateSelectors
.concat('iframe')
.join(',');
const isFocusable = function (node, options) {
options = options || {};
if (!node) {
throw new Error('No node provided');
}
if (matches.call(node, focusableCandidateSelector) === false) {
return false;
}
return isNodeMatchingSelectorFocusable(options, node);
};
export { tabbable, focusable, isTabbable, isFocusable };