pangu
Version:
Opinionated paranoid text spacing: automatically inserts whitespace between CJK (Chinese, Japanese, Korean) and ANS (alphabetical letters, numerical digits and symbols)
59 lines (58 loc) • 2.26 kB
JavaScript
//#region src/browser/dom/visibility-detector.ts
var VisibilityDetector = class {
cache = /* @__PURE__ */ new WeakMap();
clearCache() {
this.cache = /* @__PURE__ */ new WeakMap();
}
isElementVisuallyHidden(element) {
const style = getComputedStyle(element);
if (style.display === "none") return true;
if (style.visibility === "hidden") return true;
if (parseFloat(style.opacity) === 0) return true;
const clip = style.clip;
if (clip && (clip.includes("rect(1px, 1px, 1px, 1px)") || clip.includes("rect(0px, 0px, 0px, 0px)") || clip.includes("rect(0, 0, 0, 0)"))) return true;
if (style.overflow === "hidden" && style.position === "absolute") {
const height = parseInt(style.height, 10);
const width = parseInt(style.width, 10);
if (height === 1 && width === 1) return true;
}
return false;
}
shouldSkipSpacingAfterNode(node) {
let elementToCheck = null;
if (node instanceof Element) elementToCheck = node;
else if (node.parentElement) elementToCheck = node.parentElement;
if (elementToCheck && this.isElementVisuallyHiddenCached(elementToCheck)) return true;
let currentElement = elementToCheck?.parentElement;
while (currentElement) {
if (this.isElementVisuallyHiddenCached(currentElement)) return true;
currentElement = currentElement.parentElement;
}
return false;
}
shouldSkipSpacingBeforeNode(node) {
let previousNode = node.previousSibling;
if (!previousNode && node.parentElement) {
let parent = node.parentElement;
while (parent && !previousNode) {
previousNode = parent.previousSibling;
if (!previousNode) parent = parent.parentElement;
}
}
if (previousNode) {
if (previousNode instanceof Element && this.isElementVisuallyHiddenCached(previousNode)) return true;
else if (previousNode instanceof Text && previousNode.parentElement && this.isElementVisuallyHiddenCached(previousNode.parentElement)) return true;
}
return false;
}
isElementVisuallyHiddenCached(element) {
const cached = this.cache.get(element);
if (cached !== void 0) return cached;
const hidden = this.isElementVisuallyHidden(element);
this.cache.set(element, hidden);
return hidden;
}
};
//#endregion
export { VisibilityDetector };
//# sourceMappingURL=visibility-detector.js.map