Skip to content

parse: "in select" insertion mode inserts disallowed children instead of ignoring them #36

Closed

Description

@gaborbernat

turbohtml documents WHATWG conformance for parse, but its "in select" insertion mode does not match the spec. Start tags that are not option/optgroup/hr/select/input/keygen/textarea/script/template are a parse error and must be ignored, yet turbohtml inserts them as real children of <select> (e.g. div, p, pre, ul). Additionally, textarea (like input/keygen) must pop the select and be reprocessed as a sibling, but turbohtml nests it inside <select>. input/keygen are handled correctly, which shows the mode is only partially enforced. html5lib, the spec reference implementation, ignores div and reprocesses textarea as a sibling.

import turbohtml, html5lib
from xml.etree import ElementTree as ET

for inp in ['<select><div>x</select>', '<select><textarea>z']:
    print(repr(inp))
    print('  turbo:', turbohtml.parse(inp).serialize())
    print('  h5lib:', ET.tostring(html5lib.parse(inp), encoding='unicode'))

Expected

For <select><div>x</select>: the div start tag is ignored (parse error) and the character data is kept in the select, giving <select>x</select>.

For <select><textarea>z: the textarea start tag pops the select and is reprocessed, so <textarea>z</textarea> becomes a sibling after an (empty) <select>:

<select></select><textarea>z</textarea>

This matches html5lib output.

Actual

<select><div>x</div></select>      # div inserted as a real child of select
<select><textarea>z</textarea></select>   # textarea nested inside select

Generic disallowed tags (p, pre, ul, ...) are likewise inserted rather than ignored. input/keygen already do the correct pop-and-reprocess, so enforcement is only partial.

Spec / promise reference

WHATWG HTML, "The rules for parsing tokens in HTML content", the in select insertion mode:

  • A start tag whose tag name is one of input, keygen, textarea: parse error; if the stack of open elements does not have a select in select scope, ignore the token; otherwise pop elements until a select has been popped, reset the insertion mode appropriately, and reprocess the token.
  • "Anything else" (start tag that is not option/optgroup/hr/select/input/keygen/textarea/script/template): parse error, ignore the token.

turbohtml inserts disallowed elements instead of ignoring them, and omits the pop-and-reprocess path for textarea. Since turbohtml.parse documents WHATWG conformance (html5lib as the reference), this is a conformance bug.

Activity

  1. added
    bug
    on Jun 17, 2026
  2. gaborbernat commented on Jun 17, 2026

    @gaborbernat
    MemberAuthor

    Contribution policy (applies to every turbohtml issue & PR)

    Conformance — when in doubt, the WHATWG HTML/Encoding/Selectors specs are authoritative. Where a spec is silent or genuinely ambiguous, follow what most competitors do (html5lib, lexbor, html5ever/ammonia, Go net/html, lxml). Differential-vs-html5lib alone is insufficient — html5lib 1.1 lags the living standard.

    Performance — no regressions — every fix/feature must run tox -e bench and show no regression vs baseline; prefer changes that improve the relevant numbers. Keep the public Python surface thin; push hot paths into C.

    New features — design for max performance — before implementing, inspect competing C/C++/Rust/Go implementations (lexbor, Gumbo, html5ever/ammonia, Go net/html) and design for maximum throughput from the start.

    Documentation — all four Diátaxis dimensions — every feature change must update the docs across tutorials, how-to guides, reference, and explanation.

    Changelog — every PR must add a towncrier news fragment at docs/changelog/<issue#>.<type>.rst, where <type> is one of breaking, feature, bugfix, doc, packaging, misc.

  3. gaborbernat commented on Jun 17, 2026

    @gaborbernat
    MemberAuthor

    Working on this — fix in progress.

  4. gaborbernat commented on Jun 17, 2026

    @gaborbernat
    MemberAuthor

    Closing as invalid — turbohtml already matches the current WHATWG living standard, and this issue cites the removed "in select" insertion mode.

    The "in select" and "in select in table" insertion modes no longer exist in the spec. The current standard enumerates only in head, in head noscript, after head/in body, text, in table, in table text, in caption, in column group, in table body, in row, in cell, in template, in frameset, after frameset, in foreign content, and the before/after modes — there is no parsing-main-inselect anchor. A <select> start tag and all <select> content are now handled by the in body insertion mode.

    Why: whatwg/html#10548 "Define customizable <select>" (merged 2025-07-22) introduced stylable, author-controlled <select> elements that hold rich content (buttons, <div>s, and a <selectedcontent> element). The old in-select modes discarded any non-option/optgroup content at parse time, which would destroy that markup, so both modes were removed and select content now parses with the in-body rules. The standalone "Relax select parser" PR (#10557) was closed the same day in favor of #10548.

    So <select><div>x</select> keeping the <div>, and <select><textarea>z nesting the <textarea>, are spec-correct. html5lib 1.1 and lexbor still implement the old in-select mode and therefore lag the living standard here; per the contribution policy, differential-vs-html5lib is insufficient and the spec is authoritative.

    A genuine but opposite-direction inconsistency did surface (<select><input> pops the select while <select><keygen> does not); that is tracked separately.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions