Skip to content

Commit

Permalink
toggleClass()
Browse files Browse the repository at this point in the history
  • Loading branch information
statianzo committed Aug 30, 2012
1 parent 73f4055 commit 5c99a6f
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 0 deletions.
7 changes: 7 additions & 0 deletions src/jquip.js
Expand Up @@ -434,6 +434,13 @@ window['$'] = window['jquip'] = (function(){
p['clone'] = function() {
return $(this.map(function() { return this.cloneNode(true); }));
};
p['toggleClass'] = function(className, val) {
return this['each'](function() {
var el = $(this);
(typeof val === 'undefined' ? !el.hasClass(className) : val)
? el.addClass(className) : el.removeClass(className);
});
};

$['Expr'] = {
'hidden': function(el){
Expand Down
1 change: 1 addition & 0 deletions test/runner.html
Expand Up @@ -14,6 +14,7 @@
<script type="text/javascript" src="spec/display.js"></script>
<script type="text/javascript" src="spec/wrap.js"></script>
<script type="text/javascript" src="spec/attrs.js"></script>
<script type="text/javascript" src="spec/css.js"></script>


<script type="text/javascript">
Expand Down
33 changes: 33 additions & 0 deletions test/spec/css.js
@@ -0,0 +1,33 @@
(function(){
describe('jquip.toggleClass', function() {
var el;
beforeEach(function() {
el = jquip('<div class="foo bar">');
});

it('removes class if exists', function() {
el.toggleClass('foo');
expect(el.hasClass('foo')).toBe(false);
});

it('adds class if not present', function() {
el.toggleClass('qux');
expect(el.hasClass('qux')).toBe(true);
});

it('removes if switch is false', function() {
el.toggleClass('bar', false);
expect(el.hasClass('bar')).toBe(false);
el.toggleClass('bar', false);
expect(el.hasClass('bar')).toBe(false);
});

it('adds if switch is true', function() {
el.toggleClass('bar', true);
expect(el.hasClass('bar')).toBe(true);

el.toggleClass('zing', true);
expect(el.hasClass('zing')).toBe(true);
});
});
}());

0 comments on commit 5c99a6f

Please sign in to comment.