Escape quotes and backslashes in exported CSS font-family

Found while testing crates/svg/src/css.rs:84 (font_props)

Co-Authored-By: Claude Haiku 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
NetterSilver
2026-10-17 16:38:42 +07:00
committed by bflatastic
co-authored by Claude Haiku 5.5
parent d18135bfcd
commit a631a0df6c
2 changed files with 18 additions and 1 deletions
+6 -1
View File
@@ -81,7 +81,12 @@ pub(crate) fn font_descriptor(st: &CharStyle) -> (u16, bool) {
/// (600 for Semibold, 300 for Light), `bold` for 700 and left out for 400.
pub(crate) fn font_props(st: &CharStyle, len: &dyn Fn(f64) -> String) -> Props {
let mut p = Props::new();
let fam = if st.font_family.contains(|c: char| c.is_whitespace() || c == ',') { format!("'{}'", st.font_family) } else { st.font_family.clone() };
// A quoted family escapes `\` and `'`, which would otherwise end the string early.
let fam = if st.font_family.contains(|c: char| c.is_whitespace() || matches!(c, ',' | '\'' | '\\')) {
format!("'{}'", st.font_family.replace('\\', "\\\\").replace('\'', "\\'"))
} else {
st.font_family.clone()
};
p.push(("font-family", fam));
p.push(("font-size", len(st.size * st.v_scale / 100.0)));
let (weight, italic) = font_descriptor(st);
+12
View File
@@ -259,3 +259,15 @@ fn groups_stand_for_their_objects_and_hidden_ones_are_left_out() {
assert_eq!(sheet.rules.len(), 1, "one visible rectangle, once: {sheet:?}");
assert_eq!(sheet.text(), sheet.rules[0].text());
}
#[test]
fn font_family_with_quote_or_backslash_is_escaped() {
for (family, want) in [("Bob's Font", r"'Bob\'s Font'"), (r"Back\slash Font", r"'Back\\slash Font'"), ("Bob's", r"'Bob\'s'")] {
let mut d = doc();
let st = CharStyle { font_family: family.into(), size: 18.0, ..CharStyle::default() };
let n = text(&mut d, "Hello", st);
let id = add(&mut d, n);
let r = one(&d, id, &CssOptions::default());
assert_eq!(prop(&r, "font-family"), Some(want));
}
}