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

4 files changed

Lines changed: 33 additions & 2 deletions

File tree

‎lib/internal/event_target.js‎

Lines changed: 3 additions & 2 deletions
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+
constwebidl=require('internal/webidl');
4546

4647
constkIsEventTarget=SymbolFor('nodejs.event_target');
4748
constkIsNodeEventTarget=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
constcapture=options?.capture===true;
669670

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

‎lib/internal/webidl.js‎

Lines changed: 17 additions & 0 deletions
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+
constconverters={__proto__: null};
24+
2225
// https://webidl.spec.whatwg.org/#abstract-opdef-integerpart
2326
constintegerPart=MathTrunc;
2427

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

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

‎test/parallel/test-bootstrap-modules.js‎

Lines changed: 1 addition & 0 deletions
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‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -714,3 +714,15 @@ let asyncTest = Promise.resolve();
714714
name: 'TypeError',
715715
});
716716
}
717+
718+
{
719+
constet=newEventTarget();
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)