Merge pull request #1585 from storytold/extract/shared-score-xml

Use shared XML escaping for MusicXML and SVG scores
This commit is contained in:
FredApps authored and GitHub committed 2026-11-16 06:55:01 +01:00
commit 5976545fe1
4 files changed
+24 -7

No files matched your search

Generated
+10
View File
@@ -1220,6 +1220,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"
@@ -4525,6 +4534,7 @@ dependencies = [
"craft-audio",
"craft-docpath",
"craft-scraft-format",
"craft-xml",
"log",
"proptest",
"serde",
+1
View File
@@ -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" }
craft-mime = { version = "0.1.0", git = "https://github.com/storytold/craft-libs", rev = "ea24422ae9859dc5da398d0462bc87f8e02e0d10" }
# internal
soundcraft-time = { path = "crates/time" }
+1
View File
@@ -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
+12 -7
View File
@@ -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::<String>()
.replace('&', "&amp;")
.replace('<', "&lt;")
.replace('>', "&gt;")
.replace('"', "&quot;")
// 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("&#9;", "\t").replace("&#10;", "\n").replace("&#13;", "\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}é𝄞&#9;&#10;&#13;";
assert_eq!(xml_escape(input), "&lt;&amp;&gt;&quot;'\t\n\ré𝄞&amp;#9;&amp;#10;&amp;#13;");
assert_eq!(xml_escape(""), "");
assert_eq!(xml_escape("Already &amp; escaped"), "Already &amp;amp; escaped");
}
#[test]
fn split_units_uses_representable_values() {
assert_eq!(split_units(16), vec![16]);