Backtestsjsouprun-20261006-181938#2634

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

Align <body> and <frameset> fragment parsing to the spec

Better

Our patch was judged better than the merged fix.

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

Both patches make the same production change in HtmlTreeBuilderState. Each replaces `(stack.size() > 2 && !stack.get(1).nameIs("body"))` with `!stack.get(1).nameIs("body")` for both the `body` and `frameset` start-tag branches. This fixes the root cause: a 2-element stack such as [html, span] in a body fragment was not being ignored. Correctness is therefore equal.

The difference is in the tests. Patch A adds one test, and it covers only `<frameset>` (body and div contexts, plus an html-context check). It never exercises the `<body>` start-tag branch it also changed.

Patch B tests both branches:
- `<span><frameset>…` and `<span><body class=x>…` in a body context, checking that later content still lands in the span and that body attributes are not merged.
- The same cases through `Jsoup.parseBodyFragment`.
- A template context.
- Full-document regression checks showing that `<body>` attribute merging and the frameset-ok replacement of an implied body still work in normal parsing.

These tests would fail before the fix and pass after it. They also guard against the obvious risk of over-ignoring in document parsing.

Scope and craft are close. Patch A adds a CHANGES.md entry and drops the `// only in fragment case` comment; Patch B keeps the comment. Neither choice matters much. Patch B wins on more thorough, meaningful test coverage of both changed code paths.

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

8m 4s

Wall-clock time in the container

Full test suite

No new failures

2,426 passed · 0 skipped · 2,426 total

Contamination audit

Clean

No sign the agent saw the real fix

Patch size

42vs 20 merged

Changed lines, ours vs merged

The spec says to ignore either start tag when the second open element is not body, but the current impl doesn't do that exactly. For example, parsing <span><frameset> as a body fragment currently returns <frameset> instead of <span>.

The two patches

Our patch+40 −2
src/main/java/org/jsoup/parser/HtmlTreeBuilderState.javaindex fba3b0a3..7e039a40 100644@@ -344,7 +344,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 {@@ -357,7 +357,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()) {src/test/java/org/jsoup/parser/HtmlParserTest.javaindex bb165e28..b9d881db 100644@@ -765,6 +765,44 @@ public class HtmlParserTest {         // no body, no table. No crash!     } +    @Test void ignoresFramesetStartTagInBodyFragmentWhenSecondElementNotBody() {+        // spec: ignore <frameset> when the second element on the stack is not body (fragment case)+        Element body = new Element("body");+        List<Node> nodes = Parser.parseFragment("<span><frameset><frame></frameset>One", body, "");+        assertEquals(1, nodes.size());+        assertEquals("<span>One</span>", TextUtil.stripNewlines(((Element) nodes.get(0)).outerHtml()));++        Document doc = Jsoup.parseBodyFragment("<span><frameset>");+        assertEquals("<span></span>", doc.body().html());++        // template context also routes start tags through InBody+        nodes = Parser.parseFragment("<span><frameset><frame></frameset>Two", new Element("template"), "");+        assertEquals(1, nodes.size());+        assertEquals("<span>Two</span>", TextUtil.stripNewlines(((Element) nodes.get(0)).outerHtml()));+    }++    @Test void ignoresBodyStartTagInBodyFragmentWhenSecondElementNotBody() {+        // spec: ignore <body> when the second element on the stack is not body (fragment case)+        Element body = new Element("body");+        List<Node> nodes = Parser.parseFragment("<span><body class=x>One", body, "");+        assertEquals(1, nodes.size());+        assertEquals("<span>One</span>", ((Element) nodes.get(0)).outerHtml());+        assertFalse(body.hasAttr("class"));++        Document doc = Jsoup.parseBodyFragment("<span><body class=x>Two");+        assertEquals("<span>Two</span>", doc.body().html());+        assertFalse(doc.body().hasAttr("class"));+    }++    @Test void bodyAndFramesetStartTagsStillHandledInDocument() {+        Document doc = Jsoup.parse("<body><span><body class=x>One");+        assertEquals("x", doc.body().attr("class"));+        assertEquals("<span>One</span>", doc.body().html());++        doc = Jsoup.parse("<span><frameset><frame></frameset>"); // implied body, frameset-ok still set+        assertEquals("<html><head></head><frameset><frame></frameset></html>", TextUtil.stripNewlines(doc.html()));+    }+     @Test void placesCommentsAfterFrameset() {         String html = "<html><head></head><frameset><frame></frameset><!--after frameset--></html><!--after html-->";         Document doc = Jsoup.parse(html); 
Merged fix · PR #+16 −4
CHANGES.mdindex ee407436..1a94d87c 100644@@ -35,6 +35,7 @@ * Fixed HTML parse of `<dialog>`, `<main>`, and `<search>` so their start and end tags close open paragraphs correctly. [#2629](https://github.com/jhy/jsoup/issues/2629) * Updated the parse of malformed end tags such as `</ COMMENT >` to match the spec (`<!-- COMMENT -->`). [#2631](https://github.com/jhy/jsoup/issues/2631) * Source position tracking no longer impacts the adoption algo when repeated HTML formatting tags are reconstructed after a paragraph closes. [#2633](https://github.com/jhy/jsoup/issues/2633)+* Fixed fragment parsing to ignore `<body>` and `<frameset>` start tags when the second open element is not `body`, matching the HTML spec. [#2634](https://github.com/jhy/jsoup/issues/2634)  ## 1.23.2 (2026-Aug-26) src/main/java/org/jsoup/parser/HtmlTreeBuilderState.javaindex fba3b0a3..ab337be1 100644@@ -344,8 +344,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")) {-                        // only in fragment case+                    if (stack.size() < 2 || !stack.get(1).nameIs("body") || tb.onStack("template")) {                         return false; // ignore                     } else {                         tb.framesetOk(false);@@ -357,8 +356,7 @@ enum HtmlTreeBuilderState {                 case "frameset":                     tb.error(this);                     stack = tb.getStack();-                    if (stack.size() < 2|| (stack.size() > 2 && !stack.get(1).nameIs("body"))) {-                        // only in fragment case+                    if (stack.size() < 2 || !stack.get(1).nameIs("body")) {                         return false; // ignore                     } else if (!tb.framesetOk()) {                         return false; // ignore framesetsrc/test/java/org/jsoup/parser/HtmlParserTest.javaindex bb165e28..dc0e3ab8 100644@@ -746,6 +746,19 @@ public class HtmlParserTest {         // no body auto vivification     } +    @Test void ignoresFramesetAfterSpanInFragments() {+        for (String context : new String[]{"body", "div"}) {+            List<Node> nodes = Parser.htmlParser().parseFragmentInput("<span><frameset>", new Element(context), "");+            assertEquals(1, nodes.size(), context);+            assertEquals("span", (nodes.get(0)).normalName(), context);+        }++        List<Node> htmlNodes = Parser.htmlParser().parseFragmentInput("<frameset><span>", new Element("html"), "");+        assertEquals(2, htmlNodes.size());+        assertEquals("head", (htmlNodes.get(0)).normalName());+        assertEquals("frameset", (htmlNodes.get(1)).normalName());+    }+     @Test void retainsWhitespaceFromMixedFramesetText() {         // in and after a frameset, the parser inserts ASCII whitespace and ignores the remaining characters         Document doc = Jsoup.parse("<!doctype html><frameset> te st</frameset> te st");