Skip to content

Commit

Permalink
Fix bug in filter
Browse files Browse the repository at this point in the history
Selectors passed to the `filter` method should only be applied to the
top-level elements in the selection. In other words, descendent elements
should *not* be considered when filtering by a selector string.

From [the jQuery documentation on
`$.fn.filter`](http://api.jquery.com/filter/):

> **Description**: Reduce the set of matched elements to those that
> match the selector or pass the function's test.

This should resolve issue #158.
  • Loading branch information
jugglinmike committed Feb 3, 2013
1 parent 6f0a64a commit 0464dd8
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/api/traversing.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ var map = exports.map = function(fn) {
var filter = exports.filter = function(match) {
var make = _.bind(this.make, this);
return make(_.filter(this, _.isString(match) ?
function(el) { return select(match, el).length; }
function(el) { return select(match, el)[0] === el; }
: function(el, i) { return match.call(make(el), i, el); }
));
};
Expand Down
5 changes: 5 additions & 0 deletions test/api.traversing.js
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,11 @@ describe('$(...)', function() {
var pear = $('li', fruits).filter('.pear').text();
expect(pear).to.be('Pear');
});

it('(selector) : should not consider nested elements', function() {
var lis = $(fruits).filter('li');
expect(lis).to.have.length(0);
});

it('(fn) : should reduce the set of matched elements to those that pass the function\'s test', function() {
var orange = $('li', fruits).filter(function(i, el) {
Expand Down

0 comments on commit 0464dd8

Please sign in to comment.