GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — NodeTraversor revisits nodes created in head() causing infinite loop when mutating (since 1.21.2) · Issue #2472 · jhy/jsoup

NodeTraversor revisits nodes created in head() causing infinite loop when mutating (since 1.21.2) #2472

New issue
Copy link
New issue
Copy link
Closed
Closed
NodeTraversor revisits nodes created in head() causing infinite loop when mutating (since 1.21.2)#2472
Copy link
Assignees
jhy
Labels
bugA confirmed bug, that we should fixfixedAn {bug|improvement} that has been {fixed|implemented}improvementAn improvement / new feature idea
Milestone
1.22.2

Description

@kna
kna
opened on Mar 3, 2026
Issue body actions

Since 1.21.2, this code will enter an infinite loop because of 967306f .

public class Main {
    public static void main(String[] args) {
        Document document = Jsoup.parseBodyFragment("<div>a</div>");
        document.traverse(new NodeVisitor() {
            @Override
            public void head(Node node, int depth) {
                if (node instanceof TextNode) {
                    Node newNode = new TextNode("b");

                    node.replaceWith(newNode);
                    // or
                    // node.before(newNode);
                    // node.remove();
                    // or
                    // node.after(newNode);
                    // node.remove();
                }
            }
        });
    }
}

head() visits new nodes created in head() and it causes infinite loop.
It might be OK to revisit a new node to deal with it.

But this behaviour have some problems:

  • you can't use replaceWith() in head()
  • you can use before() without remove(), but not with remove()

I'm not sure if this is a bug or not. If isn't, please close this.

Activity

  1. jhy
    self-assigned this
    on Mar 4, 2026
  2. jhy
    closed this as completedin b6fabf8on Mar 6, 2026
  3. jhy commented on Mar 6, 2026

    @jhy
    jhy
    on Mar 6, 2026
    Owner
    More actions

    Thanks for the clear report and regression analysis! Yes, this was a bug.

    The change in 1.21.2 for #2355 made the traversor re-enter head() on whatever node ended up in the current sibling position after a replaceWith(), before()+remove(), or after()+remove(). In cases like your example, that could loop indefinitely.

    I've fixed that by refactoring the traversal path into a clearer forward-cursor walk. head() is called once for the node at the current cursor position; if that position is replaced, traversal continues from there without calling head() on the replacement again. That fixes the loop and makes the mutation path much easier to reason about.

    I also added regression tests and tightened the Javadocs to spell out the supported contract:

    • structural changes to the current node are supported during head()
    • structural changes to the current node are not supported during tail()
    • nodes inserted before the current cursor are not visited in the same traversal
    • children inserted under the current node, and later siblings, may still be visited

    Please go ahead and test with a snapshot install and confirm that it's fixed for you.

    Reacted by Naoki Kawamukai
  4. jhy
    added
    bugA confirmed bug, that we should fix
    fixedAn {bug|improvement} that has been {fixed|implemented}
    on Mar 6, 2026
  5. jhy
    added this to the 1.22.2 milestone on Mar 6, 2026
  6. pull
    added a commit that references this issue on Mar 6, 2026

    Refactor NodeTraversor.traverse to have a clearer state machine impl

    This issue will close once commit b6fabf8 is merged into the 'master' branch.
    b6fabf8
  7. jhy commented on Mar 6, 2026

    @jhy
    jhy
    on Mar 6, 2026
    Owner
    More actions

    (Reworked the structural mod detection to support more cases like unwrap etc, in a96388d)

  8. kna commented on Mar 6, 2026

    @kna
    kna
    on Mar 6, 2026
    Author
    More actions

    @jhy
    Thank you so much! I'll check it with my code next week.

    Reacted by Jonathan Hedley
  9. jhy
    added a commit that references this issue on Mar 8, 2026

    Update CHANGES.md for NodeTraversor fixes

    c1d0812
  10. jhy commented on Mar 8, 2026

    @jhy
    jhy
    on Mar 8, 2026
    Owner
    More actions

    Just to detail this a bit more, it ended up being a bit broader than just the loop fix.

    NodeTraversor now has a clearer and more capable mutation contract during head(): current-node edits like remove(), replaceWith(), and unwrap() recover from the correct cursor position, exposed children and later siblings may still be visited when they are still ahead of the traversal, and traversal stays within the original root subtree.

    That makes single-pass DOM rewrite visitors more predictable and easier to reason about, beyond fixing the 1.21.2 regression that could loop when replacing the current node.

  11. jhy
    added
    improvementAn improvement / new feature idea
    on Mar 8, 2026
  12. kna commented on Mar 9, 2026

    @kna
    kna
    on Mar 9, 2026
    Author
    More actions

    @jhy
    I've checked jsoup on master branch with my code and it works well. Thank you.

    Reacted by Jonathan Hedley
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

  • @jhy
    jhy

Labels

bugA confirmed bug, that we should fixfixedAn {bug|improvement} that has been {fixed|implemented}improvementAn improvement / new feature idea

Type

No type

Projects

No projects

    Milestone

    • 1.22.2

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

    You are visitor 01402792 — GeezerVison does not store the pages you look up.
    Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.