From d593a8f99d62bc834c081045a51202979a63bc71 Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Sun, 15 Nov 2026 10:19:11 -0500 Subject: [PATCH 1/2] Use existing craft-xml escaping for XLSX output --- Cargo.lock | 1 + Cargo.toml | 1 + crates/xlsx/Cargo.toml | 1 + crates/xlsx/src/xml.rs | 67 ++++++++++++------------------------------ 4 files changed, 22 insertions(+), 48 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 395518b46..d0c38e036 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2349,6 +2349,7 @@ dependencies = [ "craft-offcrypto", "craft-opc", "craft-xls", + "craft-xml", "gridcraft-core", "gridcraft-formula", "gridcraft-functions", diff --git a/Cargo.toml b/Cargo.toml index debe80119..ce72d1bc3 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -40,6 +40,7 @@ craft-opc = { git = "https://github.com/storytold/craft-libs", rev = "bb57d422d0 # turn them into workbooks. craft-csv = { git = "https://github.com/storytold/craft-libs", rev = "bb57d422d0bc842cbde430955825553ab7b976f4", version = "0.1.0" } # Excel 97-2003 (.xls, BIFF8) workbooks; `crates/xlsx/src/xls.rs` maps them onto the model. +craft-xml = { git = "https://github.com/storytold/craft-libs", rev = "bb57d422d0bc842cbde430955825553ab7b976f4", version = "0.1.0" } craft-xls = { git = "https://github.com/storytold/craft-libs", rev = "bb57d422d0bc842cbde430955825553ab7b976f4", version = "0.1.0" } # foundation diff --git a/crates/xlsx/Cargo.toml b/crates/xlsx/Cargo.toml index 7825b2d23..3bdaaa28a 100644 --- a/crates/xlsx/Cargo.toml +++ b/crates/xlsx/Cargo.toml @@ -23,6 +23,7 @@ craft-csv = { workspace = true } craft-offcrypto = { workspace = true } craft-opc = { workspace = true } craft-xls = { workspace = true } +craft-xml = { workspace = true } thiserror = { workspace = true } quick-xml = { workspace = true } zip = { workspace = true } diff --git a/crates/xlsx/src/xml.rs b/crates/xlsx/src/xml.rs index d9ff04f51..fc07d2048 100644 --- a/crates/xlsx/src/xml.rs +++ b/crates/xlsx/src/xml.rs @@ -359,31 +359,9 @@ pub fn parse_streaming(xml: &[u8], split: &[&str], cb: &mut dyn FnMut(El) -> Res // ---------------------------------------------------------------- writing helpers -/// Escapes text content. -pub fn esc(s: &str) -> String { - let mut o = String::with_capacity(s.len()); - push_esc(&mut o, s); - o -} - -/// Appends [`esc`]`(s)` to `o`. -pub fn push_esc(o: &mut String, s: &str) { - if s.bytes().all(|b| matches!(b, b'\t' | b'\n' | 0x20..=0x7E) && !matches!(b, b'&' | b'<' | b'>')) { - o.push_str(s); - return; - } - o.reserve(s.len()); - for c in s.chars() { - match c { - '&' => o.push_str("&"), - '<' => o.push_str("<"), - '>' => o.push_str(">"), - '\r' => o.push_str(" "), - c if is_xml_char(c) => o.push(c), - _ => {} - } - } -} +// XML 1.0 byte escaping is shared; spreadsheet string and phonetic policy stays here. +use craft_xml::is_xml_char; +pub use craft_xml::{esc, esc_attr, push_esc}; /// Appends `n` in decimal. pub fn push_u64(o: &mut String, mut n: u64) { @@ -422,29 +400,6 @@ pub fn push_a1(o: &mut String, c: gridcraft_core::CellRef) { push_u64(o, u64::from(c.row) + 1); } -/// Escapes an attribute value (double-quoted). -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 is_xml_char(c) => o.push(c), - _ => {} - } - } - o -} - -fn is_xml_char(c: char) -> bool { - matches!(c, '\t' | '\n' | '\r' | '\u{20}'..='\u{D7FF}' | '\u{E000}'..='\u{FFFD}' | '\u{10000}'..='\u{10FFFF}') -} - /// ST_Xstring encoding for cell text: characters XML can't carry and carriage returns become /// `_xHHHH_`, and literal `_xHHHH_` sequences are protected with `_x005F_`. The result still needs /// [`esc`]. @@ -805,3 +760,19 @@ mod tests { assert_eq!("1.5E20".parse::().unwrap(), 1.5e20); } } + +#[cfg(test)] +mod shared_escape_parity { + use super::*; + #[test] + fn xml_content_and_attributes_preserve_grid_bytes() { + let value = "&<>\"'\t\n\r\0\u{1f}\u{fffe}\u{ffff}é😀"; + assert_eq!(esc(value), "&<>\"'\t\n é😀"); + assert_eq!(esc_attr(value), "&<>"' é😀"); + let mut appended = "prefix:".to_owned(); + push_esc(&mut appended, value); + assert_eq!(appended, format!("prefix:{}", esc(value))); + assert_eq!(esc("plain ASCII\t\n"), "plain ASCII\t\n"); + assert_eq!(decode_xstring(&encode_xstring("_x000D_\r\0😀")), "_x000D_\r\0😀"); + } +} From f22b4ed9dd80ae0fefbb404982062c68111c0bb8 Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Sun, 15 Nov 2026 14:07:48 -0500 Subject: [PATCH 2/2] xlsx: import shared append escaping only for tests --- crates/xlsx/src/xml.rs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/crates/xlsx/src/xml.rs b/crates/xlsx/src/xml.rs index fc07d2048..f7ee745a6 100644 --- a/crates/xlsx/src/xml.rs +++ b/crates/xlsx/src/xml.rs @@ -361,7 +361,9 @@ pub fn parse_streaming(xml: &[u8], split: &[&str], cb: &mut dyn FnMut(El) -> Res // XML 1.0 byte escaping is shared; spreadsheet string and phonetic policy stays here. use craft_xml::is_xml_char; -pub use craft_xml::{esc, esc_attr, push_esc}; +#[cfg(test)] +use craft_xml::push_esc; +pub use craft_xml::{esc, esc_attr}; /// Appends `n` in decimal. pub fn push_u64(o: &mut String, mut n: u64) {