Skip to content

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

Closed

Description

@MicyToy

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()

Activity

  1. added a commit that references this issue on Oct 23, 2024
  2. MicyToy commented on Oct 23, 2024

    @MicyToy
    ContributorAuthor

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

  3. added a commit that references this issue on Nov 25, 2024
  4. added this to the 1.18.2 milestone on Nov 25, 2024
  5. added a commit that references this issue on Nov 25, 2024
    1a91aac
  6. jhy commented on Nov 25, 2024

    @jhy
    Owner

    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

Metadata

Metadata

Assignees

No one assigned

    Labels

    improvementAn improvement / new feature idea

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions