Skip to content

fix: mark component roots and check menu fills the way the browser reads them - #510

Merged
JuroOravec merged 1 commit into
mainfrom
fix/html-structure-like-browser
Oct 28, 2026
Merged

JuroOravec merged 1 commit into
mainfrom
fix/html-structure-like-browser

Conversation

@JuroOravec

@JuroOravec JuroOravec commented Oct 28, 2026 •

Copy link
Copy Markdown
Member

Fixes cases 1 and 2 of #302, the two the 0.7.0 triage kept. The other cases stay open in #302 for v1.1.

Case 1: the attribute that scopes a component's CSS lands on the wrong tags

mark_html (crates/citry_html_transform/src/marker.rs) adds a component's data-cid-* attribute to the root-level tags of its HTML. Scoped CSS and root lookups depend on it. It counted nesting with shortcuts the browser does not take, so later root tags lost the marker or nested tags got it:

  • It knew only script, style, textarea and title as elements whose content is text.
  • It treated any /> as closing the element, while the browser keeps a non-void HTML element written with />, such as <div/> or <textarea/>, open.
  • Every end tag lowered the count, whatever element it named.
mark_html("<xmp></div></xmp><p>x</p>", ["data-cX"], "c-render-id")
# before: '<xmp data-cX=""></div></xmp><p>x</p>'
# after:  '<xmp data-cX=""></div></xmp><p data-cX="">x</p>'
mark_html("<div/><p>x</p>", ["data-cX"], "c-render-id")
# before: '<div data-cX=""/><p data-cX="">x</p>'
# after:  '<div data-cX=""/><p>x</p>'

The scanner now keeps the names and namespaces of the open elements and follows the browser's rules:

  • Text elements: the content of <script>, <style>, <textarea>, <title>, <xmp>, <iframe>, <noembed>, <noframes> and <plaintext> is text. <plaintext> runs to the end of the input. <noscript> stays markup: with scripting off the browser reads it as markup, and Citry's template grammar places child components there.
  • End tags: a block end tag such as </div> closes its element and everything inside it when the element is in scope. A table end tag (</table>, </tbody>, </tr> and the like) first closes an open cell or caption, so omitted </td> tags work. Any other end tag closes the innermost element with its name, unless an element the browser calls special comes first. An end tag the browser ignores closes nothing.
  • Implied ends: a new <li>, <dt> or <dd>, <option>, <button>, cell, row, table section or heading ends an open one where the browser does, and a block start ends an open <p>.
  • SVG and MathML: /> closes the element, and <style>, <script> and <title> hold markup. The children of <foreignObject>, <desc>, <title>, <mi>, <mo>, <mn>, <ms> and <mtext> are HTML; <mglyph> and <malignmark> stay MathML.
  • Void elements: the void list is html5ever's full one, which adds basefont, bgsound, frame, image and keygen.
  • Placeholders: the <template c-render-id> placeholder Citry leaves for a child component is still split out inside text content and inside <noscript>, so the child's HTML is kept. A child rendered inside a <textarea> now writes its HTML there (as text in the page), where the page used to get the placeholder. serialize.py now joins a literal placeholder's text back into a leaf at the two places that dropped it, so the per-row path keeps a placeholder written inside a <textarea> like the other paths.
  • Speed: each tag name is classified once (classify), and the open-element stack counts the elements each rule can close, so a rule skips its search when nothing it closes is open. A release-build micro-benchmark gives the same times as main: 3.3 µs for a 1.4 KB frame, and about 0.9 ms for 350 KB flat or inside 20 nested <div>s.

New test: tests/marker_browser.rs marks 104 inputs, parses the result with html5ever as the content of a <div>, and checks that every top-level element carries the marker and no nested element or text does. The inputs cover:

  • the issue's cases, and each text element;
  • self-closed tags inside parents (<div><i class="icon"/></div>), stray and misnested end tags;
  • implied ends for lists, <p>, <button> and headings;
  • tables, including omitted </td> and </tr>, <object>, SVG and MathML self-closing, integration points and <mglyph>;
  • void names, placeholders, comments, CDATA and uppercase names.

