Repository navigation
fix: decode Vue directive values and interpolations as Vue does - #322
Merged
Merged
Conversation
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 & Jerry'"` assigned `Tom & 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]>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #313
What was wrong
In a component that uses Vue, a string written inside a directive value
kept its character references:
set
msgtoTom & Jerry. Vue's own compiler (@vue/compiler-dom3.5.44) sets
Tom & Jerry. The raw;of&also made Citry readthe handler as a statement block. The same happened in
v-if,v-show,v-for,v-slotand#nameparameters,v-model(in the updatehandler),
v-text,v-memoand 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:
third_party/rust/vize_s1_to_s2), which writes atemplate when it can, parsed each expression from the source as
written. Only bound values and
v-htmlwere decoded, later, in theemitter.
vize_atelier_corecode generator, which writes a templatewhen 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-modelupdate handler came out wrong; the other read-backs nowdecode too, but no test case reached them. Its parser also leaves an
interpolation's text as written.
v-if="n > 0"worked before only by accident: the raw text is notJavaScript, so S2 declined the template, and the other lane happened to
read that one position decoded.
The fix
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 aslice of the source, so
Cxremembers which authored value it came fromand reports that span for it. The emitter's own decoding of bound values
and
v-htmlis removed, so nothing is decoded twice. Names and dynamicarguments are not decoded, because HTML never decodes them.
vize_atelier_corelane. A newentitymodule gives the text Vuereads from an expression span: the attribute rule inside an attribute
value, the text rule inside
{{ }}. Every step that reads a value backfrom the source now uses it; argument and name read-backs stay as
written. It finds out whether a span sits inside
{{ }}by looking backto the nearest quote,
=or>.transform_interpolationdecodes theinterpolation's text. The crate depends on
htmlize1.1.0 for this,which
vize_armaturealready uses.0.420.0+citry.5,because the compiled code changes for templates with references.
citrychecks that the compiler reports exactly this identity. BothREADME.citry.mdfiles list the change.Proof
crates/citry_vue_compiler/tests/directive_value_entities.rschecks 27 cases (26 templates) on S2 alone and on the
vize_atelier_corelane alone, against the code Vue 3.5.44 writes. Theycover each directive kind, a loop source, a
<template v-for>key and aslot outlet's bound name (with
&amp;, which decodes once to&), the statement-block rule (with&, and with&#40;, whose decoded;does make a statement block, as in Vue),v-if="n > 0"on purpose on both lanes, a bound value that keeps©=by the attribute rule, a dynamic argument, and interpolations.Both tests fail on
main. The lane without S2 is reached throughvize_atelier_dom'sdavinci-differentialentry point, which this crateturns on only as a dev-dependency feature.
(1,432) that the
citryandcitry_uitest suites make onmain, andreplayed 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('citryEvent7', $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.
test_directive_values_decode_character_references_as_vue_doeshydrates
<p :data-n="'a & b'.length" v-text="'x < y'">and getsdata-n="5"andx < y.The existing
vize_s1_to_s2andvize_atelier_coretest targets have thesame results as on
main(emit_if,metamorphicanddynamic_v_model_argumentfail there too, and the targets that needinstado 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>. Atemplate 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 nowdescribe the decoding.
Changelog: a Fixes line in
CHANGELOG.md.Not fixed here
These already happen on
mainand are not directive values. A follow-upPR fixes them:
title="&#40;"or the text&#40;shows(on bothlanes, where Vue and the browser show
(: the code generator decodesa numeric reference a second time.
title="a¬in", stays as written in Citry, as in the browser, whileVue's compiler writes
a¬in.<component is="a&b">keeps&, and#[x&y]writes an empty slot key.🤖 Generated with Claude Code