Backtestsjsouprun-20261006-181938#2601

Issue #2601 Merged fix PR #2602 Run 6 Oct 2026 · run-20261006-181938 jhy/jsoup

Fix the adoption agency algo

Worse

The merged fix was judged better than our patch.

In this judgment, Patch A = our agent, Patch B = the merged fix.

Both patches fix the same core problems in nearly the same way. Each puts a synthetic html element at the bottom of the stack instead of the context copy. Each routes the adoption agency's step 14 through the 'appropriate place' insertion with fostering. Each walks the stack so a template more recent than the last table receives fostered content. Each saves and restores the foster flag around nested InTable processing, which fixes the consecutive-paragraph case. Each ignores an errant </col> in column-group mode, sends column-group EOF through InBody handling, and uses the adjusted current node for namespace and CDATA checks.

The key difference is what fragment output looks like. Patch A detaches the context copy and returns the synthetic html root's children. Parsed fragment nodes therefore now have an <html> element as their parent, and streaming emits 'html' instead of the context tag. Patch A had to rewrite existing tests to accept this (parseFragmentOnCreatedDocument, nestedPFragments, canStreamFragment, canIterateFragment). That is a user-visible behaviour change the issue never asked for.

Patch B keeps the context copy as the visible result container in the document. It redirects insertions aimed at the hidden root into that container, covering elements, text, comments and fostered nodes. It also maps the root to the container in onNodeInserted, onNodeClosed, isOpen and copyOpenElementsTo. Streamed nodes therefore sit in the context-named container from the moment they are inserted, which is exactly what the issue describes. Existing parent expectations remain valid, and Patch B removes only the one test line whose expectation the issue explicitly calls wrong (the nested tr). It also keeps DataNode handling for script contexts by redirecting to the context before checking the tag.

Patch B's tests are broader on the issue's scenarios:
- parameterized adoption cases for tbody, thead, tfoot and tr, plus template-in-table and table-in-template orderings, run in both document and fragment form;
- live streaming parentage checks for p and table contexts;
- stop() behaviour;
- a check that real column groups still close correctly.

Patch A has two advantages:
- It guards against pos == 0 in the stack-parent fallback; Patch B's stack.get(pos - 1) would throw there, though that state is practically unreachable.
- It tightens the body/frameset fragment checks, an adjacent fix outside the issue's ask.

These do not outweigh the API regression in Patch A's fragment container. Patch B is the better fix.

The judge saw only the two patches, labelled A and B in random order, with issue links and version numbers removed. The reasoning above is shown verbatim.

Solve

24m 55s

Wall-clock time in the container

Full test suite

No new failures

2,246 passed · 0 skipped · 2,246 total

Contamination audit

Clean

No sign the agent saw the real fix

Patch size

324vs 377 merged

Changed lines, ours vs merged

There are a few related issues in table parsing, affecting both full documents and fragments.

The adoption agency algorithm moves blocks into a table instead of applying foster parenting.
For example:

<table><b><p>X</b>

Currently produces:

<b></b><table><p><b>X</b></p></table>

Expected:

<b></b><p><b>X</b></p><table></table>

Consecutive paragraphs also lose foster parenting when the second paragraph implicitly closes the first:

<table><tr><p id=first><p id=second>

Both paragraphs should appear before the table, in input order, but the second ends up inside the row. These cases also affect table sections and fragment parsing.

Also foster parenting needs to respect an intervening template instead of moving content outside the older table.

And: column-group fragments can incorrectly close their parsing context. Setting either of these as a colgroup element’s HTML should produce just <col id="kept">, without introducing row or column-group wrappers:

<tr><col id=kept>
</colgroup><col id=kept>

Also an errant </col> incorrectly closes the current column group, splitting subsequent columns into another group. EOF handling in column-group mode also needs to follow the normal body/template cleanup rules.

The root cause is that fragment parsing currently uses a copy of the context element as its stack root.
That can introduce redundant structure. For example, setting a tr element’s HTML to <tr><td>One</td></tr> nests another row instead of producing just <td>One</td>. It can also leave parsed nodes outside the result container during streaming, only moving them back at fragment completion.

The two patches

