Skip to content

Method nextElementSibling() returns null after adding an element to a document that was cloned #951

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

Closed
khdimon opened this issue Oct 9, 2017 · 1 comment
Labels
bug A confirmed bug, that we should fix
Milestone

Comments

@khdimon
Copy link

khdimon commented Oct 9, 2017

If I clone a document, and add an element by the method after(), and try to get the new element by the method nextElementSibling() I get null. In the same time the method nextSibling() successfully returns this element.
If I do the same with the original document everything is fine.

Code:

String html = "<!DOCTYPE html><html lang=\"en\"><head></head><body><div>Initial element</div></body></html>";
Document original = Jsoup.parse(html);
Document clone = original.clone();

Element originalElement = original.body().child(0);
originalElement.after("<div>New element</div>");
Element originalNextElementSibling = originalElement.nextElementSibling();
Element originalNextSibling = (Element) originalElement.nextSibling();
System.out.println("originalNextElementSibling:\n" + originalNextElementSibling);
System.out.println("originalNextSibling:\n" + originalNextSibling);
System.out.println();

Element cloneElement = clone.body().child(0);
cloneElement.after("<div>New element</div>");
Element cloneNextElementSibling = cloneElement.nextElementSibling();
Element cloneNextSibling = (Element) cloneElement.nextSibling();
System.out.println("cloneNextElementSibling:\n" + cloneNextElementSibling);
System.out.println("cloneNextSibling:\n" + cloneNextSibling);

Output:

originalNextElementSibling:
<div>
 New element
</div>
originalNextSibling:
<div>
 New element
</div>

cloneNextElementSibling:
null
cloneNextSibling:
<div>
New element
</div>
@jhy jhy added this to the 1.11.1 milestone Oct 9, 2017
@jhy jhy closed this as completed in c324fab Oct 9, 2017
@jhy jhy added the bug A confirmed bug, that we should fix label Oct 9, 2017
@jhy
Copy link
Owner

jhy commented Oct 9, 2017

Thanks for the detail in your report. Fixed!

Joxebus pushed a commit to Joxebus/jsoup that referenced this issue Oct 9, 2017

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
This commit fix the ISSUE jhy#951

Problem: Once a document is cloned and try to append an after element the document cloned
doesn't recognize the parent and that result in a nextElementSibling equals to null.

Solution: The Element.clone() method calls Element.doClone() with self as parameter to create a new instance.

Changes:
- Element.clone calls to doClone method with self as arg
- New test case added to verify the generated objects are different and with no null values.

"Omar Bautista" <joxebus@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug A confirmed bug, that we should fix
Projects
None yet
Development

No branches or pull requests

2 participants