Skip to content

Commit f2e046d

Browse files
committed
fix: 修复font-family解析失败导致编译异常
1 parent 5001c97 commit f2e046d

File tree

1 file changed

+27
-5
lines changed

1 file changed

+27
-5
lines changed

src/parse_style_properties.rs

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -272,11 +272,33 @@ pub fn parse_style_properties(properties: &Vec<(String, Property)>) -> DeclsAndV
272272
final_properties.push(StyleValueType::Expr(Expr::new(
273273
CSSPropertyType::FontFamily,
274274
{
275-
let result = value.value_to_css_string(PrinterOptions::default()).unwrap();
276-
let final_str = result.replace(r"\.", ".");
277-
generate_expr_lit_str!(final_str)
278-
}
279-
)));
275+
// 直接从 Property::FontFamily 提取原始值,避免 CSS 转义
276+
let font_family_str = match value {
277+
Property::FontFamily(font_families) => {
278+
// 提取所有字体名称,用逗号连接,不进行 CSS 转义
279+
font_families.iter()
280+
.map(|family| {
281+
match family {
282+
lightningcss::properties::font::FontFamily::FamilyName(name) => name.as_ref().to_string(),
283+
lightningcss::properties::font::FontFamily::Generic(generic) => generic.to_css_string(PrinterOptions::default()).unwrap(),
284+
}
285+
})
286+
.collect::<Vec<_>>()
287+
.join(", ")
288+
},
289+
_ => {
290+
// 如果不是 FontFamily 类型,回退到 CSS 字符串方式,但去掉转义
291+
value.value_to_css_string(PrinterOptions::default())
292+
.map(|s| s.replace("\\", "").replace("\"", ""))
293+
.unwrap_or_else(|e| {
294+
eprintln!("fontFamily value_to_css_string failed: {:?}, value: {:?}", e, value);
295+
String::new()
296+
})
297+
}
298+
};
299+
generate_expr_lit_str!(font_family_str)
300+
}
301+
)));
280302
}
281303
"lineHeight" => {
282304
final_properties.push(StyleValueType::LineHeight(LineHeight::from((

0 commit comments

Comments
 (0)