From 4bdda5ffad013739a6f001fb7cd773771277b6e8 Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Sun, 15 Nov 2026 13:23:47 -0500 Subject: [PATCH] Reuse shared XML escaping for score exports --- Cargo.lock | 10 ++++++++++ Cargo.toml | 1 + crates/engine/Cargo.toml | 1 + crates/engine/src/score.rs | 19 ++++++++++++------- 4 files changed, 24 insertions(+), 7 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index d02878c5..e2c5b0f6 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1205,6 +1205,15 @@ dependencies = [ "thiserror 2.0.21", ] +[[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 = "craft-zip" version = "0.1.0" @@ -4490,6 +4499,7 @@ dependencies = [ "craft-audio", "craft-docpath", "craft-scraft-format", + "craft-xml", "log", "proptest", "serde", diff --git a/Cargo.toml b/Cargo.toml index fa0c1dee..db22c7f0 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -11,6 +11,7 @@ rust-version = "1.95" repository = "https://github.com/storytold/soundcraft" [workspace.dependencies] +craft-xml = { version = "0.1.0", git = "https://github.com/storytold/craft-libs", rev = "ea24422ae9859dc5da398d0462bc87f8e02e0d10" } # internal soundcraft-time = { path = "crates/time" } soundcraft-midi = { path = "crates/midi" } diff --git a/crates/engine/Cargo.toml b/crates/engine/Cargo.toml index f9f1e2df..f0ab377f 100644 --- a/crates/engine/Cargo.toml +++ b/crates/engine/Cargo.toml @@ -8,6 +8,7 @@ rust-version.workspace = true repository.workspace = true [dependencies] +craft-xml.workspace = true soundcraft-time.workspace = true craft-audio.workspace = true craft-scraft-format.workspace = true diff --git a/crates/engine/src/score.rs b/crates/engine/src/score.rs index 05056058..c5dfd016 100644 --- a/crates/engine/src/score.rs +++ b/crates/engine/src/score.rs @@ -273,13 +273,10 @@ fn spell(pitch: u8) -> (char, i32, i32) { } fn xml_escape(s: &str) -> String { - s.chars() - .filter(|c| matches!(c, '\t' | '\n' | '\r' | '\u{20}'..='\u{D7FF}' | '\u{E000}'..='\u{FFFD}' | '\u{10000}'..='\u{10FFFF}')) - .collect::() - .replace('&', "&") - .replace('<', "<") - .replace('>', ">") - .replace('"', """) + // Keep the score export's existing literal whitespace and quote spelling. The + // shared attribute writer owns XML legality/escaping; only its generated + // whitespace references are restored (input ampersands were escaped first). + craft_xml::esc_attr(s).replace(" ", "\t").replace(" ", "\n").replace(" ", "\r") } fn note_type(units: u32) -> (&'static str, bool) { @@ -610,6 +607,14 @@ fn rest(o: &mut String, x: f64, y0: f64, sp: f64, units: u32) { mod tests { use super::*; + #[test] + fn shared_xml_escaping_preserves_score_bytes() { + let input = "<&>\"'\t\n\r\0\u{1f}\u{fffe}\u{ffff}é𝄞 "; + assert_eq!(xml_escape(input), "<&>"'\t\n\ré𝄞&#9;&#10;&#13;"); + assert_eq!(xml_escape(""), ""); + assert_eq!(xml_escape("Already & escaped"), "Already &amp; escaped"); + } + #[test] fn split_units_uses_representable_values() { assert_eq!(split_units(16), vec![16]);