Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

.children() gets applied to all descendants not just immediate descendants #17

Closed
matthewmueller opened this issue Dec 19, 2011 · 9 comments
Labels

Comments

@matthewmueller
Copy link
Member

See #6 for more information

@jmonster
Copy link

jmonster commented Feb 5, 2012

Do you plan to implement this? What about selectors like 'ul > li' to grab immediate children?

@matthewmueller
Copy link
Member Author

I took a look at this earlier - this should be addressed in cheerio-soupselect. I'm hoping to close this bug this weekend.

Here's where the discussion will be: matthewmueller/cheerio-soupselect#7

@siddMahen
Copy link

I'm not understanding the feature/bug here. I thought that .children() was supposed to return only the immediate descendants (see here). Could you perhaps provide a code example?

Currently it seems as thought:

var cheerio = require("cheerio"),
    $ = cheerio.load('<p><h1></h1><div><anotherd></anotherd></div></p>');

console.log($("p").children());

Returns the appropriate result:

{ '0': 
   { type: 'tag',
     name: 'h1',
     attribs: {},
     parent: 
      { type: 'tag',
        name: 'p',
        children: [Object],
        attribs: {},
        parent: [Object],
        prev: null,
        next: null },
     prev: null,
     next: 
      { type: 'tag',
        name: 'div',
        children: [Object],
        attribs: {},
        parent: [Object],
        prev: [Circular],
        next: null },
     children: [] },
  '1': 
   { type: 'tag',
     name: 'div',
     children: [ [Object] ],
     attribs: {},
     parent: 
      { type: 'tag',
        name: 'p',
        children: [Object],
        attribs: {},
        parent: [Object],
        prev: null,
        next: null },
     prev: 
      { type: 'tag',
        name: 'h1',
        attribs: {},
        parent: [Object],
        prev: null,
        next: [Circular],
        children: [] },
     next: null },
  length: 2 }

@matthewmueller
Copy link
Member Author

It's the selector that looks through the children when it shouldn't. Try:

var $ = cheerio.load('<html><body><h2 class = "header">Header</h2></body></html>')
$('html').children('.header');

I think this is best managed by adding a depth parameter in cheerio-soupselect. Anything else seems like a hack in the .find() function.

@siddMahen
Copy link

Ahhh, I see the problem.

@jmonster
Copy link

high fives for whoever implements depth + more css3-style selectors ("ul > li")

@matthewmueller
Copy link
Member Author

:-) it's on my todo list but unfortunately my todo list is very long :-/

@fb55
Copy link
Member

fb55 commented Aug 28, 2012

You should be able to use Array.prototype.filter with the method you get from CSSselect as the only argument. (That should also be the most performant way.)

@jugglinmike
Copy link
Member

This was indirectly resolved by commit 0464dd8, and an regression test was added in commit 024ac31.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

5 participants