Skip to content

Commit

Permalink
fix(ngAnimate): TypeError Cannot call method 'querySelectorAll' in ca…
Browse files Browse the repository at this point in the history
…ncelChildAnimations

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 angular#6205
  • Loading branch information
Stanislav Sysoev authored and matsko committed Feb 21, 2014
1 parent 0c9abc3 commit 3491955
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 10 deletions.
25 changes: 15 additions & 10 deletions src/ngAnimate/animate.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
16 changes: 16 additions & 0 deletions test/ngAnimate/animateSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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('<div><div class="animated" ng-if="valid" ng-repeat="item in items"></div></div>')($rootScope));

$rootScope.$digest();

$rootScope.items = [];

$rootScope.$digest();

expect(element.children().length).toBe(0);
}));
});
});

0 comments on commit 3491955

Please sign in to comment.