@@ -203,15 +203,31 @@ class EventTarget {
203203 [ kRemoveListener ] ( size , type , listener , capture ) { }
204204
205205 addEventListener ( type , listener , options = { } ) {
206- validateListener ( listener ) ;
207- type = String ( type ) ;
206+ if ( arguments . length < 2 )
207+ throw new ERR_MISSING_ARGS ( ' type' , 'listener' ) ;
208208
209+ // We validateOptions before the shouldAddListeners check because the spec
210+ // requires us to hit getters.
209211 const {
210212 once,
211213 capture,
212214 passive
213215 } = validateEventListenerOptions ( options ) ;
214216
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+
215231 let root = this [ kEvents ] . get ( type ) ;
216232
217233 if ( root === undefined ) {
@@ -242,9 +258,15 @@ class EventTarget {
242258 }
243259
244260 removeEventListener ( type , listener , options = { } ) {
245- validateListener ( listener ) ;
261+ if ( ! shouldAddListener ( listener ) )
262+ return ;
263+
246264 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+
248270 const root = this [ kEvents ] . get ( type ) ;
249271 if ( root === undefined || root . next === undefined )
250272 return ;
@@ -426,13 +448,17 @@ Object.defineProperties(NodeEventTarget.prototype, {
426448
427449// EventTarget API
428450
429- function validateListener ( listener ) {
451+ function shouldAddListener ( listener ) {
430452 if ( typeof listener === 'function' ||
431453 ( listener != null &&
432454 typeof listener === 'object' &&
433455 typeof listener . handleEvent === 'function' ) ) {
434- return ;
456+ return true ;
435457 }
458+
459+ if ( listener == null )
460+ return false ;
461+
436462 throw new ERR_INVALID_ARG_TYPE ( 'listener' , 'EventListener' , listener ) ;
437463}
438464
0 commit comments