Skip to content

Commit

Permalink
fix(user-interaction): handle null listener in addEventListener (open…
Browse files Browse the repository at this point in the history
…-telemetry#765)

Co-authored-by: Valentin Marchaud <contact@vmarchaud.fr>
  • Loading branch information
t2t2 and vmarchaud authored Dec 5, 2021
1 parent 686c3e7 commit aacfe82
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -259,14 +259,19 @@ export class UserInteractionInstrumentation extends InstrumentationBase<unknown>
*/
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];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down

0 comments on commit aacfe82

Please sign in to comment.