Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

Commit c914cd9

Browse files
Stanislav Sysoevmatsko
Stanislav Sysoev
authored andcommitted
fix(ngAnimate): TypeError Cannot call method 'querySelectorAll' in cancelChildAnimations
When an element containing both ng-repeat and ng-if directives attempts to remove any items from the repeat collection, the following error is thrown: "TypeError Cannot call method 'querySelectorAll' of undefined". This happens because the cancelChildAnimations code naively belives that the jqLite object always has an element node within it. The fix in this commit addresses to securely check to see if a node was properly extracted before any child elements are inspected. Closes #6205
1 parent e988199 commit c914cd9

File tree

2 files changed

+31
-10
lines changed

2 files changed

+31
-10
lines changed

src/ngAnimate/animate.js

+15-10
Original file line numberDiff line numberDiff line change
@@ -942,16 +942,21 @@ angular.module('ngAnimate', ['ng'])
942942

943943
function cancelChildAnimations(element) {
944944
var node = extractElementNode(element);
945-
forEach(node.querySelectorAll('.' + NG_ANIMATE_CLASS_NAME), function(element) {
946-
element = angular.element(element);
947-
var data = element.data(NG_ANIMATE_STATE);
948-
if(data && data.active) {
949-
angular.forEach(data.active, function(operation) {
950-
(operation.done || noop)(true);
951-
cancelAnimations(operation.animations);
952-
});
953-
}
954-
});
945+
if (node) {
946+
var nodes = angular.isFunction(node.getElementsByClassName) ?
947+
node.getElementsByClassName(NG_ANIMATE_CLASS_NAME) :
948+
node.querySelectorAll('.' + NG_ANIMATE_CLASS_NAME);
949+
forEach(nodes, function(element) {
950+
element = angular.element(element);
951+
var data = element.data(NG_ANIMATE_STATE);
952+
if(data && data.active) {
953+
angular.forEach(data.active, function(operation) {
954+
(operation.done || noop)(true);
955+
cancelAnimations(operation.animations);
956+
});
957+
}
958+
});
959+
}
955960
}
956961

957962
function cancelAnimations(animations) {

test/ngAnimate/animateSpec.js

+16
Original file line numberDiff line numberDiff line change
@@ -3370,5 +3370,21 @@ describe("ngAnimate", function() {
33703370
expect(stat).toBe('gone');
33713371
});
33723372
});
3373+
3374+
it('should not throw an error when only comment nodes are rendered in the animation',
3375+
inject(function($rootScope, $compile) {
3376+
3377+
$rootScope.items = [1,2,3,4,5];
3378+
3379+
var element = html($compile('<div><div class="animated" ng-if="valid" ng-repeat="item in items"></div></div>')($rootScope));
3380+
3381+
$rootScope.$digest();
3382+
3383+
$rootScope.items = [];
3384+
3385+
$rootScope.$digest();
3386+
3387+
expect(element.children().length).toBe(0);
3388+
}));
33733389
});
33743390
});

0 commit comments

Comments
 (0)