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

Commit

Permalink
perf(jqLite): avoid setting class attribute when not changed
Browse files Browse the repository at this point in the history
  • Loading branch information
jbedard committed Aug 9, 2017
1 parent 9b99741 commit 1762a98
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 5 deletions.
16 changes: 11 additions & 5 deletions src/jqLite.js
Original file line number Diff line number Diff line change
Expand Up @@ -422,29 +422,35 @@ 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));
}
}
}

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));
}
}
}

Expand Down
28 changes: 28 additions & 0 deletions test/jqLiteSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -928,6 +928,20 @@ describe('jqLite', function() {
});


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();
});


it('should not add duplicate classes', function() {
var jqA = jqLite(a);
expect(a.className).toBe('');
Expand Down Expand Up @@ -1059,6 +1073,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();
});
});
});

Expand Down

0 comments on commit 1762a98

Please sign in to comment.