From c1782ea1f5ead7219acbafe90aaedf076eeda242 Mon Sep 17 00:00:00 2001 From: Luigi Pinca Date: Thu, 22 Jul 2021 15:50:47 +0200 Subject: [PATCH] events: allow the options argument to be null Make `EventTarget.prototype.addEventListener()` accept `null` as a valid value for the `options` argument. PR-URL: https://github.com/nodejs/node/pull/39486 Reviewed-By: James M Snell Reviewed-By: Antoine du Hamel Reviewed-By: Colin Ihrig Reviewed-By: Robert Nagy --- lib/internal/event_target.js | 3 +++ test/parallel/test-eventtarget.js | 9 +++++++++ 2 files changed, 12 insertions(+) diff --git a/lib/internal/event_target.js b/lib/internal/event_target.js index f5bc4844166342..fc814b87c36138 100644 --- a/lib/internal/event_target.js +++ b/lib/internal/event_target.js @@ -541,6 +541,9 @@ function shouldAddListener(listener) { function validateEventListenerOptions(options) { if (typeof options === 'boolean') return { capture: options }; + + if (options === null) + return {}; validateObject(options, 'options', { allowArray: true, allowFunction: true, }); diff --git a/test/parallel/test-eventtarget.js b/test/parallel/test-eventtarget.js index 63e20513a4c9b2..2a013875dc3225 100644 --- a/test/parallel/test-eventtarget.js +++ b/test/parallel/test-eventtarget.js @@ -178,6 +178,15 @@ let asyncTest = Promise.resolve(); eventTarget.dispatchEvent(event); } +{ + // The `options` argument can be `null`. + const eventTarget = new EventTarget(); + const event = new Event('foo'); + const fn = common.mustCall((event) => strictEqual(event.type, 'foo')); + eventTarget.addEventListener('foo', fn, null); + eventTarget.dispatchEvent(event); +} + { const uncaughtException = common.mustCall((err, event) => { strictEqual(err.message, 'boom');