Skip to content

Commit

Permalink
Add .addClass, .removeClass, .toggleClass and .hasClass methods kupri…
Browse files Browse the repository at this point in the history
  • Loading branch information
kupriyanenko committed Mar 9, 2015
1 parent 531ca24 commit 2a61f8e
Show file tree
Hide file tree
Showing 2 changed files with 125 additions and 0 deletions.
56 changes: 56 additions & 0 deletions src/attributes.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
};
69 changes: 69 additions & 0 deletions test/attributes.js
Original file line number Diff line number Diff line change
Expand Up @@ -160,4 +160,73 @@ describe('jBone Attributes', function() {
a.removeData();
});

it('hasClass(name) should check if class exist', function() {
var a = jBone('<div class="name">');
var b = jBone('<div>');

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('<div>');
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('<div class="name">');
a.addClass('name');

expect(a[0].getAttribute('class')).be.eql('name');
});

it('addClass(first second) should add multiple classes', function() {
var a = jBone('<div>');
a.addClass('first second');

expect(a[0].getAttribute('class')).be.eql('first second');
});

it('removeClass(name) should remove existing class', function() {
var a = jBone('<div class="name">');
a.removeClass('name');

expect(a[0].getAttribute('class')).be.eql('');
});

it('removeClass(first second) should remove multiple classes', function() {
var a = jBone('<div class="first second">');
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('<div class="name">');
a.removeClass('not-name');

expect(a[0].getAttribute('class')).be.eql('name');

var b = jBone('<div>');
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('<div>');
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('<div class="name">');
a.toggleClass('name');

expect(a[0].getAttribute('class')).be.eql('');
});
});

0 comments on commit 2a61f8e

Please sign in to comment.