Backtestsjsouprun-20261006-181938#2611

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

Update select parsing to the current HTML Standard

Better

Our patch was judged better than the merged fix.

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

The production changes in the two patches are nearly identical. Both:
- remove the InSelect and InSelectInTable modes, SelectScopeMember and inSelectScope;
- drop the select case from resetInsertionMode;
- add a select-fragment-context check;
- make input and nested select close an open select in scope;
- make hr generate implied end tags when a select is in scope;
- split option and optgroup handling to follow the current spec;
- add select to InBodyEndClosers.

There are two small code differences:
- For an option or optgroup outside a select, B pops the current option directly with `tb.pop()`. This matches the spec text ("pop the current node") more literally. A routes through `processEndTag("option")`, which behaves the same here.
- For a nested select, A uses `break` and B uses `return false`. Both ignore the token after popping, so the result is equivalent.

Correctness is effectively equal, so tests decide it. A adds three small tests: select content retained, hr closing option and optgroup, and input closing select.

B adds a dedicated SelectParsingTest with broad coverage:
- customizable select content, including button and selectedcontent, with zero errors;
- formatting-element reconstruction;
- textarea and keygen staying inside the select;
- nested select;
- input inside a table within a select not closing the select, since the table is a scope boundary;
- optgroup and option implied-end behaviour;
- options outside a select;
- stray `</select>`;
- select inside table cells and fostered out of a table;
- the select fragment context, both via parseFragment and via `Element.html()`.

B's coverage exercises the edge cases of the scope-based rules and the fragment paths. It also updates the namespace-aware option test rather than just deleting its assertions, and its changelog entry is more informative. On tests and thoroughness, B is the stronger patch.

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 13s

Wall-clock time in the container

Full test suite

No new failures

2,340 passed · 0 skipped · 2,340 total

Contamination audit

Clean

No sign the agent saw the real fix

Patch size

355vs 234 merged

Changed lines, ours vs merged

jsoup has the former InSelect and InSelectInTable insertion modes.

The current HTML parsing rules handle select content through the in-body insertion mode, including customizable select content.

We should update the tree builder to follow the current rules and retire the obsolete select insertion modes.

The two patches

