From 5bcc6aefad6e2eb30f87ec0b9e9ffcc236971b75 Mon Sep 17 00:00:00 2001 From: Long Yinan Date: Fri, 23 Oct 2015 00:45:38 +0800 Subject: [PATCH] fix(utils): should have no effect when called addEventListener/removeEventListener without eventListener. --- lib/utils.js | 6 ++++++ test/patch/element.spec.js | 14 ++++++++++++++ 2 files changed, 20 insertions(+) diff --git a/lib/utils.js b/lib/utils.js index 228c2f9b9..8e5b62493 100644 --- a/lib/utils.js +++ b/lib/utils.js @@ -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 @@ -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]; diff --git a/test/patch/element.spec.js b/test/patch/element.spec.js index b90d172af..66fb876b3 100644 --- a/test/patch/element.spec.js +++ b/test/patch/element.spec.js @@ -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 = [];