Merged in fix/tabbable-errors (pull request #1187)

Fix: Can only call Element.querySelectorAll on instances of Element

* Fix: Can only call Element.querySelectorAll on instances of Element
errors


Approved-by: Erik Tiekstra
This commit is contained in:
Linus Flood
2025-01-17 11:38:51 +00:00
parent d229b5c463
commit b6185e166a

View File

@@ -47,17 +47,21 @@ export function focusable(element: HTMLElement, isTabIndexNotNaN: boolean) {
} }
export function tabbable(element: HTMLElement) { export function tabbable(element: HTMLElement) {
let tabIndex = element.getAttribute("tabindex") const tabIndexAttr = element.getAttribute("tabindex")
//@ts-ignore const tabIndex = tabIndexAttr !== null ? Number(tabIndexAttr) : undefined
if (tabIndex === null) tabIndex = undefined const isTabIndexNaN = tabIndex === undefined || isNaN(tabIndex)
//@ts-ignore
const isTabIndexNaN = isNaN(tabIndex)
//@ts-ignore
return (isTabIndexNaN || tabIndex >= 0) && focusable(element, !isTabIndexNaN) return (isTabIndexNaN || tabIndex >= 0) && focusable(element, !isTabIndexNaN)
} }
export default function findTabbableDescendants( export default function findTabbableDescendants(
element: HTMLElement element: HTMLElement | null | undefined
): HTMLElement[] { ): HTMLElement[] {
return [].slice.call(element.querySelectorAll("*"), 0).filter(tabbable) if (!(element instanceof HTMLElement)) {
return []
}
return Array.from(element.querySelectorAll("*"))
.filter((el): el is HTMLElement => el instanceof HTMLElement)
.filter(tabbable)
} }