Skip to content

Commit

Permalink
Fix window.event implementation
Browse files Browse the repository at this point in the history
* Return undefined instead of null by default
* Setting window.event replaces the property

Closes #3132.
  • Loading branch information
domenic committed Mar 13, 2021
1 parent 2d82763 commit ec3649d
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 3 deletions.
5 changes: 4 additions & 1 deletion lib/jsdom/browser/Window.js
Expand Up @@ -441,7 +441,10 @@ function Window(options) {
return customElementRegistry;
},
get event() {
return idlUtils.wrapperForImpl(window._currentEvent);
return window._currentEvent ? idlUtils.wrapperForImpl(window._currentEvent) : undefined;
},
set event(value) {
Object.defineProperty(window, "event", { configurable: true, enumerable: true, writable: true, value });
}
});

Expand Down
3 changes: 1 addition & 2 deletions test/web-platform-tests/to-run.yaml
Expand Up @@ -164,8 +164,7 @@ EventListener-incumbent-global-1.sub.html: [timeout, Multi-globals]
EventListener-incumbent-global-2.sub.html: [timeout, Multi-globals]
EventListener-invoke-legacy.html: [timeout, Animation stuff not implemented]
EventTarget-dispatchEvent.html: [fail, We don't support every event interface yet]
event-global-extra.window.html: [timeout, Not implemented]
event-global.html: [fail, Not implemented]
event-global-extra.window.html: [fail, We're supposed to check the event listener's global (not the Event's global)]
relatedTarget.window.html: [fail, Unknown]
webkit-animation-end-event.html: [fail, WebKit-prefixed events not implemented]
webkit-animation-iteration-event.html: [fail, WebKit-prefixed events not implemented]
Expand Down
@@ -0,0 +1,31 @@
<!DOCTYPE html>
<meta charset="utf-8">
<title>window.event is [Replaceable]</title>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>

<script>
"use strict";

test(() => {
window.event = "foo";

assert_equals(window.event, "foo");
}, "Setting window.event replaces it");

test(() => {
const target = document.createElement("div");
assert_equals(window.event, "foo", "Still set before dispatch");

const clickEvent = new Event("click");
let fired = false;
target.addEventListener("click", () => {
assert_equals(window.event, "foo", "Still set during dispatch");
fired = true;
});

target.dispatchEvent(clickEvent);
assert_equals(window.event, "foo", "still set after dispatch");
assert_true(fired);
}, "Dispatching does not affect window.event after it's been replaced");
</script>

0 comments on commit ec3649d

Please sign in to comment.