Skip to content

Commit 5ac7f0e

Browse files
authored
fix(text selector): allow single quoted text (microsoft#1952)
1 parent e6c2cad commit 5ac7f0e

File tree

4 files changed

+55
-7
lines changed

4 files changed

+55
-7
lines changed

docs/selectors.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ document
2424

2525
For convenience, selectors in the wrong format are heuristically converted to the right format:
2626
- Selector starting with `//` is assumed to be `xpath=selector`. Example: `page.click('//html')` is converted to `page.click('xpath=//html')`.
27-
- Selector starting with `"` is assumed to be `text=selector`. Example: `page.click('"foo"')` is converted to `page.click('text="foo"')`.
27+
- Selector surrounded with quotes (either `"` or `'`) is assumed to be `text=selector`. Example: `page.click('"foo"')` is converted to `page.click('text="foo"')`.
2828
- Otherwise, selector is assumed to be `css=selector`. Example: `page.click('div')` is converted to `page.click('css=div')`.
2929

3030
## Examples
@@ -59,7 +59,7 @@ const handle = await divHandle.$('css=span');
5959

6060
### css and css:light
6161

62-
`css` is a default engine - any malformed selector not starting with `//` nor with `"` is assumed to be a css selector. For example, Playwright converts `page.$('span > button')` to `page.$('css=span > button')`.
62+
`css` is a default engine - any malformed selector not starting with `//` nor surrounded with quotes is assumed to be a css selector. For example, Playwright converts `page.$('span > button')` to `page.$('css=span > button')`.
6363

6464
`css:light` engine is equivalent to [`Document.querySelector`](https://developer.mozilla.org/en/docs/Web/API/Document/querySelector) and behaves according to the CSS spec. However, it does not pierce shadow roots, which may be inconvenient when working with [Shadow DOM and Web Components](https://developer.mozilla.org/en-US/docs/Web/Web_Components/Using_shadow_DOM). For that reason, `css` engine pierces shadow roots. More specifically, every [Descendant combinator](https://developer.mozilla.org/en-US/docs/Web/CSS/Descendant_combinator) pierces an arbitrary number of open shadow roots, including the implicit descendant combinator at the start of the selector.
6565

@@ -109,11 +109,11 @@ Note that `xpath` does not pierce shadow roots.
109109
Text engine finds an element that contains a text node with the passed text. For example, `page.click('text=Login')` clicks on a login button, and `page.waitForSelector('"lazy loaded text")` waits for the `"lazy loaded text"` to appear in the page.
110110

111111
- By default, the match is case-insensitive, ignores leading/trailing whitespace and searches for a substring. This means `text= Login` matches `<button>Button loGIN (click me)</button>`.
112-
- Text body can be escaped with double quotes for precise matching, insisting on exact match, including specified whitespace and case. This means `text="Login "` will only match `<button>Login </button>` with exactly one space after "Login".
112+
- Text body can be escaped with single or double quotes for precise matching, insisting on exact match, including specified whitespace and case. This means `text="Login "` will only match `<button>Login </button>` with exactly one space after "Login". Quoted text follows the usual escaping rules, e.g. use `\"` to escape double quote in a double-quoted string: `text="foo\"bar"`.
113113
- Text body can also be a JavaScript-like regex wrapped in `/` symbols. This means `text=/^\\s*Login$/i` will match `<button> loGIN</button>` with any number of spaces before "Login" and no spaces after.
114114
- Input elements of the type `button` and `submit` are rendered with their value as text, and text engine finds them. For example, `text=Login` matches `<input type=button value="Login">`.
115115

116-
Malformed selector starting with `"` is assumed to be a text selector. For example, Playwright converts `page.click('"Login"')` to `page.click('text="Login"')`.
116+
Malformed selector surrounded with quotes (either `"` or `'`) is assumed to be a text selector. For example, Playwright converts `page.click('"Login"')` to `page.click('text="Login"')`.
117117

118118
`text` engine open pierces shadow roots similarly to `css`, while `text:light` does not. Text engine first searches for elements in the light dom in the iteration order, and then recursively inside open shadow roots in the iteration order. It does not search inside closed shadow roots or iframes.
119119

src/injected/textSelectorEngine.ts

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,10 +48,27 @@ export function createTextSelector(shadow: boolean): SelectorEngine {
4848
return engine;
4949
}
5050

51+
function unescape(s: string): string {
52+
if (!s.includes('\\'))
53+
return s;
54+
const r: string[] = [];
55+
let i = 0;
56+
while (i < s.length) {
57+
if (s[i] === '\\' && i + 1 < s.length)
58+
i++;
59+
r.push(s[i++]);
60+
}
61+
return r.join('');
62+
}
63+
5164
type Matcher = (text: string) => boolean;
5265
function createMatcher(selector: string): Matcher {
53-
if (selector[0] === '"' && selector[selector.length - 1] === '"') {
54-
const parsed = JSON.parse(selector);
66+
if (selector.length > 1 && selector[0] === '"' && selector[selector.length - 1] === '"') {
67+
const parsed = unescape(selector.substring(1, selector.length - 1));
68+
return text => text === parsed;
69+
}
70+
if (selector.length > 1 && selector[0] === "'" && selector[selector.length - 1] === "'") {
71+
const parsed = unescape(selector.substring(1, selector.length - 1));
5572
return text => text === parsed;
5673
}
5774
if (selector[0] === '/' && selector.lastIndexOf('/') > 0) {

src/selectors.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,10 @@ export class Selectors {
197197
if (eqIndex !== -1 && part.substring(0, eqIndex).trim().match(/^[a-zA-Z_0-9-+:]+$/)) {
198198
name = part.substring(0, eqIndex).trim();
199199
body = part.substring(eqIndex + 1);
200-
} else if (part.startsWith('"')) {
200+
} else if (part.length > 1 && part[0] === '"' && part[part.length - 1] === '"') {
201+
name = 'text';
202+
body = part;
203+
} else if (part.length > 1 && part[0] === "'" && part[part.length - 1] === "'") {
201204
name = 'text';
202205
body = part;
203206
} else if (/^\(*\/\//.test(part)) {

test/queryselector.spec.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -509,9 +509,37 @@ describe('text selector', () => {
509509

510510
await page.setContent(`<div>yo<div>ya</div>hey<div>hey</div></div>`);
511511
expect(await page.$eval(`text=hey`, e => e.outerHTML)).toBe('<div>yo<div>ya</div>hey<div>hey</div></div>');
512+
expect(await page.$eval(`text="yo">>text="ya"`, e => e.outerHTML)).toBe('<div>ya</div>');
513+
expect(await page.$eval(`text='yo'>> text="ya"`, e => e.outerHTML)).toBe('<div>ya</div>');
514+
expect(await page.$eval(`text="yo" >>text='ya'`, e => e.outerHTML)).toBe('<div>ya</div>');
515+
expect(await page.$eval(`text='yo' >> text='ya'`, e => e.outerHTML)).toBe('<div>ya</div>');
516+
expect(await page.$eval(`'yo'>>"ya"`, e => e.outerHTML)).toBe('<div>ya</div>');
517+
expect(await page.$eval(`"yo" >> 'ya'`, e => e.outerHTML)).toBe('<div>ya</div>');
512518

513519
await page.setContent(`<div>yo<span id="s1"></span></div><div>yo<span id="s2"></span><span id="s3"></span></div>`);
514520
expect(await page.$$eval(`text=yo`, es => es.map(e => e.outerHTML).join('\n'))).toBe('<div>yo<span id="s1"></span></div>\n<div>yo<span id="s2"></span><span id="s3"></span></div>');
521+
522+
await page.setContent(`<div>'</div><div>"</div><div>\\</div><div>x</div>`);
523+
expect(await page.$eval(`text='\\''`, e => e.outerHTML)).toBe('<div>\'</div>');
524+
expect(await page.$eval(`text='"'`, e => e.outerHTML)).toBe('<div>"</div>');
525+
expect(await page.$eval(`text="\\""`, e => e.outerHTML)).toBe('<div>"</div>');
526+
expect(await page.$eval(`text="'"`, e => e.outerHTML)).toBe('<div>\'</div>');
527+
expect(await page.$eval(`text="\\x"`, e => e.outerHTML)).toBe('<div>x</div>');
528+
expect(await page.$eval(`text='\\x'`, e => e.outerHTML)).toBe('<div>x</div>');
529+
expect(await page.$eval(`text='\\\\'`, e => e.outerHTML)).toBe('<div>\\</div>');
530+
expect(await page.$eval(`text="\\\\"`, e => e.outerHTML)).toBe('<div>\\</div>');
531+
expect(await page.$eval(`text="`, e => e.outerHTML)).toBe('<div>"</div>');
532+
expect(await page.$eval(`text='`, e => e.outerHTML)).toBe('<div>\'</div>');
533+
expect(await page.$eval(`"x"`, e => e.outerHTML)).toBe('<div>x</div>');
534+
expect(await page.$eval(`'x'`, e => e.outerHTML)).toBe('<div>x</div>');
535+
let error = await page.$(`"`).catch(e => e);
536+
expect(error.message).toContain(WEBKIT ? 'SyntaxError' : 'querySelector');
537+
error = await page.$(`'`).catch(e => e);
538+
expect(error.message).toContain(WEBKIT ? 'SyntaxError' : 'querySelector');
539+
540+
await page.setContent(`<div> ' </div><div> " </div>`);
541+
expect(await page.$eval(`text="`, e => e.outerHTML)).toBe('<div> " </div>');
542+
expect(await page.$eval(`text='`, e => e.outerHTML)).toBe('<div> \' </div>');
515543
});
516544

517545
it('create', async ({page}) => {

0 commit comments

Comments
 (0)