runline 0.23.0 → 0.24.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,157 @@
1
+ /**
2
+ * Copyright (c) Microsoft Corporation.
3
+ *
4
+ * Licensed under the Apache License, Version 2.0 (the "License");
5
+ * you may not use this file except in compliance with the License.
6
+ * You may obtain a copy of the License at
7
+ *
8
+ * http://www.apache.org/licenses/LICENSE-2.0
9
+ *
10
+ * Unless required by applicable law or agreed to in writing, software
11
+ * distributed under the License is distributed on an "AS IS" BASIS,
12
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ * See the License for the specific language governing permissions and
14
+ * limitations under the License.
15
+ */
16
+ let globalOptions = {};
17
+ export function setGlobalOptions(options) {
18
+ globalOptions = options;
19
+ }
20
+ export function getGlobalOptions() {
21
+ return globalOptions;
22
+ }
23
+ export function isInsideScope(scope, element) {
24
+ while (element) {
25
+ if (scope.contains(element))
26
+ return true;
27
+ element = enclosingShadowHost(element);
28
+ }
29
+ return false;
30
+ }
31
+ export function enclosingElement(node) {
32
+ if (node.nodeType === 1 /* Node.ELEMENT_NODE */)
33
+ return node;
34
+ return node.parentElement ?? undefined;
35
+ }
36
+ export function parentElementOrShadowHost(element) {
37
+ if (element.parentElement)
38
+ return element.parentElement;
39
+ if (!element.parentNode)
40
+ return;
41
+ if (element.parentNode.nodeType === 11 /* Node.DOCUMENT_FRAGMENT_NODE */ && element.parentNode.host)
42
+ return element.parentNode.host;
43
+ }
44
+ export function enclosingShadowRootOrDocument(element) {
45
+ let node = element;
46
+ while (node.parentNode)
47
+ node = node.parentNode;
48
+ if (node.nodeType === 11 /* Node.DOCUMENT_FRAGMENT_NODE */ || node.nodeType === 9 /* Node.DOCUMENT_NODE */)
49
+ return node;
50
+ }
51
+ function enclosingShadowHost(element) {
52
+ while (element.parentElement)
53
+ element = element.parentElement;
54
+ return parentElementOrShadowHost(element);
55
+ }
56
+ // Assumption: if scope is provided, element must be inside scope's subtree.
57
+ export function closestCrossShadow(element, css, scope) {
58
+ while (element) {
59
+ const closest = element.closest(css);
60
+ if (scope && closest !== scope && closest?.contains(scope))
61
+ return;
62
+ if (closest)
63
+ return closest;
64
+ element = enclosingShadowHost(element);
65
+ }
66
+ }
67
+ export function getElementComputedStyle(element, pseudo) {
68
+ const cache = pseudo === '::before' ? cacheStyleBefore : pseudo === '::after' ? cacheStyleAfter : cacheStyle;
69
+ if (cache && cache.has(element))
70
+ return cache.get(element);
71
+ const style = element.ownerDocument && element.ownerDocument.defaultView ? element.ownerDocument.defaultView.getComputedStyle(element, pseudo) : undefined;
72
+ cache?.set(element, style);
73
+ return style;
74
+ }
75
+ export function isElementStyleVisibilityVisible(element, style) {
76
+ style = style ?? getElementComputedStyle(element);
77
+ if (!style)
78
+ return true;
79
+ // Element.checkVisibility checks for content-visibility and also looks at
80
+ // styles up the flat tree including user-agent ShadowRoots, such as the
81
+ // details element for example.
82
+ // All the browser implement it, but WebKit has a bug which prevents us from using it:
83
+ // https://bugs.webkit.org/show_bug.cgi?id=264733
84
+ // @ts-ignore
85
+ if (Element.prototype.checkVisibility && globalOptions.browserNameForWorkarounds !== 'webkit') {
86
+ if (!element.checkVisibility())
87
+ return false;
88
+ }
89
+ else {
90
+ // Manual workaround for WebKit that does not have checkVisibility.
91
+ const detailsOrSummary = element.closest('details,summary');
92
+ if (detailsOrSummary !== element && detailsOrSummary?.nodeName === 'DETAILS' && !detailsOrSummary.open)
93
+ return false;
94
+ }
95
+ if (style.visibility !== 'visible')
96
+ return false;
97
+ return true;
98
+ }
99
+ export function computeBox(element) {
100
+ // Note: this logic should be similar to waitForDisplayedAtStablePosition() to avoid surprises.
101
+ const style = getElementComputedStyle(element);
102
+ if (!style)
103
+ return { visible: true, inline: false };
104
+ const cursor = style.cursor;
105
+ if (style.display === 'contents') {
106
+ // display:contents is not rendered itself, but its child nodes are.
107
+ for (let child = element.firstChild; child; child = child.nextSibling) {
108
+ if (child.nodeType === 1 /* Node.ELEMENT_NODE */ && isElementVisible(child))
109
+ return { visible: true, inline: false, cursor };
110
+ if (child.nodeType === 3 /* Node.TEXT_NODE */ && isVisibleTextNode(child))
111
+ return { visible: true, inline: true, cursor };
112
+ }
113
+ return { visible: false, inline: false, cursor };
114
+ }
115
+ if (!isElementStyleVisibilityVisible(element, style))
116
+ return { cursor, visible: false, inline: false };
117
+ const rect = element.getBoundingClientRect();
118
+ return { cursor, visible: rect.width > 0 && rect.height > 0, inline: style.display === 'inline' };
119
+ }
120
+ export function isElementVisible(element) {
121
+ return computeBox(element).visible;
122
+ }
123
+ export function isVisibleTextNode(node) {
124
+ // https://stackoverflow.com/questions/1461059/is-there-an-equivalent-to-getboundingclientrect-for-text-nodes
125
+ const range = node.ownerDocument.createRange();
126
+ range.selectNode(node);
127
+ const rect = range.getBoundingClientRect();
128
+ return rect.width > 0 && rect.height > 0;
129
+ }
130
+ export function elementSafeTagName(element) {
131
+ const tagName = element.tagName;
132
+ if (typeof tagName === 'string') // Fast path.
133
+ return tagName.toUpperCase();
134
+ // Named inputs, e.g. <input name=tagName>, will be exposed as fields on the parent <form>
135
+ // and override its properties.
136
+ if (element instanceof HTMLFormElement)
137
+ return 'FORM';
138
+ // Elements from the svg namespace do not have uppercase tagName right away.
139
+ return element.tagName.toUpperCase();
140
+ }
141
+ let cacheStyle;
142
+ let cacheStyleBefore;
143
+ let cacheStyleAfter;
144
+ let cachesCounter = 0;
145
+ export function beginDOMCaches() {
146
+ ++cachesCounter;
147
+ cacheStyle ??= new Map();
148
+ cacheStyleBefore ??= new Map();
149
+ cacheStyleAfter ??= new Map();
150
+ }
151
+ export function endDOMCaches() {
152
+ if (!--cachesCounter) {
153
+ cacheStyle = undefined;
154
+ cacheStyleBefore = undefined;
155
+ cacheStyleAfter = undefined;
156
+ }
157
+ }