mirror of
https://github.com/storytold/vectorcraft.git
synced 2026-10-24 17:56:50 -06:00
Merge pull request #2657 from Silvertree2010/fix/h2438-css-svg-export-writes-an-invalid
Escape quotes and backslashes in exported CSS font-family
This commit is contained in:
@@ -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);
|
||||
|
||||
@@ -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));
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user