2022-09-15 07:25:20 +00:00
|
|
|
const HIDDEN_VISIBILITY_VALUES = ['hidden', 'collapse'];
|
|
|
|
|
2022-09-15 06:22:20 +00:00
|
|
|
/**
|
|
|
|
* @internal
|
|
|
|
*/
|
|
|
|
export const checkVisibility = (
|
|
|
|
node: Node | null,
|
|
|
|
visible?: boolean
|
|
|
|
): Node | boolean => {
|
|
|
|
if (!node) {
|
|
|
|
return visible === false;
|
|
|
|
}
|
|
|
|
if (visible === undefined) {
|
|
|
|
return node;
|
|
|
|
}
|
|
|
|
const element = (
|
|
|
|
node.nodeType === Node.TEXT_NODE ? node.parentElement : node
|
|
|
|
) as Element;
|
|
|
|
|
|
|
|
const style = window.getComputedStyle(element);
|
|
|
|
const isVisible =
|
2022-09-15 07:25:20 +00:00
|
|
|
style &&
|
|
|
|
!HIDDEN_VISIBILITY_VALUES.includes(style.visibility) &&
|
2022-11-14 08:34:07 +00:00
|
|
|
!isBoundingBoxEmpty(element);
|
2022-09-15 06:22:20 +00:00
|
|
|
return visible === isVisible ? node : false;
|
|
|
|
};
|
|
|
|
|
2022-11-14 08:34:07 +00:00
|
|
|
function isBoundingBoxEmpty(element: Element): boolean {
|
2022-09-15 06:22:20 +00:00
|
|
|
const rect = element.getBoundingClientRect();
|
2022-11-14 08:34:07 +00:00
|
|
|
return rect.width === 0 || rect.height === 0;
|
2022-09-15 06:22:20 +00:00
|
|
|
}
|