2022-05-11 13:49:12 +01:00
|
|
|
/**
|
|
|
|
* Copyright (c) Microsoft Corporation.
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
export function isInsideScope(scope: Node, element: Element | undefined): boolean {
|
|
|
|
while (element) {
|
|
|
|
if (scope.contains(element))
|
|
|
|
return true;
|
|
|
|
element = enclosingShadowHost(element);
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
export function parentElementOrShadowHost(element: Element): Element | undefined {
|
|
|
|
if (element.parentElement)
|
|
|
|
return element.parentElement;
|
|
|
|
if (!element.parentNode)
|
|
|
|
return;
|
|
|
|
if (element.parentNode.nodeType === 11 /* Node.DOCUMENT_FRAGMENT_NODE */ && (element.parentNode as ShadowRoot).host)
|
|
|
|
return (element.parentNode as ShadowRoot).host;
|
|
|
|
}
|
|
|
|
|
|
|
|
export function enclosingShadowRootOrDocument(element: Element): Document | ShadowRoot | undefined {
|
|
|
|
let node: Node = element;
|
|
|
|
while (node.parentNode)
|
|
|
|
node = node.parentNode;
|
|
|
|
if (node.nodeType === 11 /* Node.DOCUMENT_FRAGMENT_NODE */ || node.nodeType === 9 /* Node.DOCUMENT_NODE */)
|
|
|
|
return node as Document | ShadowRoot;
|
|
|
|
}
|
|
|
|
|
|
|
|
function enclosingShadowHost(element: Element): Element | undefined {
|
|
|
|
while (element.parentElement)
|
|
|
|
element = element.parentElement;
|
|
|
|
return parentElementOrShadowHost(element);
|
|
|
|
}
|
|
|
|
|
2023-06-13 21:25:39 -07:00
|
|
|
export function closestCrossShadow(element: Element | undefined, css: string, scope?: Document | Element): Element | undefined {
|
2022-05-11 13:49:12 +01:00
|
|
|
while (element) {
|
|
|
|
const closest = element.closest(css);
|
2023-06-13 21:25:39 -07:00
|
|
|
if (scope && closest !== scope && closest?.contains(scope))
|
|
|
|
return;
|
2022-05-11 13:49:12 +01:00
|
|
|
if (closest)
|
|
|
|
return closest;
|
|
|
|
element = enclosingShadowHost(element);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-07 15:10:18 -08:00
|
|
|
export function getElementComputedStyle(element: Element, pseudo?: string): CSSStyleDeclaration | undefined {
|
|
|
|
return element.ownerDocument && element.ownerDocument.defaultView ? element.ownerDocument.defaultView.getComputedStyle(element, pseudo) : undefined;
|
|
|
|
}
|
|
|
|
|
|
|
|
export function isElementStyleVisibilityVisible(element: Element, style?: CSSStyleDeclaration): boolean {
|
|
|
|
style = style ?? getElementComputedStyle(element);
|
|
|
|
if (!style)
|
|
|
|
return true;
|
|
|
|
// Element.checkVisibility checks for content-visibility and also looks at
|
|
|
|
// styles up the flat tree including user-agent ShadowRoots, such as the
|
|
|
|
// details element for example.
|
|
|
|
// @ts-ignore Typescript doesn't know that checkVisibility exists yet.
|
|
|
|
if (Element.prototype.checkVisibility) {
|
|
|
|
// @ts-ignore Typescript doesn't know that checkVisibility exists yet.
|
|
|
|
if (!element.checkVisibility({ checkOpacity: false, checkVisibilityCSS: false }))
|
|
|
|
return false;
|
|
|
|
} else {
|
|
|
|
// Manual workaround for WebKit that does not have checkVisibility.
|
|
|
|
const detailsOrSummary = element.closest('details,summary');
|
|
|
|
if (detailsOrSummary !== element && detailsOrSummary?.nodeName === 'DETAILS' && !(detailsOrSummary as HTMLDetailsElement).open)
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (style.visibility !== 'visible')
|
|
|
|
return false;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2022-05-11 13:49:12 +01:00
|
|
|
export function isElementVisible(element: Element): boolean {
|
|
|
|
// Note: this logic should be similar to waitForDisplayedAtStablePosition() to avoid surprises.
|
2023-02-07 15:10:18 -08:00
|
|
|
const style = getElementComputedStyle(element);
|
|
|
|
if (!style)
|
2022-05-11 13:49:12 +01:00
|
|
|
return true;
|
|
|
|
if (style.display === 'contents') {
|
|
|
|
// display:contents is not rendered itself, but its child nodes are.
|
|
|
|
for (let child = element.firstChild; child; child = child.nextSibling) {
|
|
|
|
if (child.nodeType === 1 /* Node.ELEMENT_NODE */ && isElementVisible(child as Element))
|
|
|
|
return true;
|
|
|
|
if (child.nodeType === 3 /* Node.TEXT_NODE */ && isVisibleTextNode(child as Text))
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2023-02-07 15:10:18 -08:00
|
|
|
if (!isElementStyleVisibilityVisible(element, style))
|
2022-09-22 13:48:58 -07:00
|
|
|
return false;
|
2022-05-11 13:49:12 +01:00
|
|
|
const rect = element.getBoundingClientRect();
|
|
|
|
return rect.width > 0 && rect.height > 0;
|
|
|
|
}
|
|
|
|
|
2023-06-08 16:00:48 -07:00
|
|
|
export function isVisibleTextNode(node: Text) {
|
2022-05-11 13:49:12 +01:00
|
|
|
// https://stackoverflow.com/questions/1461059/is-there-an-equivalent-to-getboundingclientrect-for-text-nodes
|
2023-02-17 11:19:53 -08:00
|
|
|
const range = node.ownerDocument.createRange();
|
2022-05-11 13:49:12 +01:00
|
|
|
range.selectNode(node);
|
|
|
|
const rect = range.getBoundingClientRect();
|
|
|
|
return rect.width > 0 && rect.height > 0;
|
|
|
|
}
|