Debian Codegraph @

node/tabbable/dist/index.esm.js

raw
1 /*!
2 * tabbable 6.5.0
3 * @license MIT, https://github.com/focus-trap/tabbable/blob/master/LICENSE
4 */
5 // NOTE: separate `:not()` selectors has broader browser support than the newer
6 // `:not([inert], [inert] *)` (Feb 2023)
7 var candidateSelectors = ['input:not([inert]):not([inert] *)', 'select:not([inert]):not([inert] *)', 'textarea:not([inert]):not([inert] *)', 'a[href]:not([inert]):not([inert] *)', 'area[href]:not([inert]):not([inert] *)', 'button:not([inert]):not([inert] *)', '[tabindex]:not(slot):not([inert]):not([inert] *)', 'audio[controls]:not([inert]):not([inert] *)', 'video[controls]:not([inert]):not([inert] *)', '[contenteditable]:not([contenteditable="false"]):not([inert]):not([inert] *)', 'details>summary:first-of-type:not([inert]):not([inert] *)', 'details:not([inert]):not([inert] *)'];
8 var candidateSelector = /* #__PURE__ */candidateSelectors.join(',');
9 var NoElement = typeof Element === 'undefined';
10 var matches = NoElement ? function () {} : Element.prototype.matches || Element.prototype.msMatchesSelector || Element.prototype.webkitMatchesSelector;
11 var getRootNode = !NoElement && Element.prototype.getRootNode ? function (element) {
12 var _element$getRootNode;
13 return element === null || element === void 0 ? void 0 : (_element$getRootNode = element.getRootNode) === null || _element$getRootNode === void 0 ? void 0 : _element$getRootNode.call(element);
14 } : function (element) {
15 return element === null || element === void 0 ? void 0 : element.ownerDocument;
16 };
17
18 /**
19 * Determines if a node is inert or in an inert ancestor.
20 * @param {Node} [node]
21 * @param {boolean} [lookUp] If true and `node` is not inert, looks up at ancestors to
22 * see if any of them are inert. If false, only `node` itself is considered.
23 * @returns {boolean} True if inert itself or by way of being in an inert ancestor.
24 * False if `node` is falsy.
25 */
26 var _isInert = function isInert(node, lookUp) {
27 var _node$getAttribute;
28 if (lookUp === void 0) {
29 lookUp = true;
30 }
31 // CAREFUL: JSDom does not support inert at all, so we can't use the `HTMLElement.inert`
32 // JS API property; we have to check the attribute, which can either be empty or 'true';
33 // if it's `null` (not specified) or 'false', it's an active element
34 var inertAtt = node === null || node === void 0 ? void 0 : (_node$getAttribute = node.getAttribute) === null || _node$getAttribute === void 0 ? void 0 : _node$getAttribute.call(node, 'inert');
35 var inert = inertAtt === '' || inertAtt === 'true';
36
37 // NOTE: this could also be handled with `node.matches('[inert], :is([inert] *)')`
38 // if it weren't for `matches()` not being a function on shadow roots; the following
39 // code works for any kind of node
40 var result = inert || lookUp && node && (
41 // closest does not exist on shadow roots, so we fall back to a manual
42 // lookup upward, in case it is not defined.
43 typeof node.closest === 'function' ? node.closest('[inert]') : _isInert(node.parentNode));
44 return result;
45 };
46
47 /**
48 * Determines if a node's content is editable.
49 * @param {Element} [node]
50 * @returns True if it's content-editable; false if it's not or `node` is falsy.
51 */
52 var isContentEditable = function isContentEditable(node) {
53 var _node$getAttribute2;
54 // CAREFUL: JSDom does not support the `HTMLElement.isContentEditable` API so we have
55 // to use the attribute directly to check for this, which can either be empty or 'true';
56 // if it's `null` (not specified) or 'false', it's a non-editable element
57 var attValue = node === null || node === void 0 ? void 0 : (_node$getAttribute2 = node.getAttribute) === null || _node$getAttribute2 === void 0 ? void 0 : _node$getAttribute2.call(node, 'contenteditable');
58 return attValue === '' || attValue === 'true';
59 };
60
61 /**
62 * @param {Element} el container to check in
63 * @param {boolean} includeContainer add container to check
64 * @param {(node: Element) => boolean} filter filter candidates
65 * @returns {Element[]}
66 */
67 var getCandidates = function getCandidates(el, includeContainer, filter) {
68 // even if `includeContainer=false`, we still have to check it for inertness because
69 // if it's inert (either by itself or via its parent), then all its children are inert
70 if (_isInert(el)) {
71 return [];
72 }
73 var candidates = Array.prototype.slice.apply(el.querySelectorAll(candidateSelector));
74 if (includeContainer && matches.call(el, candidateSelector)) {
75 candidates.unshift(el);
76 }
77 candidates = candidates.filter(filter);
78 return candidates;
79 };
80
81 /**
82 * @callback GetShadowRoot
83 * @param {Element} element to check for shadow root
84 * @returns {ShadowRoot|boolean} ShadowRoot if available or boolean indicating if a shadowRoot is attached but not available.
85 */
86
87 /**
88 * @callback ShadowRootFilter
89 * @param {Element} shadowHostNode the element which contains shadow content
90 * @returns {boolean} true if a shadow root could potentially contain valid candidates.
91 */
92
93 /**
94 * @typedef {Object} CandidateScope
95 * @property {Element} scopeParent contains inner candidates
96 * @property {Element[]} candidates list of candidates found in the scope parent
97 */
98
99 /**
100 * @typedef {Object} IterativeOptions
101 * @property {GetShadowRoot|boolean} getShadowRoot true if shadow support is enabled; falsy if not;
102 * if a function, implies shadow support is enabled and either returns the shadow root of an element
103 * or a boolean stating if it has an undisclosed shadow root
104 * @property {(node: Element) => boolean} filter filter candidates
105 * @property {boolean} flatten if true then result will flatten any CandidateScope into the returned list
106 * @property {ShadowRootFilter} shadowRootFilter filter shadow roots;
107 */
108
109 /**
110 * @param {Element[]} elements list of element containers to match candidates from
111 * @param {boolean} includeContainer add container list to check
112 * @param {IterativeOptions} options
113 * @returns {Array.<Element|CandidateScope>}
114 */
115 var _getCandidatesIteratively = function getCandidatesIteratively(elements, includeContainer, options) {
116 var candidates = [];
117 var elementsToCheck = Array.from(elements);
118 while (elementsToCheck.length) {
119 var element = elementsToCheck.shift();
120 if (_isInert(element, false)) {
121 // no need to look up since we're drilling down
122 // anything inside this container will also be inert
123 continue;
124 }
125 if (element.tagName === 'SLOT') {
126 // add shadow dom slot scope (slot itself cannot be focusable)
127 var assigned = element.assignedElements();
128 var content = assigned.length ? assigned : element.children;
129 var nestedCandidates = _getCandidatesIteratively(content, true, options);
130 if (options.flatten) {
131 candidates.push.apply(candidates, nestedCandidates);
132 } else {
133 candidates.push({
134 scopeParent: element,
135 candidates: nestedCandidates
136 });
137 }
138 } else {
139 // check candidate element
140 var validCandidate = matches.call(element, candidateSelector);
141 if (validCandidate && options.filter(element) && (includeContainer || !elements.includes(element))) {
142 candidates.push(element);
143 }
144
145 // iterate over shadow content if possible
146 var shadowRoot = element.shadowRoot ||
147 // check for an undisclosed shadow
148 typeof options.getShadowRoot === 'function' && options.getShadowRoot(element);
149
150 // no inert look up because we're already drilling down and checking for inertness
151 // on the way down, so all containers to this root node should have already been
152 // vetted as non-inert
153 var validShadowRoot = !_isInert(shadowRoot, false) && (!options.shadowRootFilter || options.shadowRootFilter(element));
154 if (shadowRoot && validShadowRoot) {
155 // add shadow dom scope IIF a shadow root node was given; otherwise, an undisclosed
156 // shadow exists, so look at light dom children as fallback BUT create a scope for any
157 // child candidates found because they're likely slotted elements (elements that are
158 // children of the web component element (which has the shadow), in the light dom, but
159 // slotted somewhere _inside_ the undisclosed shadow) -- the scope is created below,
160 // _after_ we return from this recursive call
161 var _nestedCandidates = _getCandidatesIteratively(shadowRoot === true ? element.children : shadowRoot.children, true, options);
162 if (options.flatten) {
163 candidates.push.apply(candidates, _nestedCandidates);
164 } else {
165 candidates.push({
166 scopeParent: element,
167 candidates: _nestedCandidates
168 });
169 }
170 } else {
171 // there's not shadow so just dig into the element's (light dom) children
172 // __without__ giving the element special scope treatment
173 elementsToCheck.unshift.apply(elementsToCheck, element.children);
174 }
175 }
176 }
177 return candidates;
178 };
179
180 /**
181 * @private
182 * Determines if the node has an explicitly specified `tabindex` attribute.
183 * @param {HTMLElement} node
184 * @returns {boolean} True if so; false if not.
185 */
186 var hasTabIndex = function hasTabIndex(node) {
187 return !isNaN(parseInt(node.getAttribute('tabindex'), 10));
188 };
189
190 /**
191 * Determine the tab index of a given node.
192 * @param {HTMLElement} node
193 * @returns {number} Tab order (negative, 0, or positive number).
194 * @throws {Error} If `node` is falsy.
195 */
196 var getTabIndex = function getTabIndex(node) {
197 if (!node) {
198 throw new Error('No node provided');
199 }
200 if (node.tabIndex < 0) {
201 // in Chrome, <details/>, <audio controls/> and <video controls/> elements get a default
202 // `tabIndex` of -1 when the 'tabindex' attribute isn't specified in the DOM,
203 // yet they are still part of the regular tab order; in FF, they get a default
204 // `tabIndex` of 0; since Chrome still puts those elements in the regular tab
205 // order, consider their tab index to be 0.
206 // Also browsers do not return `tabIndex` correctly for contentEditable nodes;
207 // so if they don't have a tabindex attribute specifically set, assume it's 0.
208 if ((/^(AUDIO|VIDEO|DETAILS)$/.test(node.tagName) || isContentEditable(node)) && !hasTabIndex(node)) {
209 return 0;
210 }
211 }
212 return node.tabIndex;
213 };
214
215 /**
216 * Determine the tab index of a given node __for sort order purposes__.
217 * @param {HTMLElement} node
218 * @param {boolean} [isScope] True for a custom element with shadow root or slot that, by default,
219 * has tabIndex -1, but needs to be sorted by document order in order for its content to be
220 * inserted into the correct sort position.
221 * @returns {number} Tab order (negative, 0, or positive number).
222 */
223 var getSortOrderTabIndex = function getSortOrderTabIndex(node, isScope) {
224 var tabIndex = getTabIndex(node);
225 if (tabIndex < 0 && isScope && !hasTabIndex(node)) {
226 return 0;
227 }
228 return tabIndex;
229 };
230 var sortOrderedTabbables = function sortOrderedTabbables(a, b) {
231 return a.tabIndex === b.tabIndex ? a.documentOrder - b.documentOrder : a.tabIndex - b.tabIndex;
232 };
233 var isInput = function isInput(node) {
234 return node.tagName === 'INPUT';
235 };
236 var isHiddenInput = function isHiddenInput(node) {
237 return isInput(node) && node.type === 'hidden';
238 };
239 var isDetailsWithSummary = function isDetailsWithSummary(node) {
240 var r = node.tagName === 'DETAILS' && Array.prototype.slice.apply(node.children).some(function (child) {
241 return child.tagName === 'SUMMARY';
242 });
243 return r;
244 };
245 var getCheckedRadio = function getCheckedRadio(nodes, form) {
246 for (var i = 0; i < nodes.length; i++) {
247 if (nodes[i].checked && nodes[i].form === form) {
248 return nodes[i];
249 }
250 }
251 };
252 var isTabbableRadio = function isTabbableRadio(node) {
253 if (!node.name) {
254 return true;
255 }
256 var radioScope = node.form || getRootNode(node);
257 var queryRadios = function queryRadios(name) {
258 return radioScope.querySelectorAll('input[type="radio"][name="' + name + '"]');
259 };
260 var radioSet;
261 if (typeof window !== 'undefined' && typeof window.CSS !== 'undefined' && typeof window.CSS.escape === 'function') {
262 radioSet = queryRadios(window.CSS.escape(node.name));
263 } else {
264 try {
265 radioSet = queryRadios(node.name);
266 } catch (err) {
267 // eslint-disable-next-line no-console
268 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);
269 return false;
270 }
271 }
272 var checked = getCheckedRadio(radioSet, node.form);
273 return !checked || checked === node;
274 };
275 var isRadio = function isRadio(node) {
276 return isInput(node) && node.type === 'radio';
277 };
278 var isNonTabbableRadio = function isNonTabbableRadio(node) {
279 return isRadio(node) && !isTabbableRadio(node);
280 };
281
282 // determines if a node is ultimately attached to the window's document
283 var isNodeAttached = function isNodeAttached(node) {
284 var _nodeRoot;
285 // The root node is the shadow root if the node is in a shadow DOM; some document otherwise
286 // (but NOT _the_ document; see second 'If' comment below for more).
287 // If rootNode is shadow root, it'll have a host, which is the element to which the shadow
288 // is attached, and the one we need to check if it's in the document or not (because the
289 // shadow, and all nodes it contains, is never considered in the document since shadows
290 // behave like self-contained DOMs; but if the shadow's HOST, which is part of the document,
291 // is hidden, or is not in the document itself but is detached, it will affect the shadow's
292 // visibility, including all the nodes it contains). The host could be any normal node,
293 // or a custom element (i.e. web component). Either way, that's the one that is considered
294 // part of the document, not the shadow root, nor any of its children (i.e. the node being
295 // tested).
296 // To further complicate things, we have to look all the way up until we find a shadow HOST
297 // that is attached (or find none) because the node might be in nested shadows...
298 // If rootNode is not a shadow root, it won't have a host, and so rootNode should be the
299 // document (per the docs) and while it's a Document-type object, that document does not
300 // appear to be the same as the node's `ownerDocument` for some reason, so it's safer
301 // to ignore the rootNode at this point, and use `node.ownerDocument`. Otherwise,
302 // using `rootNode.contains(node)` will _always_ be true we'll get false-positives when
303 // node is actually detached.
304 // NOTE: If `nodeRootHost` or `node` happens to be the `document` itself (which is possible
305 // if a tabbable/focusable node was quickly added to the DOM, focused, and then removed
306 // from the DOM as in https://github.com/focus-trap/focus-trap-react/issues/905), then
307 // `ownerDocument` will be `null`, hence the optional chaining on it.
308 var nodeRoot = node && getRootNode(node);
309 var nodeRootHost = (_nodeRoot = nodeRoot) === null || _nodeRoot === void 0 ? void 0 : _nodeRoot.host;
310
311 // in some cases, a detached node will return itself as the root instead of a document or
312 // shadow root object, in which case, we shouldn't try to look further up the host chain
313 var attached = false;
314 if (nodeRoot && nodeRoot !== node) {
315 var _nodeRootHost, _nodeRootHost$ownerDo, _node$ownerDocument;
316 attached = !!((_nodeRootHost = nodeRootHost) !== null && _nodeRootHost !== void 0 && (_nodeRootHost$ownerDo = _nodeRootHost.ownerDocument) !== null && _nodeRootHost$ownerDo !== void 0 && _nodeRootHost$ownerDo.contains(nodeRootHost) || node !== null && node !== void 0 && (_node$ownerDocument = node.ownerDocument) !== null && _node$ownerDocument !== void 0 && _node$ownerDocument.contains(node));
317 while (!attached && nodeRootHost) {
318 var _nodeRoot2, _nodeRootHost2, _nodeRootHost2$ownerD;
319 // since it's not attached and we have a root host, the node MUST be in a nested shadow DOM,
320 // which means we need to get the host's host and check if that parent host is contained
321 // in (i.e. attached to) the document
322 nodeRoot = getRootNode(nodeRootHost);
323 nodeRootHost = (_nodeRoot2 = nodeRoot) === null || _nodeRoot2 === void 0 ? void 0 : _nodeRoot2.host;
324 attached = !!((_nodeRootHost2 = nodeRootHost) !== null && _nodeRootHost2 !== void 0 && (_nodeRootHost2$ownerD = _nodeRootHost2.ownerDocument) !== null && _nodeRootHost2$ownerD !== void 0 && _nodeRootHost2$ownerD.contains(nodeRootHost));
325 }
326 }
327 return attached;
328 };
329 var isZeroArea = function isZeroArea(node) {
330 var _node$getBoundingClie = node.getBoundingClientRect(),
331 width = _node$getBoundingClie.width,
332 height = _node$getBoundingClie.height;
333 return width === 0 && height === 0;
334 };
335 var isHidden = function isHidden(node, _ref) {
336 var displayCheck = _ref.displayCheck,
337 getShadowRoot = _ref.getShadowRoot;
338 if (displayCheck === 'full-native') {
339 if ('checkVisibility' in node) {
340 // Chrome >= 105, Edge >= 105, Firefox >= 106, Safari >= 17.4
341 // @see https://developer.mozilla.org/en-US/docs/Web/API/Element/checkVisibility#browser_compatibility
342 var visible = node.checkVisibility({
343 // Checking opacity might be desirable for some use cases, but natively,
344 // opacity zero elements _are_ focusable and tabbable.
345 checkOpacity: false,
346 opacityProperty: false,
347 contentVisibilityAuto: true,
348 visibilityProperty: true,
349 // This is an alias for `visibilityProperty`. Contemporary browsers
350 // support both. However, this alias has wider browser support (Chrome
351 // >= 105 and Firefox >= 106, vs. Chrome >= 121 and Firefox >= 122), so
352 // we include it anyway.
353 checkVisibilityCSS: true
354 });
355 return !visible;
356 }
357 // Fall through to manual visibility checks
358 }
359
360 // NOTE: visibility will be `undefined` if node is detached from the document
361 // (see notes about this further down), which means we will consider it visible
362 // (this is legacy behavior from a very long way back)
363 // NOTE: we check this regardless of `displayCheck="none"` because this is a
364 // _visibility_ check, not a _display_ check
365 var _getComputedStyle = getComputedStyle(node),
366 visibility = _getComputedStyle.visibility;
367 if (visibility === 'hidden' || visibility === 'collapse') {
368 return true;
369 }
370 var isDirectSummary = matches.call(node, 'details>summary:first-of-type');
371 var nodeUnderDetails = isDirectSummary ? node.parentElement : node;
372 if (matches.call(nodeUnderDetails, 'details:not([open]) *')) {
373 return true;
374 }
375 if (!displayCheck || displayCheck === 'full' ||
376 // full-native can run this branch when it falls through in case
377 // Element#checkVisibility is unsupported
378 displayCheck === 'full-native' || displayCheck === 'legacy-full') {
379 if (typeof getShadowRoot === 'function') {
380 // figure out if we should consider the node to be in an undisclosed shadow and use the
381 // 'non-zero-area' fallback
382 var originalNode = node;
383 while (node) {
384 var parentElement = node.parentElement;
385 var rootNode = getRootNode(node);
386 if (parentElement && !parentElement.shadowRoot && getShadowRoot(parentElement) === true // check if there's an undisclosed shadow
387 ) {
388 // node has an undisclosed shadow which means we can only treat it as a black box, so we
389 // fall back to a non-zero-area test
390 return isZeroArea(node);
391 } else if (node.assignedSlot) {
392 // iterate up slot
393 node = node.assignedSlot;
394 } else if (!parentElement && rootNode !== node.ownerDocument) {
395 // cross shadow boundary
396 node = rootNode.host;
397 } else {
398 // iterate up normal dom
399 node = parentElement;
400 }
401 }
402 node = originalNode;
403 }
404 // else, `getShadowRoot` might be true, but all that does is enable shadow DOM support
405 // (i.e. it does not also presume that all nodes might have undisclosed shadows); or
406 // it might be a falsy value, which means shadow DOM support is disabled
407
408 // Since we didn't find it sitting in an undisclosed shadow (or shadows are disabled)
409 // now we can just test to see if it would normally be visible or not, provided it's
410 // attached to the main document.
411 // NOTE: We must consider case where node is inside a shadow DOM and given directly to
412 // `isTabbable()` or `isFocusable()` -- regardless of `getShadowRoot` option setting.
413
414 if (isNodeAttached(node)) {
415 // this works wherever the node is: if there's at least one client rect, it's
416 // somehow displayed; it also covers the CSS 'display: contents' case where the
417 // node itself is hidden in place of its contents; and there's no need to search
418 // up the hierarchy either
419 return !node.getClientRects().length;
420 }
421
422 // Else, the node isn't attached to the document, which means the `getClientRects()`
423 // API will __always__ return zero rects (this can happen, for example, if React
424 // is used to render nodes onto a detached tree, as confirmed in this thread:
425 // https://github.com/facebook/react/issues/9117#issuecomment-284228870)
426 //
427 // It also means that even window.getComputedStyle(node).display will return `undefined`
428 // because styles are only computed for nodes that are in the document.
429 //
430 // NOTE: THIS HAS BEEN THE CASE FOR YEARS. It is not new, nor is it caused by tabbable
431 // somehow. Though it was never stated officially, anyone who has ever used tabbable
432 // APIs on nodes in detached containers has actually implicitly used tabbable in what
433 // was later (as of v5.2.0 on Apr 9, 2021) called `displayCheck="none"` mode -- essentially
434 // considering __everything__ to be visible because of the innability to determine styles.
435 //
436 // v6.0.0: As of this major release, the default 'full' option __no longer treats detached
437 // nodes as visible with the 'none' fallback.__
438 if (displayCheck !== 'legacy-full') {
439 return true; // hidden
440 }
441 // else, fallback to 'none' mode and consider the node visible
442 } else if (displayCheck === 'non-zero-area') {
443 // NOTE: Even though this tests that the node's client rect is non-zero to determine
444 // whether it's displayed, and that a detached node will __always__ have a zero-area
445 // client rect, we don't special-case for whether the node is attached or not. In
446 // this mode, we do want to consider nodes that have a zero area to be hidden at all
447 // times, and that includes attached or not.
448 return isZeroArea(node);
449 }
450
451 // visible, as far as we can tell, or per current `displayCheck=none` mode, we assume
452 // it's visible
453 return false;
454 };
455
456 // form fields (nested) inside a disabled fieldset are not focusable/tabbable
457 // unless they are in the _first_ <legend> element of the top-most disabled
458 // fieldset
459 var isDisabledFromFieldset = function isDisabledFromFieldset(node) {
460 if (/^(INPUT|BUTTON|SELECT|TEXTAREA)$/.test(node.tagName)) {
461 var parentNode = node.parentElement;
462 // check if `node` is contained in a disabled <fieldset>
463 while (parentNode) {
464 if (parentNode.tagName === 'FIELDSET' && parentNode.disabled) {
465 // look for the first <legend> among the children of the disabled <fieldset>
466 for (var i = 0; i < parentNode.children.length; i++) {
467 var child = parentNode.children.item(i);
468 // when the first <legend> (in document order) is found
469 if (child.tagName === 'LEGEND') {
470 // if its parent <fieldset> is not nested in another disabled <fieldset>,
471 // return whether `node` is a descendant of its first <legend>
472 return matches.call(parentNode, 'fieldset[disabled] *') ? true : !child.contains(node);
473 }
474 }
475 // the disabled <fieldset> containing `node` has no <legend>
476 return true;
477 }
478 parentNode = parentNode.parentElement;
479 }
480 }
481
482 // else, node's tabbable/focusable state should not be affected by a fieldset's
483 // enabled/disabled state
484 return false;
485 };
486 var isNodeMatchingSelectorFocusable = function isNodeMatchingSelectorFocusable(options, node) {
487 if (node.disabled || isHiddenInput(node) || isHidden(node, options) ||
488 // For a details element with a summary, the summary element gets the focus
489 isDetailsWithSummary(node) || isDisabledFromFieldset(node)) {
490 return false;
491 }
492 return true;
493 };
494 var isNodeMatchingSelectorTabbable = function isNodeMatchingSelectorTabbable(options, node) {
495 if (isNonTabbableRadio(node) || getTabIndex(node) < 0 || !isNodeMatchingSelectorFocusable(options, node)) {
496 return false;
497 }
498 return true;
499 };
500 var isShadowRootTabbable = function isShadowRootTabbable(shadowHostNode) {
501 var tabIndex = parseInt(shadowHostNode.getAttribute('tabindex'), 10);
502 if (isNaN(tabIndex) || tabIndex >= 0) {
503 return true;
504 }
505 // If a custom element has an explicit negative tabindex,
506 // browsers will not allow tab targeting said element's children.
507 return false;
508 };
509
510 /**
511 * @param {Array.<Element|CandidateScope>} candidates
512 * @returns Element[]
513 */
514 var _sortByOrder = function sortByOrder(candidates) {
515 var regularTabbables = [];
516 var orderedTabbables = [];
517 candidates.forEach(function (item, i) {
518 var isScope = !!item.scopeParent;
519 var element = isScope ? item.scopeParent : item;
520 var candidateTabindex = getSortOrderTabIndex(element, isScope);
521 var elements = isScope ? _sortByOrder(item.candidates) : element;
522 if (candidateTabindex === 0) {
523 isScope ? regularTabbables.push.apply(regularTabbables, elements) : regularTabbables.push(element);
524 } else {
525 orderedTabbables.push({
526 documentOrder: i,
527 tabIndex: candidateTabindex,
528 item: item,
529 isScope: isScope,
530 content: elements
531 });
532 }
533 });
534 return orderedTabbables.sort(sortOrderedTabbables).reduce(function (acc, sortable) {
535 sortable.isScope ? acc.push.apply(acc, sortable.content) : acc.push(sortable.content);
536 return acc;
537 }, []).concat(regularTabbables);
538 };
539 var tabbable = function tabbable(container, options) {
540 options = options || {};
541 var candidates;
542 if (options.getShadowRoot) {
543 candidates = _getCandidatesIteratively([container], options.includeContainer, {
544 filter: isNodeMatchingSelectorTabbable.bind(null, options),
545 flatten: false,
546 getShadowRoot: options.getShadowRoot,
547 shadowRootFilter: isShadowRootTabbable
548 });
549 } else {
550 candidates = getCandidates(container, options.includeContainer, isNodeMatchingSelectorTabbable.bind(null, options));
551 }
552 return _sortByOrder(candidates);
553 };
554 var focusable = function focusable(container, options) {
555 options = options || {};
556 var candidates;
557 if (options.getShadowRoot) {
558 candidates = _getCandidatesIteratively([container], options.includeContainer, {
559 filter: isNodeMatchingSelectorFocusable.bind(null, options),
560 flatten: true,
561 getShadowRoot: options.getShadowRoot
562 });
563 } else {
564 candidates = getCandidates(container, options.includeContainer, isNodeMatchingSelectorFocusable.bind(null, options));
565 }
566 return candidates;
567 };
568 var isTabbable = function isTabbable(node, options) {
569 options = options || {};
570 if (!node) {
571 throw new Error('No node provided');
572 }
573 if (matches.call(node, candidateSelector) === false) {
574 return false;
575 }
576 return isNodeMatchingSelectorTabbable(options, node);
577 };
578 var focusableCandidateSelector = /* #__PURE__ */candidateSelectors.concat('iframe:not([inert]):not([inert] *)').join(',');
579 var isFocusable = function isFocusable(node, options) {
580 options = options || {};
581 if (!node) {
582 throw new Error('No node provided');
583 }
584 if (matches.call(node, focusableCandidateSelector) === false) {
585 return false;
586 }
587 return isNodeMatchingSelectorFocusable(options, node);
588 };
589
590 export { focusable, getTabIndex, isFocusable, isTabbable, tabbable };
591 //# sourceMappingURL=index.esm.js.map