Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Iframe Javascript URLs #122

Merged
merged 4 commits into from
Mar 27, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/lib/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -525,6 +525,7 @@ export const enum NodeName {
export const enum StateProp {
errorHandlers = 'error',
loadHandlers = 'load',
src = 0,
loadErrorStatus = 1,
cssRules = 2,
innerHTML = 3,
Expand Down
15 changes: 13 additions & 2 deletions src/lib/web-worker/worker-iframe.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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;
Expand Down
5 changes: 5 additions & 0 deletions src/lib/web-worker/worker-node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,11 @@ export const createNodeCstr = (
if (isIFrame) {
// an iframe element's instanceId is also
// the winId of its contentWindow
const src = getInstanceStateValue<string>(newNode, StateProp.src);
if (src && src.startsWith('javascript:')) {
const scriptContent = src.split('javascript:')[1];
runScriptContent(env, instanceId, scriptContent, winId, '');
}
insertIframe(instanceId, newNode);
}
if (isScript) {
Expand Down
4 changes: 4 additions & 0 deletions tests/platform/iframe/iframe.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);');
});
17 changes: 17 additions & 0 deletions tests/platform/iframe/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -854,6 +854,23 @@ <h1>Iframe</h1>
})();
</script>
</li>

<li>
<strong>set javascript: source</strong>
<code id="testSetJavascriptSrc"></code>
<script type="text/partytown">
(function() {
const elm = document.getElementById('testSetJavascriptSrc');

const iframe = document.createElement('iframe');
iframe.src = 'javascript:void(0);';
elm.parentNode.appendChild(iframe);

elm.textContent = iframe.src;
elm.className = 'testSetJavascriptSrc';
})();
</script>
</li>
</ul>

<hr />
Expand Down