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

Commit

Permalink
fix(jqLite): properly toggle multiple classes
Browse files Browse the repository at this point in the history
Fixes #4467
Closes #6448
  • Loading branch information
pkozlowski-opensource committed Feb 27, 2014
1 parent 27873ac commit 4e73c80
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 3 deletions.
11 changes: 8 additions & 3 deletions src/jqLite.js
Original file line number Diff line number Diff line change
Expand Up @@ -840,10 +840,15 @@ forEach({
removeClass: jqLiteRemoveClass,

toggleClass: function(element, selector, condition) {
if (isUndefined(condition)) {
condition = !jqLiteHasClass(element, selector);
if (selector) {
forEach(selector.split(' '), function(className){
var classCondition = condition;
if (isUndefined(classCondition)) {
classCondition = !jqLiteHasClass(element, className);
}
(classCondition ? jqLiteAddClass : jqLiteRemoveClass)(element, className);
});
}
(condition ? jqLiteAddClass : jqLiteRemoveClass)(element, selector);
},

parent: function(element) {
Expand Down
51 changes: 51 additions & 0 deletions test/jqLiteSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -666,6 +666,57 @@ describe('jqLite', function() {
expect(jqLite(b).hasClass('abc')).toEqual(false);

});

it('should allow toggling multiple classes without a condition', function () {
var selector = jqLite([a, b]);
expect(selector.toggleClass('abc cde')).toBe(selector);
expect(jqLite(a).hasClass('abc')).toBe(true);
expect(jqLite(a).hasClass('cde')).toBe(true);
expect(jqLite(b).hasClass('abc')).toBe(true);
expect(jqLite(b).hasClass('cde')).toBe(true);

expect(selector.toggleClass('abc cde')).toBe(selector);
expect(jqLite(a).hasClass('abc')).toBe(false);
expect(jqLite(a).hasClass('cde')).toBe(false);
expect(jqLite(b).hasClass('abc')).toBe(false);
expect(jqLite(b).hasClass('cde')).toBe(false);

expect(selector.toggleClass('abc')).toBe(selector);
expect(selector.toggleClass('abc cde')).toBe(selector);
expect(jqLite(a).hasClass('abc')).toBe(false);
expect(jqLite(a).hasClass('cde')).toBe(true);
expect(jqLite(b).hasClass('abc')).toBe(false);
expect(jqLite(b).hasClass('cde')).toBe(true);

expect(selector.toggleClass('abc cde')).toBe(selector);
expect(jqLite(a).hasClass('abc')).toBe(true);
expect(jqLite(a).hasClass('cde')).toBe(false);
expect(jqLite(b).hasClass('abc')).toBe(true);
expect(jqLite(b).hasClass('cde')).toBe(false);
});

it('should allow toggling multiple classes with a condition', function () {
var selector = jqLite([a, b]);
selector.addClass('abc');
expect(selector.toggleClass('abc cde', true)).toBe(selector);
expect(jqLite(a).hasClass('abc')).toBe(true);
expect(jqLite(a).hasClass('cde')).toBe(true);
expect(jqLite(b).hasClass('abc')).toBe(true);
expect(jqLite(b).hasClass('cde')).toBe(true);

selector.removeClass('abc');
expect(selector.toggleClass('abc cde', false)).toBe(selector);
expect(jqLite(a).hasClass('abc')).toBe(false);
expect(jqLite(a).hasClass('cde')).toBe(false);
expect(jqLite(b).hasClass('abc')).toBe(false);
expect(jqLite(b).hasClass('cde')).toBe(false);
});

it('should not break for null / undefined selectors', function () {
var selector = jqLite([a, b]);
expect(selector.toggleClass(null)).toBe(selector);
expect(selector.toggleClass(undefined)).toBe(selector);
});
});


Expand Down

0 comments on commit 4e73c80

Please sign in to comment.