From 0ae2ecee9ad155167f71002923c2f28a02175b67 Mon Sep 17 00:00:00 2001 From: Miguel Montalvo Date: Sun, 27 Mar 2022 04:07:12 -0700 Subject: [PATCH] fix: Iframe javascript URLs (#122) Co-authored-by: Miguel Montalvo --- src/lib/types.ts | 1 + src/lib/web-worker/worker-iframe.ts | 15 +++++++++++++-- src/lib/web-worker/worker-node.ts | 5 +++++ tests/platform/iframe/iframe.spec.ts | 4 ++++ tests/platform/iframe/index.html | 17 +++++++++++++++++ 5 files changed, 40 insertions(+), 2 deletions(-) diff --git a/src/lib/types.ts b/src/lib/types.ts index 91d8fc23..c2512b12 100644 --- a/src/lib/types.ts +++ b/src/lib/types.ts @@ -529,6 +529,7 @@ export const enum NodeName { export const enum StateProp { errorHandlers = 'error', loadHandlers = 'load', + src = 0, loadErrorStatus = 1, cssRules = 2, innerHTML = 3, diff --git a/src/lib/web-worker/worker-iframe.ts b/src/lib/web-worker/worker-iframe.ts index a44e8f7c..3d98f7b4 100644 --- a/src/lib/web-worker/worker-iframe.ts +++ b/src/lib/web-worker/worker-iframe.ts @@ -10,7 +10,7 @@ import { import { getPartytownScript, resolveUrl } from './worker-exec'; import { getter, sendToMain, setter } from './worker-proxy'; import { HTMLSrcElementDescriptorMap } from './worker-src-element'; -import { setInstanceStateValue } from './worker-state'; +import { setInstanceStateValue, getInstanceStateValue } from './worker-state'; import { StateProp, WebWorkerEnvironment, @@ -35,10 +35,21 @@ export const patchHTMLIFrameElement = (WorkerHTMLIFrameElement: any, env: WebWor src: { get() { - let src = getIframeEnv(this).$location$.href; + let src = getInstanceStateValue(this, StateProp.src); + if (src && src.startsWith('javascript:')) { + return src; + } + src = getIframeEnv(this).$location$.href; return src.startsWith('about:') ? '' : src; }, set(src: string) { + if (!src) { + return; + } + if (src.startsWith('javascript:')) { + setInstanceStateValue(this, StateProp.src, src); + return; + } if (!src.startsWith('about:')) { let xhr = new XMLHttpRequest(); let xhrStatus: number; diff --git a/src/lib/web-worker/worker-node.ts b/src/lib/web-worker/worker-node.ts index 405b5520..0bf78bd3 100644 --- a/src/lib/web-worker/worker-node.ts +++ b/src/lib/web-worker/worker-node.ts @@ -69,6 +69,11 @@ export const createNodeCstr = ( if (isIFrame) { // an iframe element's instanceId is also // the winId of its contentWindow + const src = getInstanceStateValue(newNode, StateProp.src); + if (src && src.startsWith('javascript:')) { + const scriptContent = src.split('javascript:')[1]; + runScriptContent(env, instanceId, scriptContent, winId, ''); + } insertIframe(instanceId, newNode); } if (isScript) { diff --git a/tests/platform/iframe/iframe.spec.ts b/tests/platform/iframe/iframe.spec.ts index 33faf3c9..18b5606a 100644 --- a/tests/platform/iframe/iframe.spec.ts +++ b/tests/platform/iframe/iframe.spec.ts @@ -135,4 +135,8 @@ test('iframe', async ({ page }) => { await page.waitForSelector('.testWindowFrames'); const testWindowFrames = page.locator('#testWindowFrames'); await expect(testWindowFrames).toHaveText('window-frames'); + + await page.waitForSelector('.testSetJavascriptSrc'); + const testSetJavascriptSrc = page.locator('#testSetJavascriptSrc'); + await expect(testSetJavascriptSrc).toHaveText('javascript:void(0);'); }); diff --git a/tests/platform/iframe/index.html b/tests/platform/iframe/index.html index 126cfc48..8d4cede2 100644 --- a/tests/platform/iframe/index.html +++ b/tests/platform/iframe/index.html @@ -854,6 +854,23 @@

Iframe

})(); + +
  • + set javascript: source + + +