diff --git a/Cargo.lock b/Cargo.lock index 2e771fe..48cf38f 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -970,6 +970,16 @@ dependencies = [ "wayland-client", ] +[[package]] +name = "craft-xml" +version = "0.1.0" +source = "git+https://github.com/storytold/craft-libs?rev=b6d9feed23ceae249b40593ed84a668343091627#b6d9feed23ceae249b40593ed84a668343091627" +dependencies = [ + "quick-xml 0.41.0", + "serde", + "thiserror 2.0.21", +] + [[package]] name = "craft-zip" version = "0.1.0" @@ -2735,6 +2745,7 @@ dependencies = [ "craft-fonts-build", "craft-mcmat", "craft-texture", + "craft-xml", "dds", "flate2", "gltf", diff --git a/Cargo.toml b/Cargo.toml index e0cd8b6..c3d9dff 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -43,6 +43,8 @@ repository = "https://github.com/storytold/materialcraft" [workspace.dependencies] craft-texture-dds = { package = "craft-texture", version = "0.1.0", git = "https://github.com/storytold/craft-libs", rev = "726ad0f8290bedb6ced60fa06200a6b574f9990d", features = ["dds"] } +# Shared XML escaping; SBS model and serialization policy stay app-local. +craft-xml = { version = "0.1.0", git = "https://github.com/storytold/craft-libs", rev = "b6d9feed23ceae249b40593ed84a668343091627" } resvg = { version = "0.47", default-features = false } # internal materialcraft-core = { path = "crates/core" } diff --git a/crates/formats/Cargo.toml b/crates/formats/Cargo.toml index d46c9e3..45d6fea 100644 --- a/crates/formats/Cargo.toml +++ b/crates/formats/Cargo.toml @@ -9,6 +9,7 @@ license.workspace = true workspace = true [dependencies] +craft-xml = { workspace = true } craft-mcmat = { workspace = true } materialcraft-core = { workspace = true } materialcraft-image = { workspace = true } diff --git a/crates/formats/src/sbs/xml.rs b/crates/formats/src/sbs/xml.rs index 7382406..248bbd4 100644 --- a/crates/formats/src/sbs/xml.rs +++ b/crates/formats/src/sbs/xml.rs @@ -23,19 +23,13 @@ pub(super) fn child_value<'a>(node: XmlNode<'a, '_>, tag: &str) -> Option<&'a st } pub(super) fn xml_escape(text: &str) -> String { let mut out = String::with_capacity(text.len()); - for c in text.chars() { - match c { - '&' => out.push_str("&"), - '<' => out.push_str("<"), - '>' => out.push_str(">"), - '"' => out.push_str("""), - '\'' => out.push_str("'"), - '\t' => out.push_str(" "), - '\n' => out.push_str(" "), - '\r' => out.push_str(" "), - c if (c as u32) < 0x20 || c == '\u{fffe}' || c == '\u{ffff}' => {} - c => out.push(c), + // SBS historically escapes apostrophes too, although attributes are double quoted. + // Keep that byte-level policy while sharing XML validity/attribute escaping. + for (i, part) in text.split('\'').enumerate() { + if i != 0 { + out.push_str("'"); } + craft_xml::push_esc_attr(&mut out, part); } out } @@ -43,3 +37,24 @@ pub(super) fn is_parameters_element(xml: &str) -> bool { let options = roxmltree::ParsingOptions { allow_dtd: false, nodes_limit: 1_000_000 }; xml.len() < (16 << 20) && roxmltree::Document::parse_with_options(xml, options).is_ok_and(|d| d.root_element().tag_name().name() == "parameters") } + +#[cfg(test)] +mod shared_escape_tests { + use super::xml_escape; + + #[test] + fn sbs_attribute_wire_spelling_is_preserved() { + assert_eq!(xml_escape("&<>\"'\t\n\r"), "&<>"' "); + assert_eq!(xml_escape("\0\u{1f}\u{fffe}\u{ffff}é😀"), "é😀"); + assert_eq!(xml_escape("''"), "''"); + assert_eq!(xml_escape(""), ""); + } + + #[test] + fn written_attribute_roundtrips_valid_unicode_and_whitespace() { + let value = "élan 😀 ' \" & < >\t\n\r"; + let xml = format!("", xml_escape(value)); + let doc = roxmltree::Document::parse(&xml).unwrap(); + assert_eq!(doc.root_element().attribute("v"), Some(value)); + } +}