diff --git a/packages/puppeteer-core/src/injected/PQuerySelector.ts b/packages/puppeteer-core/src/injected/PQuerySelector.ts index c136cda772d..3bd75c4e9b1 100644 --- a/packages/puppeteer-core/src/injected/PQuerySelector.ts +++ b/packages/puppeteer-core/src/injected/PQuerySelector.ts @@ -172,10 +172,10 @@ class PQueryEngine { } class DepthCalculator { - #cache = new Map(); + #cache = new WeakMap(); - calculate(node: Node, depth: number[] = []): number[] { - if (node instanceof Document) { + calculate(node: Node | null, depth: number[] = []): number[] { + if (node === null) { return depth; } if (node instanceof ShadowRoot) { @@ -196,7 +196,7 @@ class DepthCalculator { ++index; } - const value = this.calculate(node.parentNode as Node, [index]); + const value = this.calculate(node.parentNode, [index]); this.#cache.set(node, value); return [...value, ...depth]; }