jsoup icon indicating copy to clipboard operation
jsoup copied to clipboard

Add Prototype Style DOM Navigation

Open epochcoder opened this issue 11 years ago • 4 comments

the Element class now has the ability to quickly go up/down/next/previous according to the specified CSS selector

Example:

Document doc = Jsoup.connect("https://en.wikipedia.org/wiki/Java_(programming_language)").get();

Element fullTableOfContentsWhileNavigating = doc
            .down("li.toclevel-1.tocsection-13")
            .previous("li")
            .down("a[href=#A_more_comprehensive_example]")
            .up("div#toc");

epochcoder avatar Jan 09 '14 11:01 epochcoder

up/down/next/previous what can it do ?some one can tell me?

act262 avatar Jan 09 '14 16:01 act262

Thanks @epochcoder. Can you help me understand the value prop here? What is the use case / do you have some examples where this is simpler than current methods? Seems like I'd just select the right element in the first place with a single selector.

jhy avatar Oct 02 '14 05:10 jhy

Hi @jhy, thank you very much for the reply!

firstly, let me start of by saying I may be biased towards this as I have been using these methods in PrototypeJS for quite some time. :)

Following is an example of where I think this may benefit a lot:

Let's say i'm iterating all td elements within a document, they differ by their table classname:

Elements tds = doc.select("td");

Now I need to determine from where these td elements came, so the obvious solution would be to either select the tables with their different class names (doc.select("table[class="tab1"]")) and then iterate the td's, or to (.parent().parent()) and test. however both of these solutions require prior knowledge of the document being parsed,

With (up()), I can simply do td.up("table[class]") and do my tests.

This is a simple use-case I thought of, but since I am used to (up/down/next/previous) I can think of a lot more.

Please let me know if I can give you more info and thank you for considering this pull :)

epochcoder avatar Oct 02 '14 07:10 epochcoder

Hi @jhy, Is there any more info you might require?

I would really love to see this in the next version of jSoup :)

epochcoder avatar Feb 04 '15 12:02 epochcoder

(Closing old PRs; thanks for your interest and apologies for the late review. I don't plan to merge this; mostly because the names aren't familiar to me or I think most others, not being users of Prototype. We do have closest which I think aligns to up? And select() which is down()?. If the siblings selectors are useful we could add those too.)

jhy avatar Dec 30 '23 01:12 jhy