Skip to content

Appending a node to an Element after empty() throws an Index out of bounds #2013

@jhy

Description

@jhy
String html = "<div><p>One<p>Two<p>Three</div>";
Document doc = Jsoup.parse(html);

Element div = doc.expectFirst("div");
Element p = div.expectFirst("p");
div
    .empty()
    .appendChild(p);

Produces Index 0 out of bounds for length 0

Root cause is that after emptying, the child still has the original parent, and then on reinsert into the same original parent, the replacement / move code breaks.

Source: https://www.bennadel.com/blog/4524-jsoup-error-index-out-of-bounds-for-length.htm

Metadata

Metadata

Assignees

Labels

bugA confirmed bug, that we should fixfixedAn {bug|improvement} that has been {fixed|implemented}

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions