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

There's null issue in org.jsoup.select.Elements#set #2212

Closed
MicyToy opened this issue Oct 12, 2024 · 2 comments
Closed

There's null issue in org.jsoup.select.Elements#set #2212

MicyToy opened this issue Oct 12, 2024 · 2 comments
Milestone

Comments

@MicyToy
Copy link
Contributor

MicyToy commented Oct 12, 2024

the new org.jsoup.select.Elements#set(int, Element) method since 1.17.1 has an issue.

this call old.replaceWith(element) method, and this method calls parentNode.replaceChild(this, in). in the replaceChild method, out's parentNode is removed out.parentNode = null;. this may cause an null issue.

eg: a parent node has 5 child elements, if I replace the 3rd element twice, eg: first replace 1 with 3, and then replace 3 with 5, then will throw an exception.

org.jsoup.helper.ValidationException: Object must not be null
	at org.jsoup.nodes.Node.replaceWith(Node.java:512)
	at org.jsoup.select.Elements.set(Elements.java:703)
	at org.jsoup.select.Elements.set(Elements.java:30)

you also can test it with Collections.shuffle()

MicyToy added a commit to MicyToy/jsoup that referenced this issue Oct 23, 2024
@MicyToy
Copy link
Contributor Author

MicyToy commented Oct 23, 2024

I've pushed a test case for this.
Issue 2212

MicyToy added a commit to MicyToy/jsoup that referenced this issue Oct 23, 2024
MicyToy added a commit to MicyToy/jsoup that referenced this issue Oct 23, 2024
jhy pushed a commit that referenced this issue Nov 25, 2024
@jhy jhy closed this as completed in 1a91aac Nov 25, 2024
@jhy jhy added this to the 1.18.2 milestone Nov 25, 2024
@jhy
Copy link
Owner

jhy commented Nov 25, 2024

Thanks, I added support for that case.

When calling set or other methods which re-parent nodes, the outgoing node is detached from the DOM by removing its parent. So now the replaceChild method tests to see if the outgoing node has a parent, and if not will fall back to trying to use the incoming node's parent. If that doesn't have a parent either, the flow will necessarily still abandon.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants
@jhy @MicyToy and others