All 104 agree with html5ever.

Not modelled:

  • A stray </p> makes the browser create an empty <p> that has no tag in the source, so no marker can go on it. The test leaves this shape out and says why.
  • The browser ends SVG or MathML content early at some HTML tags (<p>, <div>, <img> and others), and the scanner keeps those inside.
  • <annotation-xml encoding="text/html"> is read as MathML, because the scanner does not read attributes.
  • The scan sees one component's HTML at a time, so a child's open <plaintext> does not affect the parent's later tags.
  • The browser moves misnested formatting elements (<a>1<a>2, <b>a<p>b</b>, <nobr>) and some HTML tags inside SVG, which the scanner does not repeat. With <noscript> read as markup, unbalanced content inside it, such as <noscript><p>x</noscript>, is also not modelled. Nor is content the browser moves out in front of a table (<table><p>x</p></table>), or a <td> outside any table, which the browser drops.

The Python walk for row markers: packages/py/citry/citry/_vue/leaf_program.py places row markers at compile time and must agree with the native scan. It now keeps a stack of open element names. It mirrors the text-element and void lists. It declines whenever one of the rules it does not copy could apply: an end tag that is not the innermost element's, an implied end, SVG, MathML, <plaintext>, or a non-void />.

Case 2: Citry UI rejects valid fills that contain <span/>

feed_typed_direct_output (packages/py/citry_ui/citry_ui/components/_direct_output.py) fed a self-closed template element to its HTML parser as <span/>, followed by the </span> the compiler always emits. The page writes <span></span>, so the parser saw an extra end tag. A CMenuItem label or a CAccordionItem title holding <span/> then failed with "may contain only valid direct ... declarations". The feeder now writes / only for a void element, as the page does.

