Skip to content

fix: accept character references the browser reads as text in <c-raw> blocks and Markup - #310

Merged
JuroOravec merged 2 commits into
mainfrom
fix/c-raw-character-references
Oct 24, 2026
Merged

JuroOravec merged 2 commits into
mainfrom
fix/c-raw-character-references

Conversation

@JuroOravec

Copy link
Copy Markdown
Member

Fixes #298

What was wrong

In a component with browser behavior (js), Citry checks that each
<c-raw> block and each Markup value is complete HTML, because Vue
inserts it as one block (validate_html_fragment_boundary in
crates/citry_html_transform/src/output_scanner.rs). The check refused
every tokenizer error, including the ones the HTML standard reports for a
character reference it still decodes. So a footer with &copy 2026
failed:

ValueError: The <c-raw> block at line 2, column 16 is not a complete
HTML fragment: '<p>&copy 2026 Example</p>'. ... every tag it opens must
be closed inside it, ...

The message described tag rules the HTML does not break.

The fix

Of the issue's two options I took the first, with the browser as the
reference: a character reference the browser reads as text is accepted,
and any other tokenizer error gets a message that names it.

  • Accepted (is_character_reference_error): a legacy name without
    ; (&copy 2026, &nbsp), an unknown name (&foo;, kept as
    written), and numeric references to a control character (&#127;, and
    &#x80; to &#x9f;, most of which map to Windows-1252), a
    noncharacter (&#xfdd0;), NUL, a surrogate or a value past U+10FFFF
    (the last three read as U+FFFD), plus &# with no digits (kept as
    written). These are exactly the HTML standard's character-reference
    errors. Each one becomes text, so it cannot open or close an element. A
    Rust test checks, for each text case, that html5gum decodes it exactly
    as parse5 7.3.0 (the parser jsdom uses) does; I checked parse5
    separately.
  • Named: any other tokenizer error now comes back as opaque HTML has the HTML parse error <name>, and the Python message says the block "is
    not well-formed HTML", names the error and links to the HTML standard's
    list of parse errors. A stray < (invalid-first-character-of-tag-name
    and its two relatives) also gets "A '<' that does not start a tag must
    be written as '<'", which the tag-rule message used to carry. Shape
    problems (an unclosed tag, a self-closed <iframe/>) keep the tag-rule
    message, now without the < clause it can no longer reach.

The full page parse that guards hydration
(parse_div_fragment_with_lexical_text, the same tokenizer callback)
stays strict about character references: the server escapes & in
everything it writes, and <c-raw> blocks and Markup values are
checked by this fragment check and by static_html_in_context, then cut
out of the page parse. The reviewer confirmed such blocks hydrate, with
the default check and with the full parse forced.

validate_html_fragment_boundary now returns Result<(), String>
instead of Result<(), &'static str> so the message can carry the
error's name; its only callers are the PyO3 wrapper (unchanged) and
Citry's Python code. Its doc comment records the decision. The raw,
expressions and 0.6.0 upgrade docs now mention the "is not well-formed
HTML" message for a stray <.

Tests

  • Rust (citry_html_transform): the issue's table plus surrogate and
    out-of-range references, each accepted with the decoded text checked,
    and the named-error and shape-error messages.
  • citry_core: the Python API accepts the references and names
    eof-in-comment.
  • citry: the issue's footer, with &copy 2026, &#127; and &#x80;,
    renders, writes the block as is and hydrates; a <p a="1"b="2"> block
    and a stray < each fail with the named-error message.

Changelog: a Fixes line in CHANGELOG.md and a Fixed line in
packages/py/citry_core/CHANGELOG.md.

Found during review, not fixed here

  • In an interactive component, a legacy name in a template attribute is
    decoded with the text rule: <a href="/search?q=x&region=eu">
    compiles to href: "/search?q=x®ion=eu", so the URL breaks, while
    Vue's own compiler and a static Citry page keep &region=eu.
  • A tag-less Markup("a&#127;b") in an interactive component shows
    ab, because Python's html.unescape drops &#127;, &#xfdd0; and
    &#x81;, while the browser and static pages keep the code point.

Both are separate code paths and need their own issues.

🤖 Generated with Claude Code

… and Markup

In an interactive component, Citry checks that a <c-raw> block or Markup
value is complete HTML (validate_html_fragment_boundary), because Vue
inserts it as one block. The check refused every tokenizer error, so
`&copy 2026` (a legacy name without `;`), `&nbsp`, `&#127;`, `&#x80;` or
`&#xfdd0;` failed with a message about tag rules the HTML does not break.

- The check now lets every character reference error pass
  (is_character_reference_error: missing semicolon, unknown name,
  control, noncharacter, null, surrogate and out-of-range references, and
  `&#` with no digits). Each reference becomes text, so it cannot open or
  close an element; html5gum decodes the text cases as parse5 7.3.0 does,
  which a Rust test checks. The full page parse that guards hydration
  stays strict: the server escapes `&` in what it writes, and <c-raw> and
  Markup blocks are cut out of that parse after their own checks.
- For any other tokenizer error, the check now names it. The Python
  message says the HTML is not well-formed, names the error, links to the
  HTML standard's list of parse errors and, for a stray `<`, says to
  write it as `&lt;`; the tag-rule message keeps the shape problems.
- The raw, expressions and 0.6.0 upgrade docs mention the new message.

Fixes #298

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@JuroOravec
JuroOravec merged commit f96f207 into main Oct 24, 2026
29 checks passed
@JuroOravec
JuroOravec deleted the fix/c-raw-character-references branch October 24, 2026 12:55
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.

In an interactive component, a <c-raw> block with &copy (no ;) or &#127; fails as "not a complete HTML fragment"

1 participant