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

Commit

Permalink
fix(jqLite): ignore class methods on comment elements
Browse files Browse the repository at this point in the history
Since c785267 jqLite uses setAttribute (rather than className property) in order to change classes. Some elements (eg. Comment) do not have this method which blows up.

jQuery silently ignores these method calls (because it uses className), so to get the same behavior as jQuery, we check for setAttribute method first.
  • Loading branch information
vojtajina committed Oct 9, 2013
1 parent b6a37d1 commit 64fd2c4
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 2 deletions.
5 changes: 3 additions & 2 deletions src/jqLite.js
Original file line number Diff line number Diff line change
Expand Up @@ -279,12 +279,13 @@ function JQLiteData(element, key, value) {
}

function JQLiteHasClass(element, selector) {
if (!element.getAttribute) return false;
return ((" " + (element.getAttribute('class') || '') + " ").replace(/[\n\t]/g, " ").
indexOf( " " + selector + " " ) > -1);
}

function JQLiteRemoveClass(element, cssClasses) {
if (cssClasses) {
if (cssClasses && element.setAttribute) {
forEach(cssClasses.split(' '), function(cssClass) {
element.setAttribute('class', trim(
(" " + (element.getAttribute('class') || '') + " ")
Expand All @@ -296,7 +297,7 @@ function JQLiteRemoveClass(element, cssClasses) {
}

function JQLiteAddClass(element, cssClasses) {
if (cssClasses) {
if (cssClasses && element.setAttribute) {
var existingClasses = (' ' + (element.getAttribute('class') || '') + ' ')
.replace(/[\n\t]/g, " ");

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


it('should ignore comment elements', function() {
var comment = jqLite(document.createComment('something'));

comment.addClass('whatever');
comment.hasClass('whatever');
comment.toggleClass('whatever');
comment.removeClass('whatever');
});


describe('hasClass', function() {
it('should check class', function() {
var selector = jqLite([a, b]);
Expand Down

0 comments on commit 64fd2c4

Please sign in to comment.