Skip to content

Commit 7b41285

Browse files
committed
fix($animate): ensure the DOM operation isn't run twice
Depending on the animations placed on ngClass, the DOM operation may run twice causing a race condition between addClass and removeClass. Depending on what classes are removed and added via $compile this may cause all CSS classes to be removed accidentally from the element being animated. Closes angular#4949
1 parent 8425e9f commit 7b41285

File tree

2 files changed

+48
-4
lines changed

2 files changed

+48
-4
lines changed

src/ngAnimate/animate.js

+14-4
Original file line numberDiff line numberDiff line change
@@ -563,7 +563,7 @@ angular.module('ngAnimate', ['ng'])
563563
//the animation if any matching animations are not found at all.
564564
//NOTE: IE8 + IE9 should close properly (run closeAnimation()) in case a NO animation is not found.
565565
if (animationsDisabled(element, parentElement) || matches.length === 0) {
566-
domOperation();
566+
fireDOMOperation();
567567
closeAnimation();
568568
return;
569569
}
@@ -596,7 +596,7 @@ angular.module('ngAnimate', ['ng'])
596596
//this would mean that an animation was not allowed so let the existing
597597
//animation do it's thing and close this one early
598598
if(animations.length === 0) {
599-
domOperation();
599+
fireDOMOperation();
600600
fireDoneCallbackAsync();
601601
return;
602602
}
@@ -616,7 +616,7 @@ angular.module('ngAnimate', ['ng'])
616616
//is so that the CSS classes present on the element can be properly examined.
617617
if((animationEvent == 'addClass' && element.hasClass(className)) ||
618618
(animationEvent == 'removeClass' && !element.hasClass(className))) {
619-
domOperation();
619+
fireDOMOperation();
620620
fireDoneCallbackAsync();
621621
return;
622622
}
@@ -627,6 +627,7 @@ angular.module('ngAnimate', ['ng'])
627627

628628
element.data(NG_ANIMATE_STATE, {
629629
running:true,
630+
className:className,
630631
structural:!isClassBased,
631632
animations:animations,
632633
done:onBeforeAnimationsComplete
@@ -637,7 +638,7 @@ angular.module('ngAnimate', ['ng'])
637638
invokeRegisteredAnimationFns(animations, 'before', onBeforeAnimationsComplete);
638639

639640
function onBeforeAnimationsComplete(cancelled) {
640-
domOperation();
641+
fireDOMOperation();
641642
if(cancelled === true) {
642643
closeAnimation();
643644
return;
@@ -695,6 +696,15 @@ angular.module('ngAnimate', ['ng'])
695696
doneCallback && $timeout(doneCallback, 0, false);
696697
}
697698

699+
//it is less complicated to use a flag than managing and cancelling
700+
//timeouts containing multiple callbacks.
701+
function fireDOMOperation() {
702+
if(!fireDOMOperation.hasBeenRun) {
703+
fireDOMOperation.hasBeenRun = true;
704+
domOperation();
705+
}
706+
}
707+
698708
function closeAnimation() {
699709
if(!closeAnimation.hasBeenRun) {
700710
closeAnimation.hasBeenRun = true;

test/ngAnimate/animateSpec.js

+34
Original file line numberDiff line numberDiff line change
@@ -2599,4 +2599,38 @@ describe("ngAnimate", function() {
25992599
});
26002600
});
26012601

2602+
it('should only perform the DOM operation once',
2603+
inject(function($sniffer, $compile, $rootScope, $rootElement, $animate, $timeout) {
2604+
2605+
if (!$sniffer.transitions) return;
2606+
2607+
ss.addRule('.base-class', '-webkit-transition:1s linear all;' +
2608+
'transition:1s linear all;');
2609+
2610+
$animate.enabled(true);
2611+
2612+
var element = $compile('<div class="base-class one two"></div>')($rootScope);
2613+
$rootElement.append(element);
2614+
jqLite($document[0].body).append($rootElement);
2615+
2616+
$animate.removeClass(element, 'base-class one two');
2617+
2618+
//still true since we're before the reflow
2619+
expect(element.hasClass('base-class')).toBe(true);
2620+
2621+
//this will cancel the remove animation
2622+
$animate.addClass(element, 'base-class one two');
2623+
2624+
//the cancellation was a success and the class was added right away
2625+
//since there was no successive animation for the after animation
2626+
expect(element.hasClass('base-class')).toBe(true);
2627+
2628+
//the reflow...
2629+
$timeout.flush();
2630+
2631+
//the reflow DOM operation was commenced but it ran before so it
2632+
//shouldn't run agaun
2633+
expect(element.hasClass('base-class')).toBe(true);
2634+
}));
2635+
26022636
});

0 commit comments

Comments
 (0)