Skip to content

Element.cachedChildren() with incorrect values in 1.20.1 causes misbehavior #2334

@rgevaerd

Description

@rgevaerd

Executing the following code:

String listHtml = "<ul>" +
    "<li><h2>initial1</h2></li>" +
    "</ul>";
Document doc0 = Jsoup.parseBodyFragment(listHtml);
doc0.outputSettings().prettyPrint(false);
doc0.outputSettings().charset("UTF-8");
doc0.outputSettings().syntax(OutputSettings.Syntax.html);
 
doc0.body().children().first();
 
Document doc = doc0.clone();
doc.outputSettings().prettyPrint(false);
doc.outputSettings().charset("UTF-8");
doc.outputSettings().syntax(OutputSettings.Syntax.html);
 
Element list = doc.body().selectFirst("ul");
 
for (int i = 0; i < list.children().size(); i++)
    list.child(i).children();
 
int growthSize = 3;
 
Element firstItem = list.firstElementChild();
while (list.children().size() < growthSize)
    list.appendChild(firstItem.clone());
 
Elements listItems = list.children();
for (int i = 0; i < listItems.size(); i++)
{
    Element item = listItems.get(i);
    Element h2 = item.child(0);
    h2.text("other text " + i);
}
 
System.out.println(list.outerHtml());

In 1.19.1 outputs, correctly:
<ul><li><h2>other text 0</h2></li><li><h2>other text 1</h2></li><li><h2>other text 2</h2></li></ul>

But in 1.20.1 outputs, incorrectly:
<ul><li><h2>other text 2</h2></li><li><h2>initial1</h2></li><li><h2>initial1</h2></li></ul>

I found out that the wrong behavior in 1.20.1 is due to Element.cachedChildren() of all <li> elements are returning the <h2> of the first <li> element.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugA confirmed bug, that we should fixfixedAn {bug|improvement} that has been {fixed|implemented}

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions