Skip to content

Commit c0d0162

Browse files
committed
implement jQuery's .has()
1 parent 6ba5338 commit c0d0162

File tree

3 files changed

+57
-0
lines changed

3 files changed

+57
-0
lines changed

Readme.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -448,6 +448,24 @@ $('li').filter(function(i, el) {
448448
//=> 2
449449
```
450450

451+
#### .has( selector ) <br /> .has( element )
452+
453+
Filters the set of matched elements to only those which have the given DOM element as a descendant or which have a descendant that matches the given selector. Equivalent to `.filter(':has(selector)')`.
454+
455+
Selector:
456+
457+
```js
458+
$('ul').has('.pear').attr('id');
459+
//=> fruits
460+
```
461+
462+
Element:
463+
464+
```js
465+
$('ul').has($('.pear')[0]).attr('id');
466+
//=> fruits
467+
```
468+
451469
#### .first()
452470
Will select the first element of a cheerio object
453471

lib/api/traversing.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -328,6 +328,13 @@ var makeFilterMethod = function(filterFn) {
328328
exports.filter = makeFilterMethod(_.filter);
329329
exports.not = makeFilterMethod(_.reject);
330330

331+
exports.has = function(selectorOrHaystack) {
332+
var that = this;
333+
return exports.filter.call(this, function() {
334+
return that._make(this).find(selectorOrHaystack).length > 0;
335+
});
336+
};
337+
331338
exports.first = function() {
332339
return this.length > 1 ? this._make(this[0]) : this;
333340
};

test/api.traversing.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -825,6 +825,38 @@ describe('$(...)', function() {
825825
});
826826
});
827827

828+
describe('.has', function() {
829+
830+
beforeEach(function() {
831+
$ = cheerio.load(food);
832+
});
833+
834+
it('(selector) : should reduce the set of matched elements to those with descendants that match the selector', function() {
835+
var $fruits = $('#fruits,#vegetables').has('.pear');
836+
expect($fruits).to.have.length(1);
837+
expect($fruits[0]).to.be($('#fruits')[0]);
838+
});
839+
840+
it('(selector) : should only consider nested elements', function() {
841+
var $empty = $('#fruits').has('#fruits');
842+
expect($empty).to.have.length(0);
843+
});
844+
845+
it('(element) : should reduce the set of matched elements to those that are ancestors of the provided element', function() {
846+
var $fruits = $('#fruits,#vegetables').has($('.pear')[0]);
847+
expect($fruits).to.have.length(1);
848+
expect($fruits[0]).to.be($('#fruits')[0]);
849+
});
850+
851+
it('(element) : should only consider nested elements', function() {
852+
var $fruits = $('#fruits');
853+
var fruits = $fruits[0];
854+
var $empty = $fruits.has(fruits);
855+
856+
expect($empty).to.have.length(0);
857+
});
858+
});
859+
828860
describe('.first', function() {
829861

830862
it('() : should return the first item', function() {

0 commit comments

Comments
 (0)