From 4e73c80b17bd237a8491782bcf9e19f1889e12ed Mon Sep 17 00:00:00 2001 From: Pawel Kozlowski Date: Tue, 25 Feb 2014 19:55:34 +0100 Subject: [PATCH] fix(jqLite): properly toggle multiple classes Fixes #4467 Closes #6448 --- src/jqLite.js | 11 +++++++--- test/jqLiteSpec.js | 51 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 59 insertions(+), 3 deletions(-) diff --git a/src/jqLite.js b/src/jqLite.js index b820f5e71710..809ede034a00 100644 --- a/src/jqLite.js +++ b/src/jqLite.js @@ -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) { diff --git a/test/jqLiteSpec.js b/test/jqLiteSpec.js index 056c6145ab34..482c05f4f730 100644 --- a/test/jqLiteSpec.js +++ b/test/jqLiteSpec.js @@ -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); + }); });