From 84a956f56ba9ce74e9dd0f95ff40fdd14be87b1d Mon Sep 17 00:00:00 2001 From: jrandolf <101637635+jrandolf@users.noreply.github.com> Date: Thu, 29 Jun 2023 13:00:57 +0200 Subject: [PATCH] fix: fix escaping algo for P selectors (#10474) --- .../src/injected/PSelectorParser.ts | 20 +++++++----------- test/assets/p-selectors.html | 1 + test/src/queryhandler.spec.ts | 21 +++++++++++++++++++ 3 files changed, 30 insertions(+), 12 deletions(-) diff --git a/packages/puppeteer-core/src/injected/PSelectorParser.ts b/packages/puppeteer-core/src/injected/PSelectorParser.ts index 19bb9e300077a..a7096f99d556b 100644 --- a/packages/puppeteer-core/src/injected/PSelectorParser.ts +++ b/packages/puppeteer-core/src/injected/PSelectorParser.ts @@ -33,19 +33,15 @@ TOKENS['combinator'] = /\s*(>>>>?|[\s>+~])\s*/g; const ESCAPE_REGEXP = /\\[\s\S]/g; const unquote = (text: string): string => { - if (text.length > 1) { - for (const char of ['"', "'"]) { - if (!text.startsWith(char) || !text.endsWith(char)) { - continue; - } - return text - .slice(char.length, -char.length) - .replace(ESCAPE_REGEXP, match => { - return match.slice(1); - }); - } + if (text.length <= 1) { + return text; + } + if ((text[0] === '"' || text[0] === "'") && text.endsWith(text[0])) { + text = text.slice(1, -1); } - return text; + return text.replace(ESCAPE_REGEXP, match => { + return match[1] as string; + }); }; export function parsePSelectors( diff --git a/test/assets/p-selectors.html b/test/assets/p-selectors.html index 1bcda60467657..24900623d8ef1 100644 --- a/test/assets/p-selectors.html +++ b/test/assets/p-selectors.html @@ -2,6 +2,7 @@
+My name is Jun (pronounced like "June")