fix(selector generator): properly escape re used in has-text (#30767)

Fixes #30499.
This commit is contained in:
Dmitry Gozman 2024-05-13 12:40:46 -07:00 committed by GitHub
parent 64b4ac1732
commit 8334191b94
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 13 additions and 2 deletions

View File

@ -325,8 +325,10 @@ function buildTextCandidates(injectedScript: InjectedScript, element: Element, i
const cssToken: SelectorToken = { engine: 'css', selector: cssEscape(element.nodeName.toLowerCase()), score: kCSSTagNameScore };
for (const alternative of alternatives)
candidates.push([cssToken, { engine: 'internal:has-text', selector: escapeForTextSelector(alternative.text, false), score: kTextScore - alternative.scoreBouns }]);
if (text.length <= 80)
candidates.push([cssToken, { engine: 'internal:has-text', selector: '/^' + escapeRegExp(text) + '$/', score: kTextScoreRegex }]);
if (text.length <= 80) {
const re = new RegExp('^' + escapeRegExp(text) + '$');
candidates.push([cssToken, { engine: 'internal:has-text', selector: escapeForTextSelector(re, false), score: kTextScoreRegex }]);
}
}
const ariaRole = getAriaRole(element);

View File

@ -203,6 +203,15 @@ it.describe('selector generator', () => {
expect(await generate(page, 'div div')).toBe(`div >> internal:has-text=/^Hello world$/`);
});
it('should use internal:has-text with regexp with a quote', async ({ page }) => {
await page.setContent(`
<span>Hello'world</span>
<div><div>Hello'<span>world</span></div>extra</div>
<a>Goodbye'<span>world</span></a>
`);
expect(await generate(page, 'div div')).toBe(`div >> internal:has-text=/^Hello\\'world$/`);
});
it('should chain text after parent', async ({ page }) => {
await page.setContent(`
<div>Hello <span>world</span></div>