fefit / visdom

A library use jQuery like API for html parsing & node selecting & node mutation, suitable for web scraping and html confusion.
MIT License
110 stars 6 forks source link

Please add support for html method for all elements #12

Closed Random-G closed 2 years ago

Random-G commented 2 years ago

Hello, I'd like to get all elements with html tags and attribute. The currently available methods are .html() and .outer_html(). However, these methods only target the first element. The .text() gets all the elements in plain text. Is there a way to achieve this purpose with html?

fefit commented 2 years ago

@Random-G the behavior of the html() and text() method just follow the jquery do, i don't know why jquery design like that, because sometimes it's indeed confusing. now if you want to get the combined html of all the elements, you may do like this:

let html = "<div><span>1<span></div><div><span>2</span></div>";
let root = Vis::load(html)?;
let divs = root.find("div");
let all_div_html = divs.map(|_, ele| ele.html()).join(""); // <span>1<span><span>2</span>

it need to write some more code, if it's a common need, maybe we can add a sugar method html_all() or other names whaterver for this purpose, do you have any good idea?

Random-G commented 2 years ago

@fefit Thank you very much for your response. html_all() or htmls() sounds good.

fefit commented 2 years ago

@Random-G htmls() sounds great! i will append the htmls() and outer_htmls() methods in the new version, thanks for your advice and feedback!

fefit commented 2 years ago

the new version v0.5.6 has published to resolve this~

Random-G commented 2 years ago

@fefit Thank you very much for the update.