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 authored and GitHub committed 2022-10-13 09:58:40 +00:00
1 parent 488474618c
commit 03fb789fb9
3 files changed
+12 -2

No files matched your search

+3 -1
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;
@@ -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',
});
});
}
-1
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)"
]
}