Skip to content

Commit

Permalink
events: allow the options argument to be null
Browse files Browse the repository at this point in the history
Make `EventTarget.prototype.addEventListener()` accept `null` as a valid
value for the `options` argument.

PR-URL: #39486
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Robert Nagy <ronagy@icloud.com>
  • Loading branch information
lpinca authored and BethGriggs committed Jul 29, 2021
1 parent 487c45f commit 6f2989c
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 0 deletions.
3 changes: 3 additions & 0 deletions lib/internal/event_target.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
});
Expand Down
9 changes: 9 additions & 0 deletions test/parallel/test-eventtarget.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down

0 comments on commit 6f2989c

Please sign in to comment.