Skip to content

Commit

Permalink
Merge pull request #202 from zero21xxx/is-implementation
Browse files Browse the repository at this point in the history
added: .is(selector) [zero21xxx]
  • Loading branch information
matthewmueller committed Jun 6, 2013
2 parents 877fd34 + 14eb828 commit 74ddd00
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 2 deletions.
4 changes: 4 additions & 0 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,10 @@ $('.apple').addClass('red').removeClass().html()

> See http://api.jquery.com/removeClass/ for more information.
#### .is( selector )
#### .is( function(index) )
Checks the current list of elements and returns `true` if _any_ of the elements match the selector. If using a predicate function, the function is executed in the context of the selected element, so `this` refers to the current element.


### Traversing

Expand Down
8 changes: 8 additions & 0 deletions lib/api/attributes.js
Original file line number Diff line number Diff line change
Expand Up @@ -217,3 +217,11 @@ var removeClass = exports.removeClass = function(value) {
).join(' ');
});
};

var is = exports.is = function (selector) {
if (selector) {
return this.filter(selector).length > 0;
}
return false;
}

28 changes: 26 additions & 2 deletions test/api.attributes.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,13 +53,13 @@ describe('$(...)', function() {
$apple.attr('href', 'http://github.com/"><script>alert("XSS!")</script><br');
expect($apple.html()).to.not.contain('<script>alert("XSS!")</script>');
});

it('(key, value) : should coerce values to a string', function() {
var $apple = $('.apple', fruits);
$apple.attr('data-test', 1);
expect($apple[0].attribs['data-test']).to.equal('1');
expect($apple.attr('data-test')).to.equal('1');
});
});
});

describe('.val', function() {
Expand Down Expand Up @@ -257,4 +257,28 @@ describe('$(...)', function() {

});

describe('.is', function () {
it('() should return false', function () {
expect($('li.apple', fruits).is()).to.be(false)
})
it('(true selector) should return true', function () {
expect($('#vegetables', vegetables).is('ul')).to.be(true)
})
it('(false selector) should return false', function () {
expect($('#vegetables', vegetables).is('div')).to.be(false)
})
it('(true predicate) should return true', function () {
var result = $('li', fruits).is(function() {
return this.hasClass('pear')
})
expect(result).to.be(true)
})
it('(false predicate) should return false', function () {
var result = $('li', fruits).last().is(function() {
return this.name === 'ul'
})
expect(result).to.be(false)
})
})

});

0 comments on commit 74ddd00

Please sign in to comment.