diff --git a/lib/internal/event_target.js b/lib/internal/event_target.js index fa689ef38cee01..61aa31afa4449e 100644 --- a/lib/internal/event_target.js +++ b/lib/internal/event_target.js @@ -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); diff --git a/lib/internal/webidl.js b/lib/internal/webidl.js index 66edd63ca16a65..67c01418f167f7 100644 --- a/lib/internal/webidl.js +++ b/lib/internal/webidl.js @@ -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, }; diff --git a/test/parallel/test-bootstrap-modules.js b/test/parallel/test-bootstrap-modules.js index 3a8cbfd0b69d28..0a671eb95eb6d4 100644 --- a/test/parallel/test-bootstrap-modules.js +++ b/test/parallel/test-bootstrap-modules.js @@ -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', diff --git a/test/parallel/test-eventtarget.js b/test/parallel/test-eventtarget.js index ce372f7d6fc8f8..44638ca7393c7b 100644 --- a/test/parallel/test-eventtarget.js +++ b/test/parallel/test-eventtarget.js @@ -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); +}