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

Commit ff5cf73

Browse files
committed
fix($animate): prevent cancellation timestamp from being too far in the future
Closes #6748
1 parent f552f25 commit ff5cf73

File tree

2 files changed

+29
-8
lines changed

2 files changed

+29
-8
lines changed

src/ngAnimate/animate.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1199,7 +1199,7 @@ angular.module('ngAnimate', ['ng'])
11991199

12001200
//but it may not need to cancel out the existing timeout
12011201
//if the timestamp is less than the previous one
1202-
var futureTimestamp = Date.now() + (totalTime * 1000);
1202+
var futureTimestamp = Date.now() + totalTime;
12031203
if(futureTimestamp <= closingTimestamp) {
12041204
return;
12051205
}

test/ngAnimate/animateSpec.js

+28-7
Original file line numberDiff line numberDiff line change
@@ -1390,24 +1390,43 @@ describe("ngAnimate", function() {
13901390
}));
13911391

13921392
it("should intelligently cancel former timeouts and close off a series of elements a final timeout", function() {
1393+
var currentTimestamp = Date.now();
1394+
spyOn(Date,'now').andCallFake(function() {
1395+
return currentTimestamp;
1396+
});
1397+
13931398
var cancellations = 0;
13941399
module(function($provide) {
13951400
$provide.decorator('$timeout', function($delegate) {
13961401
var _cancel = $delegate.cancel;
1397-
$delegate.cancel = function() {
1398-
cancellations++;
1399-
return _cancel.apply($delegate, arguments);
1402+
$delegate.cancel = function(timer) {
1403+
if(timer) {
1404+
cancellations++;
1405+
return _cancel.apply($delegate, arguments);
1406+
}
14001407
};
14011408
return $delegate;
14021409
});
14031410
})
14041411
inject(function($animate, $rootScope, $compile, $sniffer, $timeout) {
14051412
if (!$sniffer.transitions) return;
14061413

1407-
ss.addRule('.animate-me', '-webkit-transition:1s linear all;' +
1414+
ss.addRule('.animate-me div', '-webkit-transition:1s linear all;' +
14081415
'transition:1s linear all;');
14091416

1410-
element = $compile(html('<div><div class="animate-me" ng-repeat="item in items"></div></div>'))($rootScope);
1417+
ss.addRule('.animate-me-longer div', '-webkit-transition:1.5s linear all;' +
1418+
'transition:1.5s linear all;');
1419+
1420+
element = $compile(html('<div class="animate-me-longer">' +
1421+
' <div ng-repeat="item in items"></div>' +
1422+
'</div>'))($rootScope);
1423+
$rootScope.items = [0];
1424+
$rootScope.$digest();
1425+
$animate.triggerReflow();
1426+
1427+
currentTimestamp += 2250; //1.5 * 1500 = 2250
1428+
1429+
element[0].className = 'animate-me';
14111430

14121431
$rootScope.items = [1,2,3,4,5,6,7,8,9,10];
14131432
var totalOperations = $rootScope.items.length;
@@ -1416,9 +1435,11 @@ describe("ngAnimate", function() {
14161435

14171436
$rootScope.items = [0];
14181437
$animate.triggerReflow();
1438+
1439+
currentTimestamp += 1500; //1.5 * 1000 = 1500
14191440
$timeout.flush(1500);
14201441

1421-
expect(cancellations).toBeLessThan(totalOperations);
1442+
expect(cancellations).toBe(1);
14221443
expect(element.children().length).toBe(10);
14231444
cancellations = 0;
14241445

@@ -1428,7 +1449,7 @@ describe("ngAnimate", function() {
14281449
$animate.triggerReflow();
14291450
$timeout.flush(1500);
14301451
expect(element.children().length).toBe(1);
1431-
expect(cancellations).toBeLessThan(totalOperations);
1452+
expect(cancellations).toBe(1);
14321453
});
14331454
});
14341455

0 commit comments

Comments
 (0)