Skip to content

fix: leave Vue string literals holding a lone surrogate to the browser - #349

Merged
JuroOravec merged 2 commits into
mainfrom
fix/vue-lone-surrogate-literals
Oct 24, 2026
Merged

JuroOravec merged 2 commits into
mainfrom
fix/vue-lone-surrogate-literals

Conversation

@JuroOravec

Copy link
Copy Markdown
Member

Fixes #296

What was wrong

In a component with js, Citry renders Vue directives on the server. A
Vue expression holding a lone surrogate escape (half of a UTF-16 pair,
with no partner), such as v-text="'a\ud800b'", put U+FFFD followed by
the escape's hex digits into the server's HTML, as <p>a\ufffdd800b</p>
(U+FFFD, then d800). In the browser, Vue renders a string holding
U+D800, so the server's text did not match what Vue shows.

The server reads the render code that Vue's compiler generates by parsing
it with oxc, a JavaScript parser written in Rust. A Rust string cannot
hold a lone surrogate, so oxc stores such a literal in its
own encoding and sets StringLiteral::lone_surrogates. Citry read
literal.value without checking that flag. The page's UTF-8 HTML cannot
hold the value either, so the server cannot write it.

The fix

Every place that takes a value or a name from an oxc string literal now
checks for such a literal first, mostly through one of two helpers, which
return nothing for it:

  • literal_text reads a string literal's text;
  • key_name reads the fixed name of an object key, including a string or
    template literal key.

They live in crates/citry_vue_compiler/src/lib.rs, and
crates/citry_template_parser/src/browser.rs has a copy of each. When a
helper returns nothing, each part does this:

  • Server render (server_render.rs): a string value is declined as
    text-value, the code it already uses for other text it cannot write as
    Vue does. A key it cannot hold, as in obj['\ud800'], is a
    browser-only value (browser-value). A dynamic v-text is written
    empty, as for any value only the browser knows, and hydration sets it.
    Tags, comment text, slot names and prop key lists from the render code
    give render-shape; of these, only a slot name, such as
    <slot :name="'\ud800'">, can hold such a literal from a template.
  • Hydration plan (hydration.rs): a tag, comment, component name,
    slot name or prop key holding such a literal stops the plan, as for
    render code it cannot read.
  • Component source analysis (browser.rs): a props, inject or
    emits list, or an option section, that holds such a name is marked
    unknown, as a non-literal entry already is. Such a name is also left
    out of the scope writes it records.

A literal without a lone surrogate reads as before, including an escape
for a complete character such as 'e\u00e9f' and a real U+FFFD.

One case that rendered before now goes to the browser: a comparison or a
condition that reads such a literal, as in
v-if="'\ud800' === 'x'". The old code compared the two encoded
strings. That gave the right answer only because both sides used the same
encoding, and it could be wrong: '\ud800' === '\ufffdd800' came out
true. Keeping the real UTF-16 value is not worth it for a case this rare.

The HydrationDecline docstring and
docs/design/vue_ssr_selected_tree_plan.md now name the case.

Tests

  • server_render.rs, a_lone_surrogate_in_a_string_literal_is_left_to_the_browser:
    v-text, :title, {{ }} and a member key, each with the HTML and the
    declines, plus \u00e9, which renders.
  • hydration.rs, a_string_literal_holding_a_lone_surrogate_stops_the_plan:
    render code with such a tag, comment, component name and prop key.
  • browser.rs, names_holding_a_lone_surrogate_leave_their_section_unknown
    and scope_writes_skip_names_holding_a_lone_surrogate.
  • test_vue_hydration_regions.py,
    test_a_lone_surrogate_escape_in_a_vue_expression_is_left_to_the_browser:
    renders the issue's example and checks the HTML and the declines.

Each test fails without the fix.

Changelog: a Fixes line in CHANGELOG.md.

🤖 Generated with Claude Code

JuroOravec and others added 2 commits October 24, 2026 21:26
A Vue expression such as `v-text="'a\ud800b'"` made the server write
U+FFFD followed by `d800` into the page. A lone surrogate is half of a
UTF-16 pair with no partner. A Rust string cannot hold one, so oxc stores
such a string literal in its own encoding and sets
`StringLiteral::lone_surrogates`, and Citry read that encoding as the
value. The page's UTF-8 HTML cannot hold the value either, so the server
cannot write what Vue renders in the browser.

Every read of an oxc string literal in the Vue compiler crate now goes
through `literal_text` or `key_name`, which return nothing for such a
literal. The server render leaves such a value to Vue: it declines it as
`text-value` for a string and `browser-value` for a key it cannot look
up, or writes the element empty for a dynamic `v-text`. A comparison or a condition that reads such a literal is
now left to the browser too. The hydration plan treats the render as one
it cannot read. The component source analysis marks a `props`, `inject`,
`emits` or option section holding such a name as unknown, and leaves such
a name out of the scope writes it records.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@JuroOravec
JuroOravec merged commit 1ff7b40 into main Oct 24, 2026
6 of 27 checks passed
@JuroOravec
JuroOravec deleted the fix/vue-lone-surrogate-literals branch October 24, 2026 23:01
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.

A \ud800 escape in a Vue expression renders as "�d800" in the server's HTML

1 participant