Skip to content

Commit c3e149f

Browse files
committed
fix($animate): ignore invalid option parameter values
Prior to this fix there was another patch that threw an exception if the provided options value was not of an object type. While this is correct in terms of logic, it caused issues with plugins and tools that are designed to work with multiple version of Angular. This fix ensures that these plugins work since an invalid options value is ignored by `$animate`. Closes angular#11826
1 parent 0fc5851 commit c3e149f

File tree

2 files changed

+89
-7
lines changed

2 files changed

+89
-7
lines changed

src/ng/animate.js

+13-7
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,12 @@ function splitClasses(classes) {
4040
return obj;
4141
}
4242

43+
function prepareAnimateOptions(options) {
44+
return isObject(options)
45+
? options
46+
: {};
47+
}
48+
4349
var $$CoreAnimateRunnerProvider = function() {
4450
this.$get = ['$q', '$$rAF', function($q, $$rAF) {
4551
function AnimateRunner() {}
@@ -420,7 +426,7 @@ var $AnimateProvider = ['$provide', function($provide) {
420426
after = after && jqLite(after);
421427
parent = parent || after.parent();
422428
domInsert(element, parent, after);
423-
return $$animateQueue.push(element, 'enter', options);
429+
return $$animateQueue.push(element, 'enter', prepareAnimateOptions(options));
424430
},
425431

426432
/**
@@ -446,7 +452,7 @@ var $AnimateProvider = ['$provide', function($provide) {
446452
after = after && jqLite(after);
447453
parent = parent || after.parent();
448454
domInsert(element, parent, after);
449-
return $$animateQueue.push(element, 'move', options);
455+
return $$animateQueue.push(element, 'move', prepareAnimateOptions(options));
450456
},
451457

452458
/**
@@ -463,7 +469,7 @@ var $AnimateProvider = ['$provide', function($provide) {
463469
* @return {Promise} the animation callback promise
464470
*/
465471
leave: function(element, options) {
466-
return $$animateQueue.push(element, 'leave', options, function() {
472+
return $$animateQueue.push(element, 'leave', prepareAnimateOptions(options), function() {
467473
element.remove();
468474
});
469475
},
@@ -487,7 +493,7 @@ var $AnimateProvider = ['$provide', function($provide) {
487493
* @return {Promise} the animation callback promise
488494
*/
489495
addClass: function(element, className, options) {
490-
options = options || {};
496+
options = prepareAnimateOptions(options);
491497
options.addClass = mergeClasses(options.addclass, className);
492498
return $$animateQueue.push(element, 'addClass', options);
493499
},
@@ -511,7 +517,7 @@ var $AnimateProvider = ['$provide', function($provide) {
511517
* @return {Promise} the animation callback promise
512518
*/
513519
removeClass: function(element, className, options) {
514-
options = options || {};
520+
options = prepareAnimateOptions(options);
515521
options.removeClass = mergeClasses(options.removeClass, className);
516522
return $$animateQueue.push(element, 'removeClass', options);
517523
},
@@ -536,7 +542,7 @@ var $AnimateProvider = ['$provide', function($provide) {
536542
* @return {Promise} the animation callback promise
537543
*/
538544
setClass: function(element, add, remove, options) {
539-
options = options || {};
545+
options = prepareAnimateOptions(options);
540546
options.addClass = mergeClasses(options.addClass, add);
541547
options.removeClass = mergeClasses(options.removeClass, remove);
542548
return $$animateQueue.push(element, 'setClass', options);
@@ -564,7 +570,7 @@ var $AnimateProvider = ['$provide', function($provide) {
564570
* @return {Promise} the animation callback promise
565571
*/
566572
animate: function(element, from, to, className, options) {
567-
options = options || {};
573+
options = prepareAnimateOptions(options);
568574
options.from = options.from ? extend(options.from, from) : from;
569575
options.to = options.to ? extend(options.to, to) : to;
570576

test/ng/animateSpec.js

+76
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,82 @@ describe("$animate", function() {
242242
expect(element[0].previousSibling).toBe(after);
243243
});
244244
});
245+
246+
they('$prop() should operate using a native DOM element',
247+
['enter', 'move', 'leave', 'addClass', 'removeClass', 'setClass', 'animate'], function(event) {
248+
249+
var captureSpy = jasmine.createSpy();
250+
251+
module(function($provide) {
252+
$provide.value('$$animateQueue', {
253+
push: captureSpy
254+
});
255+
});
256+
257+
inject(function($animate, $rootScope, $document, $rootElement) {
258+
var element = jqLite('<div></div>');
259+
var parent2 = jqLite('<div></div>');
260+
var parent = $rootElement;
261+
parent.append(parent2);
262+
263+
if (event !== 'enter' && event !== 'move') {
264+
parent.append(element);
265+
}
266+
267+
var fn, invalidOptions = function() { };
268+
269+
switch (event) {
270+
case 'enter':
271+
case 'move':
272+
fn = function() {
273+
$animate[event](element, parent, parent2, invalidOptions);
274+
};
275+
break;
276+
277+
case 'addClass':
278+
fn = function() {
279+
$animate.addClass(element, 'klass', invalidOptions);
280+
};
281+
break;
282+
283+
case 'removeClass':
284+
element.className = 'klass';
285+
fn = function() {
286+
$animate.removeClass(element, 'klass', invalidOptions);
287+
};
288+
break;
289+
290+
case 'setClass':
291+
element.className = 'two';
292+
fn = function() {
293+
$animate.setClass(element, 'one', 'two', invalidOptions);
294+
};
295+
break;
296+
297+
case 'leave':
298+
fn = function() {
299+
$animate.leave(element, invalidOptions);
300+
};
301+
break;
302+
303+
case 'animate':
304+
var toStyles = { color: 'red' };
305+
fn = function() {
306+
$animate.animate(element, {}, toStyles, 'klass', invalidOptions);
307+
};
308+
break;
309+
}
310+
311+
expect(function() {
312+
fn();
313+
$rootScope.$digest();
314+
}).not.toThrow();
315+
316+
var optionsArg = captureSpy.mostRecentCall.args[2];
317+
expect(optionsArg).not.toBe(invalidOptions);
318+
expect(isObject(optionsArg)).toBeTruthy();
319+
});
320+
});
245321
});
246322

247323
describe('CSS class DOM manipulation', function() {

0 commit comments

Comments
 (0)