From b0103566c980b35fbc5c0b10f253eb8442f08062 Mon Sep 17 00:00:00 2001 From: Pavel Feldman Date: Sun, 3 Apr 2022 17:47:12 -0800 Subject: [PATCH] fix(addInitScript): tolerate trailing comments (#13275) --- packages/playwright-core/src/client/clientHelper.ts | 2 +- packages/playwright-core/src/server/webkit/wkPage.ts | 2 +- tests/page/page-add-init-script.spec.ts | 7 +++++++ 3 files changed, 9 insertions(+), 2 deletions(-) diff --git a/packages/playwright-core/src/client/clientHelper.ts b/packages/playwright-core/src/client/clientHelper.ts index c4e3847d03..62959f5fd7 100644 --- a/packages/playwright-core/src/client/clientHelper.ts +++ b/packages/playwright-core/src/client/clientHelper.ts @@ -43,7 +43,7 @@ export async function evaluationScript(fun: Function | string | { path?: string, if (fun.path !== undefined) { let source = await fs.promises.readFile(fun.path, 'utf8'); if (addSourceUrl) - source += '//# sourceURL=' + fun.path.replace(/\n/g, ''); + source += '\n//# sourceURL=' + fun.path.replace(/\n/g, ''); return source; } throw new Error('Either path or content property must be present'); diff --git a/packages/playwright-core/src/server/webkit/wkPage.ts b/packages/playwright-core/src/server/webkit/wkPage.ts index 8044beb50e..4fd0bf55a6 100644 --- a/packages/playwright-core/src/server/webkit/wkPage.ts +++ b/packages/playwright-core/src/server/webkit/wkPage.ts @@ -777,7 +777,7 @@ export class WKPage implements PageDelegate { scripts.push(this._bindingToScript(binding)); scripts.push(...this._browserContext.initScripts); scripts.push(...this._page.initScripts); - return scripts.join(';'); + return scripts.join(';\n'); } async _updateBootstrapScript(): Promise { diff --git a/tests/page/page-add-init-script.spec.ts b/tests/page/page-add-init-script.spec.ts index 0477e95c61..f0a61e52c8 100644 --- a/tests/page/page-add-init-script.spec.ts +++ b/tests/page/page-add-init-script.spec.ts @@ -43,6 +43,13 @@ it('should throw without path and content', async ({ page }) => { expect(error.message).toContain('Either path or content property must be present'); }); +it('should work with trailing comments', async ({ page, asset }) => { + await page.addInitScript({ content: '// comment' }); + await page.addInitScript({ content: 'window.secret = 42;' }); + await page.goto('data:text/html,'); + expect(await page.evaluate('secret')).toBe(42); +}); + it('should support multiple scripts', async ({ page, server }) => { await page.addInitScript(function() { window['script1'] = 1;