Our patch+256 −68
CHANGES.mdindex 14e8f85b..67d273c5 100644@@ -10,6 +10,12 @@ * Standardized parser normalization of tag and attribute names so that HTML comparisons use ASCII-only case folding, and accepted control characters are preserved, aligning name handling to the HTML and XML specs. [#2594](https://github.com/jhy/jsoup/issues/2594) * Named character references without a semicolon before `-` or `_` now decode correctly in attribute values, matching HTML and browser behavior (e.g., `&copy-` becomes `©-`). [#2588](https://github.com/jhy/jsoup/issues/2588) * Support downloads > 2GB via `Response.bodyStream()` when `maxBodySize(0)` is configured. [#2593](https://github.com/jhy/jsoup/issues/2593)+* Fixed table foster parenting in the HTML parser, for both documents and fragments:+  * The adoption agency algorithm now fosters out of the table, rather than moving the block into the table. E.g. `<table><b><p>X</b>` now produces `<b></b><p><b>X</b></p><table></table>`.+  * Consecutive paragraphs (e.g. `<table><tr><p id=first><p id=second>`) are now both fostered, in input order.+  * Foster parenting now respects an intervening `template`.+  * An errant `</col>` no longer closes the current column group, and EOF in a column group follows the normal body and template cleanup.+  * Fragment parsing now uses a synthetic `html` stack root, rather than a copy of the context element. So the context can't be closed or re-nested by its content. E.g. setting a `tr`'s HTML to `<tr><td>One</td></tr>` now produces just `<td>One</td>`. Setting a `colgroup`'s HTML to `<tr><col>` now produces just `<col>`. Streamed fragment nodes now stay within the result container.  ## 1.23.2 (2026-Aug-26) src/main/java/org/jsoup/parser/HtmlTreeBuilder.javaindex e545c632..a9e526e8 100644@@ -41,7 +41,8 @@ public class HtmlTreeBuilder extends TreeBuilder {     private boolean baseUriSetFromDoc;     private @Nullable Element headElement; // the current head element     private @Nullable FormElement formElement; // the current form element-    private @Nullable Element contextElement; // fragment parse root; shallow copy of context, may be null during fragment parsing+    private @Nullable Element contextElement; // fragment context; detached shallow copy of context, not on the stack. may be null during fragment parsing+    private @Nullable Element fragmentRoot; // fragment parse root; the synthetic html element at the bottom of the stack     ArrayList<Element> formattingElements; // active (open) formatting elements     private ArrayList<HtmlTreeBuilderState> tmplInsertMode; // stack of Template Insertion modes     private @Nullable NoscriptState noscriptState; // active noscript island state@@ -72,6 +73,7 @@ public class HtmlTreeBuilder extends TreeBuilder {         headElement = null;         formElement = null;         contextElement = null;+        fragmentRoot = null;         formattingElements = new ArrayList<>();         tmplInsertMode = new ArrayList<>();         noscriptState = null;@@ -120,8 +122,12 @@ public class HtmlTreeBuilder extends TreeBuilder {                     break;             }             tokeniser.transition(contextState);-            doc.appendChild(contextElement);-            push(contextElement);+            // per spec, the stack root is a synthetic html element; the context is only consulted when resetting the+            // insertion mode, and as the adjusted current node. So the context can't be closed or fostered into.+            Element root = new Element(tagFor("html", "html", NamespaceHtml, settings), baseUri);+            fragmentRoot = root;+            doc.appendChild(root);+            push(root);             resetInsertionMode();              // setup form element to nearest form on context (up ancestor chain). ensures form controls are associated@@ -135,19 +141,13 @@ public class HtmlTreeBuilder extends TreeBuilder {                 formSearch = formSearch.parent();             } -            if (htmlContext && contextName.equals("noscript")) enterNoscript(contextElement);+            if (htmlContext && contextName.equals("noscript")) enterNoscript(root);         }     }      @Override List<Node> completeParseFragment() {-        if (contextElement != null) {-            // depending on context and the input html, content may have been added outside of the root el-            // e.g. context=p, input=div, the div will have been pushed out.-            List<Node> nodes = contextElement.siblingNodes();-            if (!nodes.isEmpty())-                contextElement.insertChildren(-1, nodes);-            return contextElement.childNodes();-        }+        if (fragmentRoot != null)+            return fragmentRoot.childNodes(); // all fragment content is inserted within the root, including fostered nodes         else             return doc.childNodes();     }@@ -220,7 +220,7 @@ public class HtmlTreeBuilder extends TreeBuilder {     private boolean closeNoscriptEndTag(Token.EndTag end) {         String name = end.normalName();         NoscriptState island = Validate.expectNotNull(noscriptState, "Bug: noscript end tag processed with no island state");-        if (name.equals("noscript") && island.boundary != contextElement) {+        if (name.equals("noscript") && island.boundary != fragmentRoot) {             endNoscript();             return true;         }@@ -239,7 +239,7 @@ public class HtmlTreeBuilder extends TreeBuilder {         // If the stack of open elements is empty         if (stack.isEmpty())             return true;-        final Element el = currentElement();+        final Element el = adjustedCurrentElement();         final String ns = el.tag().namespace();          // If the adjusted current node is an element in the HTML namespace@@ -305,6 +305,17 @@ public class HtmlTreeBuilder extends TreeBuilder {         return Parser.NamespaceSvg.equals(el.tag().namespace()) && StringUtil.in(el.tagName(), TagSvgHtmlIntegration);     } +    /**+     Gets the adjusted current node: the context element if this is a fragment parse and only the root is open;+     otherwise the current element. The stack must not be empty.+     */+    @Override+    Element adjustedCurrentElement() {+        if (contextElement != null && stack.size() == 1)+            return contextElement;+        return currentElement();+    }+     boolean process(Token token, HtmlTreeBuilderState state) {         return state.process(token, this);     }@@ -462,12 +473,7 @@ public class HtmlTreeBuilder extends TreeBuilder {         if (parser.getErrors().canAddError() && el.hasAttr("xmlns") && !el.attr("xmlns").equals(el.tag().namespace()))             error("Invalid xmlns attribute [%s] on tag [%s]", el.attr("xmlns"), el.tagName()); -        Element target = currentElOrDoc();-        if (isFosterInserts() && StringUtil.inSorted(target.normalName(), InTableFoster))-            insertInFosterParent(el);-        else-            target.appendChild(el);-+        insertAtAppropriatePlace(el, currentElOrDoc());         push(el);     } @@ -509,7 +515,7 @@ public class HtmlTreeBuilder extends TreeBuilder {          if (characterToken.isCData())             node = new CDataNode(data);-        else if (el.tag().is(Tag.Data))+        else if (textTagFor(el).is(Tag.Data))             node = new DataNode(data);         else             node = new TextNode(data);@@ -517,6 +523,11 @@ public class HtmlTreeBuilder extends TreeBuilder {         onNodeInserted(node);     } +    /** Gets the tag that governs text inserted into the element; the fragment root defers to the context. */+    private Tag textTagFor(Element el) {+        return el == fragmentRoot && contextElement != null ? contextElement.tag() : el.tag();+    }+     ArrayList<Element> getStack() {         return stack;     }@@ -530,12 +541,6 @@ public class HtmlTreeBuilder extends TreeBuilder {         return getFromStack(elName) != null;     } -    /** Checks if there is an HTML element with the given name above the synthetic fragment context. */-    boolean onStackAboveContext(String elName) {-        Element el = getFromStack(elName);-        return el != null && el != contextElement;-    }-     /** Gets the nearest (lowest) HTML element with the given name from the stack. */     @Nullable     Element getFromStack(String elName) {@@ -1135,26 +1140,39 @@ public class HtmlTreeBuilder extends TreeBuilder {         formattingElements.add(null);     } +    /**+     Inserts the node at the foster parenting location: into the last template if it is lower in the stack than the+     last table; otherwise before the last table, or into the element above it on the stack when the table has no+     parent; or into the root when there is no table (fragment case).+     https://html.spec.whatwg.org/multipage/parsing.html#appropriate-place-for-inserting-a-node+     */     void insertInFosterParent(Node in) {-        Element fosterParent;-        Element lastTable = getFromStack("table");-        boolean isLastTableParent = false;-        if (lastTable != null) {-            if (lastTable.parent() != null) {-                fosterParent = lastTable.parent();-                isLastTableParent = true;-            } else-                fosterParent = aboveOnStack(lastTable);-        } else { // no table == frag-            fosterParent = stack.get(0);+        for (int pos = stack.size() - 1; pos >= 0; pos--) {+            Element el = stack.get(pos);+            if (el.elementIs("template", NamespaceHtml)) {+                el.appendChild(in); // template contents are the template's children+                return;+            }+            if (el.elementIs("table", NamespaceHtml)) {+                if (el.parent() != null)+                    el.before(in);+                else+                    stack.get(pos > 0 ? pos - 1 : 0).appendChild(in);+                return;+            }         }+        stack.get(0).appendChild(in); // no table == frag+    } -        if (isLastTableParent) {-            Validate.notNull(lastTable); // last table cannot be null by this point.-            lastTable.before(in);-        }+    /**+     Inserts a node at the appropriate place, using the supplied override target: fosters the node if foster parenting+     is enabled and the target is a table element; otherwise appends it to the target.+     */+    void insertAtAppropriatePlace(Node in, Element target) {+        if (isFosterInserts() && target.tag().namespace().equals(NamespaceHtml) && inSorted(target.normalName(), InTableFoster))+            insertInFosterParent(in);         else-            fosterParent.appendChild(in);+            target.appendChild(in);     }      // Template Insertion Mode stacksrc/main/java/org/jsoup/parser/HtmlTreeBuilderState.javaindex 09b27bfd..e603d05e 100644@@ -165,7 +165,7 @@ enum HtmlTreeBuilderState {                     } else if (inSorted(name, Constants.InHeadEnd)) {                         return anythingElse(t, tb);                     } else if (name.equals("template")) {-                        if (!tb.onStackAboveContext(name)) {+                        if (!tb.onStack(name)) {                             tb.error(this);                         } else {                             tb.generateImpliedEndTags(true);@@ -345,7 +345,7 @@ enum HtmlTreeBuilderState {                 case "body":                     tb.error(this);                     stack = tb.getStack();-                    if (stack.size() < 2 || (stack.size() > 2 && !stack.get(1).nameIs("body")) || tb.onStack("template")) {+                    if (stack.size() < 2 || !stack.get(1).nameIs("body") || tb.onStack("template")) {                         // only in fragment case                         return false; // ignore                     } else {@@ -358,7 +358,7 @@ enum HtmlTreeBuilderState {                 case "frameset":                     tb.error(this);                     stack = tb.getStack();-                    if (stack.size() < 2|| (stack.size() > 2 && !stack.get(1).nameIs("body"))) {+                    if (stack.size() < 2 || !stack.get(1).nameIs("body")) {                         // only in fragment case                         return false; // ignore                     } else if (!tb.framesetOk()) {@@ -930,9 +930,7 @@ enum HtmlTreeBuilderState {                 } // end inner loop # 13                  // 14. Insert whatever lastNode ended up being in the previous step at the [appropriate place for inserting a node], but using commonAncestor as the _override target_.-                // todo - impl https://html.spec.whatwg.org/multipage/parsing.html#appropriate-place-for-inserting-a-node fostering-                // just use commonAncestor as target:-                commonAncestor.appendChild(lastEl);+                tb.insertAtAppropriatePlace(lastEl, commonAncestor); // fosters if the common ancestor is a table element                 // 15. [Create an element for the token] for which formattingElement was created, in the [HTML namespace], with furthestBlock as the intended parent.                 Element adoptor = tb.recreateElement(formatEl);                 // 16. Take all of the child nodes of furthestBlock and append them to the element created in the last step.@@ -1064,18 +1062,18 @@ enum HtmlTreeBuilderState {                 }                 return true; // todo: as above todo             } else if (t.isEOF()) {-                if (tb.currentElementIs("html"))-                    tb.error(this);-                return true; // stops parsing+                return tb.process(t, InBody); // checks for errors, closes any templates, and stops parsing             }             return anythingElse(t, tb);         }          boolean anythingElse(Token t, HtmlTreeBuilder tb) {             tb.error(this);+            // restore (not clear) after, as nested processing (e.g. an implied </p>) may run through here too+            boolean wasFostering = tb.isFosterInserts();             tb.setFosterInserts(true);             tb.process(t, InBody);-            tb.setFosterInserts(false);+            tb.setFosterInserts(wasFostering);             return true;         }     },@@ -1093,9 +1091,10 @@ enum HtmlTreeBuilderState {                             // InTable anything else section:                             tb.error(this);                             if (inSorted(tb.currentElement().normalName(), InTableFoster)) {+                                boolean wasFostering = tb.isFosterInserts();                                 tb.setFosterInserts(true);                                 tb.process(c, InBody);-                                tb.setFosterInserts(false);+                                tb.setFosterInserts(wasFostering);                             } else {                                 tb.process(c, InBody);                             }@@ -1189,6 +1188,9 @@ enum HtmlTreeBuilderState {                                 tb.transition(InTable);                             }                             break;+                        case "col":+                            tb.error(this); // ignore; does not close the column group+                            return false;                         case "template":                             tb.process(t, InHead);                             break;@@ -1197,10 +1199,7 @@ enum HtmlTreeBuilderState {                     }                     break;                 case EOF:-                    if (tb.currentElementIs("html"))-                        return true; // stop parsing; frag case-                    else-                        return anythingElse(t, tb);+                    return tb.process(t, InBody); // checks for errors, closes any templates, and stops parsing                 default:                     return anythingElse(t, tb);             }@@ -1579,7 +1578,7 @@ enum HtmlTreeBuilderState {                     }                     break;                 case EOF:-                    if (!tb.onStackAboveContext("template")) { // stop parsing+                    if (!tb.onStack("template")) { // stop parsing                         return true;                     }                     tb.error(this);@@ -1775,7 +1774,7 @@ enum HtmlTreeBuilderState {                      // Any other start:                     // (whatwg says to fix up tag name and attribute case per a table - we will preserve original case instead)-                    String namespace = tb.currentElement().tag().namespace();+                    String namespace = tb.adjustedCurrentElement().tag().namespace();                     tb.insertForeignElementFor(start, namespace);                     // (self-closing handled in insert)                     // if self-closing svg script -- level and execution elidedsrc/main/java/org/jsoup/parser/Tokeniser.javaindex 39e7748d..5832e658 100644@@ -265,7 +265,7 @@ final class Tokeniser {     /** Test if CDATA sections are allowed at the adjusted current node. */     boolean isCdataAllowed() {         return syntax == Document.OutputSettings.Syntax.xml-            || (treeBuilder.hasCurrentElement() && !Parser.NamespaceHtml.equals(treeBuilder.currentElement().tag().namespace()));+            || (treeBuilder.hasCurrentElement() && !Parser.NamespaceHtml.equals(treeBuilder.adjustedCurrentElement().tag().namespace()));     }      /** Test if the pending end tag matches the last emitted start tag. */src/main/java/org/jsoup/parser/TreeBuilder.javaindex e2db0b0c..02da4ed2 100644@@ -231,6 +231,14 @@ abstract class TreeBuilder {         return stack.get(stack.size() - 1);     } +    /**+     Gets the adjusted current node, for namespace-dependent decisions. Defaults to the current element; the HTML+     builder substitutes the fragment context when only the fragment root is open. The stack must not be empty.+     */+    Element adjustedCurrentElement() {+        return currentElement();+    }+     /**      Tests if there is a current open element.      */src/test/java/org/jsoup/parser/HtmlParserTest.javaindex 4db0c738..3b4daa9f 100644@@ -1920,7 +1920,141 @@ public class HtmlParserTest {         // https://github.com/jhy/jsoup/issues/1603         Element element = new Element("tr");         element.html("<tr><td>One</td></tr>");-        assertEquals("<tr>\n <tr>\n  <td>One</td>\n </tr>\n</tr>", element.outerHtml());+        assertEquals("<tr>\n <td>One</td>\n</tr>", element.outerHtml()); // the context row is not repeated+    }++    @Test void fragmentContextIsNotOnStack() {+        // the context isn't the stack root, so table / select / frameset / body content can't close or re-nest it+        Element tbody = new Element("tbody");+        tbody.html("<tbody><tr><td>One</td></tr></tbody>");+        assertEquals("<tbody><tr><td>One</td></tr></tbody>", TextUtil.stripNewlines(tbody.outerHtml()));++        Element table = new Element("table");+        table.html("<table><tr><td>One</td></tr></table><p>Two</p>");+        assertEquals("<table><tbody><tr><td>One</td></tr></tbody><p>Two</p></table>", // no table in scope, so <table> ignored+            TextUtil.stripNewlines(table.outerHtml()));++        Element select = new Element("select");+        select.html("</select><option>One");+        assertEquals("<select><option>One</option></select>", TextUtil.stripNewlines(select.outerHtml()));+    }++    @Test void fragmentIgnoresFramesetAndBodyWithoutBodyContext() {+        // the second stack element isn't body in a fragment, so frameset and body start tags are ignored+        Element body = new Element("body");+        body.html("<div><frameset><p>a");+        assertEquals("<div><p>a</p></div>", TextUtil.stripNewlines(body.html()));++        Element div = new Element("div");+        div.html("<span><body id=x><frameset><frame>");+        assertEquals("<span></span>", TextUtil.stripNewlines(div.html()));+        assertFalse(div.hasAttr("id"));+    }++    @Test void fragmentKeepsDataContext() {+        // text in a data context (e.g. script) is still data, though the stack root is the synthetic html element+        Element script = new Element("script");+        script.html("if (a < b) {}");+        assertInstanceOf(DataNode.class, script.childNode(0));+        assertEquals("if (a < b) {}", script.data());+    }++    @Test void adoptionAgencyFostersFurthestBlockOutOfTable() {+        // the furthest block is fostered out of the table, rather than appended into it+        Document doc = Jsoup.parse("<table><b><p>X</b>");+        assertEquals("<b></b><p><b>X</b></p><table></table>", TextUtil.stripNewlines(doc.body().html()));++        doc = Jsoup.parse("<table><tbody><b><p>X</b>");+        assertEquals("<b></b><p><b>X</b></p><table><tbody></tbody></table>", TextUtil.stripNewlines(doc.body().html()));++        doc = Jsoup.parse("<table><tr><b><p>X</b>");+        assertEquals("<b></b><p><b>X</b></p><table><tbody><tr></tr></tbody></table>", TextUtil.stripNewlines(doc.body().html()));+    }++    @Test void adoptionAgencyFostersInFragments() {+        for (String context : new String[]{"table", "tbody", "tr"}) {+            Element el = new Element(context);+            el.html("<b><p>X</b>");+            assertEquals("<b></b><p><b>X</b></p>", TextUtil.stripNewlines(el.html()), context);+        }++        Element div = new Element("div");+        div.html("<table><b><p>X</b>");+        assertEquals("<b></b><p><b>X</b></p><table></table>", TextUtil.stripNewlines(div.html()));+    }++    @Test void consecutiveParagraphsAreFosteredInOrder() {+        // the second p implies </p>, which must not end foster parenting for the second p+        Document doc = Jsoup.parse("<table><tr><p id=first><p id=second>");+        assertEquals("<p id=\"first\"></p><p id=\"second\"></p><table><tbody><tr></tr></tbody></table>",+            TextUtil.stripNewlines(doc.body().html()));++        doc = Jsoup.parse("<table><tbody><p id=first><p id=second>");+        assertEquals("<p id=\"first\"></p><p id=\"second\"></p><table><tbody></tbody></table>",+            TextUtil.stripNewlines(doc.body().html()));++        doc = Jsoup.parse("<table><p id=first><p id=second>");+        assertEquals("<p id=\"first\"></p><p id=\"second\"></p><table></table>",+            TextUtil.stripNewlines(doc.body().html()));++        doc = Jsoup.parse("<div><table><tr><p id=first><p id=second></table></div>");+        assertEquals("<div><p id=\"first\"></p><p id=\"second\"></p><table><tbody><tr></tr></tbody></table></div>",+            TextUtil.stripNewlines(doc.body().html()));+    }++    @Test void consecutiveParagraphsAreFosteredInOrderInFragments() {+        for (String context : new String[]{"table", "tbody", "tr"}) {+            Element el = new Element(context);+            el.html("<p id=first><p id=second>");+            assertEquals("<p id=\"first\"></p><p id=\"second\"></p>", TextUtil.stripNewlines(el.html()), context);+        }++        Element div = new Element("div");+        div.html("<table><tr><p id=first><p id=second>");+        assertEquals("<p id=\"first\"></p><p id=\"second\"></p><table><tbody><tr></tr></tbody></table>",+            TextUtil.stripNewlines(div.html()));+    }++    @Test void fosterParentingRespectsInterveningTemplate() {+        // a template more recent than the last table holds the fostered content+        Document doc = Jsoup.parse("<table><template><tr><p>One</p></template></table>");+        assertEquals("<table><template><tr></tr><p>One</p></template></table>", TextUtil.stripNewlines(doc.body().html()));++        doc = Jsoup.parse("<table><template><tr><b><p>X</b></template></table>");+        assertEquals("<table><template><tr></tr><b></b><p><b>X</b></p></template></table>",+            TextUtil.stripNewlines(doc.body().html()));++        // but a table within the template is fostered within the template+        doc = Jsoup.parse("<table><template><table><tr><p>One</p></table></template></table>");+        assertEquals("<table><template><p>One</p><table><tbody><tr></tr></tbody></table></template></table>",+            TextUtil.stripNewlines(doc.body().html()));+    }++    @Test void colgroupFragmentDoesNotCloseContext() {+        Element colgroup = new Element("colgroup");+        colgroup.html("<tr><col id=kept>");+        assertEquals("<col id=\"kept\">", colgroup.html());++        colgroup = new Element("colgroup");+        colgroup.html("</colgroup><col id=kept>");+        assertEquals("<col id=\"kept\">", colgroup.html());+    }++    @Test void errantColEndTagDoesNotCloseColgroup() {+        Document doc = Jsoup.parse("<table><colgroup><col id=1></col><col id=2></table>");+        assertEquals("<table><colgroup><col id=\"1\"><col id=\"2\"></colgroup></table>",+            TextUtil.stripNewlines(doc.body().html()));++        Element colgroup = new Element("colgroup");+        colgroup.html("<col id=1></col><col id=2>");+        assertEquals("<col id=\"1\"><col id=\"2\">", TextUtil.stripNewlines(colgroup.html()));+    }++    @Test void columnGroupEofClosesTemplates() {+        // EOF in a template's column group closes the template and continues to the body, as in body mode+        Document doc = Jsoup.parse("<template><colgroup><col>");+        assertEquals("<html><head><template><colgroup><col></colgroup></template></head><body></body></html>",+            TextUtil.stripNewlines(doc.html()));     }      @Test public void parseFragmentOnCreatedDocument() {@@ -1930,7 +2064,7 @@ public class HtmlParserTest {         assertEquals(1, nodes.size());         Node node = nodes.get(0);         assertEquals("h2", node.nodeName());-        assertEquals("<p>\n <h2>text</h2>\n</p>", node.parent().outerHtml());+        assertEquals("<html>\n <h2>text</h2>\n</html>", node.parent().outerHtml()); // parsed within the synthetic html root     }      @Test public void nestedPFragments() {@@ -1939,7 +2073,7 @@ public class HtmlParserTest {         List<Node> nodes = new Document("").parser().parseFragmentInput(bareFragment, new Element("p"), "");         assertEquals(2, nodes.size());         Node node = nodes.get(0);-        assertEquals("<p><p></p><a></a></p>", TextUtil.stripNewlines(node.parent().outerHtml())); // mis-nested because fragment forced into the element, OK+        assertEquals("<html><p></p><a></a></html>", TextUtil.stripNewlines(node.parent().outerHtml())); // parsed within the synthetic html root     }      @Test public void nestedAnchorAdoption() {src/test/java/org/jsoup/parser/StreamParserTest.javaindex f5ee7c87..be2b6cb8 100644@@ -622,9 +622,9 @@ class StreamParserTest {         try (StreamParser parser = new StreamParser(Parser.htmlParser()).parseFragment(html, context, "")) {             StringBuilder seen = new StringBuilder();             parser.stream().forEachOrdered(el -> trackSeen(el, seen));-            assertEquals("td[One];tr#1+;td[Two];tr#2+;td[Three];tr#3;tbody;table;#root;", seen.toString());+            assertEquals("td[One];tr#1+;td[Two];tr#2+;td[Three];tr#3;tbody;html;#root;", seen.toString());             // checks expected order, and the + indicates that element had a next sibling at time of emission-            // note that we don't get a full doc, just the fragment (and the context at the end of the stack)+            // note that we don't get a full doc, just the fragment (and the synthetic html root at the end of the stack)              assertTrue(isClosed(parser)); // as read to completion         }@@ -643,9 +643,9 @@ class StreamParserTest {                 trackSeen(it.next(), seen);             } -            assertEquals("td[One];tr#1+;td[Two];tr#2+;td[Three];tr#3;tbody;table;#root;", seen.toString());+            assertEquals("td[One];tr#1+;td[Two];tr#2+;td[Three];tr#3;tbody;html;#root;", seen.toString());             // checks expected order, and the + indicates that element had a next sibling at time of emission-            // note that we don't get a full doc, just the fragment (and the context at the end of the stack)+            // note that we don't get a full doc, just the fragment (and the synthetic html root at the end of the stack)              assertTrue(isClosed(parser)); // as read to completion         }@@ -681,6 +681,29 @@ class StreamParserTest {         }     } +    @Test+    void fragmentNodesAreInResultContainerWhileStreaming() throws IOException {+        // a div implicitly closes a p; with the context p, content must still be parsed into the fragment's container+        String html = "<div id=one>One</div><span id=two>Two</span>";+        Element context = new Element("p");++        try (StreamParser parser = new StreamParser(Parser.htmlParser()).parseFragment(html, context, "")) {+            Element div = parser.expectNext("div");+            Element container = div.parent();+            assertNotNull(container);+            assertNotSame(parser.document(), container);++            Element span = parser.expectNext("span");+            assertSame(container, span.parent());++            List<Node> nodes = parser.completeFragment();+            assertEquals(2, nodes.size());+            assertSame(div, nodes.get(0));+            assertSame(span, nodes.get(1));+            assertSame(container, div.parent()); // not moved at completion+        }+    }+     @Test     void canStreamFragmentXml() throws IOException {         String html = "<tr id=1><td>One</td></tr><tr id=2><td>Two</td></tr><tr id=3><td>Three</td></tr>"; 
Merged fix · PR #2602+319 −58
CHANGES.mdindex 14e8f85b..f74fb519 100644@@ -10,6 +10,7 @@ * Standardized parser normalization of tag and attribute names so that HTML comparisons use ASCII-only case folding, and accepted control characters are preserved, aligning name handling to the HTML and XML specs. [#2594](https://github.com/jhy/jsoup/issues/2594) * Named character references without a semicolon before `-` or `_` now decode correctly in attribute values, matching HTML and browser behavior (e.g., `&copy-` becomes `©-`). [#2588](https://github.com/jhy/jsoup/issues/2588) * Support downloads > 2GB via `Response.bodyStream()` when `maxBodySize(0)` is configured. [#2593](https://github.com/jhy/jsoup/issues/2593)+* Updated the adoption-agency algorithm to align to the HTML5 spec. Misnested formatting in tables now places recovered blocks correctly, along with related table and fragment parsing cases. For example, `<table><b><p>X</b>` now places the paragraph before the table rather than inside it. [#2601](https://github.com/jhy/jsoup/issues/2601)  ## 1.23.2 (2026-Aug-26) src/main/java/org/jsoup/parser/HtmlTreeBuilder.javaindex e545c632..8961fe86 100644@@ -17,6 +17,7 @@ import org.jspecify.annotations.Nullable;  import java.io.Reader; import java.util.ArrayList;+import java.util.Collection; import java.util.List;  import static org.jsoup.internal.StringUtil.inSorted;@@ -41,7 +42,8 @@ public class HtmlTreeBuilder extends TreeBuilder {     private boolean baseUriSetFromDoc;     private @Nullable Element headElement; // the current head element     private @Nullable FormElement formElement; // the current form element-    private @Nullable Element contextElement; // fragment parse root; shallow copy of context, may be null during fragment parsing+    private @Nullable Element contextElement; // context copy and fragment output container; not on the stack+    private @Nullable Element fragmentRoot; // internal stack root; contextElement receives fragment nodes     ArrayList<Element> formattingElements; // active (open) formatting elements     private ArrayList<HtmlTreeBuilderState> tmplInsertMode; // stack of Template Insertion modes     private @Nullable NoscriptState noscriptState; // active noscript island state@@ -72,6 +74,7 @@ public class HtmlTreeBuilder extends TreeBuilder {         headElement = null;         formElement = null;         contextElement = null;+        fragmentRoot = null;         formattingElements = new ArrayList<>();         tmplInsertMode = new ArrayList<>();         noscriptState = null;@@ -120,8 +123,9 @@ public class HtmlTreeBuilder extends TreeBuilder {                     break;             }             tokeniser.transition(contextState);+            fragmentRoot = new Element(tagFor("html", "html", NamespaceHtml, settings), baseUri);             doc.appendChild(contextElement);-            push(contextElement);+            push(fragmentRoot);             resetInsertionMode();              // setup form element to nearest form on context (up ancestor chain). ensures form controls are associated@@ -135,21 +139,12 @@ public class HtmlTreeBuilder extends TreeBuilder {                 formSearch = formSearch.parent();             } -            if (htmlContext && contextName.equals("noscript")) enterNoscript(contextElement);+            if (htmlContext && contextName.equals("noscript")) enterNoscript(fragmentRoot);         }     }      @Override List<Node> completeParseFragment() {-        if (contextElement != null) {-            // depending on context and the input html, content may have been added outside of the root el-            // e.g. context=p, input=div, the div will have been pushed out.-            List<Node> nodes = contextElement.siblingNodes();-            if (!nodes.isEmpty())-                contextElement.insertChildren(-1, nodes);-            return contextElement.childNodes();-        }-        else-            return doc.childNodes();+        return (contextElement != null ? contextElement : doc).childNodes();     }      @Override@@ -220,7 +215,7 @@ public class HtmlTreeBuilder extends TreeBuilder {     private boolean closeNoscriptEndTag(Token.EndTag end) {         String name = end.normalName();         NoscriptState island = Validate.expectNotNull(noscriptState, "Bug: noscript end tag processed with no island state");-        if (name.equals("noscript") && island.boundary != contextElement) {+        if (name.equals("noscript") && island.boundary != fragmentRoot) {             endNoscript();             return true;         }@@ -239,7 +234,7 @@ public class HtmlTreeBuilder extends TreeBuilder {         // If the stack of open elements is empty         if (stack.isEmpty())             return true;-        final Element el = currentElement();+        final Element el = adjustedCurrentElement();         final String ns = el.tag().namespace();          // If the adjusted current node is an element in the HTML namespace@@ -462,15 +457,18 @@ public class HtmlTreeBuilder extends TreeBuilder {         if (parser.getErrors().canAddError() && el.hasAttr("xmlns") && !el.attr("xmlns").equals(el.tag().namespace()))             error("Invalid xmlns attribute [%s] on tag [%s]", el.attr("xmlns"), el.tagName()); -        Element target = currentElOrDoc();-        if (isFosterInserts() && StringUtil.inSorted(target.normalName(), InTableFoster))-            insertInFosterParent(el);-        else-            target.appendChild(el);-+        insertNode(el, currentElOrDoc());         push(el);     } +    /** Inserts a node at the appropriate location for the target. */+    void insertNode(Node node, Element target) {+        if (isFosterInserts() && target.tag().namespace().equals(NamespaceHtml) && inSorted(target.normalName(), InTableFoster))+            insertInFosterParent(node);+        else+            insertionTarget(target).appendChild(node);+    }+     /** Inserts a comment into the current element, or the document when there is none. */     void insertCommentNode(Token.Comment token) {         insertCommentNode(token, currentElOrDoc());@@ -479,7 +477,7 @@ public class HtmlTreeBuilder extends TreeBuilder {     /** Inserts a comment into the supplied target. */     void insertCommentNode(Token.Comment token, Element target) {         Comment node = new Comment(token.getData());-        target.appendChild(node);+        insertionTarget(target).appendChild(node);         onNodeInserted(node);     } @@ -506,6 +504,7 @@ public class HtmlTreeBuilder extends TreeBuilder {     void insertCharacterToElement(Token.Character characterToken, Element el) {         final Node node;         final String data = characterToken.getData();+        el = insertionTarget(el);          if (characterToken.isCData())             node = new CDataNode(data);@@ -521,6 +520,36 @@ public class HtmlTreeBuilder extends TreeBuilder {         return stack;     } +    /** Gets the insertion target for fragment content. */+    private Element insertionTarget(Element target) {+        // the html root is only used by the tree builder; content belongs in the context copy+        return target == fragmentRoot && contextElement != null ? contextElement : target;+    }++    /** Notifies listeners that a node was inserted. */+    @Override void onNodeInserted(Node node) {+        // listeners observe the fragment's context copy, not its parser-only html root+        super.onNodeInserted(node == fragmentRoot && contextElement != null ? contextElement : node);+    }++    /** Notifies listeners that a node was closed. */+    @Override void onNodeClosed(Node node) {+        super.onNodeClosed(node == fragmentRoot && contextElement != null ? contextElement : node);+    }++    /** Tests whether an element is open. */+    @Override boolean isOpen(Element element) {+        // the context copy remains open for streaming while its parser root is open+        return super.isOpen(element == contextElement && fragmentRoot != null ? fragmentRoot : element);+    }++    /** Copies the open elements. */+    @Override void copyOpenElementsTo(Collection<? super Element> elements) {+        // streaming tracks the context copy in place of the parser-only root+        for (Element element : stack)+            elements.add(insertionTarget(element));+    }+     boolean onStack(Element el) {         return stack.contains(el);     }@@ -530,10 +559,15 @@ public class HtmlTreeBuilder extends TreeBuilder {         return getFromStack(elName) != null;     } -    /** Checks if there is an HTML element with the given name above the synthetic fragment context. */-    boolean onStackAboveContext(String elName) {-        Element el = getFromStack(elName);-        return el != null && el != contextElement;+    /** Gets the adjusted current element for foreign-content dispatch. */+    private Element adjustedCurrentElement() {+        // fragment parsing uses the context at the parser root+        return fragmentParsing && stack.size() == 1 && contextElement != null ? contextElement : currentElement();+    }++    /** Gets the namespace used to process the current token. */+    @Override String currentElNs() {+        return adjustedCurrentElement().tag().namespace();     }      /** Gets the nearest (lowest) HTML element with the given name from the stack. */@@ -1135,26 +1169,24 @@ public class HtmlTreeBuilder extends TreeBuilder {         formattingElements.add(null);     } -    void insertInFosterParent(Node in) {-        Element fosterParent;-        Element lastTable = getFromStack("table");-        boolean isLastTableParent = false;-        if (lastTable != null) {-            if (lastTable.parent() != null) {-                fosterParent = lastTable.parent();-                isLastTableParent = true;-            } else-                fosterParent = aboveOnStack(lastTable);-        } else { // no table == frag-            fosterParent = stack.get(0);-        }--        if (isLastTableParent) {-            Validate.notNull(lastTable); // last table cannot be null by this point.-            lastTable.before(in);+    /** Inserts a foster-parented node. */+    private void insertInFosterParent(Node in) {+        for (int pos = stack.size() - 1; pos >= 0; pos--) {+            Element el = stack.get(pos);+            if (el.elementIs("template", NamespaceHtml)) {+                // template contents are stored directly on the element+                el.appendChild(in);+                return;+            }+            if (el.elementIs("table", NamespaceHtml)) {+                if (el.parent() != null)+                    el.before(in);+                else+                    insertionTarget(stack.get(pos - 1)).appendChild(in);+                return;+            }         }-        else-            fosterParent.appendChild(in);+        insertionTarget(stack.get(0)).appendChild(in);     }      // Template Insertion Mode stacksrc/main/java/org/jsoup/parser/HtmlTreeBuilderState.javaindex 09b27bfd..5c76253a 100644@@ -165,7 +165,7 @@ enum HtmlTreeBuilderState {                     } else if (inSorted(name, Constants.InHeadEnd)) {                         return anythingElse(t, tb);                     } else if (name.equals("template")) {-                        if (!tb.onStackAboveContext(name)) {+                        if (!tb.onStack(name)) {                             tb.error(this);                         } else {                             tb.generateImpliedEndTags(true);@@ -930,9 +930,7 @@ enum HtmlTreeBuilderState {                 } // end inner loop # 13                  // 14. Insert whatever lastNode ended up being in the previous step at the [appropriate place for inserting a node], but using commonAncestor as the _override target_.-                // todo - impl https://html.spec.whatwg.org/multipage/parsing.html#appropriate-place-for-inserting-a-node fostering-                // just use commonAncestor as target:-                commonAncestor.appendChild(lastEl);+                tb.insertNode(lastEl, commonAncestor);                 // 15. [Create an element for the token] for which formattingElement was created, in the [HTML namespace], with furthestBlock as the intended parent.                 Element adoptor = tb.recreateElement(formatEl);                 // 16. Take all of the child nodes of furthestBlock and append them to the element created in the last step.@@ -1073,9 +1071,11 @@ enum HtmlTreeBuilderState {          boolean anythingElse(Token t, HtmlTreeBuilder tb) {             tb.error(this);+            boolean fosterInserts = tb.isFosterInserts();             tb.setFosterInserts(true);             tb.process(t, InBody);-            tb.setFosterInserts(false);+            // synthetic end tags can reenter this handler; restore the outer token's foster flag+            tb.setFosterInserts(fosterInserts);             return true;         }     },@@ -1189,6 +1189,9 @@ enum HtmlTreeBuilderState {                                 tb.transition(InTable);                             }                             break;+                        case "col":+                            tb.error(this);+                            return false;                         case "template":                             tb.process(t, InHead);                             break;@@ -1197,10 +1200,7 @@ enum HtmlTreeBuilderState {                     }                     break;                 case EOF:-                    if (tb.currentElementIs("html"))-                        return true; // stop parsing; frag case-                    else-                        return anythingElse(t, tb);+                    return tb.process(t, InBody);                 default:                     return anythingElse(t, tb);             }@@ -1579,7 +1579,7 @@ enum HtmlTreeBuilderState {                     }                     break;                 case EOF:-                    if (!tb.onStackAboveContext("template")) { // stop parsing+                    if (!tb.onStack("template")) { // stop parsing                         return true;                     }                     tb.error(this);@@ -1775,7 +1775,7 @@ enum HtmlTreeBuilderState {                      // Any other start:                     // (whatwg says to fix up tag name and attribute case per a table - we will preserve original case instead)-                    String namespace = tb.currentElement().tag().namespace();+                    String namespace = tb.currentElNs();                     tb.insertForeignElementFor(start, namespace);                     // (self-closing handled in insert)                     // if self-closing svg script -- level and execution elidedsrc/main/java/org/jsoup/parser/Tokeniser.javaindex 39e7748d..4c05b4d0 100644@@ -265,7 +265,7 @@ final class Tokeniser {     /** Test if CDATA sections are allowed at the adjusted current node. */     boolean isCdataAllowed() {         return syntax == Document.OutputSettings.Syntax.xml-            || (treeBuilder.hasCurrentElement() && !Parser.NamespaceHtml.equals(treeBuilder.currentElement().tag().namespace()));+            || (treeBuilder.hasCurrentElement() && !Parser.NamespaceHtml.equals(treeBuilder.currentElNs()));     }      /** Test if the pending end tag matches the last emitted start tag. */src/main/java/org/jsoup/parser/TreeBuilder.javaindex e2db0b0c..28cae3da 100644@@ -223,6 +223,11 @@ abstract class TreeBuilder {         return 512;     } +    /** The namespace used to process the current token. */+    String currentElNs() {+        return currentElement().tag().namespace();+    }+     /**      Gets the current open element for tree-construction decisions.      The stack must not be empty; use {@link #hasCurrentElement()} when it may be.src/test/java/org/jsoup/parser/HtmlParserTest.javaindex 4db0c738..39439e10 100644@@ -1043,6 +1043,48 @@ public class HtmlParserTest {         assertEquals("<b>1</b>\n<p><b>2</b>3</p>", doc.body().html());     } +    @ParameterizedTest+    @MethodSource("adoptionTableCases")+    void fostersAdoptedFormattingInDocument(String input, String expected) {+        // the formatting end tag reparents its block using the common ancestor as the override target+        Document doc = Jsoup.parse("<body>" + input);+        doc.outputSettings().prettyPrint(false);+        assertEquals(expected, doc.body().html());+    }++    @ParameterizedTest+    @MethodSource("adoptionTableCases")+    void fostersAdoptedFormattingInFragment(String input, String expected) {+        Element context = new Element("div");+        List<Node> nodes = Parser.parseFragment(input, context, "");+        Element container = (Element) nodes.get(0).parent();+        assertNotNull(container);+        container.ownerDocument().outputSettings().prettyPrint(false);+        assertEquals(expected, container.html());+        for (Node node : nodes)+            assertSame(container, node.parent());+    }++    /** Covers table override targets and template/table stack ordering during adoption. */+    static Stream<Arguments> adoptionTableCases() {+        return Stream.of(+            Arguments.of("<table><b><p>X</b>",+                "<b></b><p><b>X</b></p><table></table>"),+            Arguments.of("<table><tbody><b><p>X</b>",+                "<b></b><p><b>X</b></p><table><tbody></tbody></table>"),+            Arguments.of("<table><thead><b><p>X</b>",+                "<b></b><p><b>X</b></p><table><thead></thead></table>"),+            Arguments.of("<table><tfoot><b><p>X</b>",+                "<b></b><p><b>X</b></p><table><tfoot></tfoot></table>"),+            Arguments.of("<table><tr><b><p>X</b>",+                "<b></b><p><b>X</b></p><table><tbody><tr></tr></tbody></table>"),+            Arguments.of("<table><template><tr><b><p>X</b>",+                "<table><template><tr></tr><b></b><p><b>X</b></p></template></table>"),+            Arguments.of("<template><table><b><p>X</b>",+                "<template><b></b><p><b>X</b></p><table></table></template>")+        );+    }+     @Test public void handlesMisnestedAInDivs() {         String h = "<a 1><div 2><div 3><a 4>child</a></div></div></a>";         String w = "<a 1></a> <div 2> <a 1></a> <div 3> <a 1></a><a 4>child</a> </div> </div>";@@ -1920,7 +1962,8 @@ public class HtmlParserTest {         // https://github.com/jhy/jsoup/issues/1603         Element element = new Element("tr");         element.html("<tr><td>One</td></tr>");-        assertEquals("<tr>\n <tr>\n  <td>One</td>\n </tr>\n</tr>", element.outerHtml());+        // the context is not an open tr; the redundant start and end tags are ignored+        assertEquals("<tr>\n <td>One</td>\n</tr>", element.outerHtml());     }      @Test public void parseFragmentOnCreatedDocument() {@@ -1933,6 +1976,23 @@ public class HtmlParserTest {         assertEquals("<p>\n <h2>text</h2>\n</p>", node.parent().outerHtml());     } +    @Test void fragmentRootReceivesTextCommentsAndReparentedFormatting() {+        // adoption recovery and ordinary insertion both redirect the stack root to the result container+        Element context = new Element("div").attr("id", "context");+        List<Node> nodes = Parser.parseFragment("Before<!--comment--><b>One<p>Two</b>Three", context, "");+        Element container = (Element) nodes.get(0).parent();+        assertNotNull(container);+        assertEquals("div", container.normalName());+        assertEquals("context", container.id());+        container.ownerDocument().outputSettings().prettyPrint(false);+        assertEquals("Before<!--comment--><b>One</b><p><b>Two</b>Three</p>", container.html());+        assertEquals(4, nodes.size());+        for (Node node : nodes)+            assertSame(container, node.parent());+        assertEquals(1, container.ownerDocument().childrenSize());+        assertEquals("", context.html());+    }+     @Test public void nestedPFragments() {         // https://github.com/jhy/jsoup/issues/1602         String bareFragment = "<p></p><a></a>";@@ -2045,6 +2105,66 @@ public class HtmlParserTest {         assertEquals(want, TextUtil.stripNewlines(doc.body().html()));     } +    @Test void fostersParagraphsAfterImplicitCloseInTableFragment() {+        Document doc = Jsoup.parse("<table></table>");+        doc.outputSettings().prettyPrint(false);+        Element table = doc.expectFirst("table");+        // closing the first paragraph must preserve foster insertion for the second+        table.html("<tr><p id=first><p id=second>");+        assertEquals("<tbody><tr></tr></tbody><p id=\"first\"></p><p id=\"second\"></p>", table.html());+    }++    @Test void fostersParagraphsAfterImplicitCloseInDocument() {+        // both paragraphs belong before the table, in input order+        Document doc = Jsoup.parse("<table><tr><p id=first><p id=second>");+        doc.outputSettings().prettyPrint(false);+        assertEquals("<p id=\"first\"></p><p id=\"second\"></p><table><tbody><tr></tr></tbody></table>", doc.body().html());+    }++    @Test void colgroupFragmentIgnoresRowStart() {+        Document doc = Jsoup.parse("<table><colgroup></colgroup></table>");+        doc.outputSettings().prettyPrint(false);+        Element colgroup = doc.expectFirst("colgroup");+        // an incompatible token must not pop the fragment context or change its insertion mode+        colgroup.html("<tr><col id=kept>");+        assertEquals("<col id=\"kept\">", colgroup.html());+    }++    @Test void colgroupFragmentIgnoresContextEndTag() {+        Document doc = Jsoup.parse("<table><colgroup></colgroup></table>");+        doc.outputSettings().prettyPrint(false);+        Element colgroup = doc.expectFirst("colgroup");+        // an explicit closing tag cannot close the external fragment context either+        colgroup.html("</colgroup><col id=kept>");+        assertEquals("<col id=\"kept\">", colgroup.html());+    }++    @Test void colEndTagDoesNotCloseColumnGroup() {+        Document doc = Jsoup.parse("<table><colgroup><col id=first></col><col id=second></table>");+        doc.outputSettings().prettyPrint(false);+        assertEquals("<colgroup><col id=\"first\"><col id=\"second\"></colgroup>", doc.expectFirst("table").html());+    }++    @Test void inputColumnGroupsStillClose() {+        // actual col groups can close explicitly or implicitly, including inside templates+        for (String input : new String[]{"<colgroup><col></colgroup><colgroup><col>", "<colgroup><col><colgroup><col>"}) {+            for (String context : new String[]{"table", "template"}) {+                Document doc = Jsoup.parse("<body><" + context + "></" + context + ">");+                doc.outputSettings().prettyPrint(false);+                Element el = doc.expectFirst(context);+                el.html(input);+                assertEquals("<colgroup><col></colgroup><colgroup><col></colgroup>", el.html(), context + ": " + input);+            }+        }+    }++    @Test void columnGroupEofProcessesUnclosedTemplate() {+        // An EOF in column-group mode must reach the template cleanup and report its unclosed element+        Parser parser = Parser.htmlParser().setTrackErrors(10);+        parser.parseInput("<!doctype html><html><head></head><body><template><col>", "");+        assertFalse(parser.getErrors().isEmpty());+    }+     @Test void templateTableRowFragment() {         // https://github.com/jhy/jsoup/issues/1409 (per the fragment <tr> use case)         Document doc = Jsoup.parse("<body><table><template></template></table></body");src/test/java/org/jsoup/parser/HtmlTreeBuilderTest.javaindex a429dc98..a2e7f432 100644@@ -16,6 +16,23 @@ import static org.jsoup.parser.Parser.NamespaceHtml; import static org.junit.jupiter.api.Assertions.*;  public class HtmlTreeBuilderTest {+    @Test void fosterInsertionUsesStackParentForRemovedTable() {+        // a table removed while still open uses the element above it on the stack+        Parser parser = Parser.htmlParser();+        try (StreamParser stream = new StreamParser(parser).parseFragment("", new Element("div"), "")) {+            HtmlTreeBuilder tb = (HtmlTreeBuilder) parser.getTreeBuilder();+            Element container = stream.document().child(0);+            Element table = container.appendElement("table");+            tb.push(table);+            table.remove();+            Element paragraph = new Element("p");+            tb.setFosterInserts(true);+            tb.insertNode(paragraph, table);+            assertSame(container, paragraph.parent());+            assertEquals(1, container.childrenSize());+        }+    }+     @Test     public void ensureSearchArraysAreSorted() {         List<Object[]> treeBuilderArrays = HtmlTreeBuilderStateTest.findConstantArrays(HtmlTreeBuilder.class);src/test/java/org/jsoup/parser/StreamParserTest.javaindex f5ee7c87..2f24613d 100644@@ -614,6 +614,63 @@ class StreamParserTest {      // Fragments +    @ParameterizedTest+    @ValueSource(strings = {"p", "table"})+    void fragmentSelectionKeepsLiveResultParent(String contextTag) throws IOException {+        // block input must stay in the result container, even in p and table contexts+        Element context = new Element(contextTag).attr("id", "context").text("Original");+        String html = "<div id=first>One</div><div id=second>Two</div><aside id=unread>Later</aside>";+        try (StreamParser parser = new StreamParser(Parser.htmlParser()).parseFragment(html, context, "")) {+            Element first = parser.expectFirst("#first");+            Element container = first.parent();+            assertNotNull(container);+            assertEquals(contextTag, container.normalName());+            assertEquals("context", container.id());+            assertNotSame(context, container);+            assertSame(parser.document(), container.parent());+            assertEquals(1, parser.document().childrenSize());+            assertSame(first, parser.document().selectFirst("#context > #first"));+            assertNull(parser.document().selectFirst("#unread")); // check this is the live tree, before EOF++            List<Node> nodes = parser.completeFragment();+            assertEquals(3, nodes.size());+            assertSame(first, nodes.get(0));+            for (Node node : nodes)+                assertSame(container, node.parent()); // completion must not repair parentage+            assertEquals("Original", context.html());+            assertNull(context.parent());+        }+    }++    @ParameterizedTest+    @ValueSource(strings = {"p", "table"})+    void fragmentStreamKeepsLiveResultParent(String contextTag) throws IOException {+        // check parentage at emission, before completeFragment could relocate nodes+        Element context = new Element(contextTag).attr("id", "context");+        String html = "<div id=first>One</div><div id=second>Two</div>";+        try (StreamParser parser = new StreamParser(Parser.htmlParser()).parseFragment(html, context, "")) {+            Element container = parser.document().child(0);+            Elements emitted = new Elements();+            parser.stream().forEachOrdered(el -> {+                if (!el.normalName().equals("div")) return;+                Element parent = el.parent();+                assertNotNull(parent);+                assertEquals(contextTag, parent.normalName());+                assertEquals("context", parent.id());+                assertSame(parser.document(), parent.parent());+                assertSame(container, parent);+                assertSame(el, parser.document().selectFirst("#context > #" + el.id()));+                emitted.add(el);+            });+            assertEquals(2, emitted.size());+            List<Node> nodes = parser.completeFragment();+            assertEquals(2, nodes.size());+            for (Node node : nodes)+                assertSame(container, node.parent());+            assertEquals("", context.html());+        }+    }+     @Test     void canStreamFragment() {         String html = "<tr id=1><td>One</td><tr id=2><td>Two</td></tr><tr id=3><td>Three</td></tr>";@@ -630,6 +687,35 @@ class StreamParserTest {         }     } +    @Test void fragmentContainerSelectionWaitsForCompletion() throws IOException {+        // the visible container stays open until the internal root closes+        Element context = new Element("p").attr("id", "context");+        try (StreamParser parser = new StreamParser(Parser.htmlParser())+            .parseFragment("<div>One</div><div>Two</div>", context, "")) {+            Element container = parser.expectFirst("#context");+            assertEquals("One Two", container.text());+            assertFalse(parser.document().parser().getTreeBuilder().isOpen(container));+            assertSame(parser.document().child(0), container);+            assertEquals(container.childNodes(), parser.completeFragment());+        }+    }++    @Test void stoppedFragmentDoesNotExposeUnfinishedContainer() throws IOException {+        // stopping must defer the visible container, not the hidden stack root+        Element context = new Element("p").attr("id", "context");+        try (StreamParser parser = new StreamParser(Parser.htmlParser())+            .parseFragment("<div id=first>One</div><div>Two</div><div>Three</div>", context, "")) {+            Element first = parser.expectFirst("#first");+            Element container = first.parent();+            parser.stop();+            assertNull(parser.selectFirst("#context"));+            assertSame(container, parser.document().child(0));+            List<Node> nodes = parser.completeFragment();+            assertSame(container, nodes.get(0).parent());+            assertSame(first, nodes.get(0));+        }+    }+     @Test void canIterateFragment() {         // same as stream, just a different interface         String html = "<tr id=1><td>One</td><tr id=2><td>Two</td></tr><tr id=3><td>Three</td></tr>"; // missing </tr>, following <tr> infers it