Skip to content

Commit

Permalink
fix: ready to init event in iframe with srcdoc (#2618)
Browse files Browse the repository at this point in the history
* fix: ready to init event in iframe with srcdoc

* rewrite client test
  • Loading branch information
wentwrong committed Apr 20, 2021
1 parent ffb66d2 commit a878aef
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 1 deletion.
12 changes: 11 additions & 1 deletion src/client/sandbox/iframe.ts
Expand Up @@ -3,7 +3,13 @@ import SandboxBase from './base';
import settings from '../settings';
import nativeMethods from '../sandbox/native-methods';
import DomProcessor from '../../processing/dom';
import { isShadowUIElement, isIframeWithoutSrc, isIframeElement, isFrameElement } from '../utils/dom';
import {
isShadowUIElement,
isIframeWithoutSrc,
isIframeElement,
isFrameElement,
isIframeWithSrcdoc,
} from '../utils/dom';
import { isFirefox, isWebKit, isIE } from '../utils/browser';
import NodeMutation from './node/mutation';
import CookieSandbox from './cookie';
Expand Down Expand Up @@ -153,6 +159,10 @@ export default class IframeSandbox extends SandboxBase {
if (isShadowUIElement(el))
return;

// NOTE: the ready to init event will be raised by the self removing script
if (isIframeWithSrcdoc(el))
return;

if (isIframeElement(el) && nativeMethods.contentWindowGetter.call(el) ||
isFrameElement(el) && nativeMethods.frameContentWindowGetter.call(el))
this._raiseReadyToInitEvent(el);
Expand Down
32 changes: 32 additions & 0 deletions test/client/fixtures/sandbox/iframe-test.js
Expand Up @@ -145,6 +145,38 @@ if (nativeMethods.iframeSrcdocGetter) {
strictEqual(iframe.getAttribute('srcdoc'), html);
strictEqual(nativeMethods.getAttribute.call(iframe, 'srcdoc'), htmlUtils.processHtml(html, { isPage: true }).replace(/(sessionId)/, '$1!i'));
});

test('ready to init event should be raised after the document was initialized', function () {
var iframeLoadingEventRaised = false;

var handler = function (iframe) {
iframeLoadingEventRaised = true;

strictEqual(iframe.contentDocument.location.href, 'about:srcdoc');
};

iframeSandbox.on(iframeSandbox.RUN_TASK_SCRIPT_EVENT, handler);

var iframe = document.createElement('iframe');

iframe.setAttribute('srcdoc', '<h1>simple markup</h1>');
iframe.id = 'test' + Date.now();

document.body.appendChild(iframe);

return window.QUnitGlobals.wait(function () {
return true;
})
.then(function () {
return window.QUnitGlobals.waitForIframe(iframe);
})
.then(function () {
ok(iframeLoadingEventRaised);

iframeSandbox.off(iframeSandbox.RUN_TASK_SCRIPT_EVENT, handler);
document.body.removeChild(iframe);
});
});
}

test('should not call the contentWindow getter while cloning iframe/frame from XMLDocument (GH-2554)', function () {
Expand Down

0 comments on commit a878aef

Please sign in to comment.