Our patch+195 −160
CHANGES.mdindex a5a636e5..90dd3ea5 100644@@ -18,6 +18,7 @@ * Updated the adoption-agency algorithm to match the current HTML5 spec and to preserve formatting order when recovering misnested elements. [#2604](https://github.com/jhy/jsoup/pull/2604) * `:has()` now correctly matches sibling alternatives and descendants of siblings. Given `<div id=a></div><div><span></span></div>`, both `#a:has(+ div, > span)` and `#a:has(+ div span)` select `#a`. [#2610](https://github.com/jhy/jsoup/pull/2610) * Within node selectors, `:not()` now correctly applies `:contains()` and `:matches()` to the node value. For example, `::comment:not(:contains(foo))` excludes comments containing `foo`. [#2610](https://github.com/jhy/jsoup/pull/2610)+* Updated `select` parsing to the current HTML spec, which handles select content in the in-body insertion mode and retires the obsolete "in select" and "in select in table" modes. Customizable select content such as `<select><button><selectedcontent></selectedcontent></button><option><img><span>One</span></option></select>` is now kept in the select rather than dropped. A nested `<select>` or an `<input>` still closes the open select. A `<textarea>` or `<keygen>` inside a select now stays in it. `<hr>` within a select closes the open option.  ## 1.23.2 (2026-Aug-26) src/main/java/org/jsoup/parser/HtmlTagOptions.javaindex 1dd5e84c..7b521b71 100644@@ -14,10 +14,9 @@ final class HtmlTagOptions {     static final int ListScope          = 1 << 1;     static final int ButtonScope        = 1 << 2;     static final int TableScope         = 1 << 3;-    static final int SelectScopeMember  = 1 << 4;-    static final int ImpliedEnd         = 1 << 5;-    static final int ThoroughImpliedEnd = 1 << 6;-    static final int Special            = 1 << 7;+    static final int ImpliedEnd         = 1 << 4;+    static final int ThoroughImpliedEnd = 1 << 5;+    static final int Special            = 1 << 6;      static final String[] ScopeTags = new String[]{ // a particular element in scope             "applet", "caption", "html", "marquee", "object", "select", "table", "td", "template", "th"};@@ -26,7 +25,6 @@ final class HtmlTagOptions {     static final String[] ListScopeTags = new String[]{"ol", "ul"};     static final String[] ButtonScopeTags = new String[]{"button"};     static final String[] TableScopeTags = new String[]{"html", "table", "template"};-    static final String[] SelectScopeMemberTags = new String[]{"optgroup", "option"};     static final String[] ImpliedEndTags = new String[]{             "dd", "dt", "li", "optgroup", "option", "p", "rb", "rp", "rt", "rtc"};     static final String[] ThoroughImpliedEndTags = new String[]{@@ -53,7 +51,6 @@ final class HtmlTagOptions {             case NamespaceHtml:                 if (StringUtil.inSorted(normalName, ImpliedEndTags))         options |= ImpliedEnd;                 if (StringUtil.inSorted(normalName, ThoroughImpliedEndTags)) options |= ThoroughImpliedEnd;-                if (StringUtil.inSorted(normalName, SelectScopeMemberTags))  options |= SelectScopeMember;                 if (StringUtil.inSorted(normalName, ScopeTags))              options |= Scope;                 if (StringUtil.inSorted(normalName, ListScopeTags))          options |= ListScope;                 if (StringUtil.inSorted(normalName, ButtonScopeTags))        options |= ButtonScope;src/main/java/org/jsoup/parser/HtmlTreeBuilder.javaindex 65658bca..58fed421 100644@@ -352,6 +352,12 @@ public class HtmlTreeBuilder extends TreeBuilder {         return fragmentParsing;     } +    /** Tests if this is a fragment parse with an HTML {@code select} context element. */+    boolean isSelectFragmentContext() {+        return fragmentParsing && contextElement != null+            && NamespaceHtml.equals(contextElement.tag().namespace()) && contextElement.normalName().equals("select");+    }+     void error(HtmlTreeBuilderState state) {         if (parser.getErrors().canAddError())             parser.getErrors().add(new ParseError(reader, "Unexpected %s token [%s] when in state [%s]",@@ -769,10 +775,6 @@ public class HtmlTreeBuilder extends TreeBuilder {             String name = node != null && NamespaceHtml.equals(node.tag().namespace()) ? node.normalName() : "";              switch (name) {-                case "select":-                    transition(HtmlTreeBuilderState.InSelect);-                    // todo - should loop up (with some limit) and check for table or template hits-                    break LOOP;                 case "td":                 case "th":                     if (!last) {@@ -891,19 +893,6 @@ public class HtmlTreeBuilder extends TreeBuilder {         return inSpecificScope(targetName, HtmlTagOptions.TableScope);     } -    boolean inSelectScope(String targetName) {-        for (int pos = stack.size() -1; pos >= 0; pos--) {-            Element el = stack.get(pos);-            String elName = el.normalName();-            if (elName.equals(targetName))-                return true;-            // Select scope stops at the first element that is not option / optgroup.-            if (!el.tag().hasParserOption(HtmlTagOptions.SelectScopeMember))-                return false;-        }-        return false; // nothing left on stack-    }-     /** Tests if there is some element on the stack that is not in the provided set. */     boolean onStackNot(String[] allowedTags) {         for (int pos = stack.size() - 1; pos >= 0; pos--) {src/main/java/org/jsoup/parser/HtmlTreeBuilderState.javaindex 49c1f931..bc60d4bd 100644@@ -422,6 +422,14 @@ enum HtmlTreeBuilderState {                     tb.transition(InTable);                     break;                 case "input":+                    if (tb.isSelectFragmentContext()) {+                        tb.error(this);+                        return false; // ignore+                    }+                    if (tb.inScope("select")) {+                        tb.error(this);+                        tb.popStackToClose("select");+                    }                     tb.reconstructFormattingElements();                     el = tb.insertEmptyElementFor(startTag);                     if (!equalsIgnoreAsciiCase(el.attr("type"), "hidden"))@@ -431,6 +439,11 @@ enum HtmlTreeBuilderState {                     if (tb.inButtonScope("p")) {                         tb.processEndTag("p");                     }+                    if (tb.inScope("select")) {+                        tb.generateImpliedEndTags();+                        if (tb.inScope("option") || tb.inScope("optgroup"))+                            tb.error(this);+                    }                     tb.insertEmptyElementFor(startTag);                     tb.framesetOk(false);                     break;@@ -464,16 +477,18 @@ enum HtmlTreeBuilderState {                     tb.startNoscript(startTag);                     break;                 case "select":+                    if (tb.isSelectFragmentContext()) {+                        tb.error(this);+                        return false; // ignore+                    } else if (tb.inScope("select")) {+                        // a nested select closes the open select, and is otherwise ignored+                        tb.error(this);+                        tb.popStackToClose("select");+                        return false;+                    }                     tb.reconstructFormattingElements();                     tb.insertElementFor(startTag);                     tb.framesetOk(false);-                    if (startTag.selfClosing) break; // don't change states if not added to the stack--                    HtmlTreeBuilderState state = tb.state();-                    if (state.equals(InTable) || state.equals(InCaption) || state.equals(InTableBody) || state.equals(InRow) || state.equals(InCell))-                        tb.transition(InSelectInTable);-                    else-                        tb.transition(InSelect);                     break;                 case "math":                     tb.reconstructFormattingElements();@@ -531,10 +546,25 @@ enum HtmlTreeBuilderState {                     tb.insertElementFor(startTag);                     break; -                case "optgroup":                 case "option":-                    if (tb.currentElementIs("option"))-                        tb.processEndTag("option");+                    if (tb.inScope("select")) {+                        tb.generateImpliedEndTags("optgroup");+                        if (tb.inScope("option"))+                            tb.error(this);+                    } else if (tb.currentElementIs("option")) {+                        tb.pop();+                    }+                    tb.reconstructFormattingElements();+                    tb.insertElementFor(startTag);+                    break;+                case "optgroup":+                    if (tb.inScope("select")) {+                        tb.generateImpliedEndTags();+                        if (tb.inScope("option") || tb.inScope("optgroup"))+                            tb.error(this);+                    } else if (tb.currentElementIs("option")) {+                        tb.pop();+                    }                     tb.reconstructFormattingElements();                     tb.insertElementFor(startTag);                     break;@@ -1380,124 +1410,6 @@ enum HtmlTreeBuilderState {                 tb.processEndTag("th"); // only here if th or td in scope         }     },-    InSelect {-        @Override boolean process(Token t, HtmlTreeBuilder tb) {-            final String name;--            switch (t.type) {-                case Character:-                    tb.insertCharacterNode(t.asCharacter());-                    break;-                case Comment:-                    tb.insertCommentNode(t.asComment());-                    break;-                case Doctype:-                    tb.error(this);-                    return false;-                case StartTag:-                    Token.StartTag start = t.asStartTag();-                    name = start.normalName();-                    if (name.equals("html"))-                        return tb.process(start, InBody);-                    else if (name.equals("option")) {-                        if (tb.currentElementIs("option"))-                            tb.processEndTag("option");-                        tb.insertElementFor(start);-                    } else if (name.equals("optgroup")) {-                        if (tb.currentElementIs("option"))-                            tb.processEndTag("option"); // pop option and flow to pop optgroup-                        if (tb.currentElementIs("optgroup"))-                            tb.processEndTag("optgroup");-                        tb.insertElementFor(start);-                    } else if (name.equals("select")) {-                        tb.error(this);-                        return tb.processEndTag("select");-                    } else if (inSorted(name, InSelectEnd)) {-                        tb.error(this);-                        if (!tb.inSelectScope("select"))-                            return false; // frag-                        // spec says close select then reprocess; leads to recursion. iter directly:-                        do {-                            tb.popStackToClose("select");-                            tb.resetInsertionMode();-                        } while (tb.inSelectScope("select")); // collapse invalid nested selects-                        return tb.process(start);-                    } else if (name.equals("script") || name.equals("template")) {-                        return tb.process(t, InHead);-                    } else if (name.equals("noscript")) {-                        tb.startNoscript(start);-                    } else {-                        return anythingElse(t, tb);-                    }-                    break;-                case EndTag:-                    Token.EndTag end = t.asEndTag();-                    name = end.normalName();-                    switch (name) {-                        case "optgroup":-                            if (tb.currentElementIs("option") && tb.aboveOnStack(tb.currentElement()) != null && tb.aboveOnStack(tb.currentElement()).nameIs("optgroup"))-                                tb.processEndTag("option");-                            if (tb.currentElementIs("optgroup"))-                                tb.pop();-                            else-                                tb.error(this);-                            break;-                        case "option":-                            if (tb.currentElementIs("option"))-                                tb.pop();-                            else-                                tb.error(this);-                            break;-                        case "select":-                            if (!tb.inSelectScope(name)) {-                                tb.error(this);-                                return false;-                            } else {-                                tb.popStackToClose(name);-                                tb.resetInsertionMode();-                            }-                            break;-                        case "template":-                            return tb.process(t, InHead);-                        default:-                            return anythingElse(t, tb);-                    }-                    break;-                case EOF:-                    if (!tb.currentElementIs("html"))-                        tb.error(this);-                    break;-                default:-                    return anythingElse(t, tb);-            }-            return true;-        }--        private boolean anythingElse(Token t, HtmlTreeBuilder tb) {-            tb.error(this);-            return false;-        }-    },-    InSelectInTable {-        @Override boolean process(Token t, HtmlTreeBuilder tb) {-            if (t.isStartTag() && inSorted(t.asStartTag().normalName(), InSelectTableEnd)) {-                tb.error(this);-                tb.popStackToClose("select");-                tb.resetInsertionMode();-                return tb.process(t);-            } else if (t.isEndTag() && inSorted(t.asEndTag().normalName(), InSelectTableEnd)) {-                tb.error(this);-                if (tb.inTableScope(t.asEndTag().normalName())) {-                    tb.popStackToClose("select");-                    tb.resetInsertionMode();-                    return (tb.process(t));-                } else-                    return false;-            } else {-                return tb.process(t, InSelect);-            }-        }-    },     InTemplate {         @Override boolean process(Token t, HtmlTreeBuilder tb) {             final String name;@@ -1876,7 +1788,7 @@ enum HtmlTreeBuilderState {         static final String[] InBodyStartDrop = new String[]{"caption", "col", "colgroup", "frame", "head", "tbody", "td", "tfoot", "th", "thead", "tr"};         static final String[] InBodyEndClosers = new String[]{"address", "article", "aside", "blockquote", "button", "center", "details", "dir", "div",             "dl", "fieldset", "figcaption", "figure", "footer", "header", "hgroup", "listing", "menu",-            "nav", "ol", "pre", "section", "summary", "ul"};+            "nav", "ol", "pre", "section", "select", "summary", "ul"};         static final String[] InBodyEndOtherErrors = new String[] {"body", "dd", "dt", "html", "li", "optgroup", "option", "p", "rb", "rp", "rt", "rtc", "tbody", "td", "tfoot", "th", "thead", "tr"};         static final String[] InBodyEndAdoptionFormatters = new String[]{"a", "b", "big", "code", "em", "font", "i", "nobr", "s", "small", "strike", "strong", "tt", "u"};         static final String[] InTableToBody = new String[]{"tbody", "tfoot", "thead"};@@ -1892,8 +1804,6 @@ enum HtmlTreeBuilderState {         static final String[] InTableBodyEndIgnore = new String[]{"body", "caption", "col", "colgroup", "html", "td", "th", "tr"};         static final String[] InRowMissing = new String[]{"caption", "col", "colgroup", "tbody", "tfoot", "thead", "tr"};         static final String[] InRowIgnore = new String[]{"body", "caption", "col", "colgroup", "html", "td", "th"};-        static final String[] InSelectEnd = new String[]{"input", "keygen", "textarea"};-        static final String[] InSelectTableEnd = new String[]{"caption", "table", "tbody", "td", "tfoot", "th", "thead", "tr"};         static final String[] InTableEndIgnore = new String[]{"tbody", "tfoot", "thead"};         static final String[] InCaptionIgnore = new String[]{"body", "col", "colgroup", "html", "tbody", "td", "tfoot", "th", "thead", "tr"};         static final String[] InTemplateToHead = new String[] {"base", "basefont", "bgsound", "link", "meta", "noframes", "script", "style", "template", "title"};src/test/java/org/jsoup/parser/HtmlParserTest.javaindex e883ae73..f9946441 100644@@ -155,12 +155,14 @@ public class HtmlParserTest {     }      @Test public void parsesUnterminatedOption() {-        // bit weird this -- browsers and spec get stuck in select until there's a </select>+        // the select is a scope boundary, so the </p> doesn't close the outer p, and the content stays in the select         Document doc = Jsoup.parse("<body><p><select><option>One<option>Two</p><p>Three</p>");         Elements options = doc.select("option");         assertEquals(2, options.size());         assertEquals("One", options.first().text());-        assertEquals("TwoThree", options.last().text());+        assertEquals("Two Three", options.last().text());+        assertEquals("<p><select><option>One</option><option>Two<p></p><p>Three</p></option></select></p>",+            TextUtil.stripNewlines(doc.body().html()));     }      @Test public void testSelectWithOption() {src/test/java/org/jsoup/parser/HtmlTreeBuilderStateTest.javaindex 729e61e3..b9826a5e 100644@@ -45,7 +45,7 @@ public class HtmlTreeBuilderStateTest {     public void ensureArraysAreSorted() {         List<Object[]> constants = findConstantArrays(Constants.class);         ensureSorted(constants);-        assertEquals(37, constants.size());+        assertEquals(35, constants.size());     }      @Test public void ensureTagSearchesAreKnownTags() {src/test/java/org/jsoup/parser/HtmlTreeBuilderTest.javaindex 6e0f19ed..26834759 100644@@ -212,7 +212,7 @@ public class HtmlTreeBuilderTest {          List<Object[]> tagOptionArrays = HtmlTreeBuilderStateTest.findConstantArrays(HtmlTagOptions.class);         HtmlTreeBuilderStateTest.ensureSorted(tagOptionArrays);-        assertEquals(10, tagOptionArrays.size());+        assertEquals(9, tagOptionArrays.size());     }      @Test@@ -316,11 +316,11 @@ public class HtmlTreeBuilderTest {     @Test void parserOptionsAreNamespaceAware() {         Tag htmlOption = Tag.valueOf("option", NamespaceHtml, ParseSettings.htmlDefault);         assertTrue(htmlOption.hasParserOption(HtmlTagOptions.ImpliedEnd));-        assertTrue(htmlOption.hasParserOption(HtmlTagOptions.SelectScopeMember));+        assertTrue(htmlOption.hasParserOption(HtmlTagOptions.ThoroughImpliedEnd));          Tag svgOption = Tag.valueOf("option", Parser.NamespaceSvg, ParseSettings.htmlDefault);         assertFalse(svgOption.hasParserOption(HtmlTagOptions.ImpliedEnd));-        assertFalse(svgOption.hasParserOption(HtmlTagOptions.SelectScopeMember));+        assertFalse(svgOption.hasParserOption(HtmlTagOptions.ThoroughImpliedEnd));     }      @Test void impliedEndTagsOnlyPopHtmlElements() {src/test/java/org/jsoup/parser/SelectParsingTest.javanew file mode 100644index 00000000..e241e2b9@@ -0,0 +1,136 @@+package org.jsoup.parser;++import org.jsoup.Jsoup;+import org.jsoup.TextUtil;+import org.jsoup.nodes.Document;+import org.jsoup.nodes.Element;+import org.jsoup.nodes.Node;+import org.junit.jupiter.api.Test;++import java.util.List;++import static org.junit.jupiter.api.Assertions.*;++/**+ Tests that select, option, and optgroup content is parsed through the in body insertion mode, per the current HTML+ Standard (which retired the "in select" and "in select in table" insertion modes).+ */+public class SelectParsingTest {++    private static String body(String html) {+        return TextUtil.stripNewlines(Jsoup.parse(html).body().html());+    }++    private static int errorCount(String html) {+        Parser parser = Parser.htmlParser().setTrackErrors(10);+        parser.parseInput(html, "");+        return parser.getErrors().size();+    }++    @Test void customizableSelectContentIsKept() {+        String html = "<select><button><selectedcontent></selectedcontent></button>" ++            "<option><img src=x.png><span>One</span></option><div><option>Two</option></div></select>";+        assertEquals("<select><button><selectedcontent></selectedcontent></button>" ++            "<option><img src=\"x.png\"><span>One</span></option><div><option>Two</option></div></select>", body(html));+        assertEquals(0, errorCount(html));+    }++    @Test void formattingElementsInSelect() {+        // option does not generate an implied end for the open b, so nests within it (a parse error)+        String html = "<select><option><b>One<option>Two</select>";+        assertEquals("<select><option><b>One<option>Two</option></b></option></select>", body(html));+        assertTrue(errorCount(html) > 0);+        // formatting elements are reconstructed before the next option is inserted+        assertEquals("<select><option><b>One</b></option><b><option>Two</option></b></select>",+            body("<select><option><b>One</option><option>Two</select>"));+    }++    @Test void textareaAndKeygenStayInSelect() {+        assertEquals("<select><textarea>&lt;option&gt;</textarea><keygen></select><p>x</p>",+            body("<select><textarea><option></textarea><keygen></select><p>x"));+    }++    @Test void nestedSelectClosesOpenSelect() {+        String html = "<select><option>A<select><option>B</select>C";+        assertEquals("<select><option>A</option></select><option>BC</option>", body(html));+        assertTrue(errorCount(html) > 0);+    }++    @Test void inputClosesOpenSelect() {+        assertEquals("<select><option>A</option></select><input>B", body("<select><option>A<input>B"));+        assertEquals("<select><div><option>A</option></div></select><input>",+            body("<select><div><option>A<input></div></select>"));+    }++    @Test void inputInsideTableInSelectDoesNotCloseSelect() {+        // the table is a scope boundary, so the select is not in scope+        assertEquals("<select><table><tbody><tr><td><input></td></tr></tbody></table></select>",+            body("<select><table><tr><td><input></td></tr></table></select>"));+    }++    @Test void hrInSelectClosesOption() {+        assertEquals("<select><option>A</option><hr><option>B</option></select>",+            body("<select><option>A<hr><option>B</select>"));+        assertEquals("<select><optgroup><option>A</option></optgroup><hr></select>",+            body("<select><optgroup><option>A<hr></select>"));+    }++    @Test void optionInSelectClosesOptionButNotOptgroup() {+        assertEquals("<select><optgroup><option>A</option><option>B</option></optgroup>" ++                "<optgroup><option>C</option></optgroup></select>",+            body("<select><optgroup><option>A<option>B<optgroup><option>C</select>"));+    }++    @Test void optionInSelectClosesImpliedEndTags() {+        // the option start tag generates implied end tags within the select, so a p is closed+        assertEquals("<select><option><p>A</p></option><option>B</option></select>",+            body("<select><option><p>A<option>B</select>"));+    }++    @Test void optionsOutsideSelect() {+        assertEquals("<option>A</option><option>B</option><optgroup><option>C</option></optgroup>",+            body("<option>A<option>B<optgroup><option>C"));+    }++    @Test void selectEndTagClosesNestedContent() {+        assertEquals("<select><div><span><option>A</option></span></div></select><p>x</p>",+            body("<select><div><span><option>A</select><p>x"));+    }++    @Test void strayEndSelectIgnored() {+        String html = "<div>A</select>B</div>";+        assertEquals("<div>AB</div>", body(html));+        assertTrue(errorCount(html) > 0);+    }++    @Test void selectInCellClosedByNextCell() {+        assertEquals("<table><tbody><tr><td><select><option>A</option></select></td><td>B</td></tr></tbody></table>",+            body("<table><tr><td><select><option>A<td>B</table>"));+        assertEquals("<table><tbody><tr><td><select><option>A</option></select></td></tr></tbody></table><p>B</p>",+            body("<table><tr><td><select><option>A</table><p>B"));+    }++    @Test void selectInTableIsFostered() {+        assertEquals("<select><option>A</option></select><table><tbody><tr><td>B</td></tr></tbody></table>",+            body("<table><select><option>A</select><tr><td>B</td></tr></table>"));+        // table structure tags close the fostered select+        assertEquals("<select><option>A</option></select><table><tbody><tr><td>B</td></tr></tbody></table>",+            body("<table><select><option>A<tr><td>B</td></tr></table>"));+    }++    @Test void selectFragmentContext() {+        Element context = new Element("select");+        List<Node> nodes = Parser.parseFragment("<option>A<select><input><option>B</option><optgroup><option>C", context, "");+        Element holder = new Element("div");+        holder.appendChildren(nodes);+        assertEquals("<option>A</option><option>B</option><optgroup><option>C</option></optgroup>",+            TextUtil.stripNewlines(holder.html()));+    }++    @Test void selectFragmentViaElementHtml() {+        Document doc = Jsoup.parse("<select id=s></select>");+        Element select = doc.expectFirst("#s");+        select.html("<option>One<select><option>Two<input>");+        assertEquals("<option>One</option><option>Two</option>", TextUtil.stripNewlines(select.html()));+    }+} 
Merged fix · PR #2612+75 −159
CHANGES.mdindex a5a636e5..28721ace 100644@@ -18,6 +18,7 @@ * Updated the adoption-agency algorithm to match the current HTML5 spec and to preserve formatting order when recovering misnested elements. [#2604](https://github.com/jhy/jsoup/pull/2604) * `:has()` now correctly matches sibling alternatives and descendants of siblings. Given `<div id=a></div><div><span></span></div>`, both `#a:has(+ div, > span)` and `#a:has(+ div span)` select `#a`. [#2610](https://github.com/jhy/jsoup/pull/2610) * Within node selectors, `:not()` now correctly applies `:contains()` and `:matches()` to the node value. For example, `::comment:not(:contains(foo))` excludes comments containing `foo`. [#2610](https://github.com/jhy/jsoup/pull/2610)+* Updated `<select>` parsing to the current HTML spec, retaining elements such as buttons and other markup that the previous rules discarded. [#2611](https://github.com/jhy/jsoup/issues/2611)  ## 1.23.2 (2026-Aug-26) src/main/java/org/jsoup/parser/HtmlTagOptions.javaindex 1dd5e84c..7b521b71 100644@@ -14,10 +14,9 @@ final class HtmlTagOptions {     static final int ListScope          = 1 << 1;     static final int ButtonScope        = 1 << 2;     static final int TableScope         = 1 << 3;-    static final int SelectScopeMember  = 1 << 4;-    static final int ImpliedEnd         = 1 << 5;-    static final int ThoroughImpliedEnd = 1 << 6;-    static final int Special            = 1 << 7;+    static final int ImpliedEnd         = 1 << 4;+    static final int ThoroughImpliedEnd = 1 << 5;+    static final int Special            = 1 << 6;      static final String[] ScopeTags = new String[]{ // a particular element in scope             "applet", "caption", "html", "marquee", "object", "select", "table", "td", "template", "th"};@@ -26,7 +25,6 @@ final class HtmlTagOptions {     static final String[] ListScopeTags = new String[]{"ol", "ul"};     static final String[] ButtonScopeTags = new String[]{"button"};     static final String[] TableScopeTags = new String[]{"html", "table", "template"};-    static final String[] SelectScopeMemberTags = new String[]{"optgroup", "option"};     static final String[] ImpliedEndTags = new String[]{             "dd", "dt", "li", "optgroup", "option", "p", "rb", "rp", "rt", "rtc"};     static final String[] ThoroughImpliedEndTags = new String[]{@@ -53,7 +51,6 @@ final class HtmlTagOptions {             case NamespaceHtml:                 if (StringUtil.inSorted(normalName, ImpliedEndTags))         options |= ImpliedEnd;                 if (StringUtil.inSorted(normalName, ThoroughImpliedEndTags)) options |= ThoroughImpliedEnd;-                if (StringUtil.inSorted(normalName, SelectScopeMemberTags))  options |= SelectScopeMember;                 if (StringUtil.inSorted(normalName, ScopeTags))              options |= Scope;                 if (StringUtil.inSorted(normalName, ListScopeTags))          options |= ListScope;                 if (StringUtil.inSorted(normalName, ButtonScopeTags))        options |= ButtonScope;src/main/java/org/jsoup/parser/HtmlTreeBuilder.javaindex 65658bca..bc221bcb 100644@@ -352,6 +352,11 @@ public class HtmlTreeBuilder extends TreeBuilder {         return fragmentParsing;     } +    /** Tests whether this fragment was created with the named HTML context element. */+    boolean fragmentContextIs(String normalName) {+        return fragmentParsing && contextElement != null && contextElement.elementIs(normalName, NamespaceHtml);+    }+     void error(HtmlTreeBuilderState state) {         if (parser.getErrors().canAddError())             parser.getErrors().add(new ParseError(reader, "Unexpected %s token [%s] when in state [%s]",@@ -769,10 +774,6 @@ public class HtmlTreeBuilder extends TreeBuilder {             String name = node != null && NamespaceHtml.equals(node.tag().namespace()) ? node.normalName() : "";              switch (name) {-                case "select":-                    transition(HtmlTreeBuilderState.InSelect);-                    // todo - should loop up (with some limit) and check for table or template hits-                    break LOOP;                 case "td":                 case "th":                     if (!last) {@@ -891,19 +892,6 @@ public class HtmlTreeBuilder extends TreeBuilder {         return inSpecificScope(targetName, HtmlTagOptions.TableScope);     } -    boolean inSelectScope(String targetName) {-        for (int pos = stack.size() -1; pos >= 0; pos--) {-            Element el = stack.get(pos);-            String elName = el.normalName();-            if (elName.equals(targetName))-                return true;-            // Select scope stops at the first element that is not option / optgroup.-            if (!el.tag().hasParserOption(HtmlTagOptions.SelectScopeMember))-                return false;-        }-        return false; // nothing left on stack-    }-     /** Tests if there is some element on the stack that is not in the provided set. */     boolean onStackNot(String[] allowedTags) {         for (int pos = stack.size() - 1; pos >= 0; pos--) {src/main/java/org/jsoup/parser/HtmlTreeBuilderState.javaindex 49c1f931..7c003fe5 100644@@ -422,6 +422,14 @@ enum HtmlTreeBuilderState {                     tb.transition(InTable);                     break;                 case "input":+                    if (tb.fragmentContextIs("select")) {+                        tb.error(this);+                        return false;+                    }+                    if (tb.inScope("select")) {+                        tb.error(this);+                        tb.popStackToClose("select");+                    }                     tb.reconstructFormattingElements();                     el = tb.insertEmptyElementFor(startTag);                     if (!equalsIgnoreAsciiCase(el.attr("type"), "hidden"))@@ -431,6 +439,11 @@ enum HtmlTreeBuilderState {                     if (tb.inButtonScope("p")) {                         tb.processEndTag("p");                     }+                    if (tb.inScope("select")) {+                        tb.generateImpliedEndTags();+                        if (tb.inScope("option") || tb.inScope("optgroup"))+                            tb.error(this);+                    }                     tb.insertEmptyElementFor(startTag);                     tb.framesetOk(false);                     break;@@ -464,16 +477,18 @@ enum HtmlTreeBuilderState {                     tb.startNoscript(startTag);                     break;                 case "select":+                    if (tb.fragmentContextIs("select")) {+                        tb.error(this);+                        return false;+                    }+                    if (tb.inScope("select")) {+                        tb.error(this);+                        tb.popStackToClose("select");+                        break;+                    }                     tb.reconstructFormattingElements();                     tb.insertElementFor(startTag);                     tb.framesetOk(false);-                    if (startTag.selfClosing) break; // don't change states if not added to the stack--                    HtmlTreeBuilderState state = tb.state();-                    if (state.equals(InTable) || state.equals(InCaption) || state.equals(InTableBody) || state.equals(InRow) || state.equals(InCell))-                        tb.transition(InSelectInTable);-                    else-                        tb.transition(InSelect);                     break;                 case "math":                     tb.reconstructFormattingElements();@@ -531,10 +546,26 @@ enum HtmlTreeBuilderState {                     tb.insertElementFor(startTag);                     break; -                case "optgroup":                 case "option":-                    if (tb.currentElementIs("option"))+                    if (tb.inScope("select")) {+                        tb.generateImpliedEndTags("optgroup");+                        if (tb.inScope("option"))+                            tb.error(this);+                    } else if (tb.currentElementIs("option")) {+                        tb.processEndTag("option");+                    }+                    tb.reconstructFormattingElements();+                    tb.insertElementFor(startTag);+                    break;++                case "optgroup":+                    if (tb.inScope("select")) {+                        tb.generateImpliedEndTags();+                        if (tb.inScope("option") || tb.inScope("optgroup"))+                            tb.error(this);+                    } else if (tb.currentElementIs("option")) {                         tb.processEndTag("option");+                    }                     tb.reconstructFormattingElements();                     tb.insertElementFor(startTag);                     break;@@ -1380,124 +1411,6 @@ enum HtmlTreeBuilderState {                 tb.processEndTag("th"); // only here if th or td in scope         }     },-    InSelect {-        @Override boolean process(Token t, HtmlTreeBuilder tb) {-            final String name;--            switch (t.type) {-                case Character:-                    tb.insertCharacterNode(t.asCharacter());-                    break;-                case Comment:-                    tb.insertCommentNode(t.asComment());-                    break;-                case Doctype:-                    tb.error(this);-                    return false;-                case StartTag:-                    Token.StartTag start = t.asStartTag();-                    name = start.normalName();-                    if (name.equals("html"))-                        return tb.process(start, InBody);-                    else if (name.equals("option")) {-                        if (tb.currentElementIs("option"))-                            tb.processEndTag("option");-                        tb.insertElementFor(start);-                    } else if (name.equals("optgroup")) {-                        if (tb.currentElementIs("option"))-                            tb.processEndTag("option"); // pop option and flow to pop optgroup-                        if (tb.currentElementIs("optgroup"))-                            tb.processEndTag("optgroup");-                        tb.insertElementFor(start);-                    } else if (name.equals("select")) {-                        tb.error(this);-                        return tb.processEndTag("select");-                    } else if (inSorted(name, InSelectEnd)) {-                        tb.error(this);-                        if (!tb.inSelectScope("select"))-                            return false; // frag-                        // spec says close select then reprocess; leads to recursion. iter directly:-                        do {-                            tb.popStackToClose("select");-                            tb.resetInsertionMode();-                        } while (tb.inSelectScope("select")); // collapse invalid nested selects-                        return tb.process(start);-                    } else if (name.equals("script") || name.equals("template")) {-                        return tb.process(t, InHead);-                    } else if (name.equals("noscript")) {-                        tb.startNoscript(start);-                    } else {-                        return anythingElse(t, tb);-                    }-                    break;-                case EndTag:-                    Token.EndTag end = t.asEndTag();-                    name = end.normalName();-                    switch (name) {-                        case "optgroup":-                            if (tb.currentElementIs("option") && tb.aboveOnStack(tb.currentElement()) != null && tb.aboveOnStack(tb.currentElement()).nameIs("optgroup"))-                                tb.processEndTag("option");-                            if (tb.currentElementIs("optgroup"))-                                tb.pop();-                            else-                                tb.error(this);-                            break;-                        case "option":-                            if (tb.currentElementIs("option"))-                                tb.pop();-                            else-                                tb.error(this);-                            break;-                        case "select":-                            if (!tb.inSelectScope(name)) {-                                tb.error(this);-                                return false;-                            } else {-                                tb.popStackToClose(name);-                                tb.resetInsertionMode();-                            }-                            break;-                        case "template":-                            return tb.process(t, InHead);-                        default:-                            return anythingElse(t, tb);-                    }-                    break;-                case EOF:-                    if (!tb.currentElementIs("html"))-                        tb.error(this);-                    break;-                default:-                    return anythingElse(t, tb);-            }-            return true;-        }--        private boolean anythingElse(Token t, HtmlTreeBuilder tb) {-            tb.error(this);-            return false;-        }-    },-    InSelectInTable {-        @Override boolean process(Token t, HtmlTreeBuilder tb) {-            if (t.isStartTag() && inSorted(t.asStartTag().normalName(), InSelectTableEnd)) {-                tb.error(this);-                tb.popStackToClose("select");-                tb.resetInsertionMode();-                return tb.process(t);-            } else if (t.isEndTag() && inSorted(t.asEndTag().normalName(), InSelectTableEnd)) {-                tb.error(this);-                if (tb.inTableScope(t.asEndTag().normalName())) {-                    tb.popStackToClose("select");-                    tb.resetInsertionMode();-                    return (tb.process(t));-                } else-                    return false;-            } else {-                return tb.process(t, InSelect);-            }-        }-    },     InTemplate {         @Override boolean process(Token t, HtmlTreeBuilder tb) {             final String name;@@ -1876,7 +1789,7 @@ enum HtmlTreeBuilderState {         static final String[] InBodyStartDrop = new String[]{"caption", "col", "colgroup", "frame", "head", "tbody", "td", "tfoot", "th", "thead", "tr"};         static final String[] InBodyEndClosers = new String[]{"address", "article", "aside", "blockquote", "button", "center", "details", "dir", "div",             "dl", "fieldset", "figcaption", "figure", "footer", "header", "hgroup", "listing", "menu",-            "nav", "ol", "pre", "section", "summary", "ul"};+            "nav", "ol", "pre", "section", "select", "summary", "ul"};         static final String[] InBodyEndOtherErrors = new String[] {"body", "dd", "dt", "html", "li", "optgroup", "option", "p", "rb", "rp", "rt", "rtc", "tbody", "td", "tfoot", "th", "thead", "tr"};         static final String[] InBodyEndAdoptionFormatters = new String[]{"a", "b", "big", "code", "em", "font", "i", "nobr", "s", "small", "strike", "strong", "tt", "u"};         static final String[] InTableToBody = new String[]{"tbody", "tfoot", "thead"};@@ -1892,8 +1805,6 @@ enum HtmlTreeBuilderState {         static final String[] InTableBodyEndIgnore = new String[]{"body", "caption", "col", "colgroup", "html", "td", "th", "tr"};         static final String[] InRowMissing = new String[]{"caption", "col", "colgroup", "tbody", "tfoot", "thead", "tr"};         static final String[] InRowIgnore = new String[]{"body", "caption", "col", "colgroup", "html", "td", "th"};-        static final String[] InSelectEnd = new String[]{"input", "keygen", "textarea"};-        static final String[] InSelectTableEnd = new String[]{"caption", "table", "tbody", "td", "tfoot", "th", "thead", "tr"};         static final String[] InTableEndIgnore = new String[]{"tbody", "tfoot", "thead"};         static final String[] InCaptionIgnore = new String[]{"body", "col", "colgroup", "html", "tbody", "td", "tfoot", "th", "thead", "tr"};         static final String[] InTemplateToHead = new String[] {"base", "basefont", "bgsound", "link", "meta", "noframes", "script", "style", "template", "title"};src/test/java/org/jsoup/parser/HtmlParserTest.javaindex e883ae73..ab8e7200 100644@@ -155,12 +155,33 @@ public class HtmlParserTest {     }      @Test public void parsesUnterminatedOption() {-        // bit weird this -- browsers and spec get stuck in select until there's a </select>         Document doc = Jsoup.parse("<body><p><select><option>One<option>Two</p><p>Three</p>");         Elements options = doc.select("option");         assertEquals(2, options.size());         assertEquals("One", options.first().text());-        assertEquals("TwoThree", options.last().text());+        assertEquals("Two Three", options.last().text());+    }++    @Test void parsesSelectContentUsingInBodyRules() {+        Document doc = Jsoup.parse("<select><div>One</div><button>Two</button><datalist><option>Three</option></datalist></select>");+        Element select = doc.expectFirst("select");++        assertEquals("<div>One</div><button>Two</button><datalist><option>Three</option></datalist>",+            TextUtil.stripNewlines(select.html()));+    }++    @Test void hrClosesOpenOptionAndOptgroup() {+        Document doc = Jsoup.parse("<select><optgroup><option>One<hr>");++        assertEquals("<select><optgroup><option>One</option></optgroup><hr></select>",+            TextUtil.stripNewlines(doc.body().html()));+    }++    @Test void inputClosesOpenSelect() {+        Document doc = Jsoup.parse("<select><option>One<input>");++        assertEquals("<select><option>One</option></select><input>",+            TextUtil.stripNewlines(doc.body().html()));     }      @Test public void testSelectWithOption() {src/test/java/org/jsoup/parser/HtmlTreeBuilderStateTest.javaindex 729e61e3..b9826a5e 100644@@ -45,7 +45,7 @@ public class HtmlTreeBuilderStateTest {     public void ensureArraysAreSorted() {         List<Object[]> constants = findConstantArrays(Constants.class);         ensureSorted(constants);-        assertEquals(37, constants.size());+        assertEquals(35, constants.size());     }      @Test public void ensureTagSearchesAreKnownTags() {src/test/java/org/jsoup/parser/HtmlTreeBuilderTest.javaindex 6e0f19ed..0155675b 100644@@ -212,7 +212,7 @@ public class HtmlTreeBuilderTest {          List<Object[]> tagOptionArrays = HtmlTreeBuilderStateTest.findConstantArrays(HtmlTagOptions.class);         HtmlTreeBuilderStateTest.ensureSorted(tagOptionArrays);-        assertEquals(10, tagOptionArrays.size());+        assertEquals(9, tagOptionArrays.size());     }      @Test@@ -316,11 +316,9 @@ public class HtmlTreeBuilderTest {     @Test void parserOptionsAreNamespaceAware() {         Tag htmlOption = Tag.valueOf("option", NamespaceHtml, ParseSettings.htmlDefault);         assertTrue(htmlOption.hasParserOption(HtmlTagOptions.ImpliedEnd));-        assertTrue(htmlOption.hasParserOption(HtmlTagOptions.SelectScopeMember));          Tag svgOption = Tag.valueOf("option", Parser.NamespaceSvg, ParseSettings.htmlDefault);         assertFalse(svgOption.hasParserOption(HtmlTagOptions.ImpliedEnd));-        assertFalse(svgOption.hasParserOption(HtmlTagOptions.SelectScopeMember));     }      @Test void impliedEndTagsOnlyPopHtmlElements() {