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

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

Merged
merged 4 commits into from
Sep 4, 2017
Merged
Show file tree
Hide file tree
Changes from 3 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
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', function () {
Copy link
Contributor

Choose a reason for hiding this comment

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

test(should not throw an error when document.defualtView is null (GH-1272))

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 setTimeout the error does not reproduced
Copy link
Contributor

Choose a reason for hiding this comment

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

// 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