Skip to content

Commit

Permalink
cherry-pick(#27188): fix(locators): do not escape regular expressions…
Browse files Browse the repository at this point in the history
… with u or v flag (#27190)

Fixes #27163.
  • Loading branch information
dgozman authored Sep 19, 2023
1 parent 35d8604 commit abf9df3
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 0 deletions.
6 changes: 6 additions & 0 deletions packages/playwright-core/src/utils/isomorphic/stringUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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, '\\>\\>');
}
Expand Down
4 changes: 4 additions & 0 deletions tests/page/locator-query.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 }) => {
Expand Down

0 comments on commit abf9df3

Please sign in to comment.