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

fix(ngAnimate): ensure that only string-based addClass/removeClass values are applied #12459

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 18 additions & 18 deletions src/ng/animate.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,31 +106,31 @@ var $$CoreAnimateQueueProvider = function() {
};

function addRemoveClassesPostDigest(element, add, remove) {
var data = postDigestQueue.get(element);
var classVal;
var classVal, data = postDigestQueue.get(element);

if (!data) {
postDigestQueue.put(element, data = {});
postDigestElements.push(element);
}

if (add) {
forEach(add.split(' '), function(className) {
if (className) {
data[className] = true;
}
});
}

if (remove) {
forEach(remove.split(' '), function(className) {
if (className) {
data[className] = false;
}
});
}
var updateData = function(classes, value) {
var changed = false;
if (classes) {
classes = isString(classes) ? classes.split(' ') :
isArray(classes) ? classes : [];
forEach(classes, function(className) {
if (className) {
changed = true;
data[className] = value;
}
});
}
return changed;
};

if (postDigestElements.length > 1) return;
var classesAdded = updateData(add, true);
var classesRemoved = updateData(remove, false);
if ((!classesAdded && !classesRemoved) || postDigestElements.length > 1) return;

$rootScope.$$postDigest(function() {
forEach(postDigestElements, function(element) {
Expand Down
24 changes: 16 additions & 8 deletions src/ngAnimate/animateQueue.js
Original file line number Diff line number Diff line change
Expand Up @@ -239,22 +239,22 @@ var $$AnimateQueueProvider = ['$animateProvider', function($animateProvider) {
// These methods will become available after the digest has passed
var runner = new $$AnimateRunner();

// there are situations where a directive issues an animation for
// a jqLite wrapper that contains only comment nodes... If this
// happens then there is no way we can perform an animation
if (!node) {
close();
return runner;
}

if (isArray(options.addClass)) {
options.addClass = options.addClass.join(' ');
}

if (options.addClass && !isString(options.addClass)) {
options.addClass = null;
}

if (isArray(options.removeClass)) {
options.removeClass = options.removeClass.join(' ');
}

if (options.removeClass && !isString(options.removeClass)) {
options.removeClass = null;
}

if (options.from && !isObject(options.from)) {
options.from = null;
}
Expand All @@ -263,6 +263,14 @@ var $$AnimateQueueProvider = ['$animateProvider', function($animateProvider) {
options.to = null;
}

// there are situations where a directive issues an animation for
// a jqLite wrapper that contains only comment nodes... If this
// happens then there is no way we can perform an animation
if (!node) {
close();
return runner;
}

var className = [node.className, options.addClass, options.removeClass].join(' ');
if (!isAnimatableClassName(className)) {
close();
Expand Down
35 changes: 35 additions & 0 deletions test/ng/animateSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,41 @@ describe("$animate", function() {
});
});

they('should not issue a call to $prop if the provided class value is not a string or array', ['addClass', 'removeClass'], function(prop) {
inject(function($animate, $rootScope, $rootElement) {
var spyProp = prop === 'addClass' ? 'jqLiteAddClass' : 'jqLiteRemoveClass';
var spy = spyOn(window, spyProp).andCallThrough();

var element = jqLite('<div></div>');
var parent = $rootElement;

var options1 = {};
options1[prop] = function() {};
$animate.enter(element, parent, null, options1);

$rootScope.$digest();
expect(spy).not.toHaveBeenCalled();

var options2 = {};
options2[prop] = true;
$animate.leave(element, options2);

$rootScope.$digest();
expect(spy).not.toHaveBeenCalled();

var options3 = {};
if (prop === 'removeClass') {
element.addClass('fatias');
}

options3[prop] = ['fatias'];
$animate.enter(element, parent, null, options3);

$rootScope.$digest();
expect(spy).toHaveBeenCalled();
});
});

describe('CSS class DOM manipulation', function() {
var element;
var addClass;
Expand Down
29 changes: 29 additions & 0 deletions test/ngAnimate/animateSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,35 @@ describe("animations", function() {
});
});

they('should nullify both options.$prop when passed into an animation if it is not a string or an array', ['addClass', 'removeClass'], function(prop) {
inject(function($animate, $rootScope) {
var options1 = {};
options1[prop] = function() {};
$animate.enter(element, parent, null, options1);

expect(options1[prop]).toBeFalsy();
$rootScope.$digest();

var options2 = {};
options2[prop] = true;
$animate.leave(element, options2);

expect(options2[prop]).toBeFalsy();
$rootScope.$digest();

capturedAnimation = null;

var options3 = {};
if (prop === 'removeClass') {
element.addClass('fatias');
}

options3[prop] = ['fatias'];
$animate.enter(element, parent, null, options3);
expect(options3[prop]).toBe('fatias');
});
});

it('should throw a minErr if a regex value is used which partially contains or fully matches the `ng-animate` CSS class', function() {
module(function($animateProvider) {
assertError(/ng-animate/, true);
Expand Down