fix: text-is() should ignore comments (#33980)

This commit is contained in:
Yury Semikhatsky 2024-12-12 17:49:31 -08:00 committed by GitHub
parent e4413f2089
commit 76d46d478f
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 13 additions and 0 deletions

View File

@ -73,6 +73,8 @@ export function elementText(cache: Map<Element | ShadowRoot, ElementText>, root:
if (child.nodeType === Node.TEXT_NODE) {
value.full += child.nodeValue || '';
currentImmediate += child.nodeValue || '';
} else if (child.nodeType === Node.COMMENT_NODE) {
continue;
} else {
if (currentImmediate)
value.immediate.push(currentImmediate);

View File

@ -183,6 +183,17 @@ it('should work across nodes', async ({ page }) => {
expect(await page.$$eval(`text=/world/`, els => els.length)).toBe(1);
});
it('text-is() should ignore comments', {
annotation: { type: 'issue', description: 'https://github.com/microsoft/playwright/issues/33976' }
}, async ({ page }) => {
await page.setContent(`<div id=me>hel<!-- comment -->lo
<!-- comment -->
world</div>`);
expect(await page.$eval(`:text-is("hello world")`, e => e.id)).toBe('me');
expect(await page.locator('div', { hasText: 'hello world' }).getAttribute('id')).toBe('me');
expect(await page.getByText('hello world', { exact: true }).getAttribute('id')).toBe('me');
});
it('should work with text nodes in quoted mode', async ({ page }) => {
await page.setContent(`<div id=target1>Hello<span id=target2>wo rld </span> Hi again </div>`);
expect(await page.$eval(`text="Hello"`, e => e.id)).toBe('target1');