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

Element factory method #1411

Open
kenyons opened this issue Jul 10, 2020 · 4 comments
Open

Element factory method #1411

kenyons opened this issue Jul 10, 2020 · 4 comments

Comments

@kenyons
Copy link

kenyons commented Jul 10, 2020

This is a feature request for a more concise way of creating Elements objects directly from an HTML String. Right now, especially in unit tests, we have lots of code that looks like this:

Element e = new Element("div").html("<span>Some stuff</span><span>Second part</span>");

It would be very nice to have something more like this:

Element e = Element.of("<div><span>Some stuff</span><span>Second part</span></div>")

Or alternatively have a parser method like

Element e = JSoup.parseElement("<div></div>");

This is not only more concise, it would allow easier dynamic creation of mock elements for testing from a data file or list of HTML strings.

@jhy
Copy link
Owner

jhy commented Dec 11, 2020

Hi -- this makes sense and I think is a good idea.

One issue with them being static methods (Element.of or Jsoup.parseElement) is that HTML parse settings cannot be applied or inherited from the current doc.

Maybe we can have it like document.parseElement(html), like document.createElement.

Maybe both that and Element.of(html)

suarez12138 added a commit to suarez12138/jsoup that referenced this issue Apr 24, 2021
@xiaoxianma
Copy link

To be consistent with Jsoup.parse, we may want a function named Element.parse(String html) instead of Element.of

@xiaoxianma
Copy link

@jhy would you help review my pr? Appreciate!

@jhy
Copy link
Owner

jhy commented Dec 19, 2021

@xiaoxianma thanks - please see my comments on #1521. Similar thoughts on this.

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

Successfully merging a pull request may close this issue.

3 participants