Skip to content

Commit

Permalink
fix `The document.open function throws an error because document.defa…
Browse files Browse the repository at this point in the history
…ultView returns null` (close DevExpress#1272) (DevExpress#1279)

* fix `The document.open function throws an error because document.defaultView returns null` (close DevExpress#1272)

* test refactoring

* fix review's issues

* fix review's issues
  • Loading branch information
LavrovArtem authored and AndreyBelym committed Feb 28, 2019
1 parent 4afb704 commit 1d6caba
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 16 deletions.
11 changes: 5 additions & 6 deletions src/client/sandbox/node/document/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,10 @@ export default class DocumentSandbox extends SandboxBase {
this.documentWriter = null;
}

_isUninitializedIframeWithoutSrc (doc) {
const wnd = doc.defaultView;
const frameElement = getFrameElement(wnd);
_isUninitializedIframeWithoutSrc (win) {
const frameElement = getFrameElement(win);

return wnd !== wnd.top && frameElement && isIframeWithoutSrc(frameElement) &&
return win !== win.top && frameElement && isIframeWithoutSrc(frameElement) &&
!IframeSandbox.isIframeInitialized(frameElement);
}

Expand Down Expand Up @@ -69,7 +68,7 @@ export default class DocumentSandbox extends SandboxBase {
const documentSandbox = this;

document.open = (...args) => {
const isUninitializedIframe = this._isUninitializedIframeWithoutSrc(document);
const isUninitializedIframe = this._isUninitializedIframeWithoutSrc(window);

if (!isUninitializedIframe)
this._beforeDocumentCleaned();
Expand Down Expand Up @@ -99,7 +98,7 @@ export default class DocumentSandbox extends SandboxBase {

const result = nativeMethods.documentClose.apply(document, args);

if (!this._isUninitializedIframeWithoutSrc(document))
if (!this._isUninitializedIframeWithoutSrc(window))
this._onDocumentClosed();

return result;
Expand Down
55 changes: 45 additions & 10 deletions test/client/fixtures/sandbox/node/document-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ var SHADOW_UI_CLASSNAME = hammerhead.get('../shadow-ui/class-name');
var browserUtils = hammerhead.utils.browser;
var nativeMethods = hammerhead.nativeMethods;
var iframeSandbox = hammerhead.sandbox.iframe;
var Promise = hammerhead.Promise;

QUnit.testStart(function () {
// NOTE: The 'window.open' method used in QUnit.
Expand Down Expand Up @@ -494,22 +495,56 @@ if (!browserUtils.isIE) {
}

test('an iframe should not contain self-removing scripts after document.close (GH-871)', function () {
var iframe = document.createElement('iframe');
return createTestIframe()
.then(function (iframe) {
var iframeDocument = iframe.contentDocument;

iframe.id = 'test-gh-871';
iframeDocument.designMode = 'On';
iframeDocument.open();
iframeDocument.write('<body style=\"padding: 0; margin: 0; overflow: hidden;\"></body>');
iframeDocument.close();

document.body.appendChild(iframe);
var selfRemovingScripts = nativeMethods.querySelectorAll.call(iframeDocument,
'.' + SHADOW_UI_CLASSNAME.selfRemovingScript);

strictEqual(selfRemovingScripts.length, 0);
});
});

var iframeDocument = iframe.contentDocument;
test('should not throw an error when document.defualtView is null (GH-1272)', function () {
return new Promise(function (resolve, reject) {
var iframe = document.createElement('iframe');
var loadEventCount = 0;

iframeDocument.designMode = 'on';
iframeDocument.open();
iframeDocument.write('<body style=\"padding: 0; margin: 0; overflow: hidden;\"></body>');
iframeDocument.close();
iframe.id = 'test' + Date.now();
iframe.src = 'javascript:"";';
iframe.onload = function () {
var doc = iframe.contentDocument;

strictEqual(nativeMethods.querySelectorAll.call(document, '.' + SHADOW_UI_CLASSNAME.selfRemovingScript).length, 0);
// NOTE: Without wrapping in 'setTimeout' function the error is not reproduced
setTimeout(function () {
try {
// NOTE: Chrome throw an error after second load
if (loadEventCount++ < 2) {
doc.open();
doc.write('<div>' + loadEventCount + '</div>');
doc.close();
}
else
resolve(iframe);
}
catch (e) {
reject(e);
}
}, 100);
};

iframe.parentNode.removeChild(iframe);
document.body.appendChild(iframe);
})
.then(function (iframe) {
strictEqual(iframe.contentDocument.documentElement.innerText, '2');
document.body.removeChild(iframe);
});
});

test('querySelector should return an element if a selector contains the href attribute with hash as a value (GH-922)', function () {
Expand Down

0 comments on commit 1d6caba

Please sign in to comment.