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

fix #1411 add Element parse method #1665

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions src/main/java/org/jsoup/nodes/Element.java
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,36 @@ public Element(Tag tag, @Nullable String baseUri) {
this(tag, baseUri, null);
}

private Element() {
this("-");
}

/**
* Parse HTML String into an Element
* <p>
* Use examples:
* <ul>
* <li><code>Element e = Element.parse("<div></div>");</code></li>
* <li><code>Element e = Element.parse("<div><span>Some stuff</span><span>Second part</span></div>");</code></li>
* <li><code>Element e = Element.parse("<ul><li>item1</li><li>item2</li></ul>");</code></li>
* </ul>
* </p>
* @param html HTML to parse
* @return an Element
*/
public static Element parse(String html) {
Element dummyElement = new Element().html(html);
if (dummyElement.childNodeSize() > 1) {
throw new IllegalArgumentException("Element syntax error: Number of outer element must be one");
}
if (dummyElement.childNodeSize() == 0) {
throw new IllegalArgumentException("Element syntax error: No legal element detected");
}
Element e = dummyElement.child(0);
e.parentNode = null;
return e;
}

/**
Internal test to check if a nodelist object has been created.
*/
Expand Down
33 changes: 25 additions & 8 deletions src/test/java/org/jsoup/nodes/ElementTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -729,7 +729,7 @@ public void testWrapNoop() {

@Test
public void testWrapOnOrphan() {
Element orphan = new Element("span").text("Hello!");
Element orphan = Element.parse("<span>Hello!</span>");
assertFalse(orphan.hasParent());
Element wrapped = orphan.wrap("<div></div> There!");
assertSame(orphan, wrapped);
Expand Down Expand Up @@ -1445,8 +1445,8 @@ public void testShadowElementsAreUpdated() {
assertEquals(2, els.size()); // the two Ps
assertEquals(3, nodes.size()); // the "Three" textnode

Element p3 = new Element("p").text("P3");
Element p4 = new Element("p").text("P4");
Element p3 = Element.parse("<p>P3</p>");
Element p4 = Element.parse("<p>P4</p>");
div.insertChildren(1, p3);
div.insertChildren(3, p4);
Elements els2 = div.children();
Expand Down Expand Up @@ -1839,8 +1839,8 @@ public void doesntDeleteZWJWhenNormalizingText() {
public void testReparentSeperateNodes() {
String html = "<div><p>One<p>Two";
Document doc = Jsoup.parse(html);
Element new1 = new Element("p").text("Three");
Element new2 = new Element("p").text("Four");
Element new1 = Element.parse("<p>Three</p>");
Element new2 = Element.parse("<p>Four</p>");

doc.body().insertChildren(-1, new1, new2);
assertEquals("<div><p>One</p><p>Two</p></div><p>Three</p><p>Four</p>", TextUtil.stripNewlines(doc.body().html()));
Expand All @@ -1863,14 +1863,14 @@ public void testNotActuallyAReparent() {
String html = "<div>";
Document doc = Jsoup.parse(html);
Element div = doc.selectFirst("div");
Element new1 = new Element("p").text("One");
Element new2 = new Element("p").text("Two");
Element new1 = Element.parse("<p>One</p>");
Element new2 = Element.parse("<p>Two</p>");
div.addChildren(new1, new2);

assertEquals("<div><p>One</p><p>Two</p></div>", TextUtil.stripNewlines(div.outerHtml()));

// and the issue setup:
Element new3 = new Element("p").text("Three");
Element new3 = Element.parse("<p>Three</p>");
Element wrap = new Element("nav");
wrap.addChildren(0, new1, new3);

Expand Down Expand Up @@ -2099,4 +2099,21 @@ public void childNodesAccessorDoesNotVivify() {
p.removeAttr("foo");
assertEquals(0, p.attributesSize());
}

@Test void parseFromHtmlOneElementSuccess() {
Element e = Element.parse("<div><text>test</text></div>");
assertEquals(e.tagName(), "div");
assertEquals(e.child(0).tagName(), "text");
assertEquals(e.child(0).text(), "test");
}

@Test void parseFromHtmlMultiElementsError() {
Throwable exception = assertThrows(IllegalArgumentException.class, () -> Element.parse("<li>item1</li><li>item2</li>"));
assertEquals("Element syntax error: Number of outer element must be one", exception.getMessage());
}

@Test void parseFromHtmlNoElementError() {
Throwable exception = assertThrows(IllegalArgumentException.class, () -> Element.parse(""));
assertEquals("Element syntax error: No legal element detected", exception.getMessage());
}
}