pptx: strip VML conditional comments and index o:spid for legacy previews

This commit is contained in:
Neil Cawse
2026-11-08 13:36:49 +00:00
parent 48e2203123
commit 348014c7ff
2 changed files with 63 additions and 14 deletions
+53 -7
View File
@@ -512,21 +512,51 @@ fn vml_preview(imp: &mut Imp, part: &Part, spid: &str) -> Option<deckcraft_model
/// Every shape id in a VML drawing with a picture, mapped to that picture's part: parsed once per
/// drawing however many objects look it up.
fn strip_vml_conditionals(bytes: &[u8]) -> 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"<!--[if") || rest.starts_with(b"<![if") || rest.starts_with(b"<![endif");
if conditional && let Some(end) = rest.iter().position(|b| *b == b'>') {
let mut next = start + end + 1;
if bytes.get(next..next + 3) == Some(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<String, String> {
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#"<xml xmlns:v="urn:schemas-microsoft-com:vml" xmlns:o="urn:schemas-microsoft-com:office:office"><!--[if gte vml 1]><v:shape id="_x0000_s1025" o:spid="_x0000_s2049"><v:imagedata o:relid="rId1"/></v:shape><![endif]--></xml>"#;
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"));
}
}
+10 -7
View File
@@ -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<Doc, PptxError> {
// 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();