Skip to content

Commit

Permalink
Merge pull request cheeriojs#527 from jugglinmike/add-back
Browse files Browse the repository at this point in the history
Implement `$.prototype.addBack`
  • Loading branch information
davidchambers committed Sep 19, 2014
2 parents bb0c68a + 7d307b9 commit 6675acc
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 0 deletions.
9 changes: 9 additions & 0 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -505,6 +505,15 @@ $('.apple').add('.orange').length
//=> 2
```

#### .addBack( [filter] )

Add the previous set of elements on the stack to the current set, optionally filtered by a selector.

```js
$('li').eq(0).addBack('.orange').length
//=> 2
```

### Manipulation
Methods for modifying the DOM structure.

Expand Down
8 changes: 8 additions & 0 deletions lib/api/traversing.js
Original file line number Diff line number Diff line change
Expand Up @@ -403,3 +403,11 @@ exports.add = function(other, context) {

return selection;
};

// Add the previous set of elements on the stack to the current set, optionally
// filtered by a selector.
var addBack = exports.addBack = function(selector) {
return this.add(
arguments.length ? this.prevObject.filter(selector) : this.prevObject
);
};
44 changes: 44 additions & 0 deletions test/api.traversing.js
Original file line number Diff line number Diff line change
Expand Up @@ -1275,4 +1275,48 @@ describe('$(...)', function() {
});
});

describe('.addBack', function() {
describe('() : ', function() {
it('includes siblings and self', function() {
var $selection = $('.orange').siblings().addBack();

expect($selection).to.have.length(3);
expect($selection[0]).to.be($('.apple')[0]);
expect($selection[1]).to.be($('.orange')[0]);
expect($selection[2]).to.be($('.pear')[0]);
});
it('includes children and self', function() {
var $selection = $('#fruits').children().addBack();

expect($selection).to.have.length(4);
expect($selection[0]).to.be($('#fruits')[0]);
expect($selection[1]).to.be($('.apple')[0]);
expect($selection[2]).to.be($('.orange')[0]);
expect($selection[3]).to.be($('.pear')[0]);
});
it('includes parent and self', function() {
var $selection = $('.apple').parent().addBack();

expect($selection).to.have.length(2);
expect($selection[0]).to.be($('#fruits')[0]);
expect($selection[1]).to.be($('.apple')[0]);
});
it('includes parents and self', function() {
var $ = cheerio.load(food);
var $selection = $('.apple').parents().addBack();

expect($selection).to.have.length(3);
expect($selection[0]).to.be($('#food')[0]);
expect($selection[1]).to.be($('#fruits')[0]);
expect($selection[2]).to.be($('.apple')[0]);
});
});
it('(filter) : filters the previous selection', function() {
var $selection = $('li').eq(1).addBack('.apple');

expect($selection).to.have.length(2);
expect($selection[0]).to.be($('.apple')[0]);
expect($selection[1]).to.be($('.orange')[0]);
});
});
});

0 comments on commit 6675acc

Please sign in to comment.