@selenium/atoms 0.0.0-stage → 4.51.0-nightly202609302012

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,515 @@
1
+ // Licensed to the Software Freedom Conservancy (SFC) under one
2
+ // or more contributor license agreements. See the NOTICE file
3
+ // distributed with this work for additional information
4
+ // regarding copyright ownership. The SFC licenses this file
5
+ // to you under the Apache License, Version 2.0 (the
6
+ // "License"); you may not use this file except in compliance
7
+ // with the License. You may obtain a copy of the License at
8
+ //
9
+ // http://www.apache.org/licenses/LICENSE-2.0
10
+ //
11
+ // Unless required by applicable law or agreed to in writing,
12
+ // software distributed under the License is distributed on an
13
+ // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14
+ // KIND, either express or implied. See the License for the
15
+ // specific language governing permissions and limitations
16
+ // under the License.
17
+
18
+ type OverflowState = 'none' | 'hidden' | 'scroll';
19
+
20
+ interface Rect {
21
+ left: number;
22
+ top: number;
23
+ right: number;
24
+ bottom: number;
25
+ width: number;
26
+ height: number;
27
+ }
28
+
29
+ interface ImageMapResult {
30
+ image: Element | null;
31
+ rect: Rect;
32
+ }
33
+
34
+ interface OverflowStyles {
35
+ x: string;
36
+ y: string;
37
+ }
38
+
39
+ interface Coordinate {
40
+ x: number;
41
+ y: number;
42
+ }
43
+
44
+ function isShownElement(elem: Element, optIgnoreOpacity?: boolean): boolean {
45
+ // Guards against form children that shadow tagName (e.g. <form><input name="tagName">).
46
+ var tagNameDescriptor = Object.getOwnPropertyDescriptor(Element.prototype, 'tagName');
47
+
48
+ // Per-call memoisation caches. Scoped here so they live only for the
49
+ // duration of this synchronous call — no stale-data risk, no GC pressure
50
+ // between invocations.
51
+ var computedStyleCache = new Map<Element, CSSStyleDeclaration | null>();
52
+ var clientRectCache = new Map<Element, Rect>();
53
+ var displayedCache = new Map<Node, boolean>();
54
+
55
+ function toUpperCaseTag(tagName?: string): string | undefined {
56
+ return tagName ? tagName.toUpperCase() : undefined;
57
+ }
58
+
59
+ function isElement(node: unknown, tagName?: string): node is Element {
60
+ // Use nodeType instead of instanceof to handle cross-realm Elements
61
+ // (e.g., from iframes), where instanceof checks against a different Element.
62
+ if (!node || (node as Node).nodeType !== 1 /* ELEMENT_NODE */) {
63
+ return false;
64
+ }
65
+ var asElement = node as Element;
66
+ var rawTagName = tagNameDescriptor && typeof tagNameDescriptor.get === 'function'
67
+ ? tagNameDescriptor.get.call(asElement)
68
+ : asElement.tagName;
69
+ var upperTagName = typeof rawTagName === 'string' ? rawTagName.toUpperCase() : '';
70
+ var normalizedTagName = toUpperCaseTag(tagName);
71
+ if (upperTagName === 'FORM') {
72
+ return !normalizedTagName || normalizedTagName === 'FORM';
73
+ }
74
+ return !!upperTagName && (!normalizedTagName || upperTagName === normalizedTagName);
75
+ }
76
+
77
+ function getParentElement(node: Node): Element | null {
78
+ var current = node.parentNode;
79
+ while (
80
+ current &&
81
+ current.nodeType !== Node.ELEMENT_NODE &&
82
+ current.nodeType !== Node.DOCUMENT_NODE &&
83
+ current.nodeType !== Node.DOCUMENT_FRAGMENT_NODE
84
+ ) {
85
+ current = current.parentNode;
86
+ }
87
+ return current && current.nodeType === 1 /* ELEMENT_NODE */ ? (current as Element) : null;
88
+ }
89
+
90
+ function getEffectiveStyle(elem: Element, propertyName: string): string | null {
91
+ var computed = computedStyleCache.get(elem);
92
+ if (computed === undefined) {
93
+ var win = elem.ownerDocument.defaultView;
94
+ computed = win ? (win.getComputedStyle(elem) || null) : null;
95
+ computedStyleCache.set(elem, computed);
96
+ }
97
+ if (!computed) {
98
+ return null;
99
+ }
100
+ var value = computed.getPropertyValue(propertyName);
101
+ return value || null;
102
+ }
103
+
104
+ function getOpacity(elem: Element): number {
105
+ var opacityStyle = getEffectiveStyle(elem, 'opacity');
106
+ var opacity = opacityStyle ? Number(opacityStyle) : 1;
107
+ var parent = getParentElement(elem);
108
+ return parent ? opacity * getOpacity(parent) : opacity;
109
+ }
110
+
111
+ function getParentNodeInComposedDom(node: Node): Node | null {
112
+ var parent = node.parentNode;
113
+ var slottable = node as Node & { assignedSlot?: HTMLSlotElement | null };
114
+ var parentWithShadow = parent as (Node & { shadowRoot?: ShadowRoot | null }) | null;
115
+
116
+ if (parentWithShadow && parentWithShadow.shadowRoot && slottable.assignedSlot !== undefined) {
117
+ return slottable.assignedSlot ? slottable.assignedSlot.parentNode : null;
118
+ }
119
+
120
+ return parent;
121
+ }
122
+
123
+ function createRect(left: number, top: number, width: number, height: number): Rect {
124
+ return {
125
+ left: left,
126
+ top: top,
127
+ right: left + width,
128
+ bottom: top + height,
129
+ width: width,
130
+ height: height,
131
+ };
132
+ }
133
+
134
+ function getClientRect(elem: Element): Rect {
135
+ var cachedRect = clientRectCache.get(elem);
136
+ if (cachedRect) {
137
+ return cachedRect;
138
+ }
139
+
140
+ var rect: Rect;
141
+ var imageMap = maybeFindImageMap(elem);
142
+ if (imageMap) {
143
+ rect = imageMap.rect;
144
+ } else {
145
+ var elemTagName = typeof (elem as Element).tagName === 'string' ? (elem as Element).tagName : '';
146
+ if (elemTagName.toUpperCase() === 'HTML') {
147
+ var doc = (elem as Element).ownerDocument;
148
+ // In quirks mode (no DOCTYPE), viewport dimensions come from document.body;
149
+ // documentElement.clientWidth/Height is unreliable and can be 0.
150
+ var sizeElem = doc.compatMode === 'CSS1Compat' ? doc.documentElement : (doc.body || doc.documentElement);
151
+ rect = createRect(0, 0, sizeElem.clientWidth, sizeElem.clientHeight);
152
+ } else {
153
+ try {
154
+ var nativeRect = (elem as Element).getBoundingClientRect();
155
+ rect = {
156
+ left: nativeRect.left,
157
+ top: nativeRect.top,
158
+ right: nativeRect.right,
159
+ bottom: nativeRect.bottom,
160
+ width: nativeRect.right - nativeRect.left,
161
+ height: nativeRect.bottom - nativeRect.top,
162
+ };
163
+ } catch (_error) {
164
+ rect = createRect(0, 0, 0, 0);
165
+ }
166
+ }
167
+ }
168
+
169
+ clientRectCache.set(elem, rect);
170
+ return rect;
171
+ }
172
+
173
+ function getAreaRelativeRect(area: HTMLAreaElement): Rect {
174
+ var shape = area.shape.toLowerCase();
175
+ var coords = area.coords.split(',').map(function (value) {
176
+ return Number(value.trim());
177
+ });
178
+
179
+ if (shape === 'rect' && coords.length === 4) {
180
+ return createRect(coords[0], coords[1], coords[2] - coords[0], coords[3] - coords[1]);
181
+ }
182
+
183
+ if (shape === 'circle' && coords.length === 3) {
184
+ return createRect(coords[0] - coords[2], coords[1] - coords[2], coords[2] * 2, coords[2] * 2);
185
+ }
186
+
187
+ if (shape === 'poly' && coords.length > 2) {
188
+ var minX = coords[0];
189
+ var minY = coords[1];
190
+ var maxX = minX;
191
+ var maxY = minY;
192
+
193
+ for (var index = 2; index + 1 < coords.length; index += 2) {
194
+ minX = Math.min(minX, coords[index]);
195
+ maxX = Math.max(maxX, coords[index]);
196
+ minY = Math.min(minY, coords[index + 1]);
197
+ maxY = Math.max(maxY, coords[index + 1]);
198
+ }
199
+
200
+ return createRect(minX, minY, maxX - minX, maxY - minY);
201
+ }
202
+
203
+ return createRect(0, 0, 0, 0);
204
+ }
205
+
206
+ function findImageUsingMap(mapName: string, doc: Document): Element | null {
207
+ // Use querySelector instead of a full-DOM scan; escape the map name so
208
+ // special characters in the CSS attribute value string are handled safely.
209
+ return doc.querySelector('[usemap="#' + mapName.replace(/\\/g, '\\\\').replace(/"/g, '\\"') + '"]');
210
+ }
211
+
212
+ function maybeFindImageMap(elem: Element): ImageMapResult | null {
213
+ var isMap = isElement(elem, 'MAP');
214
+ if (!isMap && !isElement(elem, 'AREA')) {
215
+ return null;
216
+ }
217
+
218
+ var map = isMap ? elem : isElement(elem.parentNode, 'MAP') ? elem.parentNode : null;
219
+ var image: Element | null = null;
220
+ var rect = createRect(0, 0, 0, 0);
221
+
222
+ if (isElement(map, 'MAP') && (map as HTMLMapElement).name) {
223
+ image = findImageUsingMap((map as HTMLMapElement).name, (map as HTMLMapElement).ownerDocument);
224
+ if (image) {
225
+ rect = getClientRect(image);
226
+ if (!isMap && isElement(elem, 'AREA') && (elem as HTMLAreaElement).shape.toLowerCase() !== 'default') {
227
+ var relativeRect = getAreaRelativeRect(elem as HTMLAreaElement);
228
+ var relativeX = Math.min(Math.max(relativeRect.left, 0), rect.width);
229
+ var relativeY = Math.min(Math.max(relativeRect.top, 0), rect.height);
230
+ var width = Math.min(relativeRect.width, rect.width - relativeX);
231
+ var height = Math.min(relativeRect.height, rect.height - relativeY);
232
+ rect = createRect(relativeX + rect.left, relativeY + rect.top, width, height);
233
+ }
234
+ }
235
+ }
236
+
237
+ return { image: image, rect: rect };
238
+ }
239
+
240
+ function getClientRegion(elem: Element): Rect {
241
+ return getClientRect(elem);
242
+ }
243
+
244
+ function getOverflowState(elem: Element): OverflowState {
245
+ var region = getClientRegion(elem);
246
+ var ownerDoc = elem.ownerDocument;
247
+ var htmlElem = ownerDoc.documentElement;
248
+ var bodyElem = ownerDoc.body;
249
+ var htmlOverflowStyle = getEffectiveStyle(htmlElem, 'overflow') || 'visible';
250
+ var treatAsFixedPosition = false;
251
+
252
+ function canBeOverflowed(container: Element, position: string | null): boolean {
253
+ if (container === htmlElem) {
254
+ return true;
255
+ }
256
+
257
+ var display = getEffectiveStyle(container, 'display') || '';
258
+ if (display.indexOf('inline') === 0 || display === 'contents') {
259
+ return false;
260
+ }
261
+
262
+ if (position === 'absolute' && getEffectiveStyle(container, 'position') === 'static') {
263
+ return false;
264
+ }
265
+
266
+ return true;
267
+ }
268
+
269
+ function getOverflowParent(current: Element): Element | null {
270
+ var position = getEffectiveStyle(current, 'position');
271
+ if (position === 'fixed') {
272
+ treatAsFixedPosition = true;
273
+ return current === htmlElem ? null : htmlElem;
274
+ }
275
+
276
+ var parent = getParentElement(current);
277
+ while (parent && !canBeOverflowed(parent, position)) {
278
+ parent = getParentElement(parent);
279
+ }
280
+ return parent;
281
+ }
282
+
283
+ function getOverflowStyles(current: Element): OverflowStyles {
284
+ var overflowElem = current;
285
+ if (htmlOverflowStyle === 'visible') {
286
+ if (current === htmlElem && bodyElem) {
287
+ overflowElem = bodyElem;
288
+ } else if (current === bodyElem) {
289
+ return { x: 'visible', y: 'visible' };
290
+ }
291
+ }
292
+
293
+ var overflow = {
294
+ x: getEffectiveStyle(overflowElem, 'overflow-x') || 'visible',
295
+ y: getEffectiveStyle(overflowElem, 'overflow-y') || 'visible',
296
+ };
297
+
298
+ if (current === htmlElem) {
299
+ overflow.x = overflow.x === 'visible' ? 'auto' : overflow.x;
300
+ overflow.y = overflow.y === 'visible' ? 'auto' : overflow.y;
301
+ }
302
+
303
+ return overflow;
304
+ }
305
+
306
+ function getScroll(current: Element): Coordinate {
307
+ if (current === htmlElem) {
308
+ return {
309
+ x: ownerDoc.defaultView ? ownerDoc.defaultView.pageXOffset : 0,
310
+ y: ownerDoc.defaultView ? ownerDoc.defaultView.pageYOffset : 0,
311
+ };
312
+ }
313
+
314
+ return { x: current.scrollLeft, y: current.scrollTop };
315
+ }
316
+
317
+ for (var container = getOverflowParent(elem); container; container = getOverflowParent(container)) {
318
+ var containerOverflow = getOverflowStyles(container);
319
+ if (containerOverflow.x === 'visible' && containerOverflow.y === 'visible') {
320
+ continue;
321
+ }
322
+
323
+ var containerRect = getClientRect(container);
324
+ if (containerRect.width === 0 || containerRect.height === 0) {
325
+ return 'hidden';
326
+ }
327
+
328
+ var underflowsX = region.right < containerRect.left;
329
+ var underflowsY = region.bottom < containerRect.top;
330
+ if ((underflowsX && containerOverflow.x === 'hidden') || (underflowsY && containerOverflow.y === 'hidden')) {
331
+ return 'hidden';
332
+ }
333
+
334
+ if ((underflowsX && containerOverflow.x !== 'visible') || (underflowsY && containerOverflow.y !== 'visible')) {
335
+ var containerScroll = getScroll(container);
336
+ var unscrollableX = region.right < containerRect.left - containerScroll.x;
337
+ var unscrollableY = region.bottom < containerRect.top - containerScroll.y;
338
+ if ((unscrollableX && containerOverflow.x !== 'visible') || (unscrollableY && containerOverflow.y !== 'visible')) {
339
+ return 'hidden';
340
+ }
341
+
342
+ var containerUnderflowState = getOverflowState(container);
343
+ return containerUnderflowState === 'hidden' ? 'hidden' : 'scroll';
344
+ }
345
+
346
+ var overflowsX = region.left >= containerRect.left + containerRect.width;
347
+ var overflowsY = region.top >= containerRect.top + containerRect.height;
348
+ if ((overflowsX && containerOverflow.x === 'hidden') || (overflowsY && containerOverflow.y === 'hidden')) {
349
+ return 'hidden';
350
+ }
351
+
352
+ if ((overflowsX && containerOverflow.x !== 'visible') || (overflowsY && containerOverflow.y !== 'visible')) {
353
+ if (treatAsFixedPosition) {
354
+ var docScroll = getScroll(container);
355
+ if (region.left >= htmlElem.scrollWidth - docScroll.x || region.right >= htmlElem.scrollHeight - docScroll.y) {
356
+ return 'hidden';
357
+ }
358
+ }
359
+
360
+ var containerOverflowState = getOverflowState(container);
361
+ return containerOverflowState === 'hidden' ? 'hidden' : 'scroll';
362
+ }
363
+ }
364
+
365
+ return 'none';
366
+ }
367
+
368
+ function isShownInternal(elem: Element, ignoreOpacity: boolean, displayedFn: (element: Node) => boolean): boolean {
369
+ if (!isElement(elem)) {
370
+ throw new Error('Argument to isShown must be of type Element');
371
+ }
372
+
373
+ if (isElement(elem, 'BODY')) {
374
+ return true;
375
+ }
376
+
377
+ if (isElement(elem, 'OPTION') || isElement(elem, 'OPTGROUP')) {
378
+ var select = (elem as Element).closest('select');
379
+ return !!select && isShownInternal(select, true, displayedFn);
380
+ }
381
+
382
+ var imageMap = maybeFindImageMap(elem);
383
+ if (imageMap) {
384
+ return !!imageMap.image && imageMap.rect.width > 0 && imageMap.rect.height > 0 &&
385
+ isShownInternal(imageMap.image, ignoreOpacity, displayedFn);
386
+ }
387
+
388
+ if (isElement(elem, 'INPUT') && (elem as HTMLInputElement).type.toLowerCase() === 'hidden') {
389
+ return false;
390
+ }
391
+
392
+ if (isElement(elem, 'NOSCRIPT')) {
393
+ return false;
394
+ }
395
+
396
+ var visibility = getEffectiveStyle(elem, 'visibility');
397
+ if (visibility === 'collapse' || visibility === 'hidden') {
398
+ return false;
399
+ }
400
+
401
+ if (!displayedFn(elem)) {
402
+ return false;
403
+ }
404
+
405
+ if (!ignoreOpacity && getOpacity(elem) === 0) {
406
+ return false;
407
+ }
408
+
409
+ function positiveSize(element: Element): boolean {
410
+ var rect = getClientRect(element);
411
+ if (rect.height > 0 && rect.width > 0) {
412
+ return true;
413
+ }
414
+
415
+ if (isElement(element, 'PATH') && (rect.height > 0 || rect.width > 0)) {
416
+ var strokeWidth = getEffectiveStyle(element, 'stroke-width');
417
+ return !!strokeWidth && parseInt(strokeWidth, 10) > 0;
418
+ }
419
+
420
+ var elementVisibility = getEffectiveStyle(element, 'visibility');
421
+ if (elementVisibility === 'collapse' || elementVisibility === 'hidden') {
422
+ return false;
423
+ }
424
+
425
+ if (!displayedFn(element)) {
426
+ return false;
427
+ }
428
+
429
+ if (getEffectiveStyle(element, 'overflow') === 'hidden') {
430
+ return false;
431
+ }
432
+
433
+ for (var index = 0; index < element.childNodes.length; index += 1) {
434
+ var child = element.childNodes[index];
435
+ if (child.nodeType === Node.TEXT_NODE) {
436
+ var text = child.nodeValue || '';
437
+ if (/^[\s]*$/.test(text) && /[\n\r\t]/.test(text)) {
438
+ continue;
439
+ }
440
+ return true;
441
+ }
442
+
443
+ if (isElement(child) && positiveSize(child)) {
444
+ return true;
445
+ }
446
+ }
447
+
448
+ return false;
449
+ }
450
+
451
+ if (!positiveSize(elem)) {
452
+ return false;
453
+ }
454
+
455
+ function hiddenByOverflow(element: Element): boolean {
456
+ if (getOverflowState(element) !== 'hidden') {
457
+ return false;
458
+ }
459
+
460
+ for (var index = 0; index < element.childNodes.length; index += 1) {
461
+ var child = element.childNodes[index];
462
+ if (isElement(child) && !hiddenByOverflow(child) && positiveSize(child)) {
463
+ return false;
464
+ }
465
+ }
466
+
467
+ return true;
468
+ }
469
+
470
+ return !hiddenByOverflow(elem);
471
+ }
472
+
473
+ function displayed(node: Node): boolean {
474
+ var cached = displayedCache.get(node);
475
+ if (cached !== undefined) {
476
+ return cached;
477
+ }
478
+
479
+ if (isElement(node)) {
480
+ var display = getEffectiveStyle(node, 'display');
481
+ var contentVisibility = getEffectiveStyle(node, 'content-visibility');
482
+ if (display === 'none' || contentVisibility === 'hidden') {
483
+ displayedCache.set(node, false);
484
+ return false;
485
+ }
486
+ }
487
+
488
+ var parent = getParentNodeInComposedDom(node);
489
+ if (typeof ShadowRoot === 'function' && parent instanceof ShadowRoot) {
490
+ if (parent.host.shadowRoot && parent.host.shadowRoot !== parent) {
491
+ displayedCache.set(node, false);
492
+ return false;
493
+ }
494
+ parent = parent.host;
495
+ }
496
+
497
+ if (parent && (parent.nodeType === Node.DOCUMENT_NODE || parent.nodeType === Node.DOCUMENT_FRAGMENT_NODE)) {
498
+ displayedCache.set(node, true);
499
+ return true;
500
+ }
501
+
502
+ if (isElement(parent, 'DETAILS') && !(<HTMLDetailsElement>parent).open && !isElement(node, 'SUMMARY')) {
503
+ displayedCache.set(node, false);
504
+ return false;
505
+ }
506
+
507
+ var result = !!parent && displayed(parent);
508
+ displayedCache.set(node, result);
509
+ return result;
510
+ }
511
+
512
+ return isShownInternal(elem, !!optIgnoreOpacity, displayed);
513
+ }
514
+
515
+ export default isShownElement;