fix: escape attr keys

This commit is contained in:
2026-01-11 16:37:17 +08:00
parent 158784cbe8
commit c8e617fe24

View File

@@ -11,16 +11,10 @@ pub(crate) trait CodegenContext {
fn get_sym(&self, id: SymId) -> &str;
}
impl<Ctx: CodegenContext> Compile<Ctx> for Ir {
fn compile(&self, ctx: &Ctx) -> String {
match self {
Ir::Int(int) => format!("{int}n"), // Generate BigInt literal
Ir::Float(float) => float.to_string(),
Ir::Str(s) => {
// Escape string for JavaScript
let mut escaped = String::with_capacity(s.val.len() + 2);
fn escape_quote_string(s: &str) -> String {
let mut escaped = String::with_capacity(s.len() + 2);
escaped.push('"');
for c in s.val.chars() {
for c in s.chars() {
match c {
'\\' => escaped.push_str("\\\\"),
'\"' => escaped.push_str("\\\""),
@@ -32,7 +26,14 @@ impl<Ctx: CodegenContext> Compile<Ctx> for Ir {
}
escaped.push('"');
escaped
}
}
impl<Ctx: CodegenContext> Compile<Ctx> for Ir {
fn compile(&self, ctx: &Ctx) -> String {
match self {
Ir::Int(int) => format!("{int}n"), // Generate BigInt literal
Ir::Float(float) => float.to_string(),
Ir::Str(s) => escape_quote_string(&s.val),
Ir::Path(p) => {
// Path needs runtime resolution for interpolated paths
let path_expr = ctx.get_ir(p.expr).compile(ctx);
@@ -259,7 +260,7 @@ impl<Ctx: CodegenContext> Compile<Ctx> for AttrSet {
for (&sym, &expr) in &self.stcs {
let key = ctx.get_sym(sym);
let value = ctx.get_ir(expr).compile(ctx);
attrs.push(format!("\"{}\": {}", key, value));
attrs.push(format!("{}: {}", escape_quote_string(key), value));
}
for (key_expr, value_expr) in &self.dyns {