Skip to content

Commit

Permalink
lib: use webidl DOMString converter in EventTarget
Browse files Browse the repository at this point in the history
PR-URL: #47514
Reviewed-By: Yagiz Nizipli <[email protected]>
Reviewed-By: Benjamin Gruenbaum <[email protected]>
  • Loading branch information
KhafraDev authored and targos committed May 2, 2023
1 parent af227b1 commit e8bc03b
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 2 deletions.
5 changes: 3 additions & 2 deletions lib/internal/event_target.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down Expand Up @@ -598,7 +599,7 @@ class EventTarget {
process.emitWarning(w);
return;
}
type = String(type);
type = webidl.converters.DOMString(type);

if (signal) {
if (signal.aborted) {
Expand Down Expand Up @@ -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);
Expand Down
17 changes: 17 additions & 0 deletions lib/internal/webidl.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ const {
NumberIsNaN,
NumberMAX_SAFE_INTEGER,
NumberMIN_SAFE_INTEGER,
String,
} = primordials;

const {
Expand All @@ -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;

Expand Down Expand Up @@ -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,
};
1 change: 1 addition & 0 deletions test/parallel/test-bootstrap-modules.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
12 changes: 12 additions & 0 deletions test/parallel/test-eventtarget.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

0 comments on commit e8bc03b

Please sign in to comment.