Debian Codegraph @

node/tabbable/dist/index.js

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