Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 1 commit into from
Sep 19, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions packages/playwright-core/src/utils/isomorphic/stringUtils.ts
Original file line number Diff line number Diff line change
@@ -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, '\\>\\>');
}
4 changes: 4 additions & 0 deletions tests/page/locator-query.spec.ts
Original file line number Diff line number Diff line change
@@ -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 }) => {