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 targos committed Sep 4, 2021
1 parent 695569f commit c1782ea
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 @@ -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,
});
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 @@ -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');
Expand Down

0 comments on commit c1782ea

Please sign in to comment.