Skip to content

Commit

Permalink
events: add null check for the signal of EventTarget.addEventListener
Browse files Browse the repository at this point in the history
To improve Web compatibility, passing null as the signal should throw an error.
Please see https://github.com/web-platform-tests/wpt/blob/master/dom/events/AddEventListenerOptions-signal.any.js
  • Loading branch information
shisama committed May 19, 2022
1 parent 20268c7 commit 33427a4
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 0 deletions.
4 changes: 4 additions & 0 deletions lib/internal/event_target.js
Original file line number Diff line number Diff line change
Expand Up @@ -482,6 +482,10 @@ class EventTarget {
}
type = String(type);

if (signal === null) {
throw new ERR_INVALID_ARG_TYPE('options.signal', 'AbortSignal', signal);
}

if (signal) {
if (signal.aborted) {
return;
Expand Down
5 changes: 5 additions & 0 deletions test/parallel/test-eventtarget-whatwg-signal.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ require('../common');

const {
strictEqual,
throws
} = require('assert');

// Manually ported from: wpt@dom/events/AddEventListenerOptions-signal.any.js
Expand Down Expand Up @@ -157,3 +158,7 @@ const {
}, { once: true });
et.dispatchEvent(new Event('foo'));
}
{
const et = new EventTarget();
throws(() => et.addEventListener('foo', () => {}, {signal: null}));
}

0 comments on commit 33427a4

Please sign in to comment.