Skip to content

Commit

Permalink
Unsubscription/removing "on" listener must not cause an error on next…
Browse files Browse the repository at this point in the history
… event (#1148)

Co-authored-by: Denys Denysiuk <ddenysiuk@playtika.com>
  • Loading branch information
ddenisyuk and Denys Denysiuk authored Aug 22, 2023
1 parent 1ff9ace commit 36ed6a9
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
15 changes: 15 additions & 0 deletions src/test/htmlelement/globalEventProperties.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import anyTest, { TestInterface } from 'ava';
import { HTMLElement, appendGlobalEventProperties } from '../../worker-thread/dom/HTMLElement';
import { createTestingDocument } from '../DocumentCreation';
import { Document } from '../../worker-thread/dom/Document';
import { Event } from '../../worker-thread/Event';
import { TransferrableKeys } from '../../transfer/TransferrableKeys';

const test = anyTest as TestInterface<{
Expand Down Expand Up @@ -84,3 +85,17 @@ test('appending as many keys as there are TransferrableKeys functions', (t) => {
element.ontouchmove = handler;
t.is(element.ontouchmove, handler);
});

test.serial('unsubscription with `null` value does not cause an error', (t) => {
const { element } = t.context;
const handler = (e: any) => console.log(e);

t.is(element.onclick, null);
element.onclick = handler;
t.is(element.onclick, handler);
element.dispatchEvent(new Event("click", {}));

element.onclick = null;
t.is(element.onclick, null);
element.dispatchEvent(new Event("click", {}));
});
4 changes: 3 additions & 1 deletion src/worker-thread/dom/HTMLElement.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@ export const appendGlobalEventProperties = (keys: Array<string>): void => {
if (stored) {
this.removeEventListener(normalizedKey, stored);
}
this.addEventListener(normalizedKey, value);
if (value instanceof Function) {
this.addEventListener(normalizedKey, value);
}
this[TransferrableKeys.propertyEventHandlers][normalizedKey] = value;
},
});
Expand Down

0 comments on commit 36ed6a9

Please sign in to comment.