Skip to content

Playwright is a framework for Web Testing and Automation. It allows testing Chromium, Firefox and WebKit with a single API.

fix(injected): reuse a single range in isVisibleTextNode (#43076)

Dmitry Gozmanc0e09f0dad45
diff --git a/packages/injected/src/domUtils.ts b/packages/injected/src/domUtils.ts
index 557e4ca9f3..4b6d1dbf9a 100644
--- a/packages/injected/src/domUtils.ts
+++ b/packages/injected/src/domUtils.ts
@@ -148,12 +148,21 @@ export function isElementVisible(element: Element): boolean {
return computeBox(element).visible;
}
+const kTextNodeRange = Symbol('playwrightTextNodeRange');
+
export function isVisibleTextNode(node: Text) {
// https://stackoverflow.com/questions/1461059/is-there-an-equivalent-to-getboundingclientrect-for-text-nodes
- const range = node.ownerDocument.createRange();
+ // Reuse a single range per document. Browsers update every live range upon each DOM mutation
+ // until the range is garbage collected, so creating a range per text node slows the page down.
+ const document = node.ownerDocument as Document & { [kTextNodeRange]?: Range };
+ const range = (document[kTextNodeRange] ??= document.createRange());
range.selectNode(node);
const rect = range.getBoundingClientRect();
- return rect.width > 0 && rect.height > 0;
+ const result = rect.width > 0 && rect.height > 0;
+ // Collapse the range back to the document, so that it does not retain the node.
+ range.setStart(document, 0);
+ range.collapse(true);
+ return result;
}
export function elementSafeTagName(element: Element) {