events: add null check for the signal of EventTarget

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: https://github.com/nodejs/node/pull/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>
This commit is contained in:
Masashi Hirano 2022-10-13 18:58:40 +09:00 committed by GitHub
parent 488474618c
commit 03fb789fb9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 12 additions and 2 deletions

View File

@ -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,
@ -575,6 +575,8 @@ class EventTarget {
}
type = String(type);
validateAbortSignal(signal, 'options.signal');
if (signal) {
if (signal.aborted) {
return;

View File

@ -4,6 +4,7 @@ require('../common');
const {
strictEqual,
throws,
} = require('assert');
// Manually ported from: wpt@dom/events/AddEventListenerOptions-signal.any.js
@ -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',
});
});
}

View File

@ -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)"
]
}