Debian Codegraph @

node/tabbable/dist/index.umd.js

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