Skip to content

fix: decode Vue directive values and interpolations as Vue does - #322

Merged
JuroOravec merged 1 commit into
mainfrom
fix/directive-value-entities
Oct 24, 2026
Merged

JuroOravec merged 1 commit into
mainfrom
fix/directive-value-entities

Conversation

@JuroOravec

Copy link
Copy Markdown
Member

Fixes #313

What was wrong

In a component that uses Vue, a string written inside a directive value
kept its character references:

<button @click="msg = 'Tom &amp; Jerry'">Set</button>

set msg to Tom &amp; Jerry. Vue's own compiler (@vue/compiler-dom
3.5.44) sets Tom & Jerry. The raw ; of &amp; also made Citry read
the handler as a statement block. The same happened in v-if, v-show,
v-for, v-slot and #name parameters, v-model (in the update
handler), v-text, v-memo and custom directive values. Interpolations
({{ }}) that reach the compiler were read the same way.

Vue decodes the references in a directive value by the HTML attribute
rule, and in an interpolation by the text rule, before it reads the
JavaScript. Citry's patched Vize compiler compiles a template on one of
two lanes, and neither did that:

  • The S2 emitter (third_party/rust/vize_s1_to_s2), which writes a
    template when it can, parsed each expression from the source as
    written. Only bound values and v-html were decoded, later, in the
    emitter.
  • The patched vize_atelier_core code generator, which writes a template
    when S2 declines, gets a decoded directive value from its parser. But
    about 20 steps read an expression back from the source by its span,
    which gives the text as written. On this lane the slot parameters and
    the v-model update handler came out wrong; the other read-backs now
    decode too, but no test case reached them. Its parser also leaves an
    interpolation's text as written.

v-if="n &gt; 0" worked before only by accident: the raw text is not
JavaScript, so S2 declined the template, and the other lane happened to
read that one position decoded.

The fix

  • S2 lane. The lowering decodes each directive value by the attribute
    rule, and each interpolation by the text rule, once, before it parses
    the JavaScript (Cx::decoded_directive_value,
    Cx::decoded_interpolation_text). A decoded value is a new string, not a
    slice of the source, so Cx remembers which authored value it came from
    and reports that span for it. The emitter's own decoding of bound values
    and v-html is removed, so nothing is decoded twice. Names and dynamic
    arguments are not decoded, because HTML never decodes them.
  • vize_atelier_core lane. A new entity module gives the text Vue
    reads from an expression span: the attribute rule inside an attribute
    value, the text rule inside {{ }}. Every step that reads a value back
    from the source now uses it; argument and name read-backs stay as
    written. It finds out whether a span sits inside {{ }} by looking back
    to the nearest quote, = or >. transform_interpolation decodes the
    interpolation's text. The crate depends on htmlize 1.1.0 for this,
    which vize_armature already uses.
  • The patched crates and the compiler identity move to 0.420.0+citry.5,
    because the compiled code changes for templates with references.
    citry checks that the compiler reports exactly this identity. Both
    README.citry.md files list the change.

Proof

  • Both lanes against Vue. crates/citry_vue_compiler/tests/directive_value_entities.rs
    checks 27 cases (26 templates) on S2 alone and on the
    vize_atelier_core lane alone, against the code Vue 3.5.44 writes. They
    cover each directive kind, a loop source, a <template v-for> key and a
    slot outlet's bound name (with &amp;amp;, which decodes once to
    &amp;), the statement-block rule (with &amp;, and with
    &amp;#40;, whose decoded ; does make a statement block, as in Vue),
    v-if="n &gt; 0" on purpose on both lanes, a bound value that keeps
    &copy= by the attribute rule, a dynamic argument, and interpolations.
    Both tests fail on main. The lane without S2 is reached through
    vize_atelier_dom's davinci-differential entry point, which this crate
    turns on only as a dev-dependency feature.
  • Nothing else changes. I recorded every distinct compile request
    (1,432) that the citry and citry_ui test suites make on main, and
    replayed them on this branch. 1,431 give byte-identical output, including
    all 1,383 templates without a character reference. The one difference is
    a form handler that Citry generates,
    v-on:submit.prevent="$citryEvents.dispatch(&#x27;citryEvent7&#x27;, $event)":
    its code is byte-identical, but its quoted event id now decodes, so the
    template stays on S2 instead of moving to the other lane, and its
    authored text plan changes from unsupported to complete.
  • End to end. test_directive_values_decode_character_references_as_vue_does
    hydrates <p :data-n="'a &amp; b'.length" v-text="'x &lt; y'"> and gets
    data-n="5" and x &lt; y.

The existing vize_s1_to_s2 and vize_atelier_core test targets have the
same results as on main (emit_if, metamorphic and
dynamic_v_model_argument fail there too, and the targets that need
insta do not build).

Compile time stays linear in the number of decoded values: the lowering
looks a decoded value's span up in a map keyed by address, and the
interpolation check reads back only to the nearest quote, = or >. A
template with 8,000 buttons, each with a decoded handler and title,
compiles in about 1.2 s, against 1.0 s without references.

Two Python docstrings in citry/_vue/compiler.py (_generated_vue_attr,
_authored_vue_attr) gave the old raw reading as their reason; they now
describe the decoding.

Changelog: a Fixes line in CHANGELOG.md.

Not fixed here

These already happen on main and are not directive values. A follow-up
PR fixes them:

  • A static title="&amp;#40;" or the text &amp;#40; shows ( on both
    lanes, where Vue and the browser show &#40;: the code generator decodes
    a numeric reference a second time.
  • A legacy name that another name continues in an attribute value, such as
    title="a&notin", stays as written in Citry, as in the browser, while
    Vue's compiler writes a¬in.
  • On S2, a static <component is="a&amp;b"> keeps &amp;, and
    #[x&amp;y] writes an empty slot key.

🤖 Generated with Claude Code

Vue decodes the character references in a directive value by the HTML
attribute rule, and in an interpolation by the text rule, before it reads
the JavaScript. Citry's patched Vize compiler read both as written, so
`@click="msg = 'Tom &amp; Jerry'"` assigned `Tom &amp; Jerry`, and the raw
`;` made the handler a statement block.

The S2 lane (vize_s1_to_s2) now decodes each directive value and
interpolation once at intake, in the lowering, and keeps the authored
span for the decoded copy, so the emitter reads it without decoding it a
second time. The vize_atelier_core lane decodes interpolations when it
transforms them, and the steps that read an expression back from the
source by its span read it through a new decoder. Names and dynamic
arguments stay as written.

A new test in citry_vue_compiler compiles each directive kind on each
lane alone and compares the code with @vue/compiler-dom 3.5.44. Replaying
the 1,432 compiles recorded from the citry and citry_ui test suites gives
byte-identical code for every one. Only the authored text plan of one
template changes: its form handler's quoted event id now decodes, so the
template stays on S2.

The patched Vize crates and the compiler identity move to
0.420.0+citry.5.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@JuroOravec
JuroOravec merged commit 90c8acd into main Oct 24, 2026
30 checks passed
@JuroOravec
JuroOravec deleted the fix/directive-value-entities branch October 24, 2026 15:14
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 Vue components, a string literal inside a directive value keeps its character references (@click="msg = 'Tom &amp; Jerry'")

1 participant