Sweep:

  • _vue/document.py's head writer had the same pattern. It wrote <title/></title>, which the browser reads the same as <title></title> but which breaks the rule that non-void elements expand. It now matches the other writers.
  • The other is_self_closing writers in packages/py already check is_void. leaf_program.py:3206 writes a Vue template, where <x/> is valid syntax.
  • The four-name text-element lists in _vue/direct_capture.py, parser.rs (#c-ignore placement), the grammar's text containers and the formatter's verbatim tags are the same class of bug. Decide HTML structure the way the browser does #302 lists them as unverified and the triage left them for v1.1, so this PR does not change them.

Tests

  • Rust:
    • tests/marker_browser.rs (above).
    • tests/marker.rs: each text element; <div/>, <textarea/> and <TEXTAREA/>; SVG and MathML self-closing; <textarea> in <foreignObject>; placeholders inside <xmp> and <noscript>; <i/> inside a parent; a stray end tag; <li/><li/>.
  • citry_core: mark_html through the Python binding.
  • citry:
    • a component's own <p> keeps its marker after Markup("<xmp></div></xmp>") and after <div>{{ icon }}</div> with Markup('<i class="icon"/>'), and gets none after Markup("<textarea/>");
    • a child component inside <noscript> is written, and one inside <textarea> writes its HTML there;
    • the row-HTML comparison test gains shapes with SVG, MathML, <xmp>, <plaintext>, list items and a placeholder inside <textarea>, where the row writer, the per-row scan and the ordinary path must give the same bytes.
  • Citry UI: a CMenuItem label and a CAccordionItem title holding <span/> render like <span></span>. Both tests fail without the fix.
  • Document shell: a self-closed <title/> is written as <title></title>, and <meta/> keeps its slash. This test fails without the fix.

Run locally:

  • cargo test -p citry_html_transform, plus clippy and fmt.
  • 7028 Python tests across the Citry UI components, citry_core, and the citry serialize, marker, component, Vue, render, deps, static, opaque, raw, markup, simple, leaf and native test files.
  • ruff, and mypy (also with --platform linux) on leaf_program.py.

Not caused by this PR: test_python_binding_consumes_the_shared_structural_corpus fails on main too. #511 fixes it.

Changelog

  • packages/py/citry_core/CHANGELOG.md: later root tags keep the scoping attribute and nested tags do not get it; a child rendered inside a <textarea> writes its HTML there.
  • packages/py/citry_ui/CHANGELOG.md: CMenu and CAccordion accept <span/>.

🤖 Generated with Claude Code

@JuroOravec
JuroOravec force-pushed the fix/html-structure-like-browser branch from 24c7f3d to ad2d61c Compare October 28, 2026 18:37
@JuroOravec JuroOravec changed the title fix: find component roots and fill structure the way the browser does fix: mark component roots and check menu fills the way the browser reads them Oct 28, 2026
@JuroOravec
JuroOravec force-pushed the fix/html-structure-like-browser branch from ad2d61c to 8603462 Compare October 28, 2026 18:55
…ads them

`mark_html` adds a component's `data-cid-*` attribute, which its scoped
CSS and root lookups depend on, to the root-level tags of its HTML. It
counted nesting with a few shortcuts the browser does not take, so later
root tags lost the marker or nested tags got it:
- it knew only four elements whose content the browser reads as text;
- it treated any `/>` as closing the element, while the browser keeps a
  non-void HTML element written with `/>`, such as `<div/>`, open;
- every end tag lowered the count, whatever element it named.

The scanner now keeps the open elements and follows the browser's rules:
- the content of `<xmp>`, `<iframe>`, `<noembed>`, `<noframes>` and
  `<plaintext>` is text;
- a block end tag closes its element when it is in scope, a table end tag
  first closes an open cell or caption, and any other end tag closes the
  element it names unless a special element comes first;
- a new `<li>`, `<dt>`, `<dd>`, `<option>`, `<button>`, cell, row, table
  section or heading ends an open one where the browser does, and a block
  start ends an open `<p>`;
- in SVG and MathML `/>` closes the element.
Each tag name is classified once, and the stack counts the open elements a
rule can close, so a release build scans as fast as before.

The `<template c-render-id>` placeholder Citry leaves for a child
component is still split out inside text content and `<noscript>`, so the
child's HTML is kept, and serialize now joins a literal placeholder's text
back into a leaf instead of dropping it. A new test parses the marked HTML
of 104 inputs with html5ever and checks the markers sit exactly on the
top-level elements. The Python walk that places row markers at compile
time follows the same rules and declines where it does not mirror one.

Citry UI's check of a menu's or accordion's direct content fed a
self-closed template element such as `<span/>` to its parser with the
`/`, followed by the end tag the compiler always emits, so the parser saw
an extra end tag and rejected valid content. It now feeds the element the
way the page writes it, and Citry writes a self-closed `<head>` element
such as `<title/>` the same way.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@JuroOravec
JuroOravec force-pushed the fix/html-structure-like-browser branch from 8603462 to fb17316 Compare October 28, 2026 18:59
@JuroOravec
JuroOravec merged commit 5b7293d into main Oct 28, 2026
0 of 26 checks passed
@JuroOravec
JuroOravec deleted the fix/html-structure-like-browser branch October 28, 2026 19:17
JuroOravec added a commit that referenced this pull request Oct 28, 2026
#510 marks the elements the browser places at a component's top level,
including a `<div>` that closes an open `<p>`. So in
`<p>text<div><a ...></a></div></p>` a finding on the link inside the
`<div>` names the component. The test still expected the older result,
where only the `<p>` was marked.

A second test keeps the case that test was written for: a child
component's `<div>` closes the parent's `<p>`, so the browser moves the
parent's link out of every root, and the report must not name a component.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant