Skip to content

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

chore: minor optimizations in selectors (#37339)

Dmitry Gozmanf3157f1a6fee
diff --git a/packages/injected/src/domUtils.ts b/packages/injected/src/domUtils.ts
index c40c6918f0..eb2b6a25f5 100644
--- a/packages/injected/src/domUtils.ts
+++ b/packages/injected/src/domUtils.ts
@@ -76,7 +76,12 @@ export function closestCrossShadow(element: Element | undefined, css: string, sc
}
export function getElementComputedStyle(element: Element, pseudo?: string): CSSStyleDeclaration | undefined {
- return element.ownerDocument && element.ownerDocument.defaultView ? element.ownerDocument.defaultView.getComputedStyle(element, pseudo) : undefined;
+ const cache = pseudo === '::before' ? cacheStyleBefore : pseudo === '::after' ? cacheStyleAfter : cacheStyle;
+ if (cache && cache.has(element))
+ return cache.get(element);
+ const style = element.ownerDocument && element.ownerDocument.defaultView ? element.ownerDocument.defaultView.getComputedStyle(element, pseudo) : undefined;
+ cache?.set(element, style);
+ return style;
}
export function isElementStyleVisibilityVisible(element: Element, style?: CSSStyleDeclaration): boolean {
@@ -144,6 +149,9 @@ export function isVisibleTextNode(node: Text) {
}
export function elementSafeTagName(element: Element) {
+ const tagName = element.tagName;
+ if (typeof tagName === 'string') // Fast path.
+ return tagName.toUpperCase();
// Named inputs, e.g. <input name=tagName>, will be exposed as fields on the parent <form>
// and override its properties.
if (element instanceof HTMLFormElement)
@@ -151,3 +159,23 @@ export function elementSafeTagName(element: Element) {
// Elements from the svg namespace do not have uppercase tagName right away.
return element.tagName.toUpperCase();
}
+
+let cacheStyle: Map<Element, CSSStyleDeclaration | undefined> | undefined;
+let cacheStyleBefore: Map<Element, CSSStyleDeclaration | undefined> | undefined;
+let cacheStyleAfter: Map<Element, CSSStyleDeclaration | undefined> | undefined;
+let cachesCounter = 0;
+
+export function beginDOMCaches() {
+ ++cachesCounter;
+ cacheStyle ??= new Map();
+ cacheStyleBefore ??= new Map();
+ cacheStyleAfter ??= new Map();
+}
+
+export function endDOMCaches() {
+ if (!--cachesCounter) {
+ cacheStyle = undefined;
+ cacheStyleBefore = undefined;
+ cacheStyleAfter = undefined;
+ }
+}
diff --git a/packages/injected/src/roleUtils.ts b/packages/injected/src/roleUtils.ts
index e652ee8c15..582c266f49 100644
--- a/packages/injected/src/roleUtils.ts
+++ b/packages/injected/src/roleUtils.ts
@@ -16,7 +16,7 @@
import * as css from '@isomorphic/cssTokenizer';
-import { closestCrossShadow, elementSafeTagName, enclosingShadowRootOrDocument, getElementComputedStyle, isElementStyleVisibilityVisible, isVisibleTextNode, parentElementOrShadowHost } from './domUtils';
+import { beginDOMCaches, closestCrossShadow, elementSafeTagName, enclosingShadowRootOrDocument, endDOMCaches, getElementComputedStyle, isElementStyleVisibilityVisible, isVisibleTextNode, parentElementOrShadowHost } from './domUtils';
import type { AriaRole } from '@isomorphic/ariaSnapshot';
@@ -367,9 +367,14 @@ export function getCSSContent(element: Element, pseudo?: '::before' | '::after')
const style = getElementComputedStyle(element, pseudo);
let content: string | undefined;
- if (style && style.display !== 'none' && style.visibility !== 'hidden') {
- // Note: all browsers ignore display:none and visibility:hidden pseudos.
- content = parseCSSContentPropertyAsString(element, style.content, !!pseudo);
+ if (style) {
+ const contentValue = style.content;
+ if (contentValue && contentValue !== 'none' && contentValue !== 'normal') {
+ if (style.display !== 'none' && style.visibility !== 'hidden') {
+ // Note: all browsers ignore display:none and visibility:hidden pseudos.
+ content = parseCSSContentPropertyAsString(element, contentValue, !!pseudo);
+ }
+ }
}
if (pseudo && content !== undefined) {
@@ -1150,6 +1155,7 @@ let cachePointerEvents: Map<Element, boolean> | undefined;
let cachesCounter = 0;
export function beginAriaCaches() {
+ beginDOMCaches();
++cachesCounter;
cacheAccessibleName ??= new Map();
cacheAccessibleNameHidden ??= new Map();
@@ -1176,6 +1182,7 @@ export function endAriaCaches() {
cachePseudoContentAfter = undefined;
cachePointerEvents = undefined;
}
+ endDOMCaches();
}
const inputTypeToRole: Record<string, AriaRole> = {
diff --git a/packages/injected/src/selectorGenerator.ts b/packages/injected/src/selectorGenerator.ts
index a7dad6644f..64a9136a18 100644
--- a/packages/injected/src/selectorGenerator.ts
+++ b/packages/injected/src/selectorGenerator.ts
@@ -16,7 +16,7 @@
import { escapeForAttributeSelector, escapeForTextSelector, escapeRegExp, quoteCSSAttributeValue } from '@isomorphic/stringUtils';
-import { closestCrossShadow, isElementVisible, isInsideScope, parentElementOrShadowHost } from './domUtils';
+import { beginDOMCaches, closestCrossShadow, endDOMCaches, isElementVisible, isInsideScope, parentElementOrShadowHost } from './domUtils';
import { beginAriaCaches, endAriaCaches, getAriaRole, getElementAccessibleName } from './roleUtils';
import { elementText, getElementLabels } from './selectorUtils';
@@ -78,6 +78,7 @@ export function generateSelector(injectedScript: InjectedScript, targetElement:
injectedScript._evaluator.begin();
const cache: Cache = { allowText: new Map(), disallowText: new Map() };
beginAriaCaches();
+ beginDOMCaches();
try {
let selectors: string[] = [];
if (options.forTextExpect) {
@@ -135,6 +136,7 @@ export function generateSelector(injectedScript: InjectedScript, targetElement:
elements: injectedScript.querySelectorAll(parsedSelector, options.root ?? targetElement.ownerDocument)
};
} finally {
+ endDOMCaches();
endAriaCaches();
injectedScript._evaluator.end();
}