Skip to content

Commit

Permalink
Merge branch 'master' of github.com:MatthewMueller/cheerio
Browse files Browse the repository at this point in the history
  • Loading branch information
matthewmueller committed Dec 17, 2012
2 parents a3016fb + 35970d5 commit b4285a4
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 10 deletions.
2 changes: 1 addition & 1 deletion lib/api/manipulation.js
Expand Up @@ -90,7 +90,7 @@ var remove = exports.remove = function(selector) {

// Filter if we have selector
if (selector)
elems = elems.find(selector);
elems = elems.filter(selector);

elems.each(function(i, el) {
var siblings = el.parent.children,
Expand Down
12 changes: 6 additions & 6 deletions lib/api/traversing.js
Expand Up @@ -6,7 +6,7 @@ var _ = require('underscore'),
var find = exports.find = function(selector) {
if (!selector) return this;
try {
var elem = select(selector, [].slice.call(this));
var elem = select(selector, [].slice.call(this.children()));
return this.make(elem);
} catch(e) {
return this.make([]);
Expand Down Expand Up @@ -66,7 +66,7 @@ var children = exports.children = function(selector) {
if (selector === undefined) return this.make(children);
else if (_.isNumber(selector)) return this.make(children[selector]);

return this.make(children).find(selector);
return this.make(children).filter(selector);
};

var each = exports.each = function(fn) {
Expand All @@ -82,11 +82,11 @@ var map = exports.map = function(fn) {
}, this);
};

var filter = exports.filter = function(match) {
return this.make(_.filter(this, _.isString(match) ?
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, i) { return match.call(this.make(el), i, el); }
, this
: function(el, i) { return match.call(make(el), i, el); }
));
};

Expand Down
4 changes: 2 additions & 2 deletions lib/cheerio.js
Expand Up @@ -61,7 +61,7 @@ var Cheerio = module.exports = function(selector, context, root) {
} else if(typeof context === 'string') {
if(isHtml(context)) {
// $('li', '<ul>...</ul>')
context = parse(context).children;
context = parse(context);
context = this.make(context, this);
} else {
// $('li', 'ul')
Expand All @@ -74,7 +74,7 @@ var Cheerio = module.exports = function(selector, context, root) {
if(!context) return this;

// #id, .class, tag
return context.find(selector);
return context.parent().find(selector);
};

/**
Expand Down
2 changes: 1 addition & 1 deletion package.json
Expand Up @@ -15,7 +15,7 @@
"dependencies": {
"cheerio-select" : "*",
"htmlparser2" : "2.x",
"underscore" : "*",
"underscore" : "~1.4",
"entities" : "0.x"
},
"devDependencies": {
Expand Down
4 changes: 4 additions & 0 deletions test/api.traversing.js
Expand Up @@ -24,6 +24,10 @@ describe('$(...)', function() {
expect($('ul', fruits).find('blah')).to.have.length(0);
});

it('(invalid single) : should query descendants only', function() {
expect($('#fruits', fruits).find('ul')).to.have.length(0);
});

it('should return empty if search already empty result', function() {
expect($('#fruits').find('li')).to.have.length(0);
});
Expand Down

0 comments on commit b4285a4

Please sign in to comment.