Skip to content

Decide HTML structure the way the browser does #302

Open

Description

@JuroOravec

What goes wrong

Several Citry checks work out where an element starts and ends from the
shape of its tags: a /> at the end, a list of void elements, or a short
list of raw-text elements (elements whose content the browser reads as
plain text, so a < inside them does not start a tag). In two cases the
browser decides differently, and these checks then disagree with the page
the browser builds:

  1. A self-closed non-void element stays open. The browser ignores the
    / in <div/>, <textarea/> or <iframe/>. For iframe,
    textarea, title, xmp, noscript, noembed, noframes, style,
    script and plaintext it then reads everything after the tag as that
    element's text, up to an end tag the source never wrote (for
    plaintext, to the end of the page).
  2. Raw text depends on the namespace and covers more than four
    elements.
    Inside <svg> or <math>, <style>, <script> and
    <title> hold markup, not text. In HTML, xmp, iframe, noscript,
    noembed, noframes and plaintext hold text too, not only script,
    style, textarea and title.

Python's html.parser.HTMLParser gets both wrong: it calls
handle_startendtag for any /> and never switches to text for it, reads
<noscript> as markup, and reads SVG <title> and <style> as text. Every
check built on it inherits the mistake. Citry's Rust scanner
(crates/citry_html_transform/src/output_scanner.rs) feeds tokens to the
tree builder of html5ever, a Rust HTML parser that follows the browser's
parsing rules, and gets these cases right.

validate_html_fragment_boundary() also accepted a self-closed raw-text
element such as <iframe/>; that one is fixed separately, in the branch fix/self-closed-raw-text-fragments (its PR is about to open). The
cases below remain.

Proven cases

Each case below was reproduced on a local build, except where it says
otherwise.

1. mark_html misplaces component root markers (medium)

mark_html adds a component's data-cid-* attribute to its root-level
tags, so scoped CSS and root lookups depend on it.
crates/citry_html_transform/src/marker.rs:19 lists only four raw-text
elements, and line 146 treats a self-closed tag as closed.

mark_html("<xmp></div></xmp><p>x</p>", ["data-cX"], "c-render-id")
# '<xmp data-cX=""></div></xmp><p>x</p>'
#   <p> is a root but gets no marker
mark_html("<textarea/><p>x</p>", ["data-cX"], "c-render-id")
# '<textarea data-cX=""/><p data-cX="">x</p>'
#   the "<p>" is textarea text, so the marker lands in the text
mark_html("<div/><p>x</p>", ["data-cX"], "c-render-id")
# '<div data-cX=""/><p data-cX="">x</p>'
#   <p> is inside the div, not a root

End to end, a component whose template is {{ html }}<p>after</p> with
html = Markup("<xmp></div></xmp>") serializes to
<xmp data-cid-…=""></div></xmp><p>after</p>: the template's own <p>
loses its marker.

Who hits it: on static pages, any Markup value or <c-raw> block
that has a self-closed non-void tag, or xmp, iframe, noscript,
noembed, noframes or plaintext content that contains </. In an
interactive (Vue) component, mark_opaque_html
(packages/py/citry/citry/_vue/opaque_html.py:35) calls mark_html only
after validate_html_fragment_boundary() has accepted the HTML, which
already rejects a self-closed <div/>, so only the raw-text case reaches
it there.

2. citry_ui rejects valid fills that contain a template <span/> (medium)

feed_typed_direct_output
(packages/py/citry_ui/citry_ui/components/_direct_output.py:55) writes a
self-closed template element to the parser as <span/>, followed by the
</span> the compiler always emits. The real output is <span></span>, so
the parser sees an extra end tag.

  • A CMenuItem whose label holds <span/> fails with "Menu collections
    may contain only valid direct Menu declarations, formatting whitespace,
    and transparent components that add no element." With
    <span></span> it renders.
  • CAccordion fails the same way: "CAccordion default content may contain
    only direct CAccordionItem components, …".

Who hits it: anyone who writes a self-closing element inside these
fills. Fix direction: feed the parser the tag as the compiler writes
it, an open tag and then a close tag.

3. CMenu accepts Markup("<textarea/>") that swallows later items (low)

_DirectMenuParser
(packages/py/citry_ui/citry_ui/components/cmenu/cmenu.py:498) accepts an
item label holding Markup("<textarea/>"), and the page is written with
two menu items. In the browser the textarea's text runs over the next
item. Markup("<textarea>") is rejected, so the check is inconsistent.

4. CImage placeholder check lets SVG content through (low to medium)

_CImageVisualParser
(packages/py/citry_ui/citry_ui/components/cimage/cimage.py:131) rejects
interactive or image content in the placeholder and fallback slots. It
accepts:

  • Markup('<svg><title><button type="button">Retry</button></title></svg>'),
    where the browser creates a real <button>;
  • Markup('<svg><style><img src="/x.png" alt="x"></style></svg>'), where
    the browser creates a real <img>.

5. The Vue host check accepts a host the browser never builds (low)

After on_serialize hooks run, _HostValidator
(packages/py/citry/citry/_vue/serialization.py:503) checks that the page
holds exactly one Vue host. It reports one host for each of these, while
html5ever builds none:

<body><textarea/><div id="app"></div></body>
<head><title/></head><body><div id="app"></div></body>
<body><noscript><div id="app"></div></noscript></body>

Who hits it: only an extension hook that writes such markup. Vue then
finds nothing to mount on.

