From 348014c7ff19b4d677b020cec6722ce103e90364 Mon Sep 17 00:00:00 2001 From: Neil Cawse Date: Sat, 7 Nov 2026 18:14:23 +0000 Subject: [PATCH 1/4] pptx: strip VML conditional comments and index o:spid for legacy previews --- crates/pptx/src/read/shapes.rs | 60 ++++++++++++++++++++++++++++++---- crates/pptx/src/xml.rs | 17 ++++++---- 2 files changed, 63 insertions(+), 14 deletions(-) diff --git a/crates/pptx/src/read/shapes.rs b/crates/pptx/src/read/shapes.rs index e1368b427..467d889da 100644 --- a/crates/pptx/src/read/shapes.rs +++ b/crates/pptx/src/read/shapes.rs @@ -512,21 +512,51 @@ fn vml_preview(imp: &mut Imp, part: &Part, spid: &str) -> Option std::borrow::Cow<'_, [u8]> { + let mut out = Vec::with_capacity(bytes.len()); + let mut at = 0; + let mut changed = false; + while at < bytes.len() { + let rest = &bytes[at..]; + let marker = rest.iter().position(|b| *b == b'<').map(|i| at + i); + let Some(start) = marker else { + out.extend_from_slice(rest); + break; + }; + out.extend_from_slice(&bytes[at..start]); + let rest = &bytes[start..]; + let conditional = rest.starts_with(b"") { + next += 3; + } + at = next; + changed = true; + continue; + } + out.push(b'<'); + at = start + 1; + } + if changed { std::borrow::Cow::Owned(out) } else { std::borrow::Cow::Borrowed(bytes) } +} + fn vml_index(pkg: &crate::opc::Package, vml: &str) -> std::collections::HashMap { let mut out = std::collections::HashMap::new(); - let Some(doc) = pkg.xml(vml) else { return out }; + let Some(bytes) = pkg.xml_bytes(vml) else { return out }; + let decoded = crate::xml::decode_xml_bytes(bytes); + let bytes = strip_vml_conditionals(&decoded); + let Ok(doc) = crate::xml::parse(&bytes) else { return out }; let rels = pkg.rels(vml); let mut shapes = vec![]; doc.root.find_all("shape", &mut shapes); for s in shapes { - let Some(id) = s.attr("id") else { continue }; - if out.contains_key(id) { - continue; // the first shape with an id wins, as the old lookup did - } let Some(img) = s.find("imagedata") else { continue }; let Some(rid) = img.attr("o:relid").or(img.attr("r:id")) else { continue }; - if let Some(r) = rels.get(rid).filter(|r| !r.external) { - out.insert(id.to_string(), r.target.clone()); + let Some(r) = rels.get(rid).filter(|r| !r.external) else { continue }; + let keys = [s.attr("o:spid").or_else(|| s.attr("spid")), s.attr("id")]; + for key in keys.into_iter().flatten().filter(|key| !key.is_empty()) { + out.entry(key.to_string()).or_insert_with(|| r.target.clone()); } } out @@ -829,3 +859,19 @@ fn cell(imp: &mut Imp, part: &Part, tc: &El) -> Cell { } c } + +#[cfg(test)] +mod vml_tests { + use super::strip_vml_conditionals; + + #[test] + fn strips_vml_conditional_blocks_and_preserves_shape_xml() { + let raw = br#""#; + let cleaned = strip_vml_conditionals(raw); + let doc = crate::xml::parse(&cleaned).expect("parse VML"); + let mut shapes = vec![]; + doc.root.find_all("shape", &mut shapes); + assert_eq!(shapes.len(), 1); + assert_eq!(shapes[0].attr("o:spid"), Some("_x0000_s2049")); + } +} diff --git a/crates/pptx/src/xml.rs b/crates/pptx/src/xml.rs index b1160a639..b0a098878 100644 --- a/crates/pptx/src/xml.rs +++ b/crates/pptx/src/xml.rs @@ -211,16 +211,19 @@ impl Doc { } } +pub(crate) fn decode_xml_bytes(bytes: &[u8]) -> std::borrow::Cow<'_, [u8]> { + if bytes.starts_with(&[0xFF, 0xFE]) || bytes.starts_with(&[0xFE, 0xFF]) { + std::borrow::Cow::Owned(decode_utf16(bytes).into_bytes()) + } else { + std::borrow::Cow::Borrowed(bytes.strip_prefix(&[0xEF, 0xBB, 0xBF]).unwrap_or(bytes)) + } +} + /// Parse XML bytes into a DOM. Bounded depth and node count; entity references are resolved. pub fn parse(bytes: &[u8]) -> Result { // Skip a UTF-8 BOM; UTF-16 parts (rare) are decoded first. - let owned; - let bytes = if bytes.starts_with(&[0xFF, 0xFE]) || bytes.starts_with(&[0xFE, 0xFF]) { - owned = decode_utf16(bytes); - owned.as_bytes() - } else { - bytes.strip_prefix(&[0xEF, 0xBB, 0xBF]).unwrap_or(bytes) - }; + let decoded = decode_xml_bytes(bytes); + let bytes = decoded.as_ref(); let mut reader = quick_xml::Reader::from_reader(bytes); { let c = reader.config_mut(); From 3e8da178f2a93808733f7e9ccb9528ffdff0e87d Mon Sep 17 00:00:00 2001 From: aiongg <67543387+aiongg@users.noreply.github.com> Date: Sat, 7 Nov 2026 18:28:35 +0000 Subject: [PATCH 2/4] Apply review fixes - crates/pptx/src/read.rs - crates/pptx/src/read/shapes.rs --- crates/pptx/src/read.rs | 2 +- crates/pptx/src/read/shapes.rs | 13 ++++++++++--- 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/crates/pptx/src/read.rs b/crates/pptx/src/read.rs index 47f6229c3..fa71f07e4 100644 --- a/crates/pptx/src/read.rs +++ b/crates/pptx/src/read.rs @@ -34,7 +34,7 @@ pub struct Imp<'a> { pub custom_show_names: HashMap, /// GUIDs (upper case) of the table styles the file defines. pub table_style_ids: Vec, - /// Per legacy VML drawing part: shape id → preview picture part (see `shapes::vml_preview`). + /// Per legacy VML drawing part: shape id or o:spid → preview picture part (see `shapes::vml_preview`). pub vml_previews: HashMap>, /// Aggregate byte count of retained chart sidecars across the presentation. pub kept_sidecar_bytes: usize, diff --git a/crates/pptx/src/read/shapes.rs b/crates/pptx/src/read/shapes.rs index 467d889da..84af20600 100644 --- a/crates/pptx/src/read/shapes.rs +++ b/crates/pptx/src/read/shapes.rs @@ -510,8 +510,7 @@ fn vml_preview(imp: &mut Imp, part: &Part, spid: &str) -> Option`, ``, ``) removed, so shapes wrapped in them are parsed. fn strip_vml_conditionals(bytes: &[u8]) -> std::borrow::Cow<'_, [u8]> { let mut out = Vec::with_capacity(bytes.len()); let mut at = 0; @@ -541,12 +540,20 @@ fn strip_vml_conditionals(bytes: &[u8]) -> std::borrow::Cow<'_, [u8]> { if changed { std::borrow::Cow::Owned(out) } else { std::borrow::Cow::Borrowed(bytes) } } +/// Every shape id and o:spid in a VML drawing with a picture, mapped to that picture's part: parsed once per +/// drawing however many objects look it up. fn vml_index(pkg: &crate::opc::Package, vml: &str) -> std::collections::HashMap { let mut out = std::collections::HashMap::new(); let Some(bytes) = pkg.xml_bytes(vml) else { return out }; let decoded = crate::xml::decode_xml_bytes(bytes); let bytes = strip_vml_conditionals(&decoded); - let Ok(doc) = crate::xml::parse(&bytes) else { return out }; + let doc = match crate::xml::parse(&bytes) { + Ok(d) => d, + Err(e) => { + log::warn!("pptx: {vml}: {e}"); + return out; + } + }; let rels = pkg.rels(vml); let mut shapes = vec![]; doc.root.find_all("shape", &mut shapes); From d0fd5d8d89597f9221ddd3240b5f6a21313a8d3e Mon Sep 17 00:00:00 2001 From: "autocraft-bot[bot]" <354215646+autocraft-bot[bot]@users.noreply.github.com> Date: Wed, 11 Nov 2026 18:57:26 +0000 Subject: [PATCH 3/4] Apply review fixes - crates/pptx/src/read/shapes.rs --- crates/pptx/src/read/shapes.rs | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/crates/pptx/src/read/shapes.rs b/crates/pptx/src/read/shapes.rs index 131cafead..80bb10f77 100644 --- a/crates/pptx/src/read/shapes.rs +++ b/crates/pptx/src/read/shapes.rs @@ -525,7 +525,11 @@ fn strip_vml_conditionals(bytes: &[u8]) -> std::borrow::Cow<'_, [u8]> { out.extend_from_slice(&bytes[at..start]); let rest = &bytes[start..]; let conditional = rest.starts_with(b"") { next += 3; From 8f82023a06b3b19ae78aff28b0607104829d86ba Mon Sep 17 00:00:00 2001 From: Neil Cawse Date: Tue, 17 Nov 2026 12:38:58 +0000 Subject: [PATCH 4/4] test(pptx): cover VML preview inside conditional comment by o:spid in ole_vml --- crates/pptx/tests/it/ole_vml.rs | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/crates/pptx/tests/it/ole_vml.rs b/crates/pptx/tests/it/ole_vml.rs index ccdc803a2..ddaae24dd 100644 --- a/crates/pptx/tests/it/ole_vml.rs +++ b/crates/pptx/tests/it/ole_vml.rs @@ -32,6 +32,11 @@ fn deck() -> Vec { /// [`deck`] with `frames` in place of [`FRAME`]. fn deck_with(frames: &str) -> Vec { + deck_with_vml(frames, VML) +} + +/// [`deck`] with `frames` in place of [`FRAME`] and `vml` as the drawing. +fn deck_with_vml(frames: &str, vml: &str) -> Vec { let bytes = deckcraft_pptx::export(&defaults::new_presentation(None)).expect("export"); let mut z = zip::ZipArchive::new(Cursor::new(bytes)).expect("zip"); let mut out = zip::ZipWriter::new(Cursor::new(Vec::new())); @@ -55,7 +60,7 @@ fn deck_with(frames: &str) -> Vec { out.write_all(&d).expect("write"); } for (name, data) in [ - ("ppt/drawings/vmlDrawing1.vml", VML.as_bytes().to_vec()), + ("ppt/drawings/vmlDrawing1.vml", vml.as_bytes().to_vec()), ("ppt/drawings/_rels/vmlDrawing1.vml.rels", VML_RELS.as_bytes().to_vec()), ("ppt/media/other.png", png(10)), ("ppt/media/preview.png", png(200)), @@ -77,6 +82,18 @@ fn an_ole_object_without_a_picture_shows_its_vml_preview() { assert_eq!(media.name, "preview.png"); } +#[test] +fn a_vml_preview_inside_a_conditional_comment_is_found_by_its_o_spid() { + let head = &VML[..VML.find('>').expect("xml start") + 1]; + let vml = format!( + r#"{head}"# + ); + let p = deckcraft_pptx::import(&deck_with_vml(FRAME, &vml)).expect("import"); + let s = p.slides[0].shapes.iter().find(|s| s.name == "Object 3").expect("frame"); + let ShapeKind::Opaque { preview: Some(m), .. } = &s.kind else { panic!("{:?}", s.kind) }; + assert_eq!(p.media.iter().find(|x| x.id == *m).map(|x| x.name.as_str()), Some("preview.png")); +} + #[test] fn activex_controls_show_their_pictures() { let p = deckcraft_pptx::import(&deck()).expect("import");