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

Commit

Permalink
fix(utils): should have no effect when called addEventListener/remove…
Browse files Browse the repository at this point in the history
…EventListener without eventListener.
  • Loading branch information
Brooooooklyn authored and vicb committed Oct 24, 2015
1 parent d44d699 commit 5bcc6ae
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 0 deletions.
6 changes: 6 additions & 0 deletions lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,9 @@ 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
Expand Down Expand Up @@ -129,6 +132,9 @@ function patchEventTargetMethods(obj) {
// 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]) {
var _bound = handler[boundFnsKey];
Expand Down
14 changes: 14 additions & 0 deletions test/patch/element.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,20 @@ describe('element', function () {
expect(eventListener.handleEvent).not.toHaveBeenCalled();
});

it('should have no effect while calling addEventListener without listener', function () {
expect(function() {
button.addEventListener('click', null);
button.addEventListener('click', undefined);
}).not.toThrowError();
});

it('should have no effect while calling removeEventListener without listener', function () {
expect(function() {
button.removeEventListener('click', null);
button.removeEventListener('click', undefined);
}).not.toThrowError();
});


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

0 comments on commit 5bcc6ae

Please sign in to comment.