Skip to content

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

perf(aria): cache computed values when building aria snapshots (#41867)

Dmitry Gozman0cbee5ab8e7f
diff --git a/packages/injected/src/domUtils.ts b/packages/injected/src/domUtils.ts
index efa9e50564..c88184506a 100644
--- a/packages/injected/src/domUtils.ts
+++ b/packages/injected/src/domUtils.ts
@@ -85,6 +85,15 @@ export function getElementComputedStyle(element: Element, pseudo?: string): CSSS
}
export function isElementStyleVisibilityVisible(element: Element, style?: CSSStyleDeclaration): boolean {
+ const cached = cacheStyleVisibility?.get(element);
+ if (cached !== undefined)
+ return cached;
+ const result = computeElementStyleVisibilityVisible(element, style);
+ cacheStyleVisibility?.set(element, result);
+ return result;
+}
+
+function computeElementStyleVisibilityVisible(element: Element, style?: CSSStyleDeclaration): boolean {
style = style ?? getElementComputedStyle(element);
if (!style)
return true;
@@ -144,8 +153,15 @@ export function isVisibleTextNode(node: Text) {
export function elementSafeTagName(element: Element) {
const tagName = element.tagName;
- if (typeof tagName === 'string') // Fast path.
- return tagName.toUpperCase();
+ if (typeof tagName === 'string') { // Fast path.
+ // Tag names in html documents are already uppercase. Lowercase names come from
+ // svg/mathml elements and from xml/xhtml documents, and they all start with
+ // a lowercase letter, so uppercasing can be skipped otherwise.
+ const firstCharCode = tagName.charCodeAt(0);
+ if (firstCharCode >= 97 && firstCharCode <= 122)
+ return tagName.toUpperCase();
+ return tagName;
+ }
// Named inputs, e.g. <input name=tagName>, will be exposed as fields on the parent <form>
// and override its properties.
if (element instanceof HTMLFormElement)
@@ -157,6 +173,7 @@ export function elementSafeTagName(element: Element) {
let cacheStyle: Map<Element, CSSStyleDeclaration | undefined> | undefined;
let cacheStyleBefore: Map<Element, CSSStyleDeclaration | undefined> | undefined;
let cacheStyleAfter: Map<Element, CSSStyleDeclaration | undefined> | undefined;
+let cacheStyleVisibility: Map<Element, boolean> | undefined;
let cachesCounter = 0;
export function beginDOMCaches() {
@@ -164,6 +181,7 @@ export function beginDOMCaches() {
cacheStyle ??= new Map();
cacheStyleBefore ??= new Map();
cacheStyleAfter ??= new Map();
+ cacheStyleVisibility ??= new Map();
}
export function endDOMCaches() {
@@ -171,5 +189,6 @@ export function endDOMCaches() {
cacheStyle = undefined;
cacheStyleBefore = undefined;
cacheStyleAfter = undefined;
+ cacheStyleVisibility = undefined;
}
}
diff --git a/packages/injected/src/roleUtils.ts b/packages/injected/src/roleUtils.ts
index 1004fb54ef..b230fbf847 100644
--- a/packages/injected/src/roleUtils.ts
+++ b/packages/injected/src/roleUtils.ts
@@ -279,6 +279,15 @@ function hasPresentationConflictResolution(element: Element, role: string | null
}
export function getAriaRole(element: Element): AriaRole | null {
+ const cached = cacheAriaRole?.get(element);
+ if (cached !== undefined)
+ return cached;
+ const role = computeAriaRole(element);
+ cacheAriaRole?.set(element, role);
+ return role;
+}
+
+function computeAriaRole(element: Element): AriaRole | null {
const explicitRole = getExplicitAriaRole(element);
if (!explicitRole)
return getImplicitAriaRole(element);
@@ -1164,19 +1173,28 @@ function belongsToDisabledFieldSet(element: Element): boolean {
return !legendElement || !legendElement.contains(element);
}
-function hasExplicitAriaDisabled(element: Element | undefined, isAncestor = false): boolean {
- if (!element)
+function hasExplicitAriaDisabled(element: Element): boolean {
+ if (!kAriaDisabledRoles.includes(getAriaRole(element) || ''))
return false;
- if (isAncestor || kAriaDisabledRoles.includes(getAriaRole(element) || '')) {
+ return hasAriaDisabledInChain(element);
+}
+
+function hasAriaDisabledInChain(element: Element): boolean {
+ let result = cacheAriaDisabled?.get(element);
+ if (result === undefined) {
const attribute = (element.getAttribute('aria-disabled') || '').toLowerCase();
- if (attribute === 'true')
- return true;
- if (attribute === 'false')
- return false;
- // aria-disabled works across shadow boundaries.
- return hasExplicitAriaDisabled(parentElementOrShadowHost(element), true);
+ if (attribute === 'true') {
+ result = true;
+ } else if (attribute === 'false') {
+ result = false;
+ } else {
+ // aria-disabled works across shadow boundaries.
+ const parent = parentElementOrShadowHost(element);
+ result = parent ? hasAriaDisabledInChain(parent) : false;
+ }
+ cacheAriaDisabled?.set(element, result);
}
- return false;
+ return result;
}
function getAccessibleNameFromAssociatedLabels(labels: Iterable<HTMLLabelElement>, options: AccessibleNameOptions): CompositeString {
@@ -1236,11 +1254,15 @@ let cachePseudoContent: Map<Element, string | undefined> | undefined;
let cachePseudoContentBefore: Map<Element, string | undefined> | undefined;
let cachePseudoContentAfter: Map<Element, string | undefined> | undefined;
let cachePointerEvents: Map<Element, boolean> | undefined;
+let cacheAriaRole: Map<Element, AriaRole | null> | undefined;
+let cacheAriaDisabled: Map<Element, boolean> | undefined;
let cachesCounter = 0;
export function beginAriaCaches() {
beginDOMCaches();
++cachesCounter;
+ cacheAriaRole ??= new Map();
+ cacheAriaDisabled ??= new Map();
cacheAccessibleName ??= new Map();
cacheAccessibleNameHidden ??= new Map();
cacheAccessibleNameText ??= new Map();
@@ -1269,6 +1291,8 @@ export function endAriaCaches() {
cachePseudoContentBefore = undefined;
cachePseudoContentAfter = undefined;
cachePointerEvents = undefined;
+ cacheAriaRole = undefined;
+ cacheAriaDisabled = undefined;
}
endDOMCaches();
}