import { FOCUS_AUTO } from '../constants'; import { toArray } from './array'; import { tabbables } from './tabbables'; var queryTabbables = tabbables.join(','); var queryGuardTabbables = "".concat(queryTabbables, ", [data-focus-guard]"); var getFocusablesWithShadowDom = function (parent, withGuards) { return toArray((parent.shadowRoot || parent).children).reduce(function (acc, child) { return acc.concat(child.matches(withGuards ? queryGuardTabbables : queryTabbables) ? [child] : [], getFocusablesWithShadowDom(child)); }, []); }; var getFocusablesWithIFrame = function (parent, withGuards) { var _a; // contentDocument of iframe will be null if current origin cannot access it if (parent instanceof HTMLIFrameElement && ((_a = parent.contentDocument) === null || _a === void 0 ? void 0 : _a.body)) { return getFocusables([parent.contentDocument.body], withGuards); } return [parent]; }; export var getFocusables = function (parents, withGuards) { return parents.reduce(function (acc, parent) { var _a; var focusableWithShadowDom = getFocusablesWithShadowDom(parent, withGuards); var focusableWithIframes = (_a = []).concat.apply(_a, focusableWithShadowDom.map(function (node) { return getFocusablesWithIFrame(node, withGuards); })); return acc.concat( // add all tabbables inside and within shadow DOMs in DOM order focusableWithIframes, // add if node is tabbable itself parent.parentNode ? toArray(parent.parentNode.querySelectorAll(queryTabbables)).filter(function (node) { return node === parent; }) : []); }, []); }; /** * return a list of focusable nodes within an area marked as "auto-focusable" * @param parent */ export var getParentAutofocusables = function (parent) { var parentFocus = parent.querySelectorAll("[".concat(FOCUS_AUTO, "]")); return toArray(parentFocus) .map(function (node) { return getFocusables([node]); }) .reduce(function (acc, nodes) { return acc.concat(nodes); }, []); };