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

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

Closed
jhy opened this issue Oct 18, 2023 · 2 comments
Closed
Assignees
Labels
bug Confirmed bug that we should fix fixed
Milestone

Comments

@jhy
Copy link
Owner

jhy commented Oct 18, 2023

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

@jhy jhy added the bug Confirmed bug that we should fix label Oct 18, 2023
@jhy jhy self-assigned this Oct 18, 2023
@jhy jhy closed this as completed in eff1521 Oct 18, 2023
@jhy jhy added this to the 1.16.2 milestone Oct 18, 2023
@jhy
Copy link
Owner Author

jhy commented Oct 18, 2023

Fixed by explicitly clearing the parent node from the removed children.

@jhy jhy added the fixed label Oct 18, 2023
@bennadel
Copy link

Awesome stuff, thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Confirmed bug that we should fix fixed
Projects
None yet
Development

No branches or pull requests

2 participants