cherry-pick(#27188): fix(locators): do not escape regular expressions with u or v flag (#27190)

Fixes #27163.
This commit is contained in:
Dmitry Gozman 2023-09-19 15:27:11 -07:00 committed by GitHub
parent 35d8604f8d
commit abf9df39cf
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 10 additions and 0 deletions

View file

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

View file

@ -115,6 +115,10 @@ it('should filter by regex with a single quote', async ({ page }) => {
await expect.soft(page.getByRole('button', { name: /let\\'s let\\\'s/i }).locator('span')).toHaveText('hello');
await expect.soft(page.locator('button', { hasText: /let\\\'s let\\'s/i }).locator('span')).toHaveText('hello');
await expect.soft(page.getByRole('button', { name: /let\\\'s let\\'s/i }).locator('span')).toHaveText('hello');
await page.setContent(`<button>let's hello</button>`);
await expect.soft(page.locator('button', { hasText: /let's/iu })).toHaveText(`let's hello`);
await expect.soft(page.getByRole('button', { name: /let's/iu })).toHaveText(`let's hello`);
});
it('should filter by regex and regexp flags', async ({ page }) => {