UNPKG

4.01 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3exports.isDefined = exports.isNotAGuard = exports.isGuard = exports.isAutoFocusAllowed = exports.notHiddenInput = exports.isRadioElement = exports.isHTMLInputElement = exports.isHTMLButtonElement = exports.getDataset = exports.isAutoFocusAllowedCached = exports.isVisibleCached = void 0;
4var constants_1 = require("../constants");
5var isElementHidden = function (node) {
6 // we can measure only "elements"
7 // consider others as "visible"
8 if (node.nodeType !== Node.ELEMENT_NODE) {
9 return false;
10 }
11 var computedStyle = window.getComputedStyle(node, null);
12 if (!computedStyle || !computedStyle.getPropertyValue) {
13 return false;
14 }
15 return (computedStyle.getPropertyValue('display') === 'none' || computedStyle.getPropertyValue('visibility') === 'hidden');
16};
17var getParentNode = function (node) {
18 // DOCUMENT_FRAGMENT_NODE can also point on ShadowRoot. In this case .host will point on the next node
19 return node.parentNode && node.parentNode.nodeType === Node.DOCUMENT_FRAGMENT_NODE
20 ? // eslint-disable-next-line @typescript-eslint/no-explicit-any
21 node.parentNode.host
22 : node.parentNode;
23};
24var isTopNode = function (node) {
25 // @ts-ignore
26 return node === document || (node && node.nodeType === Node.DOCUMENT_NODE);
27};
28var isInert = function (node) { return node.hasAttribute('inert'); };
29/**
30 * @see https://github.com/testing-library/jest-dom/blob/main/src/to-be-visible.js
31 */
32var isVisibleUncached = function (node, checkParent) {
33 return !node || isTopNode(node) || (!isElementHidden(node) && !isInert(node) && checkParent(getParentNode(node)));
34};
35var isVisibleCached = function (visibilityCache, node) {
36 var cached = visibilityCache.get(node);
37 if (cached !== undefined) {
38 return cached;
39 }
40 var result = isVisibleUncached(node, exports.isVisibleCached.bind(undefined, visibilityCache));
41 visibilityCache.set(node, result);
42 return result;
43};
44exports.isVisibleCached = isVisibleCached;
45var isAutoFocusAllowedUncached = function (node, checkParent) {
46 return node && !isTopNode(node) ? ((0, exports.isAutoFocusAllowed)(node) ? checkParent(getParentNode(node)) : false) : true;
47};
48var isAutoFocusAllowedCached = function (cache, node) {
49 var cached = cache.get(node);
50 if (cached !== undefined) {
51 return cached;
52 }
53 var result = isAutoFocusAllowedUncached(node, exports.isAutoFocusAllowedCached.bind(undefined, cache));
54 cache.set(node, result);
55 return result;
56};
57exports.isAutoFocusAllowedCached = isAutoFocusAllowedCached;
58var getDataset = function (node) {
59 // @ts-ignore
60 return node.dataset;
61};
62exports.getDataset = getDataset;
63var isHTMLButtonElement = function (node) { return node.tagName === 'BUTTON'; };
64exports.isHTMLButtonElement = isHTMLButtonElement;
65var isHTMLInputElement = function (node) { return node.tagName === 'INPUT'; };
66exports.isHTMLInputElement = isHTMLInputElement;
67var isRadioElement = function (node) {
68 return (0, exports.isHTMLInputElement)(node) && node.type === 'radio';
69};
70exports.isRadioElement = isRadioElement;
71var notHiddenInput = function (node) {
72 return !(((0, exports.isHTMLInputElement)(node) || (0, exports.isHTMLButtonElement)(node)) && (node.type === 'hidden' || node.disabled));
73};
74exports.notHiddenInput = notHiddenInput;
75var isAutoFocusAllowed = function (node) {
76 var attribute = node.getAttribute(constants_1.FOCUS_NO_AUTOFOCUS);
77 return ![true, 'true', ''].includes(attribute);
78};
79exports.isAutoFocusAllowed = isAutoFocusAllowed;
80var isGuard = function (node) { var _a; return Boolean(node && ((_a = (0, exports.getDataset)(node)) === null || _a === void 0 ? void 0 : _a.focusGuard)); };
81exports.isGuard = isGuard;
82var isNotAGuard = function (node) { return !(0, exports.isGuard)(node); };
83exports.isNotAGuard = isNotAGuard;
84var isDefined = function (x) { return Boolean(x); };
85exports.isDefined = isDefined;