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

Commit

Permalink
fix(util): origin addEventListener/removeEventListener should be call…
Browse files Browse the repository at this point in the history
…ed without eventListener

fixes #198
  • Loading branch information
Brooooooklyn authored and vicb committed Nov 6, 2015
1 parent 5bcc6ae commit 26e7f51
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 17 deletions.
18 changes: 5 additions & 13 deletions lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,13 +96,10 @@ function patchEventTargetMethods(obj) {
// This is required for the addEventListener hook on the root zone.
obj[keys.common.addEventListener] = obj.addEventListener;
obj.addEventListener = function (eventName, handler, useCapturing) {
if (!handler) {
return;
}
var eventType = eventName + (useCapturing ? '$capturing' : '$bubbling');
var fn;
//Ignore special listeners of IE11 & Edge dev tools, see https://github.com/angular/zone.js/issues/150
if (handler.toString() !== "[object FunctionWrapper]") {
if (handler && handler.toString() !== "[object FunctionWrapper]") {
var eventType = eventName + (useCapturing ? '$capturing' : '$bubbling');
var fn;
if (handler.handleEvent) {
// Have to pass in 'handler' reference as an argument here, otherwise it gets clobbered in
// IE9 by the arguments[1] assignment at end of this function.
Expand All @@ -125,30 +122,25 @@ function patchEventTargetMethods(obj) {
// - When `addEventListener` is called on the global context in strict mode, `this` is undefined
// see https://github.com/angular/zone.js/issues/190
var target = this || global;

return global.zone.addEventListener.apply(target, arguments);
};

// This is required for the removeEventListener hook on the root zone.
obj[keys.common.removeEventListener] = obj.removeEventListener;
obj.removeEventListener = function (eventName, handler, useCapturing) {
if (!handler) {
return;
}
var eventType = eventName + (useCapturing ? '$capturing' : '$bubbling');
if (handler[boundFnsKey] && handler[boundFnsKey][eventType]) {
if (handler && handler[boundFnsKey] && handler[boundFnsKey][eventType]) {
var _bound = handler[boundFnsKey];
arguments[1] = _bound[eventType];
delete _bound[eventType];
global.zone.dequeueTask(handler[originalFnKey]);
}

// - Inside a Web Worker, `this` is undefined, the context is `global`
// - When `addEventListener` is called on the global context in strict mode, `this` is undefined
// see https://github.com/angular/zone.js/issues/190
var target = this || global;

var result = global.zone.removeEventListener.apply(target, arguments);
global.zone.dequeueTask(handler[originalFnKey]);
return result;
};
};
Expand Down
22 changes: 18 additions & 4 deletions test/patch/element.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -114,17 +114,31 @@ describe('element', function () {
});

it('should have no effect while calling addEventListener without listener', function () {
var eventListenerZone = zone.fork({
addEventListener: jasmine.createSpy('addEventListener')
});
expect(function() {
button.addEventListener('click', null);
button.addEventListener('click', undefined);
eventListenerZone.run(function() {
button.addEventListener('click', null);
button.addEventListener('click', undefined);
});
}).not.toThrowError();
expect(eventListenerZone.addEventListener).toHaveBeenCalledWith('click', null);
expect(eventListenerZone.addEventListener).toHaveBeenCalledWith('click', undefined);
});

it('should have no effect while calling removeEventListener without listener', function () {
var eventListenerZone = zone.fork({
removeEventListener: jasmine.createSpy('removeEventListener')
});
expect(function() {
button.removeEventListener('click', null);
button.removeEventListener('click', undefined);
eventListenerZone.run(function() {
button.removeEventListener('click', null);
button.removeEventListener('click', undefined);
});
}).not.toThrowError();
expect(eventListenerZone.removeEventListener).toHaveBeenCalledWith('click', null);
expect(eventListenerZone.removeEventListener).toHaveBeenCalledWith('click', undefined);
});


Expand Down

0 comments on commit 26e7f51

Please sign in to comment.