Backtestsjsouprun-20261006-181938#2503

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

Mixed-case RCDATA end tags can promote text to markup

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 root cause the same way. Each replaces the lower-case/upper-case `nextIndexOf` probes in `containsIgnoreCase` with a linear case-insensitive scan of the buffer, so mixed-case closers like `</TiTLE>` are now found. Both keep the scan cache consistent: Patch A stores `bufPos + offset` after `bufferUp`, and Patch B stores the absolute index after calling `bufferUp` up front. Correctness is effectively equal.

Tests are comparable. Both replace the old `assertFalse("one")` with a positive assertion and add a mixed-case `CharacterReader` test. Both add parser-level tests for the issue's `<title><p>` and `<textarea>` cases that would fail before the change. A's parser test checks a little more (the following `<p>Bar</p>` and `textarea.val()`), and B's checks that no `fake` or `body p` element is created. Neither is meaningfully weaker.

The difference is in craft. Patch B extracts `rangeMatchesIgnoreCase` from the existing `matchesIgnoreCase`, so the new scan reuses the comparison logic already in the class rather than duplicating it. Patch A adds a separate `charsEqualIgnoreCase` helper and a new `nextIndexOfIgnoreCase` method. That is a parallel implementation of the same comparison, with slightly different semantics (an extra lower-case-of-upper check), which adds a second place to maintain. B also calls `bufferUp()` before the cache check, which makes the cached-index comparison against `bufPos` more obviously sound. B adds a CHANGES entry, which is neutral-to-positive. On balance B is the slightly cleaner, more cohesive 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

12m 23s

Wall-clock time in the container

Full test suite

No new failures

2,015 passed · 0 skipped · 2,015 total

Contamination audit

Clean

No sign the agent saw the real fix

Patch size

64vs 58 merged

Changed lines, ours vs merged

RCDATA elements like <title> and <textarea> should match their end tags case-insensitively. A direct mixed-case closer works as expected: <title>Foo</TiTLE> closes the title. But when tag-shaped text appears before the closer, jsoup can miss the mixed-case end tag and leave RCDATA early.

For example, <title><p>Foo</TiTLE> creates a real body p element instead of keeping <p>Foo as title text. Similarly, <textarea><img src=x></TeXtArEa> creates an img element.

This was a premature optimization: the implementation only looked for consistent-case matches. I’ll fix it with a real case-insensitive scan.

The two patches

