From f6bcebc186486dc49e9049cf4747e3f72b7a6093 Mon Sep 17 00:00:00 2001 From: bokuweb Date: Thu, 18 Jul 2024 19:11:21 +0900 Subject: [PATCH] fix: escape font (#745) --- docx-core/src/documents/elements/run_fonts.rs | 38 ++++++++++++++----- 1 file changed, 29 insertions(+), 9 deletions(-) diff --git a/docx-core/src/documents/elements/run_fonts.rs b/docx-core/src/documents/elements/run_fonts.rs index baba5dd..21a244e 100644 --- a/docx-core/src/documents/elements/run_fonts.rs +++ b/docx-core/src/documents/elements/run_fonts.rs @@ -1,6 +1,7 @@ use serde::{Deserialize, Serialize}; use crate::documents::BuildXML; +use crate::escape::escape; use crate::xml_builder::*; /* @@ -40,47 +41,56 @@ impl RunFonts { } pub fn ascii(mut self, f: impl Into) -> Self { - self.ascii = Some(f.into()); + let s = f.into(); + self.ascii = Some(escape(&s)); self } pub fn hi_ansi(mut self, f: impl Into) -> Self { - self.hi_ansi = Some(f.into()); + let s = f.into(); + self.hi_ansi = Some(escape(&s)); self } pub fn east_asia(mut self, f: impl Into) -> Self { - self.east_asia = Some(f.into()); + let s = f.into(); + self.east_asia = Some(escape(&s)); self } pub fn cs(mut self, f: impl Into) -> Self { - self.cs = Some(f.into()); + let s = f.into(); + self.cs = Some(escape(&s)); self } pub fn ascii_theme(mut self, f: impl Into) -> Self { - self.ascii_theme = Some(f.into()); + let s = f.into(); + self.ascii_theme = Some(escape(&s)); self } pub fn hi_ansi_theme(mut self, f: impl Into) -> Self { - self.hi_ansi_theme = Some(f.into()); + let s = f.into(); + self.hi_ansi_theme = Some(escape(&s)); self } pub fn east_asia_theme(mut self, f: impl Into) -> Self { - self.east_asia_theme = Some(f.into()); + let s = f.into(); + self.east_asia_theme = Some(escape(&s)); self } pub fn cs_theme(mut self, f: impl Into) -> Self { - self.cs_theme = Some(f.into()); + let s = f.into(); + self.cs_theme = Some(escape(&s)); self } pub fn hint(mut self, f: impl Into) -> Self { - self.hint = Some(f.into()); + let s = f.into(); + self.hint = Some(escape(&s)); self } } @@ -147,4 +157,14 @@ mod tests { let b = c.build(); assert_eq!(str::from_utf8(&b).unwrap(), r#""#); } + + #[test] + fn test_run_fonts_with_escape() { + let c = RunFonts::new().east_asia(r#""Calibri",sans-serif"#); + let b = c.build(); + assert_eq!( + str::from_utf8(&b).unwrap(), + r#""#, + ); + } }