From 0c1666a52ae383ffee4009201a46b45ec4da627c 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 cfd3a4922e62ad..70875ec3a142f8 100644 --- a/lib/internal/event_target.js +++ b/lib/internal/event_target.js @@ -115,14 +115,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 72389a589056e4..cbe7eb3b0e8687 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); +}