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