Skip to content

Commit

Permalink
fix: read existing iframe src
Browse files Browse the repository at this point in the history
  • Loading branch information
adamdbradley committed Feb 1, 2022
1 parent 47db069 commit 90ef609
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 22 deletions.
48 changes: 26 additions & 22 deletions src/lib/web-worker/worker-iframe.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,39 +7,24 @@ import type { Node } from './worker-node';
import { SCRIPT_TYPE } from '../utils';
import { setInstanceStateValue } from './worker-state';
import { StateProp, WorkerMessageType } from '../types';
import type { WorkerInstance } from './worker-instance';

export const HTMLIFrameDescriptorMap: PropertyDescriptorMap & ThisType<Node> = {
contentDocument: {
get() {
return (this as any).contentWindow.document;
return getIframeEnv(this).$document$;
},
},

contentWindow: {
get() {
// the winId of an iframe's contentWindow is the same
// as the instanceId of the containing iframe element
const $winId$ = this[InstanceIdKey];

if (!environments[$winId$]) {
createEnvironment(
{
$winId$,
// iframe contentWindow parent winId is the iframe element's winId
$parentWinId$: this[WinIdKey],
$url$: getter(this, ['src']) || 'about:blank',
},
true
);
}

return environments[$winId$].$window$;
return getIframeEnv(this).$window$;
},
},

src: {
get() {
let src = environments[this[InstanceIdKey]].$location$.href;
let src = getIframeEnv(this).$location$.href;
if (src.startsWith('about')) {
src = '';
}
Expand All @@ -48,8 +33,7 @@ export const HTMLIFrameDescriptorMap: PropertyDescriptorMap & ThisType<Node> = {
set(src: string) {
let xhr = new XMLHttpRequest();
let xhrStatus: number;
let winId = this[InstanceIdKey];
let env = environments[winId];
let env = getIframeEnv(this);

env.$location$.href = src = resolveUrl(getEnv(this), src);
env.$isLoading$ = 1;
Expand All @@ -73,7 +57,7 @@ export const HTMLIFrameDescriptorMap: PropertyDescriptorMap & ThisType<Node> = {
);

sendToMain(true);
webWorkerCtx.$postMessage$([WorkerMessageType.InitializeNextScript, winId]);
webWorkerCtx.$postMessage$([WorkerMessageType.InitializeNextScript, env.$winId$]);
} else {
setInstanceStateValue(this, StateProp.loadErrorStatus, xhrStatus);
env.$isLoading$ = 0;
Expand All @@ -83,3 +67,23 @@ export const HTMLIFrameDescriptorMap: PropertyDescriptorMap & ThisType<Node> = {

...HTMLSrcElementDescriptorMap,
};

const getIframeEnv = (iframe: WorkerInstance) => {
// the winId of an iframe's contentWindow is the same
// as the instanceId of the containing iframe element
const $winId$ = iframe[InstanceIdKey];

if (!environments[$winId$]) {
createEnvironment(
{
$winId$,
// iframe contentWindow parent winId is the iframe element's winId
$parentWinId$: iframe[WinIdKey],
$url$: getter(iframe, ['src']) || 'about:blank',
},
true
);
}

return environments[$winId$];
};
4 changes: 4 additions & 0 deletions tests/platform/iframe/iframe.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,4 +122,8 @@ test('iframe', async ({ page }) => {
await page.waitForSelector('.testGetByTagName');
const testGetByTagName = page.locator('#testGetByTagName');
await expect(testGetByTagName).toHaveText('#document');

await page.waitForSelector('.testGetIframeSrc');
const testGetIframeSrc = page.locator('#testGetIframeSrc');
await expect(testGetIframeSrc).toHaveText('/platform/iframe/content.html');
});
14 changes: 14 additions & 0 deletions tests/platform/iframe/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -799,6 +799,20 @@ <h1>Iframe</h1>
})();
</script>
</li>

<li>
<strong>getElementsById('existing-iframe2').getAttribute('src')</strong>
<code id="testGetIframeSrc"></code>
<iframe id="existing-iframe2" src="content.html"></iframe>
<script type="text/partytown">
(function () {
const elm = document.getElementById('testGetIframeSrc');
const iframe = document.getElementById('existing-iframe2');
elm.textContent = new URL(iframe.getAttribute('src'), location.href).pathname;
elm.className = 'testGetIframeSrc';
})();
</script>
</li>
</ul>

<hr />
Expand Down

1 comment on commit 90ef609

@vercel
Copy link

@vercel vercel bot commented on 90ef609 Feb 1, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.