UNPKG

3.73 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3exports.contains = exports.parentAutofocusables = exports.getFocusableNodes = exports.getTabbableNodes = exports.filterAutoFocusable = exports.filterFocusable = void 0;
4var array_1 = require("./array");
5var is_1 = require("./is");
6var tabOrder_1 = require("./tabOrder");
7var tabUtils_1 = require("./tabUtils");
8/**
9 * given list of focusable elements keeps the ones user can interact with
10 * @param nodes
11 * @param visibilityCache
12 */
13var filterFocusable = function (nodes, visibilityCache) {
14 return (0, array_1.toArray)(nodes)
15 .filter(function (node) { return (0, is_1.isVisibleCached)(visibilityCache, node); })
16 .filter(function (node) { return (0, is_1.notHiddenInput)(node); });
17};
18exports.filterFocusable = filterFocusable;
19var filterAutoFocusable = function (nodes, cache) {
20 if (cache === void 0) { cache = new Map(); }
21 return (0, array_1.toArray)(nodes).filter(function (node) { return (0, is_1.isAutoFocusAllowedCached)(cache, node); });
22};
23exports.filterAutoFocusable = filterAutoFocusable;
24/**
25 * !__WARNING__! Low level API.
26 * @returns all tabbable nodes
27 *
28 * @see {@link getFocusableNodes} to get any focusable element
29 *
30 * @param topNodes - array of top level HTMLElements to search inside
31 * @param visibilityCache - an cache to store intermediate measurements. Expected to be a fresh `new Map` on every call
32 */
33var getTabbableNodes = function (topNodes, visibilityCache, withGuards) {
34 return (0, tabOrder_1.orderByTabIndex)((0, exports.filterFocusable)((0, tabUtils_1.getFocusables)(topNodes, withGuards), visibilityCache), true, withGuards);
35};
36exports.getTabbableNodes = getTabbableNodes;
37/**
38 * !__WARNING__! Low level API.
39 *
40 * @returns anything "focusable", not only tabbable. The difference is in `tabIndex=-1`
41 * (without guards, as long as they are not expected to be ever focused)
42 *
43 * @see {@link getTabbableNodes} to get only tabble nodes element
44 *
45 * @param topNodes - array of top level HTMLElements to search inside
46 * @param visibilityCache - an cache to store intermediate measurements. Expected to be a fresh `new Map` on every call
47 */
48var getFocusableNodes = function (topNodes, visibilityCache) {
49 return (0, tabOrder_1.orderByTabIndex)((0, exports.filterFocusable)((0, tabUtils_1.getFocusables)(topNodes), visibilityCache), false);
50};
51exports.getFocusableNodes = getFocusableNodes;
52/**
53 * return list of nodes which are expected to be auto-focused
54 * @param topNode
55 * @param visibilityCache
56 */
57var parentAutofocusables = function (topNode, visibilityCache) {
58 return (0, exports.filterFocusable)((0, tabUtils_1.getParentAutofocusables)(topNode), visibilityCache);
59};
60exports.parentAutofocusables = parentAutofocusables;
61/*
62 * Determines if element is contained in scope, including nested shadow DOMs
63 */
64var contains = function (scope, element) {
65 if (scope.shadowRoot) {
66 return (0, exports.contains)(scope.shadowRoot, element);
67 }
68 else {
69 if (Object.getPrototypeOf(scope).contains !== undefined &&
70 Object.getPrototypeOf(scope).contains.call(scope, element)) {
71 return true;
72 }
73 return (0, array_1.toArray)(scope.children).some(function (child) {
74 var _a;
75 if (child instanceof HTMLIFrameElement) {
76 var iframeBody = (_a = child.contentDocument) === null || _a === void 0 ? void 0 : _a.body;
77 if (iframeBody) {
78 return (0, exports.contains)(iframeBody, element);
79 }
80 return false;
81 }
82 return (0, exports.contains)(child, element);
83 });
84 }
85};
86exports.contains = contains;