UNPKG

2.39 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3exports.getParentAutofocusables = exports.getFocusables = void 0;
4var constants_1 = require("../constants");
5var array_1 = require("./array");
6var tabbables_1 = require("./tabbables");
7var queryTabbables = tabbables_1.tabbables.join(',');
8var queryGuardTabbables = "".concat(queryTabbables, ", [data-focus-guard]");
9var getFocusablesWithShadowDom = function (parent, withGuards) {
10 return (0, array_1.toArray)((parent.shadowRoot || parent).children).reduce(function (acc, child) {
11 return acc.concat(child.matches(withGuards ? queryGuardTabbables : queryTabbables) ? [child] : [], getFocusablesWithShadowDom(child));
12 }, []);
13};
14var getFocusablesWithIFrame = function (parent, withGuards) {
15 var _a;
16 // contentDocument of iframe will be null if current origin cannot access it
17 if (parent instanceof HTMLIFrameElement && ((_a = parent.contentDocument) === null || _a === void 0 ? void 0 : _a.body)) {
18 return (0, exports.getFocusables)([parent.contentDocument.body], withGuards);
19 }
20 return [parent];
21};
22var getFocusables = function (parents, withGuards) {
23 return parents.reduce(function (acc, parent) {
24 var _a;
25 var focusableWithShadowDom = getFocusablesWithShadowDom(parent, withGuards);
26 var focusableWithIframes = (_a = []).concat.apply(_a, focusableWithShadowDom.map(function (node) { return getFocusablesWithIFrame(node, withGuards); }));
27 return acc.concat(
28 // add all tabbables inside and within shadow DOMs in DOM order
29 focusableWithIframes,
30 // add if node is tabbable itself
31 parent.parentNode
32 ? (0, array_1.toArray)(parent.parentNode.querySelectorAll(queryTabbables)).filter(function (node) { return node === parent; })
33 : []);
34 }, []);
35};
36exports.getFocusables = getFocusables;
37/**
38 * return a list of focusable nodes within an area marked as "auto-focusable"
39 * @param parent
40 */
41var getParentAutofocusables = function (parent) {
42 var parentFocus = parent.querySelectorAll("[".concat(constants_1.FOCUS_AUTO, "]"));
43 return (0, array_1.toArray)(parentFocus)
44 .map(function (node) { return (0, exports.getFocusables)([node]); })
45 .reduce(function (acc, nodes) { return acc.concat(nodes); }, []);
46};
47exports.getParentAutofocusables = getParentAutofocusables;