Skip to content

Commit

Permalink
Merge pull request cheeriojs#229 from jugglinmike/parent-bug
Browse files Browse the repository at this point in the history
Parent bug
  • Loading branch information
matthewmueller committed Jul 30, 2013
2 parents 7e18721 + 05d2eca commit 2cac9e4
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 14 deletions.
4 changes: 2 additions & 2 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -216,8 +216,8 @@ $('#fruits').find('li').length
//=> 3
```

#### .parent()
Gets the parent of the first selected element.
#### .parent([selector])
Get the parent of each element in the current set of matched elements, optionally filtered by a selector.

```js
$('.pear').parent().attr('id')
Expand Down
7 changes: 5 additions & 2 deletions lib/api/attributes.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,11 @@ var val = exports.val = function(value) {
var queryString = 'input[type=radio][name=' + this.attr('name') + ']:checked';
var parentEl = this;

//Go up until we hit a form or root
while(parentEl[0].name != 'form' && parentEl[0].name != 'root') parentEl = parentEl.parent();
// 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);
}

if (querying) {
return parentEl.find(queryString).attr('value');
Expand Down
25 changes: 20 additions & 5 deletions lib/api/traversing.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,26 @@ var find = exports.find = function(selector) {
return this.make(select(selector, [].slice.call(this.children())));
};

var parent = exports.parent = function(elem) {
if (this[0] && this[0].parent)
return this.make(this[0].parent);
else
return this;
// Get the parent of each element in the current set of matched elements,
// optionally filtered by a selector.
var parent = exports.parent = function(selector) {
var set = [];
var $set;

this.each(function(idx, elem) {
var parentElem = elem.parent;
if (set.indexOf(parentElem) < 0 && parentElem.type !== 'root') {
set.push(parentElem);
}
});

$set = this.make(set)

if (arguments.length) {
$set = $set.filter(selector);
}

return $set;
};

var parents = exports.parents = function(selector) {
Expand Down
6 changes: 3 additions & 3 deletions test/api.attributes.js
Original file line number Diff line number Diff line change
Expand Up @@ -123,9 +123,9 @@ describe('$(...)', function() {

it('(key) : should remove a single attr', function() {
var $fruits = $(fruits);
expect($('ul', $fruits.parent()).attr('id')).to.not.be(undefined);
$('ul', $fruits).removeAttr('id');
expect($('ul', $fruits).attr('id')).to.be(undefined);
expect($fruits.attr('id')).to.not.be(undefined);
$fruits.removeAttr('id');
expect($fruits.attr('id')).to.be(undefined);
});

it('should return cheerio object', function() {
Expand Down
33 changes: 33 additions & 0 deletions test/api.traversing.js
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,39 @@ describe('$(...)', function() {
})
});

describe('.parent', function() {

it('() : should return the parent of each matched element', function() {
var result = $('.orange', fruits).parent();
expect(result).to.have.length(1);
expect(result[0].attribs.id).to.be('fruits');
result = $('li', food).parent();
expect(result).to.have.length(2);
expect(result[0].attribs.id).to.be('fruits');
expect(result[1].attribs.id).to.be('vegetables');
});

it('() : should return an empty object for top-level elements', function() {
var result = $('ul', fruits).parent();
expect(result).to.have.length(0);
});

it('() : should not contain duplicate elements', function() {
var result = $('li', fruits).parent();
expect(result).to.have.length(1);
});

it('(selector) : should filter the matched parent elements by the selector', function() {
var result = $('.orange', fruits).parent();
expect(result).to.have.length(1);
expect(result[0].attribs.id).to.be('fruits');
result = $('li', food).parent('#fruits');
expect(result).to.have.length(1);
expect(result[0].attribs.id).to.be('fruits');
});

});

describe('.closest', function() {

it('() : should return an empty array', function() {
Expand Down
3 changes: 1 addition & 2 deletions test/api.utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,7 @@ describe('$', function() {
var $src = $('<div><span>foo</span><span>bar</span><span>baz</span></div>').children();
var $elem = $src.clone();
expect($elem.length).to.equal(3);
expect($elem.parent().length).to.equal(1);
expect($elem.parent()[0].type).to.equal('root');
expect($elem.parent()).to.have.length(0);
expect($elem.text()).to.equal($src.text());
$src.text('rofl');
expect($elem.text()).to.not.equal($src.text());
Expand Down

0 comments on commit 2cac9e4

Please sign in to comment.