jsoup
jsoup copied to clipboard
Add Prototype Style DOM Navigation
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");
up/down/next/previous what can it do ?some one can tell me?
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.
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 :)
Hi @jhy, Is there any more info you might require?
I would really love to see this in the next version of jSoup :)
(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.)