UNPKG

1.9 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3exports.getAllAffectedNodes = void 0;
4var constants_1 = require("../constants");
5var array_1 = require("./array");
6/**
7 * in case of multiple nodes nested inside each other
8 * keeps only top ones
9 * this is O(nlogn)
10 * @param nodes
11 * @returns {*}
12 */
13var filterNested = function (nodes) {
14 var contained = new Set();
15 var l = nodes.length;
16 for (var i = 0; i < l; i += 1) {
17 for (var j = i + 1; j < l; j += 1) {
18 var position = nodes[i].compareDocumentPosition(nodes[j]);
19 /* eslint-disable no-bitwise */
20 if ((position & Node.DOCUMENT_POSITION_CONTAINED_BY) > 0) {
21 contained.add(j);
22 }
23 if ((position & Node.DOCUMENT_POSITION_CONTAINS) > 0) {
24 contained.add(i);
25 }
26 /* eslint-enable */
27 }
28 }
29 return nodes.filter(function (_, index) { return !contained.has(index); });
30};
31/**
32 * finds top most parent for a node
33 * @param node
34 * @returns {*}
35 */
36var getTopParent = function (node) {
37 return node.parentNode ? getTopParent(node.parentNode) : node;
38};
39/**
40 * returns all "focus containers" inside a given node
41 * @param node - node or nodes to look inside
42 * @returns Element[]
43 */
44var getAllAffectedNodes = function (node) {
45 var nodes = (0, array_1.asArray)(node);
46 return nodes.filter(Boolean).reduce(function (acc, currentNode) {
47 var group = currentNode.getAttribute(constants_1.FOCUS_GROUP);
48 acc.push.apply(acc, (group
49 ? filterNested((0, array_1.toArray)(getTopParent(currentNode).querySelectorAll("[".concat(constants_1.FOCUS_GROUP, "=\"").concat(group, "\"]:not([").concat(constants_1.FOCUS_DISABLED, "=\"disabled\"])"))))
50 : [currentNode]));
51 return acc;
52 }, []);
53};
54exports.getAllAffectedNodes = getAllAffectedNodes;