lib: use webidl DOMString converter in EventTarget

PR-URL: https://github.com/nodejs/node/pull/47514
Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com>
Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
This commit is contained in:
Khafra 2023-04-14 06:12:28 -04:00 committed by GitHub
parent 9decb70d05
commit 41b8872cbd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 33 additions and 2 deletions

View File

@ -42,6 +42,7 @@ const {
kEnumerableProperty,
} = require('internal/util');
const { inspect } = require('util');
const webidl = require('internal/webidl');
const kIsEventTarget = SymbolFor('nodejs.event_target');
const kIsNodeEventTarget = Symbol('kIsNodeEventTarget');
@ -598,7 +599,7 @@ class EventTarget {
process.emitWarning(w);
return;
}
type = String(type);
type = webidl.converters.DOMString(type);
if (signal) {
if (signal.aborted) {
@ -664,7 +665,7 @@ class EventTarget {
if (!validateEventListener(listener))
return;
type = String(type);
type = webidl.converters.DOMString(type);
const capture = options?.capture === true;
const root = this[kEvents].get(type);

View File

@ -10,6 +10,7 @@ const {
NumberIsNaN,
NumberMAX_SAFE_INTEGER,
NumberMIN_SAFE_INTEGER,
String,
} = primordials;
const {
@ -19,6 +20,8 @@ const {
} = require('internal/errors');
const { kEmptyObject } = require('internal/util');
const converters = { __proto__: null };
// https://webidl.spec.whatwg.org/#abstract-opdef-integerpart
const integerPart = MathTrunc;
@ -157,7 +160,21 @@ function convertToInt(name, value, bitLength, options = kEmptyObject) {
return x;
}
/**
* @see https://webidl.spec.whatwg.org/#es-DOMString
* @param {any} V
* @returns {string}
*/
converters.DOMString = function DOMString(V) {
if (typeof V === 'symbol') {
throw new ERR_INVALID_ARG_VALUE('value', V);
}
return String(V);
};
module.exports = {
convertToInt,
evenRound,
converters,
};

View File

@ -60,6 +60,7 @@ const expectedModules = new Set([
'Internal Binding blob',
'NativeModule internal/url',
'NativeModule util',
'NativeModule internal/webidl',
'Internal Binding performance',
'Internal Binding permission',
'NativeModule internal/perf/utils',

View File

@ -714,3 +714,15 @@ let asyncTest = Promise.resolve();
name: 'TypeError',
});
}
{
const et = new EventTarget();
throws(() => {
et.addEventListener(Symbol('symbol'), () => {});
}, TypeError);
throws(() => {
et.removeEventListener(Symbol('symbol'), () => {});
}, TypeError);
}