From b64457d8ce999e19989a6eee52ded47d2f116593 Mon Sep 17 00:00:00 2001 From: Dmitry Gozman Date: Thu, 6 Oct 2022 16:23:40 -0700 Subject: [PATCH] fix(locators): make regex escape work when multiple spaces are present (#17893) --- .../src/utils/isomorphic/stringUtils.ts | 2 +- tests/page/selectors-get-by.spec.ts | 38 ++++++++++--------- tests/playwright-test/hooks.spec.ts | 6 +-- 3 files changed, 24 insertions(+), 22 deletions(-) diff --git a/packages/playwright-core/src/utils/isomorphic/stringUtils.ts b/packages/playwright-core/src/utils/isomorphic/stringUtils.ts index f060eeb658..f642e5cc8c 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 63510c1007..dba7978592 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(`