Skip to content

Commit

Permalink
fix(text selector): allow single quoted text (#1952)
Browse files Browse the repository at this point in the history
  • Loading branch information
dgozman authored Apr 24, 2020
1 parent e6c2cad commit 5ac7f0e
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 7 deletions.
8 changes: 4 additions & 4 deletions docs/selectors.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ document

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

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

### css and css:light

`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')`.
`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')`.

`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.

Expand Down Expand Up @@ -109,11 +109,11 @@ Note that `xpath` does not pierce shadow roots.
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.

- 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>`.
- 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".
- 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"`.
- 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.
- 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">`.

Malformed selector starting with `"` is assumed to be a text selector. For example, Playwright converts `page.click('"Login"')` to `page.click('text="Login"')`.
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"')`.

`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.

Expand Down
21 changes: 19 additions & 2 deletions src/injected/textSelectorEngine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,27 @@ export function createTextSelector(shadow: boolean): SelectorEngine {
return engine;
}

function unescape(s: string): string {
if (!s.includes('\\'))
return s;
const r: string[] = [];
let i = 0;
while (i < s.length) {
if (s[i] === '\\' && i + 1 < s.length)
i++;
r.push(s[i++]);
}
return r.join('');
}

type Matcher = (text: string) => boolean;
function createMatcher(selector: string): Matcher {
if (selector[0] === '"' && selector[selector.length - 1] === '"') {
const parsed = JSON.parse(selector);
if (selector.length > 1 && selector[0] === '"' && selector[selector.length - 1] === '"') {
const parsed = unescape(selector.substring(1, selector.length - 1));
return text => text === parsed;
}
if (selector.length > 1 && selector[0] === "'" && selector[selector.length - 1] === "'") {
const parsed = unescape(selector.substring(1, selector.length - 1));
return text => text === parsed;
}
if (selector[0] === '/' && selector.lastIndexOf('/') > 0) {
Expand Down
5 changes: 4 additions & 1 deletion src/selectors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,10 @@ export class Selectors {
if (eqIndex !== -1 && part.substring(0, eqIndex).trim().match(/^[a-zA-Z_0-9-+:]+$/)) {
name = part.substring(0, eqIndex).trim();
body = part.substring(eqIndex + 1);
} else if (part.startsWith('"')) {
} else if (part.length > 1 && part[0] === '"' && part[part.length - 1] === '"') {
name = 'text';
body = part;
} else if (part.length > 1 && part[0] === "'" && part[part.length - 1] === "'") {
name = 'text';
body = part;
} else if (/^\(*\/\//.test(part)) {
Expand Down
28 changes: 28 additions & 0 deletions test/queryselector.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -509,9 +509,37 @@ describe('text selector', () => {

await page.setContent(`<div>yo<div>ya</div>hey<div>hey</div></div>`);
expect(await page.$eval(`text=hey`, e => e.outerHTML)).toBe('<div>yo<div>ya</div>hey<div>hey</div></div>');
expect(await page.$eval(`text="yo">>text="ya"`, e => e.outerHTML)).toBe('<div>ya</div>');
expect(await page.$eval(`text='yo'>> text="ya"`, e => e.outerHTML)).toBe('<div>ya</div>');
expect(await page.$eval(`text="yo" >>text='ya'`, e => e.outerHTML)).toBe('<div>ya</div>');
expect(await page.$eval(`text='yo' >> text='ya'`, e => e.outerHTML)).toBe('<div>ya</div>');
expect(await page.$eval(`'yo'>>"ya"`, e => e.outerHTML)).toBe('<div>ya</div>');
expect(await page.$eval(`"yo" >> 'ya'`, e => e.outerHTML)).toBe('<div>ya</div>');

await page.setContent(`<div>yo<span id="s1"></span></div><div>yo<span id="s2"></span><span id="s3"></span></div>`);
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>');

await page.setContent(`<div>'</div><div>"</div><div>\\</div><div>x</div>`);
expect(await page.$eval(`text='\\''`, e => e.outerHTML)).toBe('<div>\'</div>');
expect(await page.$eval(`text='"'`, e => e.outerHTML)).toBe('<div>"</div>');
expect(await page.$eval(`text="\\""`, e => e.outerHTML)).toBe('<div>"</div>');
expect(await page.$eval(`text="'"`, e => e.outerHTML)).toBe('<div>\'</div>');
expect(await page.$eval(`text="\\x"`, e => e.outerHTML)).toBe('<div>x</div>');
expect(await page.$eval(`text='\\x'`, e => e.outerHTML)).toBe('<div>x</div>');
expect(await page.$eval(`text='\\\\'`, e => e.outerHTML)).toBe('<div>\\</div>');
expect(await page.$eval(`text="\\\\"`, e => e.outerHTML)).toBe('<div>\\</div>');
expect(await page.$eval(`text="`, e => e.outerHTML)).toBe('<div>"</div>');
expect(await page.$eval(`text='`, e => e.outerHTML)).toBe('<div>\'</div>');
expect(await page.$eval(`"x"`, e => e.outerHTML)).toBe('<div>x</div>');
expect(await page.$eval(`'x'`, e => e.outerHTML)).toBe('<div>x</div>');
let error = await page.$(`"`).catch(e => e);
expect(error.message).toContain(WEBKIT ? 'SyntaxError' : 'querySelector');
error = await page.$(`'`).catch(e => e);
expect(error.message).toContain(WEBKIT ? 'SyntaxError' : 'querySelector');

await page.setContent(`<div> ' </div><div> " </div>`);
expect(await page.$eval(`text="`, e => e.outerHTML)).toBe('<div> " </div>');
expect(await page.$eval(`text='`, e => e.outerHTML)).toBe('<div> \' </div>');
});

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

0 comments on commit 5ac7f0e

Please sign in to comment.