diff --git a/src/jqLite.js b/src/jqLite.js index 3cfae74ad080..dc017ea714d5 100644 --- a/src/jqLite.js +++ b/src/jqLite.js @@ -422,13 +422,16 @@ function jqLiteRemoveClass(element, cssClasses) { if (cssClasses && element.setAttribute) { var existingClasses = (' ' + (element.getAttribute('class') || '') + ' ') .replace(/[\n\t]/g, ' '); + var newClasses = existingClasses; forEach(cssClasses.split(' '), function(cssClass) { cssClass = trim(cssClass); - existingClasses = existingClasses.replace(' ' + cssClass + ' ', ' '); + newClasses = newClasses.replace(' ' + cssClass + ' ', ' '); }); - element.setAttribute('class', trim(existingClasses)); + if (newClasses !== existingClasses) { + element.setAttribute('class', trim(newClasses)); + } } } @@ -436,15 +439,18 @@ function jqLiteAddClass(element, cssClasses) { if (cssClasses && element.setAttribute) { var existingClasses = (' ' + (element.getAttribute('class') || '') + ' ') .replace(/[\n\t]/g, ' '); + var newClasses = existingClasses; forEach(cssClasses.split(' '), function(cssClass) { cssClass = trim(cssClass); - if (existingClasses.indexOf(' ' + cssClass + ' ') === -1) { - existingClasses += cssClass + ' '; + if (newClasses.indexOf(' ' + cssClass + ' ') === -1) { + newClasses += cssClass + ' '; } }); - element.setAttribute('class', trim(existingClasses)); + if (newClasses !== existingClasses) { + element.setAttribute('class', trim(newClasses)); + } } } diff --git a/test/jqLiteSpec.js b/test/jqLiteSpec.js index 4a081844d160..190689f82a7c 100644 --- a/test/jqLiteSpec.js +++ b/test/jqLiteSpec.js @@ -928,6 +928,20 @@ describe('jqLite', function() { expect(fakeElement.getAttribute).toHaveBeenCalledOnceWith('class'); expect(fakeElement.setAttribute).toHaveBeenCalledOnceWith('class', 'foo bar baz'); }); + + + it('should not set the attribute when classes not changed', function() { + var fakeElement = { + nodeType: 1, + setAttribute: jasmine.createSpy(), + getAttribute: jasmine.createSpy().and.returnValue('foo bar') + }; + var jqA = jqLite(fakeElement); + + jqA.addClass('foo'); + expect(fakeElement.getAttribute).toHaveBeenCalledOnceWith('class'); + expect(fakeElement.setAttribute).not.toHaveBeenCalled(); + }); } @@ -1064,6 +1078,20 @@ describe('jqLite', function() { expect(fakeElement.getAttribute).toHaveBeenCalledOnceWith('class'); expect(fakeElement.setAttribute).toHaveBeenCalledOnceWith('class', 'bar'); }); + + + it('should not set the attribute when classes not changed', function() { + var fakeElement = { + nodeType: 1, + setAttribute: jasmine.createSpy(), + getAttribute: jasmine.createSpy().and.returnValue('foo bar') + }; + var jqA = jqLite(fakeElement); + + jqA.removeClass('noexistent'); + expect(fakeElement.getAttribute).toHaveBeenCalledOnceWith('class'); + expect(fakeElement.setAttribute).not.toHaveBeenCalled(); + }); } }); });