Skip to content

Commit

Permalink
Fix crash when calling window.close() inside load event listener
Browse files Browse the repository at this point in the history
  • Loading branch information
MattiasBuelens committed Dec 2, 2021
1 parent f9de3fd commit e46f76f
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
3 changes: 3 additions & 0 deletions lib/jsdom/browser/Window.js
Original file line number Diff line number Diff line change
Expand Up @@ -910,6 +910,9 @@ function Window(options) {
} else {
window.document.addEventListener("load", () => {
fireAnEvent("load", window, undefined, {}, true);
if (!window._document) {
return; // window might've been closed already
}

const documentImpl = idlUtils.implForWrapper(window._document);
if (!documentImpl._pageShowingFlag) {
Expand Down
18 changes: 17 additions & 1 deletion test/api/from-outside.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ const path = require("path");
const { spawnSync } = require("child_process");
const { assert } = require("chai");
const { describe, it } = require("mocha-sugar-free");
const { JSDOM } = require("../..");
const { JSDOM, VirtualConsole } = require("../..");
const { delay } = require("../util");

describe("Test cases only possible to test from the outside", () => {
Expand Down Expand Up @@ -52,4 +52,20 @@ describe("Test cases only possible to test from the outside", () => {
const diffInMB = diffInBytes / 1024 / 1024;
assert.isBelow(diffInMB, 5);
});

it("window.close() should work from within a load event listener", async () => {
const errors = [];
const virtualConsole = new VirtualConsole().sendTo(console);
virtualConsole.on("jsdomError", e => {
errors.push(e);
});

const { window } = new JSDOM(``, { virtualConsole });
window.addEventListener("load", () => {
window.close();
});
await delay(0);

assert.isEmpty(errors);
});
});

0 comments on commit e46f76f

Please sign in to comment.