@@ -203,15 +203,31 @@ class EventTarget {
203
203
[ kRemoveListener ] ( size , type , listener , capture ) { }
204
204
205
205
addEventListener ( type , listener , options = { } ) {
206
- validateListener ( listener ) ;
207
- type = String ( type ) ;
206
+ if ( arguments . length < 2 )
207
+ throw new ERR_MISSING_ARGS ( ' type' , 'listener' ) ;
208
208
209
+ // We validateOptions before the shouldAddListeners check because the spec
210
+ // requires us to hit getters.
209
211
const {
210
212
once,
211
213
capture,
212
214
passive
213
215
} = validateEventListenerOptions ( options ) ;
214
216
217
+ if ( ! shouldAddListener ( listener ) ) {
218
+ // The DOM silently allows passing undefined as a second argument
219
+ // No error code for this since it is a Warning
220
+ // eslint-disable-next-line no-restricted-syntax
221
+ const w = new Error ( `addEventListener called with ${ listener } ` +
222
+ ' which has no effect.' ) ;
223
+ w . name = 'AddEventListenerArgumentTypeWarning' ;
224
+ w . target = this ;
225
+ w . type = type ;
226
+ process . emitWarning ( w ) ;
227
+ return ;
228
+ }
229
+ type = String ( type ) ;
230
+
215
231
let root = this [ kEvents ] . get ( type ) ;
216
232
217
233
if ( root === undefined ) {
@@ -242,9 +258,15 @@ class EventTarget {
242
258
}
243
259
244
260
removeEventListener ( type , listener , options = { } ) {
245
- validateListener ( listener ) ;
261
+ if ( ! shouldAddListener ( listener ) )
262
+ return ;
263
+
246
264
type = String ( type ) ;
247
- const { capture } = validateEventListenerOptions ( options ) ;
265
+ // TODO(@jasnell): If it's determined this cannot be backported
266
+ // to 12.x, then this can be simplified to:
267
+ // const capture = Boolean(options?.capture);
268
+ const capture = options != null && options . capture === true ;
269
+
248
270
const root = this [ kEvents ] . get ( type ) ;
249
271
if ( root === undefined || root . next === undefined )
250
272
return ;
@@ -426,13 +448,17 @@ Object.defineProperties(NodeEventTarget.prototype, {
426
448
427
449
// EventTarget API
428
450
429
- function validateListener ( listener ) {
451
+ function shouldAddListener ( listener ) {
430
452
if ( typeof listener === 'function' ||
431
453
( listener != null &&
432
454
typeof listener === 'object' &&
433
455
typeof listener . handleEvent === 'function' ) ) {
434
- return ;
456
+ return true ;
435
457
}
458
+
459
+ if ( listener == null )
460
+ return false ;
461
+
436
462
throw new ERR_INVALID_ARG_TYPE ( 'listener' , 'EventListener' , listener ) ;
437
463
}
438
464
0 commit comments