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

Commit 39b078b

Browse files
petebacondarwinmatsko
authored andcommitted
fix(ngAnimate): throw an error if a callback is passed to animate methods
As of bf0f550 (released in 1.3.0) it is no longer valid to pass a callback to the following functions: `enter`, `move`, `leave`, `addClass`, `removeClass`, `setClass` and `animate`. To prevent confusing error messages, this change asserts that this parameter is not a function. Closes #11826 Closes #11713
1 parent 9055b4e commit 39b078b

File tree

4 files changed

+91
-0
lines changed

4 files changed

+91
-0
lines changed

src/ng/animate.js

+12
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,12 @@ var $AnimateProvider = ['$provide', function($provide) {
170170
* page}.
171171
*/
172172
return {
173+
$$assertNoCallback: function(param) {
174+
if (isFunction(param)) {
175+
throw $animateMinErr('nocb', 'Do not pass a callback to animate methods');
176+
}
177+
},
178+
173179
animate: function(element, from, to) {
174180
applyStyles(element, { from: from, to: to });
175181
return asyncPromise();
@@ -192,6 +198,8 @@ var $AnimateProvider = ['$provide', function($provide) {
192198
* @return {Promise} the animation callback promise
193199
*/
194200
enter: function(element, parent, after, options) {
201+
this.$$assertNoCallback(options);
202+
195203
applyStyles(element, options);
196204
after ? after.after(element)
197205
: parent.prepend(element);
@@ -210,6 +218,8 @@ var $AnimateProvider = ['$provide', function($provide) {
210218
* @return {Promise} the animation callback promise
211219
*/
212220
leave: function(element, options) {
221+
this.$$assertNoCallback(options);
222+
213223
applyStyles(element, options);
214224
element.remove();
215225
return asyncPromise();
@@ -312,6 +322,8 @@ var $AnimateProvider = ['$provide', function($provide) {
312322
* @return {Promise} the animation callback promise
313323
*/
314324
setClass: function(element, add, remove, options) {
325+
this.$$assertNoCallback(options);
326+
315327
var self = this;
316328
var STORAGE_KEY = '$$animateClasses';
317329
var createdCache = false;

src/ngAnimate/animate.js

+2
Original file line numberDiff line numberDiff line change
@@ -548,6 +548,8 @@ angular.module('ngAnimate', ['ng'])
548548
options.tempClasses = options.tempClasses.split(/\s+/);
549549
}
550550
return options;
551+
} else {
552+
$delegate.$$assertNoCallback(options);
551553
}
552554
}
553555

test/ng/animateSpec.js

+39
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,45 @@ describe("$animate", function() {
177177
}));
178178
});
179179

180+
it('should throw an error when a callback function is passed as the options param',
181+
inject(function($animate, $rootScope, $document, $rootElement) {
182+
183+
var invalidCallback = function() { };
184+
var element = jqLite('<div></div>');
185+
element.attr('id', 'crazy-man');
186+
var parent = jqLite('<div></div>');
187+
var parent2 = jqLite('<div></div>');
188+
$rootElement.append(parent);
189+
$rootElement.append(parent2);
190+
191+
expect(function() {
192+
$animate.enter(element, parent, parent2, invalidCallback);
193+
}).toThrowMinErr('$animate', 'nocb', 'Do not pass a callback to animate methods');
194+
195+
expect(function() {
196+
$animate.move(element, parent, parent2, invalidCallback);
197+
}).toThrowMinErr('$animate', 'nocb', 'Do not pass a callback to animate methods');
198+
199+
parent.append(element);
200+
201+
expect(function() {
202+
$animate.addClass(element, 'klass', invalidCallback);
203+
}).toThrowMinErr('$animate', 'nocb', 'Do not pass a callback to animate methods');
204+
205+
element.className = 'klass';
206+
expect(function() {
207+
$animate.removeClass(element, 'klass', invalidCallback);
208+
}).toThrowMinErr('$animate', 'nocb', 'Do not pass a callback to animate methods');
209+
210+
expect(function() {
211+
$animate.setClass(element, 'one', 'two', invalidCallback);
212+
}).toThrowMinErr('$animate', 'nocb', 'Do not pass a callback to animate methods');
213+
214+
expect(function() {
215+
$animate.leave(element, invalidCallback);
216+
}).toThrowMinErr('$animate', 'nocb', 'Do not pass a callback to animate methods');
217+
}));
218+
180219
describe('CSS class DOM manipulation', function() {
181220
var element;
182221
var addClass;

test/ngAnimate/animateSpec.js

+38
Original file line numberDiff line numberDiff line change
@@ -2817,6 +2817,44 @@ describe("ngAnimate", function() {
28172817
});
28182818

28192819
describe("options", function() {
2820+
it('should throw an error when a callback function is passed as the options param',
2821+
inject(function($animate, $rootScope, $document, $rootElement) {
2822+
2823+
var invalidCallback = function() { };
2824+
var element = jqLite('<div></div>');
2825+
element.attr('id', 'crazy-man');
2826+
var parent = jqLite('<div></div>');
2827+
var parent2 = jqLite('<div></div>');
2828+
$rootElement.append(parent);
2829+
$rootElement.append(parent2);
2830+
2831+
expect(function() {
2832+
$animate.enter(element, parent, parent2, invalidCallback);
2833+
}).toThrowMinErr('$animate', 'nocb', 'Do not pass a callback to animate methods');
2834+
2835+
expect(function() {
2836+
$animate.move(element, parent, parent2, invalidCallback);
2837+
}).toThrowMinErr('$animate', 'nocb', 'Do not pass a callback to animate methods');
2838+
2839+
parent.append(element);
2840+
2841+
expect(function() {
2842+
$animate.addClass(element, 'klass', invalidCallback);
2843+
}).toThrowMinErr('$animate', 'nocb', 'Do not pass a callback to animate methods');
2844+
2845+
element.className = 'klass';
2846+
expect(function() {
2847+
$animate.removeClass(element, 'klass', invalidCallback);
2848+
}).toThrowMinErr('$animate', 'nocb', 'Do not pass a callback to animate methods');
2849+
2850+
expect(function() {
2851+
$animate.setClass(element, 'one', 'two', invalidCallback);
2852+
}).toThrowMinErr('$animate', 'nocb', 'Do not pass a callback to animate methods');
2853+
2854+
expect(function() {
2855+
$animate.leave(element, invalidCallback);
2856+
}).toThrowMinErr('$animate', 'nocb', 'Do not pass a callback to animate methods');
2857+
}));
28202858

28212859
it('should add and remove the temporary className value is provided', function() {
28222860
var captures = {};

0 commit comments

Comments
 (0)