diff --git a/src/jqLite.js b/src/jqLite.js index 8660ce49a155..60c156e46607 100644 --- a/src/jqLite.js +++ b/src/jqLite.js @@ -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') || '') + " ") @@ -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, " "); diff --git a/test/jqLiteSpec.js b/test/jqLiteSpec.js index de9c74da71bb..24920273e42f 100644 --- a/test/jqLiteSpec.js +++ b/test/jqLiteSpec.js @@ -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]);