Skip to content

Commit

Permalink
perf: use querySelector* for pure CSS selectors (#9835)
Browse files Browse the repository at this point in the history
  • Loading branch information
jrandolf committed Mar 13, 2023
1 parent e5b5645 commit 8aea8e0
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 4 deletions.
12 changes: 11 additions & 1 deletion packages/puppeteer-core/src/injected/PQuerySelector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,10 @@ class PQueryEngine {
}
}

type QueryableNode = {
querySelectorAll: typeof Document.prototype.querySelectorAll;
};

/**
* Queries the given node for all nodes matching the given text selector.
*
Expand All @@ -180,15 +184,21 @@ export const pQuerySelectorAll = async function* (
selector: string
): AwaitableIterable<Node> {
let selectors: ComplexPSelectorList;
let isPureCSS: boolean;
try {
selectors = parsePSelectors(selector);
[selectors, isPureCSS] = parsePSelectors(selector);
} catch (error) {
if (!isErrorLike(error)) {
throw new SelectorError(selector, String(error));
}
throw new SelectorError(selector, error.message);
}

if (isPureCSS) {
yield* (root as unknown as QueryableNode).querySelectorAll(selector);
return;
}

// If there are any empty elements, then this implies the selector has
// contiguous combinators (e.g. `>>> >>>>`) or starts/ends with one which we
// treat as illegal, similar to existing behavior.
Expand Down
12 changes: 9 additions & 3 deletions packages/puppeteer-core/src/injected/PSelectorParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,10 +75,13 @@ const unquote = (text: string): string => {
return text;
};

export function parsePSelectors(selector: string): ComplexPSelectorList {
export function parsePSelectors(
selector: string
): [selector: ComplexPSelectorList, isPureCSS: boolean] {
let isPureCSS = true;
const tokens = tokenize(selector);
if (tokens.length === 0) {
return [];
return [[], isPureCSS];
}
let compoundSelector: CompoundPSelector = [];
let complexSelector: ComplexPSelector = [compoundSelector];
Expand All @@ -89,6 +92,7 @@ export function parsePSelectors(selector: string): ComplexPSelectorList {
case 'combinator':
switch (token.content) {
case '>>>':
isPureCSS = false;
if (storage.length) {
compoundSelector.push(storage.toStringAndClear());
}
Expand All @@ -97,6 +101,7 @@ export function parsePSelectors(selector: string): ComplexPSelectorList {
complexSelector.push(compoundSelector);
continue;
case '>>>>':
isPureCSS = false;
if (storage.length) {
compoundSelector.push(storage.toStringAndClear());
}
Expand All @@ -110,6 +115,7 @@ export function parsePSelectors(selector: string): ComplexPSelectorList {
if (!token.name.startsWith('-p-')) {
break;
}
isPureCSS = false;
if (storage.length) {
compoundSelector.push(storage.toStringAndClear());
}
Expand All @@ -132,5 +138,5 @@ export function parsePSelectors(selector: string): ComplexPSelectorList {
if (storage.length) {
compoundSelector.push(storage.toStringAndClear());
}
return selectors;
return [selectors, isPureCSS];
}

0 comments on commit 8aea8e0

Please sign in to comment.