From aacfe82c85a9aebb1fdf3e38521144be09625dc8 Mon Sep 17 00:00:00 2001 From: t2t2 Date: Sun, 5 Dec 2021 19:45:32 +0200 Subject: [PATCH] fix(user-interaction): handle null listener in addEventListener (#765) Co-authored-by: Valentin Marchaud --- .../src/instrumentation.ts | 15 ++++++++++----- .../test/userInteraction.nozone.test.ts | 8 ++++++++ 2 files changed, 18 insertions(+), 5 deletions(-) diff --git a/plugins/web/opentelemetry-instrumentation-user-interaction/src/instrumentation.ts b/plugins/web/opentelemetry-instrumentation-user-interaction/src/instrumentation.ts index ae30ea092d..8317eef58e 100644 --- a/plugins/web/opentelemetry-instrumentation-user-interaction/src/instrumentation.ts +++ b/plugins/web/opentelemetry-instrumentation-user-interaction/src/instrumentation.ts @@ -259,14 +259,19 @@ export class UserInteractionInstrumentation extends InstrumentationBase */ private _patchAddEventListener() { const plugin = this; - return (original: Function) => { + return (original: EventTarget['addEventListener']) => { return function addEventListenerPatched( this: HTMLElement, - type: any, - listener: any, - useCapture: any + type: string, + listener: EventListenerOrEventListenerObject | null, + useCapture?: boolean | AddEventListenerOptions ) { - const once = useCapture && useCapture.once; + // Forward calls with listener = null + if (!listener) { + return original.call(this, type, listener, useCapture); + } + + const once = typeof useCapture === 'object' && useCapture.once; const patchedListener = function (this: HTMLElement, ...args: any[]) { let parentSpan: api.Span | undefined; const event: Event | undefined = args[0]; diff --git a/plugins/web/opentelemetry-instrumentation-user-interaction/test/userInteraction.nozone.test.ts b/plugins/web/opentelemetry-instrumentation-user-interaction/test/userInteraction.nozone.test.ts index 38c3cd240b..155a913c02 100644 --- a/plugins/web/opentelemetry-instrumentation-user-interaction/test/userInteraction.nozone.test.ts +++ b/plugins/web/opentelemetry-instrumentation-user-interaction/test/userInteraction.nozone.test.ts @@ -546,6 +546,14 @@ describe('UserInteractionInstrumentation', () => { }); }); + it('should handle null event listener argument', () => { + // @ts-expect-error Typescript typings report null listener as error + // while allowed by EventTarget['addEventListener'] and js engines + document.addEventListener('click', null); + // @ts-expect-error see above + document.removeEventListener('click', null); + }); + it('should handle disable', () => { assert.strictEqual( isWrapped(HTMLElement.prototype.addEventListener),