fix(selectors): empty text matcher (#9548)

This commit is contained in:
Yury Semikhatsky 2021-10-15 17:00:45 -07:00 committed by GitHub
parent 8397fac178
commit a186278f2e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 16 additions and 0 deletions

View file

@ -472,6 +472,8 @@ export function createLaxTextMatcher(text: string): TextMatcher {
export function createStrictTextMatcher(text: string): TextMatcher {
text = text.trim().replace(/\s+/g, ' ');
return (elementText: ElementText) => {
if (!text && !elementText.immediate.length)
return true;
return elementText.immediate.some(s => s.trim().replace(/\s+/g, ' ') === text);
};
}

View file

@ -129,6 +129,20 @@ it('should work with :text', async ({ page }) => {
expect(error2.message).toContain(`"text" engine expects a single string`);
});
it('should support empty string', async ({ page }) => {
await page.setContent(`<div></div><div>ya</div><div>\nHELLO \n world </div>`);
expect(await page.$eval(`div:text-is("")`, e => e.outerHTML)).toBe('<div></div>');
expect(await page.$$eval(`div:text-is("")`, els => els.length)).toBe(1);
expect(await page.$eval(`div:text("")`, e => e.outerHTML)).toBe('<div></div>');
expect(await page.$$eval(`div:text("")`, els => els.length)).toBe(3);
expect(await page.$eval(`div >> text=""`, e => e.outerHTML)).toBe('<div></div>');
expect(await page.$$eval(`div >> text=""`, els => els.length)).toBe(1);
expect(await page.$eval(`div >> text=/^$/`, e => e.outerHTML)).toBe('<div></div>');
expect(await page.$$eval(`div >> text=/^$/`, els => els.length)).toBe(1);
expect(await page.$eval(`div:text-matches("")`, e => e.outerHTML)).toBe('<div></div>');
expect(await page.$$eval(`div:text-matches("")`, els => els.length)).toBe(3);
});
it('should work across nodes', async ({ page }) => {
await page.setContent(`<div id=target1>Hello<i>,</i> <span id=target2>world</span><b>!</b></div>`);