Skip to content

Commit e8bc03b

Browse files
KhafraDevtargos
authored andcommitted
lib: use webidl DOMString converter in EventTarget
PR-URL: #47514 Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com> Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
1 parent af227b1 commit e8bc03b

File tree

4 files changed

+33
-2
lines changed

4 files changed

+33
-2
lines changed

lib/internal/event_target.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ const {
4242
kEnumerableProperty,
4343
} = require('internal/util');
4444
const { inspect } = require('util');
45+
const webidl = require('internal/webidl');
4546

4647
const kIsEventTarget = SymbolFor('nodejs.event_target');
4748
const kIsNodeEventTarget = Symbol('kIsNodeEventTarget');
@@ -598,7 +599,7 @@ class EventTarget {
598599
process.emitWarning(w);
599600
return;
600601
}
601-
type = String(type);
602+
type = webidl.converters.DOMString(type);
602603

603604
if (signal) {
604605
if (signal.aborted) {
@@ -664,7 +665,7 @@ class EventTarget {
664665
if (!validateEventListener(listener))
665666
return;
666667

667-
type = String(type);
668+
type = webidl.converters.DOMString(type);
668669
const capture = options?.capture === true;
669670

670671
const root = this[kEvents].get(type);

lib/internal/webidl.js

+17
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ const {
1010
NumberIsNaN,
1111
NumberMAX_SAFE_INTEGER,
1212
NumberMIN_SAFE_INTEGER,
13+
String,
1314
} = primordials;
1415

1516
const {
@@ -19,6 +20,8 @@ const {
1920
} = require('internal/errors');
2021
const { kEmptyObject } = require('internal/util');
2122

23+
const converters = { __proto__: null };
24+
2225
// https://webidl.spec.whatwg.org/#abstract-opdef-integerpart
2326
const integerPart = MathTrunc;
2427

@@ -157,7 +160,21 @@ function convertToInt(name, value, bitLength, options = kEmptyObject) {
157160
return x;
158161
}
159162

163+
/**
164+
* @see https://webidl.spec.whatwg.org/#es-DOMString
165+
* @param {any} V
166+
* @returns {string}
167+
*/
168+
converters.DOMString = function DOMString(V) {
169+
if (typeof V === 'symbol') {
170+
throw new ERR_INVALID_ARG_VALUE('value', V);
171+
}
172+
173+
return String(V);
174+
};
175+
160176
module.exports = {
161177
convertToInt,
162178
evenRound,
179+
converters,
163180
};

test/parallel/test-bootstrap-modules.js

+1
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ const expectedModules = new Set([
6060
'Internal Binding blob',
6161
'NativeModule internal/url',
6262
'NativeModule util',
63+
'NativeModule internal/webidl',
6364
'Internal Binding performance',
6465
'Internal Binding permission',
6566
'NativeModule internal/perf/utils',

test/parallel/test-eventtarget.js

+12
Original file line numberDiff line numberDiff line change
@@ -714,3 +714,15 @@ let asyncTest = Promise.resolve();
714714
name: 'TypeError',
715715
});
716716
}
717+
718+
{
719+
const et = new EventTarget();
720+
721+
throws(() => {
722+
et.addEventListener(Symbol('symbol'), () => {});
723+
}, TypeError);
724+
725+
throws(() => {
726+
et.removeEventListener(Symbol('symbol'), () => {});
727+
}, TypeError);
728+
}

0 commit comments

Comments
 (0)