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

empty node does not clear the parent of chidren #239

Closed
xzer opened this issue Sep 12, 2012 · 1 comment
Closed

empty node does not clear the parent of chidren #239

xzer opened this issue Sep 12, 2012 · 1 comment

Comments

@xzer
Copy link

xzer commented Sep 12, 2012

some source like following:

List children = parent.childrenNodes();
parent.empty();

for(Node node:children){
otherParent.appendChild(node);
}

the above source will throw a IndexOutOfBoundsException because the pre retrieved children nodes' parent is not null, further more, after I empty the parent node, node.remove() will throw the same exception due to the same reason.

I think the empty method should clear all its children's parent reference.

@jhy jhy closed this as completed in deaf9f6 Dec 23, 2012
@jhy
Copy link
Owner

jhy commented Dec 23, 2012

I've added a new method, Element#insertChildren(index, children), to make this simpler. You do not need to empty() the previous parent. If you do, the children will be emptied as well (because that list is backed by the parent list).

To move append successfully:

List<Node> children = parent.childNodes();
otherParent.insertChildren(-1, children);

zazi pushed a commit to dswarm/jsoup that referenced this issue Oct 15, 2015
Also tidied up JavaDoc, and returned Node.childNodes to a unmodifiable
list.

Fixes jhy#239 (with alternative implementation)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants