From 3f0af1e4df257203ac30a531d84a28bb2dbffc1d Mon Sep 17 00:00:00 2001 From: Dmitry Gozman Date: Thu, 6 Oct 2022 18:01:14 -0700 Subject: [PATCH] cherry-pick(#17893): fix(locators): make regex escape work when multiple spaces are present (#17894) --- .../src/utils/isomorphic/stringUtils.ts | 2 +- tests/page/selectors-get-by.spec.ts | 40 ++++++++++--------- tests/playwright-test/hooks.spec.ts | 6 +-- 3 files changed, 25 insertions(+), 23 deletions(-) diff --git a/packages/playwright-core/src/utils/isomorphic/stringUtils.ts b/packages/playwright-core/src/utils/isomorphic/stringUtils.ts index f060eeb658423..f642e5cc8c7be 100644 --- a/packages/playwright-core/src/utils/isomorphic/stringUtils.ts +++ b/packages/playwright-core/src/utils/isomorphic/stringUtils.ts @@ -68,7 +68,7 @@ export function escapeForTextSelector(text: string | RegExp, exact: boolean, cas if (exact) return '"' + text.replace(/["]/g, '\\"') + '"'; if (text.includes('"') || text.includes('>>') || text[0] === '/') - return `/${escapeForRegex(text).replace(/\s+/, '\\s+')}/` + (caseSensitive ? '' : 'i'); + return `/${escapeForRegex(text).replace(/\s+/g, '\\s+')}/` + (caseSensitive ? '' : 'i'); return text; } diff --git a/tests/page/selectors-get-by.spec.ts b/tests/page/selectors-get-by.spec.ts index 63510c10074f2..dba79785921fc 100644 --- a/tests/page/selectors-get-by.spec.ts +++ b/tests/page/selectors-get-by.spec.ts @@ -108,29 +108,31 @@ it('getByTitle should work', async ({ page }) => { }); it('getBy escaping', async ({ page }) => { - await page.setContent(`