Skip to content
This repository was archived by the owner on Feb 26, 2024. It is now read-only.

Commit 5bcc6ae

Browse files
Brooooooklynvicb
authored andcommitted
fix(utils): should have no effect when called addEventListener/removeEventListener without eventListener.
1 parent d44d699 commit 5bcc6ae

File tree

2 files changed

+20
-0
lines changed

2 files changed

+20
-0
lines changed

Diff for: lib/utils.js

+6
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,9 @@ function patchEventTargetMethods(obj) {
9696
// This is required for the addEventListener hook on the root zone.
9797
obj[keys.common.addEventListener] = obj.addEventListener;
9898
obj.addEventListener = function (eventName, handler, useCapturing) {
99+
if (!handler) {
100+
return;
101+
}
99102
var eventType = eventName + (useCapturing ? '$capturing' : '$bubbling');
100103
var fn;
101104
//Ignore special listeners of IE11 & Edge dev tools, see https://github.com/angular/zone.js/issues/150
@@ -129,6 +132,9 @@ function patchEventTargetMethods(obj) {
129132
// This is required for the removeEventListener hook on the root zone.
130133
obj[keys.common.removeEventListener] = obj.removeEventListener;
131134
obj.removeEventListener = function (eventName, handler, useCapturing) {
135+
if (!handler) {
136+
return;
137+
}
132138
var eventType = eventName + (useCapturing ? '$capturing' : '$bubbling');
133139
if (handler[boundFnsKey] && handler[boundFnsKey][eventType]) {
134140
var _bound = handler[boundFnsKey];

Diff for: test/patch/element.spec.js

+14
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,20 @@ describe('element', function () {
113113
expect(eventListener.handleEvent).not.toHaveBeenCalled();
114114
});
115115

116+
it('should have no effect while calling addEventListener without listener', function () {
117+
expect(function() {
118+
button.addEventListener('click', null);
119+
button.addEventListener('click', undefined);
120+
}).not.toThrowError();
121+
});
122+
123+
it('should have no effect while calling removeEventListener without listener', function () {
124+
expect(function() {
125+
button.removeEventListener('click', null);
126+
button.removeEventListener('click', undefined);
127+
}).not.toThrowError();
128+
});
129+
116130

117131
it('should only add a listener once for a given set of arguments', function() {
118132
var log = [];

0 commit comments

Comments
 (0)