fix(engine): escape Vue attribute values that follow literal text in a Code Connect template

This commit is contained in:
Dylan M. Taylor committed 2026-11-03 19:03:55 +00:00
1 parent 35684eaa02
commit a23c9df9d6
2 files changed
+19 -12

No files matched your search

+3
View File
@@ -260,6 +260,9 @@ fn vue_links_are_a_default_import_with_quoted_bindings() {
ex(&mut s, "instance.setProperties", json!({"ids": [i], "properties": {"Title": "Say \"hi\""}}));
ex(&mut s, "codeConnect.set", json!({"id": i, "language": "vue", "template": "<Card title=\"{title} {title}\" />"}));
assert_eq!(code(&mut s, i, "vue"), "import Card from \"./Card.vue\";\n\n<Card title=\"Say &quot;hi&quot; Say &quot;hi&quot;\" />\n");
// Literal text before the first placeholder keeps it inside the attribute value.
ex(&mut s, "codeConnect.set", json!({"id": i, "language": "vue", "template": "<Card title=\"Say: {title}\" />"}));
assert_eq!(code(&mut s, i, "vue"), "import Card from \"./Card.vue\";\n\n<Card title=\"Say: Say &quot;hi&quot;\" />\n");
// Text after a completed tag is text, even when it contains `=`.
ex(&mut s, "instance.setProperties", json!({"ids": [i], "properties": {"Title": "Buy"}}));
ex(&mut s, "codeConnect.set", json!({"id": i, "language": "vue", "template": "<p>Total = {title}</p>"}));
+16 -12
View File
@@ -288,41 +288,45 @@ fn pieces(t: &str) -> Vec<Piece<'_>> {
checkable.then(|| parse(t).ok()).flatten().unwrap_or_else(|| vec![Piece::Text(t)])
}
/// Whether `s` ends inside an unclosed opening tag (no `>` outside quotes since the last `<`).
fn inside_open_tag(s: &str) -> bool {
/// Whether `s` ends inside an unclosed opening tag (no `>` outside quotes since the last `<`), and
/// the quote (with its byte offset) still open there.
fn tag_state(s: &str) -> (bool, Option<(char, usize)>) {
let mut in_tag = false;
let mut quote = None;
let mut chars = s.chars().peekable();
while let Some(c) = chars.next() {
let mut chars = s.char_indices().peekable();
while let Some((i, c)) = chars.next() {
match quote {
Some(q) => {
Some((q, _)) => {
if c == q {
quote = None;
}
}
None => match c {
'<' if !in_tag && chars.peek().is_some_and(|ch| ch.is_ascii_alphabetic() || matches!(*ch, '/' | '!' | '?')) => {
'<' if !in_tag && chars.peek().is_some_and(|(_, ch)| ch.is_ascii_alphabetic() || matches!(*ch, '/' | '!' | '?')) => {
in_tag = true;
}
'>' if in_tag => in_tag = false,
'"' | '\'' if in_tag => quote = Some(c),
'"' | '\'' if in_tag => quote = Some((c, i)),
_ => {}
},
}
}
in_tag
(in_tag, quote)
}
/// Where a placeholder sits in a Vue template: whether the attribute is bound, and the quote the
/// template already opened (if any). `None` when it isn't in an attribute at all.
fn vue_attribute(out: &str) -> Option<(bool, Option<char>)> {
let end = out.trim_end();
if !inside_open_tag(end) {
let (in_tag, open) = tag_state(out);
if !in_tag {
return None;
}
let (after, quote) = match end.chars().last() {
Some(q @ ('"' | '\'')) => (&end[..end.len() - q.len_utf8()], Some(q)),
_ => (end, None),
// A quote still open, with literal text after it (`title="Buy {x}"`), puts the placeholder in
// that attribute's value as much as one directly after the quote does.
let (after, quote) = match open {
Some((q, at)) => (&out[..at], Some(q)),
None => (end, None),
};
let after = after.trim_end();
let eq = after.rfind('=')?;