Skip to content

Commit

Permalink
events: add null check for the signal of EventTarget
Browse files Browse the repository at this point in the history
This will improve the Web compatibility.
Passing null as the signal should throw an error.
WPT says "Passing null as the signal should throw".
Please see https://github.com/web-platform-tests/wpt/blob/master/dom/events/AddEventListenerOptions-signal.any.js

PR-URL: #43153
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
Reviewed-By: Tobias Nießen <tniessen@tnie.de>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
  • Loading branch information
shisama authored Oct 13, 2022
1 parent 4884746 commit 03fb789
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 2 deletions.
4 changes: 3 additions & 1 deletion lib/internal/event_target.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ const {
ERR_INVALID_THIS,
}
} = require('internal/errors');
const { validateObject, validateString, validateInternalField } = require('internal/validators');
const { validateAbortSignal, validateObject, validateString, validateInternalField } = require('internal/validators');

const {
customInspectSymbol,
Expand Down Expand Up @@ -575,6 +575,8 @@ class EventTarget {
}
type = String(type);

validateAbortSignal(signal, 'options.signal');

if (signal) {
if (signal.aborted) {
return;
Expand Down
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,11 @@ const {
}, { once: true });
et.dispatchEvent(new Event('foo'));
}
{
const et = new EventTarget();
[1, 1n, {}, [], null, true, 'hi', Symbol(), () => {}].forEach((signal) => {
throws(() => et.addEventListener('foo', () => {}, { signal }), {
name: 'TypeError',
});
});
}
1 change: 0 additions & 1 deletion test/wpt/status/dom/events.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
"AddEventListenerOptions-signal.any.js": {
"fail": {
"expected": [
"Passing null as the signal should throw",
"Passing null as the signal should throw (listener is also null)"
]
}
Expand Down

0 comments on commit 03fb789

Please sign in to comment.