diff --git a/Cargo.lock b/Cargo.lock index fefa3f3c8..2aef7c588 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1000,6 +1000,15 @@ dependencies = [ "web-sys", ] +[[package]] +name = "craft-xml" +version = "0.1.0" +source = "git+https://github.com/storytold/craft-libs?rev=ea24422ae9859dc5da398d0462bc87f8e02e0d10#ea24422ae9859dc5da398d0462bc87f8e02e0d10" +dependencies = [ + "quick-xml 0.41.0", + "thiserror 2.0.21", +] + [[package]] name = "crc32fast" version = "1.5.2" @@ -1234,6 +1243,7 @@ dependencies = [ name = "deckcraft-pptx" version = "0.19.0" dependencies = [ + "craft-xml", "deckcraft-color", "deckcraft-engine", "deckcraft-geom", diff --git a/Cargo.toml b/Cargo.toml index cd04dc1d7..fd27300df 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -11,6 +11,8 @@ rust-version = "1.95" repository = "https://github.com/storytold/deckcraft" [workspace.dependencies] +# XML writer primitives; parser admission and OOXML policy stay app-local. +craft-xml = { git = "https://github.com/storytold/craft-libs", rev = "ea24422ae9859dc5da398d0462bc87f8e02e0d10", version = "0.1.0" } # internal deckcraft-geom = { path = "crates/geom" } deckcraft-color = { path = "crates/color" } diff --git a/crates/pptx/Cargo.toml b/crates/pptx/Cargo.toml index 0ea79d6b4..fd9a6fc4e 100644 --- a/crates/pptx/Cargo.toml +++ b/crates/pptx/Cargo.toml @@ -9,6 +9,7 @@ description = "DeckCraft PPTX: Office Open XML (PresentationML/DrawingML) import workspace = true [dependencies] +craft-xml.workspace = true deckcraft-model = { workspace = true } deckcraft-geom = { workspace = true } deckcraft-color = { workspace = true } diff --git a/crates/pptx/src/xml.rs b/crates/pptx/src/xml.rs index b1160a639..640844ad6 100644 --- a/crates/pptx/src/xml.rs +++ b/crates/pptx/src/xml.rs @@ -354,45 +354,8 @@ fn decode_utf16(bytes: &[u8]) -> String { } } -/// Is `c` allowed in XML 1.0 text? -fn xml_char(c: char) -> bool { - matches!(c, '\t' | '\n' | '\r') || (c >= ' ' && c != '\u{FFFE}' && c != '\u{FFFF}') -} - -/// Escape text content (and drop characters XML 1.0 forbids). -pub fn esc(s: &str) -> String { - let mut o = String::with_capacity(s.len()); - for c in s.chars() { - match c { - '&' => o.push_str("&"), - '<' => o.push_str("<"), - '>' => o.push_str(">"), - '\r' => o.push_str(" "), - c if xml_char(c) => o.push(c), - _ => {} - } - } - o -} - -/// Escape an attribute value. -pub fn esc_attr(s: &str) -> String { - let mut o = String::with_capacity(s.len()); - for c in s.chars() { - match c { - '&' => o.push_str("&"), - '<' => o.push_str("<"), - '>' => o.push_str(">"), - '"' => o.push_str("""), - '\n' => o.push_str(" "), - '\r' => o.push_str(" "), - '\t' => o.push_str(" "), - c if xml_char(c) => o.push(c), - _ => {} - } - } - o -} +// Preserve the public writer helpers while sharing the XML 1.0 escaping rules. +pub use craft_xml::{esc, esc_attr}; /// A string builder for XML parts. #[derive(Default)] @@ -542,6 +505,37 @@ mod tests { assert_eq!(esc_attr("\"x\"\n"), ""x" "); } + #[test] + fn shared_escaping_preserves_xml_wire_bytes() { + let text = "&<>\"'\t\n\r\0\u{b}\u{fffe}\u{ffff}é😀 "; + assert_eq!(esc(text), "&<>\"'\t\n é😀&#13;"); + assert_eq!(esc_attr(text), "&<>"' é😀&#13;"); + assert_eq!(esc(""), ""); + assert_eq!(esc_attr(""), ""); + for code in 0..32 { + let c = char::from_u32(code).unwrap(); + let text = c.to_string(); + match c { + '\t' => { + assert_eq!(esc(&text), "\t"); + assert_eq!(esc_attr(&text), " "); + } + '\n' => { + assert_eq!(esc(&text), "\n"); + assert_eq!(esc_attr(&text), " "); + } + '\r' => { + assert_eq!(esc(&text), " "); + assert_eq!(esc_attr(&text), " "); + } + _ => { + assert_eq!(esc(&text), ""); + assert_eq!(esc_attr(&text), ""); + } + } + } + } + #[test] fn roundtrip_serialize() { let d = parse(b"x & y").unwrap();