From 82a9bbd4243f7fb9b28c049ac41c9a0def7b5212 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. --- 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 825e1e8b2597ab..a64360df3caaa9 100644 --- a/lib/internal/event_target.js +++ b/lib/internal/event_target.js @@ -598,6 +598,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 a85e2cd7f1b9f9..ffdd21a28f5743 100644 --- a/test/parallel/test-eventtarget.js +++ b/test/parallel/test-eventtarget.js @@ -177,6 +177,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, origin) => { strictEqual(err.message, 'boom');