Skip to content

Commit abf9df3

Browse files
authored
cherry-pick(#27188): fix(locators): do not escape regular expressions with u or v flag (#27190)
Fixes #27163.
1 parent 35d8604 commit abf9df3

File tree

2 files changed

+10
-0
lines changed

2 files changed

+10
-0
lines changed

packages/playwright-core/src/utils/isomorphic/stringUtils.ts

+6
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,12 @@ export function normalizeEscapedRegexQuotes(source: string) {
7474
}
7575

7676
function escapeRegexForSelector(re: RegExp): string {
77+
// Unicode mode does not allow "identity character escapes", so we do not escape and
78+
// hope that it does not contain quotes and/or >> signs.
79+
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Regular_expressions/Character_escape
80+
// TODO: rework RE usages in internal selectors away from literal representation to json, e.g. {source,flags}.
81+
if (re.unicode || (re as any).unicodeSets)
82+
return String(re);
7783
// Even number of backslashes followed by the quote -> insert a backslash.
7884
return String(re).replace(/(^|[^\\])(\\\\)*(["'`])/g, '$1$2\\$3').replace(/>>/g, '\\>\\>');
7985
}

tests/page/locator-query.spec.ts

+4
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,10 @@ it('should filter by regex with a single quote', async ({ page }) => {
115115
await expect.soft(page.getByRole('button', { name: /let\\'s let\\\'s/i }).locator('span')).toHaveText('hello');
116116
await expect.soft(page.locator('button', { hasText: /let\\\'s let\\'s/i }).locator('span')).toHaveText('hello');
117117
await expect.soft(page.getByRole('button', { name: /let\\\'s let\\'s/i }).locator('span')).toHaveText('hello');
118+
119+
await page.setContent(`<button>let's hello</button>`);
120+
await expect.soft(page.locator('button', { hasText: /let's/iu })).toHaveText(`let's hello`);
121+
await expect.soft(page.getByRole('button', { name: /let's/iu })).toHaveText(`let's hello`);
118122
});
119123

120124
it('should filter by regex and regexp flags', async ({ page }) => {

0 commit comments

Comments
 (0)