From a9081b53916afe5d52a5ff420087336004c46809 Mon Sep 17 00:00:00 2001 From: Khafra Date: Thu, 29 Aug 2024 13:56:54 -0400 Subject: [PATCH] events: allow null/undefined eventInitDict PR-URL: https://github.com/nodejs/node/pull/54643 Reviewed-By: Yagiz Nizipli Reviewed-By: LiviaMedeiros Reviewed-By: Minwoo Jung Reviewed-By: James M Snell --- lib/internal/event_target.js | 12 ++++++------ test/parallel/test-eventtarget.js | 6 ++++++ 2 files changed, 12 insertions(+), 6 deletions(-) diff --git a/lib/internal/event_target.js b/lib/internal/event_target.js index e51c9008524..aa0203b930f 100644 --- a/lib/internal/event_target.js +++ b/lib/internal/event_target.js @@ -111,14 +111,14 @@ class Event { * composed?: boolean, * }} [options] */ - constructor(type, options = kEmptyObject) { + constructor(type, options = undefined) { if (arguments.length === 0) throw new ERR_MISSING_ARGS('type'); - validateObject(options, 'options'); - const { bubbles, cancelable, composed } = options; - this.#cancelable = !!cancelable; - this.#bubbles = !!bubbles; - this.#composed = !!composed; + if (options != null) + validateObject(options, 'options'); + this.#bubbles = !!options?.bubbles; + this.#cancelable = !!options?.cancelable; + this.#composed = !!options?.composed; this[kType] = `${type}`; if (options?.[kTrustEvent]) { diff --git a/test/parallel/test-eventtarget.js b/test/parallel/test-eventtarget.js index 72389a58905..cbe7eb3b0e8 100644 --- a/test/parallel/test-eventtarget.js +++ b/test/parallel/test-eventtarget.js @@ -747,3 +747,9 @@ let asyncTest = Promise.resolve(); event.cancelBubble = true; strictEqual(event.cancelBubble, true); } + +{ + // A null eventInitDict should not throw an error. + new Event('', null); + new Event('', undefined); +}