Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

lib: use webidl DOMString converter in EventTarget #47514

Merged
merged 4 commits into from
Apr 14, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
18 changes: 18 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,22 @@ function convertToInt(name, value, bitLength, options = kEmptyObject) {
return x;
}

/**
* @see https://webidl.spec.whatwg.org/#es-DOMString
* @param {any} V
KhafraDev marked this conversation as resolved.
Show resolved Hide resolved
*/
converters.DOMString = function DOMString(V) {
// 2.
KhafraDev marked this conversation as resolved.
Show resolved Hide resolved
if (typeof V === 'symbol') {
throw new ERR_INVALID_ARG_VALUE('value', V);
}

// 3.
return String(V);
};

module.exports = {
convertToInt,
evenRound,
converters,
};
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);
}