Backtestsjsouprun-20261006-181938#2627
Script text closes early after a nested script marker
Our patch was judged better than the merged fix.
In this judgment, Patch A = our agent, Patch B = the merged fix.
Both patches fix the reported body case. The root cause is that the InBody default branch took `tag.textState()` for `script` (a raw-text style state) before reaching the InBodyStartToHead dispatch, so the ScriptData escape and double-escape states never ran. Patch B reorders the branch so InBodyStartToHead tags go through InHead first. This is spec-aligned and fixes the body case. However, it leaves another path that uses `tagFor(start).textState()` untouched: `insertNoscriptStartTag` in HtmlTreeBuilder. A script inside a noscript island would therefore still close at the first `</script>`. Patch A adds a single `textStateFor` helper that maps HTML-namespace `script` to ScriptData. It applies the helper both in the InBody default branch and in the noscript-island insertion, so it fixes the issue wherever an HTML script is tokenised. Its parameterized test covers body, head, div, table, and noscript in both head and body. Both patches also make double-escape matching ASCII case-insensitive. A does it with one `equalsIgnoreAsciiCase` in the shared handler. B lowercases the buffer at two append sites. Both are correct. B's reorder also routes title, style, and noframes in body through InHead. That is likely equivalent behaviour but adds some unrelated surface. B's CHANGES entry is fine but neutral. Tests: both add regression tests that would fail before the change. A's are broader and include the noscript contexts that B's fix misses. A is therefore more complete on correctness and better tested.
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
14m 1s
Wall-clock time in the container
Full test suite
No new failures
2,394 passed · 0 skipped · 2,394 total
Contamination audit
Clean
No sign the agent saw the real fix
Patch size
64vs 35 merged
Changed lines, ours vs merged
The issue
View on GitHubA script in the body can close too early after <!-- and a nested <script> marker:
FOO<script><!--<script>-></script>--></script>QUX
Under the HTML spec's script data double-escape rules, the first </script> is text. The following --> should return the tokenizer to ordinary script data, and the final </script> closes the element. jsoup instead closes at the first </script>, leaving -->QUX outside.
The two patches
src/main/java/org/jsoup/parser/HtmlTreeBuilder.javaindex e9d4290b..82bf159d 100644@@ -193,7 +193,7 @@ public class HtmlTreeBuilder extends TreeBuilder { Inserts a start tag inside a noscript island as plain markup. */ private boolean insertNoscriptStartTag(Token.StartTag start) {- TokeniserState textState = tagFor(start).textState();+ TokeniserState textState = textStateFor(start); Element el = insertElementFor(start); if (textState != null) { // plaintext is intentionally not TagSet-driven and remains plain fallback markup. if (start.isSelfClosing()) {@@ -210,6 +210,18 @@ public class HtmlTreeBuilder extends TreeBuilder { return true; } + /**+ Gets the tokeniser state for the content of an HTML start tag, or null if it holds markup. An HTML script uses+ the script data states (so its escaped and double-escaped text is honored), rather than the tag's raw text state.+ */+ @Nullable TokeniserState textStateFor(Token.StartTag start) {+ Tag tag = tagFor(start);+ TokeniserState textState = tag.textState();+ if (textState != null && tag.normalName().equals("script") && NamespaceHtml.equals(tag.namespace()))+ return TokeniserState.ScriptData;+ return textState;+ }+ /** Closes an island element if it matches above the current noscript boundary. */src/main/java/org/jsoup/parser/HtmlTreeBuilderState.javaindex ab7e277e..3592cc69 100644@@ -619,9 +619,8 @@ enum HtmlTreeBuilderState { tb.pushActiveFormattingElements(el); break; default:- Tag tag = tb.tagFor(startTag);- TokeniserState textState = tag.textState();- if (textState != null) { // custom rcdata or rawtext (if we were in head, will have auto-transitioned here)+ TokeniserState textState = tb.textStateFor(startTag);+ if (textState != null) { // script data, custom rcdata or rawtext (if we were in head, will have auto-transitioned here) HandleTextState(startTag, tb, textState); } else if (inSorted(name, Constants.InBodyStartPClosers)) { if (tb.inButtonScope("p")) tb.processEndTag("p");src/main/java/org/jsoup/parser/TokeniserState.javaindex 0c87b84a..a20a88a4 100644@@ -1852,7 +1852,7 @@ enum TokeniserState { case ' ': case '/': case '>':- if (t.dataBuffer.value().equals("script"))+ if (equalsIgnoreAsciiCase(t.dataBuffer.value(), "script")) t.transition(primary); else t.transition(fallback);src/test/java/org/jsoup/parser/TokeniserStateTest.javaindex 7ac4d6ef..6dc24b8c 100644@@ -141,6 +141,49 @@ public class TokeniserStateTest { assertEquals("<!--<script>foo</script", Jsoup.parse("<script><!--<script>foo</script").expectFirst("script").data()); } + @Test+ void scriptDoubleEscapeEndReturnsToEscaped() {+ // the first </script> is double-escaped text; --> returns to script data, and the last </script> closes+ Document doc = Jsoup.parse("FOO<script><!--<script>-></script>--></script>QUX");+ Element script = doc.expectFirst("script");+ assertEquals("<!--<script>-></script>-->", script.data());+ assertEquals("FOO<script><!--<script>-></script>--></script>QUX", TextUtil.stripNewlines(doc.body().html()));+ assertEquals("QUX", ((TextNode) script.nextSibling()).text());+ }++ @Test+ void scriptDoubleEscapeIsCaseInsensitive() {+ // the double escape start and end match the script tag name case-insensitively+ Document doc = Jsoup.parse("FOO<script><!--<SCRIPT>-></sCrIpT>--></script>QUX");+ Element script = doc.expectFirst("script");+ assertEquals("<!--<SCRIPT>-></sCrIpT>-->", script.data());+ assertEquals("QUX", ((TextNode) script.nextSibling()).text());+ }++ @ParameterizedTest+ @MethodSource+ void scriptDoubleEscapeInContexts(String input) {+ // script data escaping applies wherever an html script is tokenised, not only in head+ Document doc = Jsoup.parse(input);+ Element script = doc.expectFirst("script");+ assertEquals("<!--<script>-></script>-->", script.data());+ Element after = doc.expectFirst("#after");+ assertEquals("QUX", after.text());+ assertEquals(0, script.select("#after").size());+ }++ private static Arguments[] scriptDoubleEscapeInContexts() {+ String script = "<script><!--<script>-></script>--></script><b id=after>QUX</b>";+ return new Arguments[] {+ arguments("FOO" + script),+ arguments("<head>" + script),+ arguments("<div>" + script + "</div>"),+ arguments("<table>" + script + "</table>"),+ arguments("<head><noscript>" + script + "</noscript></head>"),+ arguments("<body><noscript>" + script + "</noscript>"),+ };+ }+ @Test public void testRCDATAEndTagName() { for (char c : whiteSpace) {
CHANGES.mdindex f42ddbff..1dea2149 100644@@ -26,6 +26,7 @@ * Fixed several HTML parsing edge cases across malformed doctypes, misplaced text in tables, and whitespace in framesets to match the HTML spec. [#2618](https://github.com/jhy/jsoup/pull/2618) * If a HTML void element ends up with children (via DOM manipulation), those are no longer serialized in `html()`. [#2619](https://github.com/jhy/jsoup/issues/2619) * Removed the old `command`, `device`, and `menuitem` tags, as they are no longer in the HTML spec. [#2621](https://github.com/jhy/jsoup/issues/2621)+* Double-escaped script tags in body were being closed too early. [#2627](https://github.com/jhy/jsoup/issues/2627) ## 1.23.2 (2026-Aug-26) src/main/java/org/jsoup/parser/HtmlTreeBuilderState.javaindex ab7e277e..f19ee765 100644@@ -619,15 +619,15 @@ enum HtmlTreeBuilderState { tb.pushActiveFormattingElements(el); break; default:+ if (inSorted(name, Constants.InBodyStartToHead))+ return tb.process(t, InHead); Tag tag = tb.tagFor(startTag); TokeniserState textState = tag.textState();- if (textState != null) { // custom rcdata or rawtext (if we were in head, will have auto-transitioned here)+ if (textState != null) { HandleTextState(startTag, tb, textState); } else if (inSorted(name, Constants.InBodyStartPClosers)) { if (tb.inButtonScope("p")) tb.processEndTag("p"); tb.insertElementFor(startTag);- } else if (inSorted(name, Constants.InBodyStartToHead)) {- return tb.process(t, InHead); } else if (inSorted(name, Constants.InBodyStartApplets)) { tb.reconstructFormattingElements(); tb.insertElementFor(startTag);src/main/java/org/jsoup/parser/TokeniserState.javaindex 0c87b84a..669871b3 100644@@ -1,5 +1,6 @@ package org.jsoup.parser; +import org.jsoup.internal.Normalizer; import org.jsoup.internal.StringUtil; import org.jsoup.nodes.DocumentType; @@ -362,7 +363,7 @@ enum TokeniserState { @Override void read(Tokeniser t, CharacterReader r) { if (r.matchesAsciiAlpha()) { t.createTempBuffer();- t.dataBuffer.append(r.current());+ t.dataBuffer.append(Normalizer.asciiLowerCase(r.current())); t.emit('<'); t.emit(r.current()); t.advanceTransition(ScriptDataDoubleEscapeStart);@@ -1838,7 +1839,7 @@ enum TokeniserState { private static void handleDataDoubleEscapeTag(Tokeniser t, CharacterReader r, TokeniserState primary, TokeniserState fallback) { if (r.matchesAsciiAlpha()) { String name = r.consumeLetterSequence();- t.dataBuffer.append(name);+ t.dataBuffer.append(Normalizer.asciiLowerCase(name)); t.emit(name); return; }src/test/java/org/jsoup/parser/HtmlTreeBuilderTest.javaindex 8577510d..920cf2ca 100644@@ -413,4 +413,18 @@ public class HtmlTreeBuilderTest { x2.html(inner); // <foo> will be text not el, via custom fragment context element assertEquals(inner, xEl.data()); }++ @Test void scriptInBodyUsesScriptData() {+ Document unclosed = Jsoup.parse("FOO<script type=\"text/plain\">'<!-- <sCrIpt>'</script>BAR");+ Element script = unclosed.expectFirst("script");+ assertSame(unclosed.body(), script.parent());+ assertEquals("'<!-- <sCrIpt>'</script>BAR", script.data());+ assertEquals(2, unclosed.body().childNodeSize());++ Document closed = Jsoup.parse("FOO<script><!--<script>-></script>--></script>QUX");+ script = closed.expectFirst("script");+ assertSame(closed.body(), script.parent());+ assertEquals("<!--<script>-></script>-->", script.data());+ assertEquals("QUX", closed.body().childNode(2).outerHtml());+ } }src/test/java/org/jsoup/parser/TokeniserStateTest.javaindex 7ac4d6ef..fdf6b004 100644@@ -141,6 +141,15 @@ public class TokeniserStateTest { assertEquals("<!--<script>foo</script", Jsoup.parse("<script><!--<script>foo</script").expectFirst("script").data()); } + @Test+ void scriptDoubleEscapeMatchesAsciiCase() {+ assertEquals("<!--<sCrIpt>foo</script>BAR", Jsoup.parse("<script><!--<sCrIpt>foo</script>BAR").expectFirst("script").data());++ Document doc = Jsoup.parse("<script><!--<script>foo</sCrIpt>--></script>BAR");+ assertEquals("<!--<script>foo</sCrIpt>-->", doc.expectFirst("script").data());+ assertEquals("BAR", doc.body().text());+ }+ @Test public void testRCDATAEndTagName() { for (char c : whiteSpace) {