Our patch+51 −13
src/main/java/org/jsoup/parser/CharacterReader.javaindex 4ec4b798..2e415f24 100644@@ -11,7 +11,6 @@ import java.io.UncheckedIOException; import java.io.Reader; import java.io.StringReader; import java.util.Arrays;-import java.util.Locale;  /**  CharacterReader consumes tokens off a string. Used internally by jsoup. API subject to changes.@@ -590,7 +589,7 @@ public final class CharacterReader implements AutoCloseable {     @Nullable private String lastIcSeq; // scan cache     private int lastIcIndex; // nearest found indexOf -    /** Used to check presence of </title>, </style> when we're in RCData and see a <xxx. Only finds consistent case. */+    /** Used to check presence of </title>, </style> when we're in RCData and see a <xxx. Matches case-insensitively. */     boolean containsIgnoreCase(String seq) {         if (seq.equals(lastIcSeq)) {             if (lastIcIndex == -1) return false;@@ -598,17 +597,36 @@ public final class CharacterReader implements AutoCloseable {         }         lastIcSeq = seq; -        String loScan = seq.toLowerCase(Locale.ENGLISH);-        int lo = nextIndexOf(loScan);-        if (lo > -1) {-            lastIcIndex = bufPos + lo; return true;+        int found = nextIndexOfIgnoreCase(seq);+        lastIcIndex = found > -1 ? bufPos + found : -1;+        return found > -1;+    }++    /**+     Returns the offset between the current position and the next case-insensitive instance of the sequence, within+     the current buffer.+     @param seq scan target+     @return offset to the next instance of target, or -1 if not found.+     */+    private int nextIndexOfIgnoreCase(String seq) {+        bufferUp();+        int seqLength = seq.length();+        if (seqLength == 0) return 0;+        int lastStart = bufLength - seqLength;+        for (int offset = bufPos; offset <= lastStart; offset++) {+            int j = 0;+            while (j < seqLength && charsEqualIgnoreCase(seq.charAt(j), charBuf[offset + j])) j++;+            if (j == seqLength)+                return offset - bufPos;         }+        return -1;+    } -        String hiScan = seq.toUpperCase(Locale.ENGLISH);-        int hi = nextIndexOf(hiScan);-        boolean found = hi > -1;-        lastIcIndex = found ? bufPos + hi : -1; // we don't care about finding the nearest, just that buf contains-        return found;+    private static boolean charsEqualIgnoreCase(char a, char b) {+        if (a == b) return true;+        char ua = Character.toUpperCase(a);+        char ub = Character.toUpperCase(b);+        return ua == ub || Character.toLowerCase(ua) == Character.toLowerCase(ub);     }      @Overridesrc/test/java/org/jsoup/parser/CharacterReaderTest.javaindex 8f0ee30c..9644678c 100644@@ -230,8 +230,17 @@ public class CharacterReaderTest {         CharacterReader r = new CharacterReader("One TWO three");         assertTrue(r.containsIgnoreCase("two"));         assertTrue(r.containsIgnoreCase("three"));-        // weird one: does not find one, because it scans for consistent case only-        assertFalse(r.containsIgnoreCase("one"));+        assertTrue(r.containsIgnoreCase("one"));+        assertTrue(r.containsIgnoreCase("tWo"));+        assertTrue(r.containsIgnoreCase("ONE two THREE"));+        assertFalse(r.containsIgnoreCase("four"));+    }++    @Test void containsIgnoreCaseMixedCase() {+        CharacterReader r = new CharacterReader("<p>Foo</TiTLE>");+        assertTrue(r.containsIgnoreCase("</title"));+        assertTrue(r.containsIgnoreCase("</TITLE"));+        assertFalse(r.containsIgnoreCase("</textarea"));     }      @Test void containsIgnoreCaseBuffer() {src/test/java/org/jsoup/parser/HtmlParserTest.javaindex a7336676..4e02c249 100644@@ -813,6 +813,17 @@ public class HtmlParserTest {         assertEquals("<b>Two \n <p>Test</p></b>", two.body().html());     } +    @Test public void handlesMixedCaseRcdataEndTagAfterTagText() {+        Document title = Jsoup.parse("<title><p>Foo</TiTLE><p>Bar</p>");+        assertEquals("<p>Foo", title.title());+        assertEquals(1, title.select("body p").size());+        assertEquals("Bar", title.select("body p").first().text());++        Document textarea = Jsoup.parse("<textarea><img src=x></TeXtArEa>");+        assertEquals(0, textarea.select("img").size());+        assertEquals("<img src=x>", textarea.select("textarea").first().val());+    }+     @Test public void handlesUnclosedScriptAtEof() {         assertEquals("Data", Jsoup.parse("<script>Data").select("script").first().data());         assertEquals("Data<", Jsoup.parse("<script>Data<").select("script").first().data()); 
Merged fix · PR #+43 −15
CHANGES.mdindex 1935c219..aa3a1545 100644@@ -8,6 +8,9 @@ * Aligned HTML parser scope classification with the current HTML spec for `select`, `foreignObject`, and `template`. [#2501](https://github.com/jhy/jsoup/issues/2501) * Simplified the HTML tree builder's scope, implied-end-tag, and special-element checks by caching parser-only options on Tag. That improves HTML parser throughput by about 10% on small inputs and up to about 30% on larger inputs in the benchmark fixtures. [#2502](https://github.com/jhy/jsoup/issues/2502) +### Bug Fixes+* Fixed HTML parsing of mixed-case RCDATA end tags after tag-shaped text. For example, `<title><p>Foo</TiTLE>` and `<textarea><img src=x></TeXtArEa>` now keep the tag-shaped content as text instead of promoting it to markup. [#2503](https://github.com/jhy/jsoup/issues/2503)+ ### Build Changes * Cleaned up the Maven build for the multi-release JAR so Java 8 and Java 11+ sources compile as separate source sets. This avoids spurious Java 8 compiler warnings from newer-language overlay sources, keeps long-running parser checks behind an explicit profile, and preserves the same published artifacts and runtime behavior. src/main/java/org/jsoup/parser/CharacterReader.javaindex 4ec4b798..e1f1c979 100644@@ -11,7 +11,6 @@ import java.io.UncheckedIOException; import java.io.Reader; import java.io.StringReader; import java.util.Arrays;-import java.util.Locale;  /**  CharacterReader consumes tokens off a string. Used internally by jsoup. API subject to changes.@@ -510,15 +509,22 @@ public final class CharacterReader implements AutoCloseable {         return true;     } +    /**+     Checks if the current buffer position matches the sequence case-insensitively.+     */     boolean matchesIgnoreCase(String seq) {         bufferUp();         int scanLength = seq.length();         if (scanLength > bufLength - bufPos)             return false; -        for (int offset = 0; offset < scanLength; offset++) {+        return rangeMatchesIgnoreCase(seq, bufPos);+    }++    private boolean rangeMatchesIgnoreCase(String seq, int start) {+        for (int offset = 0; offset < seq.length(); offset++) {             char scan = seq.charAt(offset);-            char target = charBuf[bufPos + offset];+            char target = charBuf[start + offset];             if (scan == target) continue;              scan = Character.toUpperCase(scan);@@ -590,25 +596,26 @@ public final class CharacterReader implements AutoCloseable {     @Nullable private String lastIcSeq; // scan cache     private int lastIcIndex; // nearest found indexOf -    /** Used to check presence of </title>, </style> when we're in RCData and see a <xxx. Only finds consistent case. */+    /** Used to check presence of </title>, </style> when we're in RCData and see a <xxx. */     boolean containsIgnoreCase(String seq) {+        bufferUp();         if (seq.equals(lastIcSeq)) {             if (lastIcIndex == -1) return false;             if (lastIcIndex >= bufPos) return true;         }         lastIcSeq = seq; -        String loScan = seq.toLowerCase(Locale.ENGLISH);-        int lo = nextIndexOf(loScan);-        if (lo > -1) {-            lastIcIndex = bufPos + lo; return true;+        int scanLength = seq.length();+        int maxStart = bufLength - scanLength;+        for (int scan = bufPos; scan <= maxStart; scan++) {+            if (rangeMatchesIgnoreCase(seq, scan)) {+                lastIcIndex = scan;+                return true;+            }         } -        String hiScan = seq.toUpperCase(Locale.ENGLISH);-        int hi = nextIndexOf(hiScan);-        boolean found = hi > -1;-        lastIcIndex = found ? bufPos + hi : -1; // we don't care about finding the nearest, just that buf contains-        return found;+        lastIcIndex = -1;+        return false;     }      @Overridesrc/test/java/org/jsoup/parser/CharacterReaderTest.javaindex 8f0ee30c..6ae2282a 100644@@ -228,10 +228,16 @@ public class CharacterReaderTest {      @Test public void containsIgnoreCase() {         CharacterReader r = new CharacterReader("One TWO three");+        assertTrue(r.containsIgnoreCase("one"));         assertTrue(r.containsIgnoreCase("two"));+        assertTrue(r.containsIgnoreCase("TwO"));         assertTrue(r.containsIgnoreCase("three"));-        // weird one: does not find one, because it scans for consistent case only-        assertFalse(r.containsIgnoreCase("one"));+        assertFalse(r.containsIgnoreCase("four"));+    }++    @Test public void containsIgnoreCaseFindsMixedCase() {+        CharacterReader r = new CharacterReader("<p>text</TiTlE>");+        assertTrue(r.containsIgnoreCase("</title>"));     }      @Test void containsIgnoreCaseBuffer() {src/test/java/org/jsoup/parser/TokeniserStateTest.javaindex 3199d8d0..fca73f95 100644@@ -89,6 +89,18 @@ public class TokeniserStateTest {         els = doc.select("textarea");         assertEquals("<fake>", els.text()); +        body = "<textarea><fake></TeXtArEa>";+        doc = Jsoup.parse(body);+        els = doc.select("textarea");+        assertEquals("<fake>", els.text());+        assertEquals(0, doc.select("fake").size());++        body = "<title><p>One</TiTlE>";+        doc = Jsoup.parse(body);+        els = doc.select("title");+        assertEquals("<p>One", els.text());+        assertEquals(0, doc.select("body p").size());+         body = "<textarea><open";         doc = Jsoup.parse(body);         els = doc.select("textarea");