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

Commit 26e7f51

Browse files
Brooooooklynvicb
authored andcommitted
fix(util): origin addEventListener/removeEventListener should be called without eventListener
fixes #198
1 parent 5bcc6ae commit 26e7f51

File tree

2 files changed

+23
-17
lines changed

2 files changed

+23
-17
lines changed

lib/utils.js

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -96,13 +96,10 @@ 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-
}
102-
var eventType = eventName + (useCapturing ? '$capturing' : '$bubbling');
103-
var fn;
10499
//Ignore special listeners of IE11 & Edge dev tools, see https://github.com/angular/zone.js/issues/150
105-
if (handler.toString() !== "[object FunctionWrapper]") {
100+
if (handler && handler.toString() !== "[object FunctionWrapper]") {
101+
var eventType = eventName + (useCapturing ? '$capturing' : '$bubbling');
102+
var fn;
106103
if (handler.handleEvent) {
107104
// Have to pass in 'handler' reference as an argument here, otherwise it gets clobbered in
108105
// IE9 by the arguments[1] assignment at end of this function.
@@ -125,30 +122,25 @@ function patchEventTargetMethods(obj) {
125122
// - When `addEventListener` is called on the global context in strict mode, `this` is undefined
126123
// see https://github.com/angular/zone.js/issues/190
127124
var target = this || global;
128-
129125
return global.zone.addEventListener.apply(target, arguments);
130126
};
131127

132128
// This is required for the removeEventListener hook on the root zone.
133129
obj[keys.common.removeEventListener] = obj.removeEventListener;
134130
obj.removeEventListener = function (eventName, handler, useCapturing) {
135-
if (!handler) {
136-
return;
137-
}
138131
var eventType = eventName + (useCapturing ? '$capturing' : '$bubbling');
139-
if (handler[boundFnsKey] && handler[boundFnsKey][eventType]) {
132+
if (handler && handler[boundFnsKey] && handler[boundFnsKey][eventType]) {
140133
var _bound = handler[boundFnsKey];
141134
arguments[1] = _bound[eventType];
142135
delete _bound[eventType];
136+
global.zone.dequeueTask(handler[originalFnKey]);
143137
}
144138

145139
// - Inside a Web Worker, `this` is undefined, the context is `global`
146140
// - When `addEventListener` is called on the global context in strict mode, `this` is undefined
147141
// see https://github.com/angular/zone.js/issues/190
148142
var target = this || global;
149-
150143
var result = global.zone.removeEventListener.apply(target, arguments);
151-
global.zone.dequeueTask(handler[originalFnKey]);
152144
return result;
153145
};
154146
};

test/patch/element.spec.js

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -114,17 +114,31 @@ describe('element', function () {
114114
});
115115

116116
it('should have no effect while calling addEventListener without listener', function () {
117+
var eventListenerZone = zone.fork({
118+
addEventListener: jasmine.createSpy('addEventListener')
119+
});
117120
expect(function() {
118-
button.addEventListener('click', null);
119-
button.addEventListener('click', undefined);
121+
eventListenerZone.run(function() {
122+
button.addEventListener('click', null);
123+
button.addEventListener('click', undefined);
124+
});
120125
}).not.toThrowError();
126+
expect(eventListenerZone.addEventListener).toHaveBeenCalledWith('click', null);
127+
expect(eventListenerZone.addEventListener).toHaveBeenCalledWith('click', undefined);
121128
});
122129

123130
it('should have no effect while calling removeEventListener without listener', function () {
131+
var eventListenerZone = zone.fork({
132+
removeEventListener: jasmine.createSpy('removeEventListener')
133+
});
124134
expect(function() {
125-
button.removeEventListener('click', null);
126-
button.removeEventListener('click', undefined);
135+
eventListenerZone.run(function() {
136+
button.removeEventListener('click', null);
137+
button.removeEventListener('click', undefined);
138+
});
127139
}).not.toThrowError();
140+
expect(eventListenerZone.removeEventListener).toHaveBeenCalledWith('click', null);
141+
expect(eventListenerZone.removeEventListener).toHaveBeenCalledWith('click', undefined);
128142
});
129143

130144

0 commit comments

Comments
 (0)