Skip to content

Commit

Permalink
Implement window.event
Browse files Browse the repository at this point in the history
This may also help with _ownerDocument is undefined, as we were passing the wrapper instead of the impl as the target override.
  • Loading branch information
domenic committed Mar 7, 2021
1 parent a88d0bf commit 6c758c9
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 19 deletions.
18 changes: 13 additions & 5 deletions lib/jsdom/browser/Window.js
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,9 @@ function Window(options) {
// HTMLFrameElement implementation.
this._length = 0;

// https://dom.spec.whatwg.org/#window-current-event
this._currentEvent = undefined;

this._pretendToBeVisual = options.pretendToBeVisual;
this._storageQuota = options.storageQuota;

Expand Down Expand Up @@ -322,6 +325,9 @@ function Window(options) {
},
get customElements() {
return customElementRegistry;
},
get event() {
return idlUtils.wrapperForImpl(window._currentEvent);
}
});

Expand Down Expand Up @@ -786,15 +792,17 @@ function Window(options) {
return; // window might've been closed already
}

const documentImpl = idlUtils.implForWrapper(window._document);

if (window.document.readyState === "complete") {
fireAnEvent("load", window, undefined, {}, window.document);
fireAnEvent("load", window, undefined, {}, documentImpl);
} else {
window.document.addEventListener("load", () => {
fireAnEvent("load", window, undefined, {}, window.document);
fireAnEvent("load", window, undefined, {}, documentImpl);

if (!idlUtils.implForWrapper(window._document)._pageShowingFlag) {
idlUtils.implForWrapper(window._document)._pageShowingFlag = true;
fireAnEvent("pageshow", window, PageTransitionEvent, { persisted: false }, window.document);
if (!documentImpl._pageShowingFlag) {
documentImpl._pageShowingFlag = true;
fireAnEvent("pageshow", window, PageTransitionEvent, { persisted: false }, documentImpl);
}
});
}
Expand Down
40 changes: 26 additions & 14 deletions lib/jsdom/living/events/EventTarget-impl.js
Original file line number Diff line number Diff line change
Expand Up @@ -271,11 +271,11 @@ function invokeEventListeners(struct, eventImpl, phase) {
eventImpl.currentTarget = idlUtils.wrapperForImpl(struct.item);

const listeners = struct.item._eventListeners;
innerInvokeEventListeners(eventImpl, listeners, phase, struct);
innerInvokeEventListeners(eventImpl, listeners, phase, struct.itemInShadowTree);
}

// https://dom.spec.whatwg.org/#concept-event-listener-inner-invoke
function innerInvokeEventListeners(eventImpl, listeners, phase) {
function innerInvokeEventListeners(eventImpl, listeners, phase, itemInShadowTree) {
let found = false;

const { type, target } = eventImpl;
Expand Down Expand Up @@ -310,25 +310,33 @@ function innerInvokeEventListeners(eventImpl, listeners, phase) {
listeners[type].splice(listeners[type].indexOf(listener), 1);
}

let window = null;
if (wrapper && wrapper._document) {
// Triggered by Window
window = wrapper;
} else if (target._ownerDocument) {
// Triggered by most webidl2js'ed instances
window = target._ownerDocument._defaultView;
} else if (wrapper._ownerDocument) {
// Currently triggered by some non-webidl2js things
window = wrapper._ownerDocument._defaultView;
}

let currentEvent;
if (window) {
currentEvent = window._currentEvent;
if (!itemInShadowTree) {
window._currentEvent = eventImpl;
}
}

if (passive) {
eventImpl._inPassiveListenerFlag = true;
}

try {
listener.callback.call(eventImpl.currentTarget, eventImpl);
} catch (e) {
let window = null;
if (wrapper && wrapper._document) {
// Triggered by Window
window = wrapper;
} else if (target._ownerDocument) {
// Triggered by most webidl2js'ed instances
window = target._ownerDocument._defaultView;
} else if (wrapper._ownerDocument) {
// Currently triggered by some non-webidl2js things
window = wrapper._ownerDocument._defaultView;
}

if (window) {
reportException(window, e);
}
Expand All @@ -337,6 +345,10 @@ function innerInvokeEventListeners(eventImpl, listeners, phase) {

eventImpl._inPassiveListenerFlag = false;

if (window) {
window._currentEvent = currentEvent;
}

if (eventImpl._stopImmediatePropagationFlag) {
return found;
}
Expand Down

0 comments on commit 6c758c9

Please sign in to comment.