6. Preload hints land inside a <title/> (low, read from the code)

_PreloadHeadLocator
(packages/py/citry/citry/_vue/serialization.py:102) finds the end of
<head> in <head><title/></head> and inserts the preload <link> tags
there. The browser reads that spot as title text, so the hints are lost.
This case was traced through the code, not run.

7. The duplicate-id warning counts the wrong ids (low, development only)

_IdScanner (packages/py/citry/citry/_page_ids.py:41):

HTML ids reported what the browser registers
<textarea/><p id="a"></p> ['a'] none (text)
<title/><p id="a"></p> ['a'] none (text)
<svg><title><p id="a"></p></title></svg> [] a
<svg><style><img id="a"></style></svg> [] a

8. The textarea first-newline fix misses <textarea/> (low)

The browser drops the first line feed after <textarea>, so Citry writes
an extra one when the value starts with a line feed. After <textarea/>,
should_compensate_textarea_lf("\nabc") returns False; after
<textarea> it returns True. The browser treats both as an open
textarea. The causes are in packages/py/citry/citry/serialize.py:
_TEXTAREA_OPEN_RE at line 93 needs whitespace or > after the name, and
_PreparedMarkupTracker at line 106 reads <textarea/> as closed.

Fix direction

Decide structure where the browser decides it. In Rust, follow the
tokenizer state that html5ever's tree builder picks for each tag, as
output_scanner.rs does. In the Python checks, replace the structure
decisions made with html.parser.HTMLParser by the Rust scanner
(citry_core.html_transform.scan_output_html or a smaller binding).
Where a Python parser stays, treat a self-closed non-void HTML tag as an
open tag and apply html5ever's raw-text rules, including the namespace.

Not verified

These have the same shape but were not run:

  • transform_html (crates/citry_html_transform/src/transformer.rs)
    reads HTML with an XML reader, so it treats /> as closed and has no
    raw-text handling. Nothing in Citry calls it.
  • Four-name raw-text lists {"script", "style", "textarea", "title"} in
    packages/py/citry/citry/_vue/direct_capture.py:1585 (#c-ignore),
    :2426 (<c-raw> inside a parent) and :2540 (Markup inside a
    parent), and crates/citry_template_parser/src/parser.rs:2764
    (#c-ignore placement). They let <c-raw>, Markup or #c-ignore
    sit inside xmp, iframe, noscript, noembed, noframes or
    plaintext in an interactive component. What the page then shows was
    not checked.
  • The template grammar's text containers
    (crates/citry_template_parser/src/grammar.pest:156-239) ignore the
    namespace and cover only four elements: <svg><title><button> is
    written escaped, and {{ v }} inside <xmp> or <noscript> is
    escaped, so the browser shows &lt; literally. This affects display
    only; the escaping still prevents injection.
  • HTML_VERBATIM_BODY_TAGS
    (crates/citry_template_formatter/src/html.rs:71) lacks xmp,
    listing and plaintext. Whether reformatting their whitespace changes
    the page was not checked.
  • _VisualParser
    (packages/py/citry_ui/citry_ui/components/ccommand_palette/ccommand_palette.py:275)
    uses the same pattern as case 4.

Checked and fine

  • static_html.rs compares html5ever trees.
  • hydration_structure.rs and the LexicalTextTracker in
    output_scanner.rs reject any self-closed non-void tag.
  • hydration_nesting.rs leaves raw-text tags to a full html5ever parse.
  • The Vue server renderer (citry_vue_compiler/src/server_render.rs)
    always writes explicit end tags.
  • The template compiler expands a non-void <x/> to <x></x>.

Activity

  1. JuroOravec commented on Oct 28, 2026

    @JuroOravec
    MemberAuthor

    #510 fixed cases 1 and 2, which the 0.7.0 triage kept. These remain, for v1.1:

    • Case 3: CMenu accepts a label holding Markup("<textarea/>"), and the textarea's text runs over the next item (_DirectMenuParser in cmenu.py).
    • Case 4: CImage's placeholder check lets SVG <title> and <style> content through, where the browser builds real <button> and <img> elements (_CImageVisualParser in cimage.py, and the same pattern in ccommand_palette.py's _VisualParser).
    • Case 5: the Vue host check counts a host inside <textarea/>, <title/> or <noscript> that the browser never builds (_HostValidator in _vue/serialization.py).
    • Case 6: preload hints land inside a <title/> in <head> (_PreloadHeadLocator).
    • Case 7: the duplicate-id warning counts ids inside <textarea/>/<title/> text and misses ids inside SVG <title>/<style> (_IdScanner in _page_ids.py).
    • Case 8: the textarea first-newline fix misses <textarea/> (serialize.py).
    • Not verified in the issue: the four-name text-element lists in _vue/direct_capture.py (#c-ignore, <c-raw> and Markup inside a parent), parser.rs (#c-ignore placement), the grammar's text containers, and the formatter's verbatim tags (HTML_VERBATIM_BODY_TAGS).
    • Gaps left in mark_html, listed in fix: mark component roots and check menu fills the way the browser reads them #510: the browser ending SVG or MathML content early at some HTML tags, <annotation-xml encoding="text/html">, misnested formatting elements, content moved out in front of a table, and a stray </p>.

    Most of the Python cases share one fix: decide structure with the Rust scanner, or apply the same rules #510 gives mark_html, rather than html.parser.HTMLParser.

  2. added
    next-sprint
    on Oct 28, 2026
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

    next-sprint

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions