From 3491955f697ea28831fa4a0c5a5762dbf00cd6e2 Mon Sep 17 00:00:00 2001 From: Stanislav Sysoev Date: Fri, 14 Feb 2014 15:11:58 +0400 Subject: [PATCH] fix(ngAnimate): TypeError Cannot call method 'querySelectorAll' in cancelChildAnimations When element with ng-repeat has an ng-if directive and user try to remove any item from array used for ng-repeat, he gets error "TypeError Cannot call method 'querySelectorAll' of undefined". This happens because in method cancelChildAnimations of ngAnimate directive not checked value returned from extractElementNode(element) method. Fix add a validation for result of extractElementNode(element) method. Closes #6205 --- src/ngAnimate/animate.js | 25 +++++++++++++++---------- test/ngAnimate/animateSpec.js | 16 ++++++++++++++++ 2 files changed, 31 insertions(+), 10 deletions(-) diff --git a/src/ngAnimate/animate.js b/src/ngAnimate/animate.js index 1a0bb172d433..ea791538cfe3 100644 --- a/src/ngAnimate/animate.js +++ b/src/ngAnimate/animate.js @@ -939,16 +939,21 @@ angular.module('ngAnimate', ['ng']) function cancelChildAnimations(element) { var node = extractElementNode(element); - forEach(node.querySelectorAll('.' + NG_ANIMATE_CLASS_NAME), function(element) { - element = angular.element(element); - var data = element.data(NG_ANIMATE_STATE); - if(data && data.active) { - angular.forEach(data.active, function(operation) { - (operation.done || noop)(true); - cancelAnimations(operation.animations); - }); - } - }); + if (node) { + var nodes = angular.isFunction(node.getElementsByClassName) ? + node.getElementsByClassName(NG_ANIMATE_CLASS_NAME) : + node.querySelectorAll('.' + NG_ANIMATE_CLASS_NAME); + forEach(nodes, function(element) { + element = angular.element(element); + var data = element.data(NG_ANIMATE_STATE); + if(data && data.active) { + angular.forEach(data.active, function(operation) { + (operation.done || noop)(true); + cancelAnimations(operation.animations); + }); + } + }); + } } function cancelAnimations(animations) { diff --git a/test/ngAnimate/animateSpec.js b/test/ngAnimate/animateSpec.js index b732d7b501da..42ce5a4dc238 100644 --- a/test/ngAnimate/animateSpec.js +++ b/test/ngAnimate/animateSpec.js @@ -3257,5 +3257,21 @@ describe("ngAnimate", function() { expect(signature).toBe('AB'); })); + + it('should not throw an error when only comment nodes are rendered in the animation', + inject(function($rootScope, $compile) { + + $rootScope.items = [1,2,3,4,5]; + + var element = html($compile('
')($rootScope)); + + $rootScope.$digest(); + + $rootScope.items = []; + + $rootScope.$digest(); + + expect(element.children().length).toBe(0); + })); }); });