Backtestsjsouprun-20261006-181938#2631
Malformed </ end tag should consume the /
Better
Our patch was judged better than the merged fix.
In this judgment, Patch A = our agent, Patch B = the merged fix.
Both patches make the same production change: they remove the `commentPending.append('/')` line in the EndTagOpen fallback branch, so the bogus comment no longer includes the consumed `/`. That is the root cause, and it gives `</ COMMENT >` → `<!-- COMMENT -->` as the spec requires. On correctness the two are identical, and both update the existing test that expected `<!--/tag-->`.
The difference is in the new tests.
- **Patch A** covers more ground. It checks the exact issue case in context (`<div>One</ COMMENT >Two</div>`, asserting both the serialized HTML and `Comment.getData()`), at top level, and with a different invalid start char (`</?foo>` → `<!--?foo-->`). It also adds an XML-parser test, confirming the shared tokeniser behaves the same in XML mode.
- **Patch B** adds a table-driven test with `</#` (the EOF path), `</ COMMENT >` and `</ COM--MENT >`. These are useful, but only check top-level comment data in the HTML parser.
All of these tests would fail before the change and pass after it. Patch A replaces the removed comment with an explanatory one about spec behaviour, which is slightly clearer. Patch B's CHANGES.md entry is neutral under the scope criterion. Because the fixes are identical, the decision rests on test coverage, where A is modestly ahead (in-context HTML plus XML).
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
6m 21s
Wall-clock time in the container
Full test suite
No new failures
2,398 passed · 0 skipped · 2,398 total
Contamination audit
Clean
No sign the agent saw the real fix
Patch size
25vs 17 merged
Changed lines, ours vs merged
The issue
View on GitHubPer the spec, </ COMMENT > should parse to <!-- COMMENT -->, not <!-- / COMMENT --> as jsoup currently does.
The two patches
Our patch+22 −3
src/main/java/org/jsoup/parser/TokeniserState.javaindex 669871b3..39cfd96d 100644@@ -139,8 +139,7 @@ enum TokeniserState { t.advanceTransition(Data); } else { t.error(this);- t.createBogusCommentPending();- t.commentPending.append('/'); // push the / back on that got us here+ t.createBogusCommentPending(); // per spec, the comment data starts empty; the consumed </ is not included t.transition(BogusComment); } }src/test/java/org/jsoup/parser/HtmlParserTest.javaindex 7887ab87..ccbc9e54 100644@@ -2156,10 +2156,25 @@ public class HtmlParserTest { Document doc = Jsoup.parse("<" + tag + ">Text</" + tag + ">"); Elements els = doc.getElementsByTag(tag); assertEquals(0, els.size());- assertEquals("<" + tag + ">Text<!--/" + tag + "-->", doc.body().html());+ assertEquals("<" + tag + ">Text<!--" + tag + "-->", doc.body().html()); } } + @Test void malformedEndTagConsumesSlash() {+ // per spec, </ followed by an invalid tag name char is a bogus comment, without the /+ Document doc = Jsoup.parse("<div>One</ COMMENT >Two</div>");+ assertEquals("<div>One<!-- COMMENT -->Two</div>", TextUtil.stripNewlines(doc.body().html()));+ Comment comment = (Comment) doc.selectFirst("div").childNode(1);+ assertEquals(" COMMENT ", comment.getData());++ Document doc2 = Jsoup.parse("</ COMMENT >");+ Comment comment2 = (Comment) doc2.childNode(0);+ assertEquals(" COMMENT ", comment2.getData());++ Document doc3 = Jsoup.parse("<p>A</?foo>B</p>");+ assertEquals("<p>A<!--?foo-->B</p>", doc3.body().html());+ }+ @Test void htmlOutputCorrectsInvalidAttributeNames() { String html = "<body style=\"color: red\" \" name\"><div =\"\"></div></body>"; Document doc = Jsoup.parse(html);src/test/java/org/jsoup/parser/XmlTreeBuilderTest.javaindex 2c574bc1..5de1c53a 100644@@ -89,6 +89,11 @@ public class XmlTreeBuilderTest { assertEquals("#comment", doc.childNode(2).nodeName()); } + @Test void malformedEndTagConsumesSlash() {+ Document doc = Jsoup.parse("<doc>One</ COMMENT >Two</doc>", "", Parser.xmlParser());+ assertEquals("<doc>One<!-- COMMENT -->Two</doc>", TextUtil.stripNewlines(doc.html()));+ }+ @Test public void xmlFragment() { String xml = "<one src='/foo/' />Two<three><four /></three>"; List<Node> nodes = Parser.parseXmlFragment(xml, "http://example.com/");
Merged fix · PR #+15 −2
CHANGES.mdindex a65a514e..6cd69959 100644@@ -31,6 +31,7 @@ * Double-escaped script tags in body were being closed too early. [#2627](https://github.com/jhy/jsoup/issues/2627) * `Node.before(Node)` and `Node.after(Node)` now leave the document unchanged when given the node itself, instead of throwing a `NullPointerException`. [#2628](https://github.com/jhy/jsoup/issues/2628) * 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) ## 1.23.2 (2026-Aug-26) src/main/java/org/jsoup/parser/TokeniserState.javaindex 669871b3..9f65fba1 100644@@ -140,7 +140,6 @@ enum TokeniserState { } else { t.error(this); t.createBogusCommentPending();- t.commentPending.append('/'); // push the / back on that got us here t.transition(BogusComment); } }src/test/java/org/jsoup/parser/HtmlParserTest.javaindex 7887ab87..eb33dc1f 100644@@ -2156,7 +2156,7 @@ public class HtmlParserTest { Document doc = Jsoup.parse("<" + tag + ">Text</" + tag + ">"); Elements els = doc.getElementsByTag(tag); assertEquals(0, els.size());- assertEquals("<" + tag + ">Text<!--/" + tag + "-->", doc.body().html());+ assertEquals("<" + tag + ">Text<!--" + tag + "-->", doc.body().html()); } } src/test/java/org/jsoup/parser/TokeniserStateTest.javaindex fdf6b004..ca824553 100644@@ -83,6 +83,19 @@ public class TokeniserStateTest { assertEquals("fake", els.text()); } + @Test+ public void malformedEndTagCommentsExcludeSolidus() {+ String[][] cases = {+ {"</#", "#"},+ {"</ COMMENT >", " COMMENT "},+ {"</ COM--MENT >", " COM--MENT "}+ };+ for (String[] testCase : cases) {+ Document doc = Jsoup.parse(testCase[0]);+ assertEquals(testCase[1], ((Comment) doc.childNode(0)).getData(), testCase[0]);+ }+ }+ @Test public void testRcdataLessthanSign() { String body;