diff --git a/packages/utils/src/internals/general.ts b/packages/utils/src/internals/general.ts index 221c84d4b9a9..af748a333fc7 100644 --- a/packages/utils/src/internals/general.ts +++ b/packages/utils/src/internals/general.ts @@ -115,7 +115,12 @@ export function expandShadowRoots(document: Document): string { for (const el of rootElement.querySelectorAll('*')) { if (el.shadowRoot) { replaceShadowDomsWithHtml(el.shadowRoot); - el.innerHTML += getShadowDomHtml(el.shadowRoot) ?? ''; + let content = el.getHTML?.({ serializableShadowRoots: true }).trim(); + + if (!(content?.length > 0)) { + content = getShadowDomHtml(el.shadowRoot) ?? ''; + } + el.innerHTML += content; } } } diff --git a/test/core/playwright_utils.test.ts b/test/core/playwright_utils.test.ts index 67d6b09427e1..936718a68956 100644 --- a/test/core/playwright_utils.test.ts +++ b/test/core/playwright_utils.test.ts @@ -244,6 +244,36 @@ describe('playwrightUtils', () => { } }, 60_000); + describe('shadow root expansion', () => { + let browser: Browser; + beforeAll(async () => { + browser = await launchPlaywright(launchContext); + }); + afterAll(async () => { + await browser.close(); + }); + + test('no expansion with ignoreShadowRoots: true', async () => { + const page = await browser.newPage(); + await page.goto(`${serverAddress}/special/shadow-root`); + const result = await playwrightUtils.parseWithCheerio(page, true); + + const text = result('body').text().trim(); + expect([...text.matchAll(/\[GOOD\]/g)]).toHaveLength(0); + expect([...text.matchAll(/\[BAD\]/g)]).toHaveLength(0); + }); + + test('expansion works', async () => { + const page = await browser.newPage(); + await page.goto(`${serverAddress}/special/shadow-root`); + const result = await playwrightUtils.parseWithCheerio(page); + + const text = result('body').text().trim(); + expect([...text.matchAll(/\[GOOD\]/g)]).toHaveLength(2); + expect([...text.matchAll(/\[BAD\]/g)]).toHaveLength(0); + }); + }); + describe('infiniteScroll()', () => { function isAtBottom() { return window.innerHeight + window.pageYOffset >= document.body.offsetHeight; diff --git a/test/core/puppeteer_utils.test.ts b/test/core/puppeteer_utils.test.ts index 13be599f1256..e46a7239ded2 100644 --- a/test/core/puppeteer_utils.test.ts +++ b/test/core/puppeteer_utils.test.ts @@ -178,6 +178,36 @@ describe('puppeteerUtils', () => { } }); + describe('parseWithCheerio() shadow root expansion works', () => { + let browser: Browser; + beforeAll(async () => { + browser = await launchPuppeteer(launchContext); + }); + afterAll(async () => { + await browser.close(); + }); + + test('no expansion with ignoreShadowRoots: true', async () => { + const page = await browser.newPage(); + await page.goto(`${serverAddress}/special/shadow-root`); + const result = await puppeteerUtils.parseWithCheerio(page, true); + + const text = result('body').text().trim(); + expect([...text.matchAll(/\[GOOD\]/g)]).toHaveLength(0); + expect([...text.matchAll(/\[BAD\]/g)]).toHaveLength(0); + }); + + test('expansion works', async () => { + const page = await browser.newPage(); + await page.goto(`${serverAddress}/special/shadow-root`); + const result = await puppeteerUtils.parseWithCheerio(page); + + const text = result('body').text().trim(); + expect([...text.matchAll(/\[GOOD\]/g)]).toHaveLength(2); + expect([...text.matchAll(/\[BAD\]/g)]).toHaveLength(0); + }); + }); + describe('blockRequests()', () => { let browser: Browser = null; beforeAll(async () => { diff --git a/test/shared/_helper.ts b/test/shared/_helper.ts index a797cb256b5d..c309465b1fb7 100644 --- a/test/shared/_helper.ts +++ b/test/shared/_helper.ts @@ -194,6 +194,31 @@ console.log('Hello world!');
Some content from inside of an iframe.