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

Commit 1002b80

Browse files
matskopetebacondarwin
authored andcommitted
fix(ngAnimate): prohibit usage of the ng-animate class with classNameFilter
Since ngAnimate uses the `ng-animate` CSS class internally to track state it is better to keep this as a reserved CSS class to avoid accidentally adding / removing the CSS class when an animation is started and closed. BREAKING CHANGE: partially or fully using a regex value containing `ng-animate` as a token is not allowed anymore. Doing so will trigger a minErr exception to be thrown. So don't do this: ```js // only animate elements that contain the `ng-animate` CSS class $animateProvider.classNameFilter(/ng-animate/); // or partially contain it $animateProvider.classNameFilter(/some-class ng-animate another-class/); ``` but this is OK: ```js $animateProvider.classNameFilter(/ng-animate-special/); ``` Closes #11431 Closes #11807
1 parent 7bb01ba commit 1002b80

File tree

5 files changed

+31
-1
lines changed

5 files changed

+31
-1
lines changed

src/ng/animate.js

+8
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
var $animateMinErr = minErr('$animate');
44
var ELEMENT_NODE = 1;
5+
var NG_ANIMATE_CLASSNAME = 'ng-animate';
56

67
function mergeClasses(a,b) {
78
if (!a && !b) return '';
@@ -233,6 +234,13 @@ var $AnimateProvider = ['$provide', function($provide) {
233234
this.classNameFilter = function(expression) {
234235
if (arguments.length === 1) {
235236
this.$$classNameFilter = (expression instanceof RegExp) ? expression : null;
237+
if (this.$$classNameFilter) {
238+
var reservedRegex = new RegExp("(\\s+|\\/)" + NG_ANIMATE_CLASSNAME + "(\\s+|\\/)");
239+
if (reservedRegex.test(this.$$classNameFilter.toString())) {
240+
throw $animateMinErr('nongcls','$animateProvider.classNameFilter(regex) prohibits accepting a regex value which matches/contains the "{0}" CSS class.', NG_ANIMATE_CLASSNAME);
241+
242+
}
243+
}
236244
}
237245
return this.$$classNameFilter;
238246
};

src/ngAnimate/.jshintrc

+1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
"isElement": false,
2121

2222
"ELEMENT_NODE": false,
23+
"NG_ANIMATE_CLASSNAME": false,
2324
"NG_ANIMATE_CHILDREN_DATA": false,
2425

2526
"assertArg": false,

src/ngAnimate/animation.js

-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
'use strict';
22

33
var $$AnimationProvider = ['$animateProvider', function($animateProvider) {
4-
var NG_ANIMATE_CLASSNAME = 'ng-animate';
54
var NG_ANIMATE_REF_ATTR = 'ng-animate-ref';
65

76
var drivers = this.drivers = [];

src/ngAnimate/shared.js

+1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ var isElement = angular.isElement;
1616
var ELEMENT_NODE = 1;
1717
var COMMENT_NODE = 8;
1818

19+
var NG_ANIMATE_CLASSNAME = 'ng-animate';
1920
var NG_ANIMATE_CHILDREN_DATA = '$$ngAnimateChildren';
2021

2122
var isPromiseLike = function(p) {

test/ngAnimate/animateSpec.js

+21
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,27 @@ describe("animations", function() {
148148
});
149149
});
150150

151+
it('should throw a minErr if a regex value is used which partially contains or fully matches the `ng-animate` CSS class', function() {
152+
module(function($animateProvider) {
153+
assertError(/ng-animate/, true);
154+
assertError(/first ng-animate last/, true);
155+
assertError(/ng-animate-special/, false);
156+
assertError(/first ng-animate-special last/, false);
157+
assertError(/first ng-animate ng-animate-special last/, true);
158+
159+
function assertError(regex, bool) {
160+
var expectation = expect(function() {
161+
$animateProvider.classNameFilter(regex);
162+
});
163+
164+
var message = '$animateProvider.classNameFilter(regex) prohibits accepting a regex value which matches/contains the "ng-animate" CSS class.';
165+
166+
bool ? expectation.toThrowMinErr('$animate', 'nongcls', message)
167+
: expectation.not.toThrowMinErr('$animate', 'nongcls', message);
168+
}
169+
});
170+
});
171+
151172
it('should complete the leave DOM operation in case the classNameFilter fails', function() {
152173
module(function($animateProvider) {
153174
$animateProvider.classNameFilter(/memorable-animation/);

0 commit comments

Comments
 (0)