Skip to content

Commit

Permalink
Merge pull request #245 from jugglinmike/parents-bug
Browse files Browse the repository at this point in the history
Correct behavior of `Cheerio#parents`
  • Loading branch information
matthewmueller committed Jul 30, 2013
2 parents 2cac9e4 + 4783553 commit 9eff2f6
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 5 deletions.
5 changes: 3 additions & 2 deletions lib/api/attributes.js
Expand Up @@ -70,12 +70,13 @@ var val = exports.val = function(value) {
switch (this.attr('type')) {
case 'radio':
var queryString = 'input[type=radio][name=' + this.attr('name') + ']:checked';
var parentEl = this;
var parentEl, root;

// Go up until we hit a form or root
parentEl = this.closest('form');
if (parentEl.length === 0) {
parentEl = this.make(this.parents().last()[0].parent);
root = (this.parents().last()[0] || this[0]).parent;
parentEl = this.make(root);
}

if (querying) {
Expand Down
3 changes: 1 addition & 2 deletions lib/api/traversing.js
Expand Up @@ -31,8 +31,7 @@ var parent = exports.parent = function(selector) {

var parents = exports.parents = function(selector) {
if (this[0] && this[0].parent) {
var elems = traverseParents(this, this[0].parent, selector, Infinity);
return elems.length ? elems : this;
return traverseParents(this, this[0].parent, selector, Infinity);
}
return this;
};
Expand Down
6 changes: 5 additions & 1 deletion test/api.traversing.js
Expand Up @@ -166,7 +166,7 @@ describe('$(...)', function() {
expect(result).to.have.length(2);
expect(result[0].attribs.id).to.be('fruits');
expect(result[1].attribs.id).to.be('food');
result = $('#food', food).parents()
result = $('#fruits', food).parents()
expect(result).to.have.length(1);
expect(result[0].attribs.id).to.be('food');
})
Expand All @@ -183,6 +183,10 @@ describe('$(...)', function() {
var result = $('.saladbar', food).parents();
expect(result).to.have.length(0);
})
it('() : should return an empty set for top-level elements', function() {
var result = $('#food', food).parents();
expect(result).to.have.length(0);
});
});

describe('.parent', function() {
Expand Down

0 comments on commit 9eff2f6

Please sign in to comment.