Skip to content

Commit 36ed6a9

Browse files
ddenisyukDenys Denysiuk
and
Denys Denysiuk
authored
Unsubscription/removing "on" listener must not cause an error on next event (#1148)
Co-authored-by: Denys Denysiuk <[email protected]>
1 parent 1ff9ace commit 36ed6a9

File tree

2 files changed

+18
-1
lines changed

2 files changed

+18
-1
lines changed

src/test/htmlelement/globalEventProperties.test.ts

+15
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import anyTest, { TestInterface } from 'ava';
22
import { HTMLElement, appendGlobalEventProperties } from '../../worker-thread/dom/HTMLElement';
33
import { createTestingDocument } from '../DocumentCreation';
44
import { Document } from '../../worker-thread/dom/Document';
5+
import { Event } from '../../worker-thread/Event';
56
import { TransferrableKeys } from '../../transfer/TransferrableKeys';
67

78
const test = anyTest as TestInterface<{
@@ -84,3 +85,17 @@ test('appending as many keys as there are TransferrableKeys functions', (t) => {
8485
element.ontouchmove = handler;
8586
t.is(element.ontouchmove, handler);
8687
});
88+
89+
test.serial('unsubscription with `null` value does not cause an error', (t) => {
90+
const { element } = t.context;
91+
const handler = (e: any) => console.log(e);
92+
93+
t.is(element.onclick, null);
94+
element.onclick = handler;
95+
t.is(element.onclick, handler);
96+
element.dispatchEvent(new Event("click", {}));
97+
98+
element.onclick = null;
99+
t.is(element.onclick, null);
100+
element.dispatchEvent(new Event("click", {}));
101+
});

src/worker-thread/dom/HTMLElement.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,9 @@ export const appendGlobalEventProperties = (keys: Array<string>): void => {
2222
if (stored) {
2323
this.removeEventListener(normalizedKey, stored);
2424
}
25-
this.addEventListener(normalizedKey, value);
25+
if (value instanceof Function) {
26+
this.addEventListener(normalizedKey, value);
27+
}
2628
this[TransferrableKeys.propertyEventHandlers][normalizedKey] = value;
2729
},
2830
});

0 commit comments

Comments
 (0)