diff --git a/src/attributes.js b/src/attributes.js index 1452796..ea37a58 100644 --- a/src/attributes.js +++ b/src/attributes.js @@ -167,3 +167,59 @@ fn.removeData = function(key) { return this; }; + +fn.addClass = function(className) { + var i = 0, + j = 0, + length = this.length, + classes = className.trim().split(/\s+/); + + for (; i < length; i++) { + j = 0; + + for (j = 0; j < classes.length; j++) { + this[i].classList.add(classes[j]); + } + } + + return this; +}; + +fn.removeClass = function(className) { + var i = 0, + j = 0, + length = this.length, + classes = className.trim().split(/\s+/); + + for (; i < length; i++) { + j = 0; + + for (j = 0; j < classes.length; j++) { + this[i].classList.remove(classes[j]); + } + } + + return this; +}; + +fn.toggleClass = function(className) { + var i = 0, length = this.length; + + for (; i < length; i++) { + this[i].classList.toggle(className); + } + + return this; +}; + +fn.hasClass = function(className) { + var i = 0, length = this.length; + + for (; i < length; i++) { + if (this[i].classList.contains(className)) { + return true; + } + } + + return false; +}; diff --git a/test/attributes.js b/test/attributes.js index 93ea868..c8dbcba 100644 --- a/test/attributes.js +++ b/test/attributes.js @@ -160,4 +160,73 @@ describe('jBone Attributes', function() { a.removeData(); }); + it('hasClass(name) should check if class exist', function() { + var a = jBone('
'); + var b = jBone('
'); + + expect(a.hasClass('name')).be.eql(true); + expect(a.hasClass('not-name')).be.eql(false); + expect(b.hasClass('name')).be.eql(false); + }); + + it('addClass(name) should add new class', function() { + var a = jBone('
'); + a.addClass('name'); + + expect(a[0].getAttribute('class')).be.eql('name'); + }); + + it('addClass(name) shouldn\'t add new class if class already exist', function() { + var a = jBone('
'); + a.addClass('name'); + + expect(a[0].getAttribute('class')).be.eql('name'); + }); + + it('addClass(first second) should add multiple classes', function() { + var a = jBone('
'); + a.addClass('first second'); + + expect(a[0].getAttribute('class')).be.eql('first second'); + }); + + it('removeClass(name) should remove existing class', function() { + var a = jBone('
'); + a.removeClass('name'); + + expect(a[0].getAttribute('class')).be.eql(''); + }); + + it('removeClass(first second) should remove multiple classes', function() { + var a = jBone('
'); + a.removeClass('first second'); + + expect(a[0].getAttribute('class')).be.eql(''); + }); + + it('removeClass(name) should work correct with not existing class', function() { + var a = jBone('
'); + a.removeClass('not-name'); + + expect(a[0].getAttribute('class')).be.eql('name'); + + var b = jBone('
'); + b.removeClass('not-name'); + + expect(b[0].getAttribute('class')).be.eql(null); + }); + + it('toggleClass(name) should add new class if class not exist', function() { + var a = jBone('
'); + a.toggleClass('name'); + + expect(a[0].getAttribute('class')).be.eql('name'); + }); + + it('toggleClass(name) should remove class if class is exist', function() { + var a = jBone('
'); + a.toggleClass('name'); + + expect(a[0].getAttribute('class')).be.eql(''); + }); });