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

Suggestion: new method Elements.parents() #20

Closed
svivian opened this issue Mar 22, 2010 · 5 comments
Closed

Suggestion: new method Elements.parents() #20

svivian opened this issue Mar 22, 2010 · 5 comments
Labels

Comments

@svivian
Copy link

svivian commented Mar 22, 2010

A function similar to jQuery's parents() - http://api.jquery.com/parents/ - would be a nice addition. The function would return all parent elements of the current Element. Or, given an optional parameter, would filter based on that.

So if you for example selected all bold text with Elements elems = doc.select('b') you could then find all bold tags that were in paragraphs with elems.parents('p'), and that would select the paragraphs themselves if you wanted to do some processing on them.

You could also add the optional selector to the parent() function too - although it is as easy in this case to simply select the parent and check if the tag or class etc matches.

@jhy
Copy link
Owner

jhy commented Mar 23, 2010

Thanks, that looks like a good idea.

It's kind of odd that there's not a native CSS selector syntax to find parents based on their children.

Might also be good to implement jquery's :has() pseudo-selector. So you can do doc.select("p:has(b)") and get the same set as using your parents example.

@svivian
Copy link
Author

svivian commented Mar 23, 2010

Heh, well I guess you'll end up implementing all of jQuery's selection and traversing methods at some point ;)

As for CSS, I read an article once on that subject of "parent selectors", the supposed reason being that it's really hard for browsers to implement (or implement efficiently, at least).

@svivian
Copy link
Author

svivian commented Mar 24, 2010

Just to clarify if it wasn't clear: in jQuery, parents selects all ancestors of an element matching the optional filter. So if you have nested divs it would fetch all of them (obviously you can just select the first one if necessary with other functions).

I don't know what your approach is with regard to the functions acting on Element vs those on Elements, if you mirror all functions etc.

@jhy
Copy link
Owner

jhy commented Mar 30, 2010

Thanks. I would look to mirror most functions from Element into Elements where it makes sense -- in this case it would be the combined set of all parents.

@jhy
Copy link
Owner

jhy commented Jun 10, 2010

Added :has(selector) pseudo-selector.
Added Element#parents() and Elements#parents() methods.

Closed by 49f0e41

michael-simons pushed a commit to michael-simons/jsoup that referenced this issue Jul 12, 2011
Added Element#parents() and Elements#parents() methods.

Fixes jhy#20
zazi pushed a commit to dswarm/jsoup that referenced this issue Oct 15, 2015
Added Element#parents() and Elements#parents() methods.

Fixes jhy#20
This issue was closed.
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

2 participants