From 8c85789cb1e71e450236099020adab40ebfcd26c Mon Sep 17 00:00:00 2001 From: Neil Cawse Date: Sat, 7 Nov 2026 18:14:24 +0000 Subject: [PATCH 1/6] pptx: round-trip p:video and p:audio media timing nodes and autoplay conditions --- crates/pptx/src/read/timing.rs | 42 +++++++++++++++++++++++--- crates/pptx/src/write.rs | 15 +++++++++- crates/pptx/src/write/timing.rs | 44 ++++++++++++++++++++++++++-- crates/pptx/tests/it/media_timing.rs | 24 +++++++++++++++ 4 files changed, 118 insertions(+), 7 deletions(-) diff --git a/crates/pptx/src/read/timing.rs b/crates/pptx/src/read/timing.rs index 1b6aa8b1b..37a7ccb53 100644 --- a/crates/pptx/src/read/timing.rs +++ b/crates/pptx/src/read/timing.rs @@ -207,11 +207,41 @@ fn behaviour_auto_reverses(c: &El) -> bool { /// Volume, mute, Loop until Stopped, Hide While Not Playing and Play Full Screen live in the timing tree /// (`p:video`/`p:audio` > `p:cMediaNode`), not on the picture: copy them onto the target's `MediaClip`. pub fn media_settings(timing: &El, ctx: &IdCtx, shapes: &mut [Shape]) { - let mut nodes = vec![]; - for tag in ["video", "audio"] { - timing.find_all(tag, &mut nodes); + fn collect_media<'a>(e: &'a El, deferred: bool, out: &mut Vec<(&'a El, bool)>, depth: usize) { + if depth > 64 || out.len() >= 10_000 { + return; + } + let mut deferred = deferred; + if e.is("cTn") + && e.attr("nodeType") != Some("tmRoot") + && let Some(st) = e.child("stCondLst") + { + let is_auto_begin = |c: &El| { + c.attr("evt") == Some("onBegin") + && c.attr("delay").map(str::trim) == Some("0") + && c.child("tgtEl").is_none() + && c.child("tn").is_none() + && c.child("rtn").is_none() + }; + let has_on_begin = st.children_named("cond").any(is_auto_begin); + if st + .children_named("cond") + .any(|c| (c.attr("evt").is_some() && !is_auto_begin(c)) || (!has_on_begin && c.attr("delay").map(str::trim) != Some("0"))) + { + deferred = true; + } + } + if e.is("video") || e.is("audio") { + out.push((e, deferred)); + return; + } + for c in e.elements() { + collect_media(c, deferred, out, depth + 1); + } } - for node in nodes.into_iter().take(10_000) { + let mut nodes = vec![]; + collect_media(timing, false, &mut nodes, 0); + for (node, deferred) in nodes { let Some(cm) = node.child("cMediaNode") else { continue }; let Some(id) = cm.path(&["tgtEl", "spTgt"]).and_then(|t| t.u32("spid")).and_then(|f| ctx.lookup(f)) else { continue }; let Some(ShapeKind::Media(clip)) = find_shape_mut(shapes, id).map(|s| &mut s.kind) else { continue }; @@ -227,6 +257,10 @@ pub fn media_settings(timing: &El, ctx: &IdCtx, shapes: &mut [Shape]) { if cm.child("cTn").and_then(|c| c.attr("repeatCount")) == Some("indefinite") { clip.loop_play = true; } + if !deferred && cm.path(&["cTn", "stCondLst", "cond"]).is_some_and(|c| c.attr("evt").is_none() && c.attr("delay").map(str::trim) == Some("0")) + { + clip.autoplay = true; + } if node.bool("fullScrn") == Some(true) { clip.full_screen = true; } diff --git a/crates/pptx/src/write.rs b/crates/pptx/src/write.rs index e4c0858c9..888703b97 100644 --- a/crates/pptx/src/write.rs +++ b/crates/pptx/src/write.rs @@ -813,7 +813,20 @@ fn slide_xml(x: &mut Exp, o: &mut Out, s: &Slide, comment_rel: Option<&str>) -> if let Some(t) = &s.transition { timing::transition(&mut w, x, o, t); } - if !s.animations.is_empty() { + if !s.animations.is_empty() + || s.shapes.iter().any(|shape| { + fn has_nondefault(shape: &deckcraft_model::Shape) -> bool { + match &shape.kind { + deckcraft_model::ShapeKind::Media(c) => { + c.autoplay || (c.volume - 1.0).abs() > 1e-6 || c.hide_while_not_playing || c.loop_play || c.full_screen + } + deckcraft_model::ShapeKind::Group { children, .. } => children.iter().any(has_nondefault), + _ => false, + } + } + has_nondefault(shape) + }) + { timing::timing(&mut w, x, &s.animations, &s.shapes, &ids); } slide_ext_list(&mut w, s.raw_ext.as_deref(), comment_rel); diff --git a/crates/pptx/src/write/timing.rs b/crates/pptx/src/write/timing.rs index 6376e3ca0..766e3abad 100644 --- a/crates/pptx/src/write/timing.rs +++ b/crates/pptx/src/write/timing.rs @@ -592,7 +592,7 @@ fn shape_index<'a>(shapes: &'a [Shape], depth: usize, out: &mut std::collections } } -/// Write `p:timing` for a slide's animations. +/// Write `p:timing` for a slide's animations and media settings. pub fn timing(w: &mut W, x: &mut Exp, anims: &[Animation], shapes: &[Shape], idmap: &IdMap) { let mut grp_count: std::collections::HashMap = Default::default(); let mut main: Vec = vec![]; @@ -686,7 +686,8 @@ pub fn timing(w: &mut W, x: &mut Exp, anims: &[Animation], shapes: &[Shape], idm None => main.extend(effs), } } - if main.is_empty() && triggered.is_empty() { + let media = collect_media(shapes, idmap); + if main.is_empty() && triggered.is_empty() && media.is_empty() { return; } let mut ids = Ids(0); @@ -757,6 +758,9 @@ pub fn timing(w: &mut W, x: &mut Exp, anims: &[Animation], shapes: &[Shape], idm w.close("p:nextCondLst"); w.close("p:seq"); } + for (spid, clip) in &media { + write_media_node(w, &mut ids, *spid, clip); + } w.close("p:childTnLst"); w.close("p:cTn"); w.close("p:par"); @@ -770,3 +774,39 @@ pub fn timing(w: &mut W, x: &mut Exp, anims: &[Animation], shapes: &[Shape], idm } w.close("p:timing"); } + +fn collect_media<'a>(shapes: &'a [Shape], idmap: &IdMap) -> Vec<(u32, &'a deckcraft_model::MediaClip)> { + let mut media = Vec::new(); + deckcraft_model::walk(shapes, &mut |shape, _| { + if let ShapeKind::Media(clip) = &shape.kind + && (clip.autoplay || (clip.volume - 1.0).abs() > 1e-6 || clip.hide_while_not_playing || clip.loop_play || clip.full_screen) + && let Some(spid) = idmap.get(shape.id) + { + media.push((spid, clip)); + } + }); + media +} + +fn write_media_node(w: &mut W, ids: &mut Ids, spid: u32, clip: &deckcraft_model::MediaClip) { + let tag = if clip.video { "p:video" } else { "p:audio" }; + w.open(tag, A::new().t("fullScrn", clip.full_screen)); + let mut cm = A::new().a("vol", (clip.volume.clamp(0.0, 1.0) * 100_000.0).round() as u32); + if clip.volume <= 1e-6 { + cm = cm.t("mute", true); + } + if clip.hide_while_not_playing { + cm = cm.a("showWhenStopped", 0); + } + w.open("p:cMediaNode", cm); + w.open("p:cTn", A::new().a("id", ids.next()).a("fill", "hold").a("display", 0).o("repeatCount", clip.loop_play.then_some("indefinite"))); + w.open0("p:stCondLst"); + w.empty("p:cond", A::new().a("delay", if clip.autoplay { "0" } else { "indefinite" })); + w.close("p:stCondLst"); + w.close("p:cTn"); + w.open0("p:tgtEl"); + w.empty("p:spTgt", A::new().a("spid", spid)); + w.close("p:tgtEl"); + w.close("p:cMediaNode"); + w.close(tag); +} diff --git a/crates/pptx/tests/it/media_timing.rs b/crates/pptx/tests/it/media_timing.rs index 8fe325964..ba1fb6174 100644 --- a/crates/pptx/tests/it/media_timing.rs +++ b/crates/pptx/tests/it/media_timing.rs @@ -74,3 +74,27 @@ fn muted_audio_has_no_volume_and_other_clips_keep_theirs() { assert_eq!(c[0].volume, 1.0); assert_eq!((c[1].volume, c[1].loop_play, c[1].full_screen), (0.0, false, false)); } + +#[test] +fn media_autoplay_respects_ancestor_conditions_and_delays() { + let immediate = clips(|v, _| { + format!( + r#""# + ) + }); + assert!(immediate[0].autoplay); + + let delayed_ancestor = clips(|v, _| { + format!( + r#""# + ) + }); + assert!(!delayed_ancestor[0].autoplay); + + let targeted_tn_ancestor = clips(|v, _| { + format!( + r#""# + ) + }); + assert!(!targeted_tn_ancestor[0].autoplay); +} From c6e21c4fb5035399c34aa62f831b5a3418f99f29 Mon Sep 17 00:00:00 2001 From: Neil Cawse Date: Sat, 7 Nov 2026 19:24:17 +0000 Subject: [PATCH 2/6] refactor(pptx): delegate empty-timing check to timing::timing --- crates/pptx/src/write.rs | 17 +---------------- 1 file changed, 1 insertion(+), 16 deletions(-) diff --git a/crates/pptx/src/write.rs b/crates/pptx/src/write.rs index 888703b97..5a0df68f9 100644 --- a/crates/pptx/src/write.rs +++ b/crates/pptx/src/write.rs @@ -813,22 +813,7 @@ fn slide_xml(x: &mut Exp, o: &mut Out, s: &Slide, comment_rel: Option<&str>) -> if let Some(t) = &s.transition { timing::transition(&mut w, x, o, t); } - if !s.animations.is_empty() - || s.shapes.iter().any(|shape| { - fn has_nondefault(shape: &deckcraft_model::Shape) -> bool { - match &shape.kind { - deckcraft_model::ShapeKind::Media(c) => { - c.autoplay || (c.volume - 1.0).abs() > 1e-6 || c.hide_while_not_playing || c.loop_play || c.full_screen - } - deckcraft_model::ShapeKind::Group { children, .. } => children.iter().any(has_nondefault), - _ => false, - } - } - has_nondefault(shape) - }) - { - timing::timing(&mut w, x, &s.animations, &s.shapes, &ids); - } + timing::timing(&mut w, x, &s.animations, &s.shapes, &ids); slide_ext_list(&mut w, s.raw_ext.as_deref(), comment_rel); w.close("p:sld"); w.finish() From 096b70421e7a9e3c73831dd95712590f58c30865 Mon Sep 17 00:00:00 2001 From: Neil Cawse Date: Sun, 8 Nov 2026 05:24:51 +0000 Subject: [PATCH 3/6] test(pptx): cover media-only timing export round-trip and autoplay condition edge cases --- crates/pptx/tests/it/media_timing.rs | 76 ++++++++++++++++++++++++---- 1 file changed, 67 insertions(+), 9 deletions(-) diff --git a/crates/pptx/tests/it/media_timing.rs b/crates/pptx/tests/it/media_timing.rs index ba1fb6174..1d2430370 100644 --- a/crates/pptx/tests/it/media_timing.rs +++ b/crates/pptx/tests/it/media_timing.rs @@ -77,24 +77,82 @@ fn muted_audio_has_no_volume_and_other_clips_keep_theirs() { #[test] fn media_autoplay_respects_ancestor_conditions_and_delays() { - let immediate = clips(|v, _| { + let immediate = clips(|v, a| { format!( - r#""# + r#""# ) }); - assert!(immediate[0].autoplay); + assert!(immediate[0].autoplay, "delay=0 ancestor allows autoplay"); + assert!(immediate[1].autoplay, "untargeted onBegin delay=0 ancestor allows autoplay"); - let delayed_ancestor = clips(|v, _| { + let delayed_and_onclick = clips(|v, a| { format!( - r#""# + r#""# ) }); - assert!(!delayed_ancestor[0].autoplay); + assert!(!delayed_and_onclick[0].autoplay, "delayed ancestor blocks autoplay"); + assert!(!delayed_and_onclick[1].autoplay, "onClick ancestor blocks autoplay"); - let targeted_tn_ancestor = clips(|v, _| { + let targeted_tn_and_own_cond = clips(|v, a| { format!( - r#""# + r#""# ) }); - assert!(!targeted_tn_ancestor[0].autoplay); + assert!(!targeted_tn_and_own_cond[0].autoplay, "targeted onBegin ancestor blocks autoplay"); + assert!(!targeted_tn_and_own_cond[1].autoplay, "media cond with explicit evt is not autoplay"); + + let delayed_media_cond = clips(|v, _| { + format!( + r#""# + ) + }); + assert!(!delayed_media_cond[0].autoplay, "non-zero media cond delay is not autoplay"); +} + +#[test] +fn media_only_slide_exports_and_round_trips_video_and_audio_timing_nodes() { + let mut p = defaults::new_presentation(None); + let video = p.add_media("clip.mp4", "video/mp4", b"video".to_vec()).expect("media"); + let audio = p.add_media("clip.m4a", "audio/mp4", b"audio".to_vec()).expect("media"); + let vid_id = ShapeId(p.alloc_id().expect("id")); + let aud_id = ShapeId(p.alloc_id().expect("id")); + let s0 = Arc::make_mut(&mut p.slides[0]); + s0.shapes.push(deckcraft_model::Shape { + id: vid_id, + name: "AutoplayVideo".into(), + xfrm: Some(Xfrm::new(40.0, 40.0, 200.0, 100.0)), + kind: ShapeKind::Media(MediaClip { + media: video, + video: true, + autoplay: true, + volume: 0.75, + hide_while_not_playing: true, + loop_play: true, + full_screen: true, + ..Default::default() + }), + ..Default::default() + }); + s0.shapes.push(deckcraft_model::Shape { + id: aud_id, + name: "ManualMutedAudio".into(), + xfrm: Some(Xfrm::new(40.0, 160.0, 200.0, 100.0)), + kind: ShapeKind::Media(MediaClip { media: audio, video: false, autoplay: false, volume: 0.0, loop_play: true, ..Default::default() }), + ..Default::default() + }); + let bytes = deckcraft_pptx::export(&p).expect("export"); + let back = deckcraft_pptx::import(&bytes).expect("import"); + let clips: Vec<&MediaClip> = back.slides[0] + .shapes + .iter() + .filter_map(|s| match &s.kind { + ShapeKind::Media(m) => Some(m), + _ => None, + }) + .collect(); + assert_eq!(clips.len(), 2); + assert!(clips[0].video && clips[0].autoplay && clips[0].hide_while_not_playing && clips[0].loop_play && clips[0].full_screen); + assert!((clips[0].volume - 0.75).abs() < 1e-4); + assert!(!clips[1].video && !clips[1].autoplay && clips[1].loop_play && !clips[1].hide_while_not_playing && !clips[1].full_screen); + assert_eq!(clips[1].volume, 0.0); } From 9fb1c5f97499fac11dd42ee04f1cc60bd1e0b774 Mon Sep 17 00:00:00 2001 From: Neil Cawse Date: Sun, 8 Nov 2026 13:50:22 +0000 Subject: [PATCH 4/6] fix(pptx): round-trip media rewind/play_across_slides and detect main-sequence autoplay --- crates/pptx/src/read/timing.rs | 42 +++++++++++++++++++++++++++- crates/pptx/src/write/timing.rs | 20 +++++++++++-- crates/pptx/tests/it/media_timing.rs | 30 ++++++++++++++++++-- docs/file-format-parity.md | 2 +- 4 files changed, 88 insertions(+), 6 deletions(-) diff --git a/crates/pptx/src/read/timing.rs b/crates/pptx/src/read/timing.rs index 37a7ccb53..58d7457db 100644 --- a/crates/pptx/src/read/timing.rs +++ b/crates/pptx/src/read/timing.rs @@ -213,7 +213,6 @@ pub fn media_settings(timing: &El, ctx: &IdCtx, shapes: &mut [Shape]) { } let mut deferred = deferred; if e.is("cTn") - && e.attr("nodeType") != Some("tmRoot") && let Some(st) = e.child("stCondLst") { let is_auto_begin = |c: &El| { @@ -257,6 +256,12 @@ pub fn media_settings(timing: &El, ctx: &IdCtx, shapes: &mut [Shape]) { if cm.child("cTn").and_then(|c| c.attr("repeatCount")) == Some("indefinite") { clip.loop_play = true; } + if cm.child("cTn").and_then(|c| c.attr("fill")) == Some("remove") { + clip.rewind = true; + } + if cm.u32("numSld").is_some_and(|n| n > 1) { + clip.play_across_slides = true; + } if !deferred && cm.path(&["cTn", "stCondLst", "cond"]).is_some_and(|c| c.attr("evt").is_none() && c.attr("delay").map(str::trim) == Some("0")) { clip.autoplay = true; @@ -265,6 +270,41 @@ pub fn media_settings(timing: &El, ctx: &IdCtx, shapes: &mut [Shape]) { clip.full_screen = true; } } + let root_deferred = timing.path(&["tnLst", "par", "cTn", "stCondLst"]).is_some_and(|st| { + let is_auto_begin = |c: &El| { + c.attr("evt") == Some("onBegin") + && c.attr("delay").map(str::trim) == Some("0") + && c.child("tgtEl").is_none() + && c.child("tn").is_none() + && c.child("rtn").is_none() + }; + let has_on_begin = st.children_named("cond").any(is_auto_begin); + st.children_named("cond") + .any(|c| (c.attr("evt").is_some() && !is_auto_begin(c)) || (!has_on_begin && c.attr("delay").map(str::trim) != Some("0"))) + }); + if !root_deferred && let Some(tn) = timing.child("tnLst") { + let mut found = vec![]; + collect(tn, At { trigger: None, group: 0, offset: 0 }, &mut 0, &mut found, 0); + for f in found.into_iter().filter(|f| f.trigger.is_none()) { + let c = f.ctn; + if c.attr("nodeType") == Some("clickEffect") { + break; + } + let class = AnimClass::from_xml(c.attr("presetClass").unwrap_or("entr")); + let preset = c.u32("presetID").unwrap_or(0); + let effect = maps::effect_for_preset(class, preset).unwrap_or_else(|| maps::fallback_effect(class)); + let delay = c.path(&["stCondLst", "cond"]).and_then(|d| ms_attr(d, "delay")).unwrap_or(0); + if class == AnimClass::Media + && effect == "play" + && f.offset == 0 + && delay == 0 + && let Some(id) = c.find("spTgt").and_then(|t| t.u32("spid")).and_then(|s| ctx.lookup(s)) + && let Some(ShapeKind::Media(clip)) = find_shape_mut(shapes, id).map(|s| &mut s.kind) + { + clip.autoplay = true; + } + } + } } /// Flatten the timing tree into the Animation Pane list. diff --git a/crates/pptx/src/write/timing.rs b/crates/pptx/src/write/timing.rs index 766e3abad..4442109af 100644 --- a/crates/pptx/src/write/timing.rs +++ b/crates/pptx/src/write/timing.rs @@ -779,7 +779,13 @@ fn collect_media<'a>(shapes: &'a [Shape], idmap: &IdMap) -> Vec<(u32, &'a deckcr let mut media = Vec::new(); deckcraft_model::walk(shapes, &mut |shape, _| { if let ShapeKind::Media(clip) = &shape.kind - && (clip.autoplay || (clip.volume - 1.0).abs() > 1e-6 || clip.hide_while_not_playing || clip.loop_play || clip.full_screen) + && (clip.autoplay + || (clip.volume - 1.0).abs() > 1e-6 + || clip.hide_while_not_playing + || clip.loop_play + || clip.rewind + || clip.play_across_slides + || clip.full_screen) && let Some(spid) = idmap.get(shape.id) { media.push((spid, clip)); @@ -798,8 +804,18 @@ fn write_media_node(w: &mut W, ids: &mut Ids, spid: u32, clip: &deckcraft_model: if clip.hide_while_not_playing { cm = cm.a("showWhenStopped", 0); } + if clip.play_across_slides { + cm = cm.a("numSld", 999); + } w.open("p:cMediaNode", cm); - w.open("p:cTn", A::new().a("id", ids.next()).a("fill", "hold").a("display", 0).o("repeatCount", clip.loop_play.then_some("indefinite"))); + w.open( + "p:cTn", + A::new() + .a("id", ids.next()) + .a("fill", if clip.rewind { "remove" } else { "hold" }) + .a("display", 0) + .o("repeatCount", clip.loop_play.then_some("indefinite")), + ); w.open0("p:stCondLst"); w.empty("p:cond", A::new().a("delay", if clip.autoplay { "0" } else { "indefinite" })); w.close("p:stCondLst"); diff --git a/crates/pptx/tests/it/media_timing.rs b/crates/pptx/tests/it/media_timing.rs index 1d2430370..1274d8dd5 100644 --- a/crates/pptx/tests/it/media_timing.rs +++ b/crates/pptx/tests/it/media_timing.rs @@ -128,6 +128,8 @@ fn media_only_slide_exports_and_round_trips_video_and_audio_timing_nodes() { volume: 0.75, hide_while_not_playing: true, loop_play: true, + rewind: true, + play_across_slides: true, full_screen: true, ..Default::default() }), @@ -151,8 +153,32 @@ fn media_only_slide_exports_and_round_trips_video_and_audio_timing_nodes() { }) .collect(); assert_eq!(clips.len(), 2); - assert!(clips[0].video && clips[0].autoplay && clips[0].hide_while_not_playing && clips[0].loop_play && clips[0].full_screen); + assert!( + clips[0].video + && clips[0].autoplay + && clips[0].hide_while_not_playing + && clips[0].loop_play + && clips[0].rewind + && clips[0].play_across_slides + && clips[0].full_screen + ); assert!((clips[0].volume - 0.75).abs() < 1e-4); - assert!(!clips[1].video && !clips[1].autoplay && clips[1].loop_play && !clips[1].hide_while_not_playing && !clips[1].full_screen); + assert!( + !clips[1].video + && !clips[1].autoplay + && clips[1].loop_play + && !clips[1].rewind + && !clips[1].play_across_slides + && !clips[1].hide_while_not_playing + && !clips[1].full_screen + ); assert_eq!(clips[1].volume, 0.0); + + // Main-sequence automatic Play (mediacall) effect with indefinite media node also sets autoplay. + let main_seq_play = self::clips(|v, _| { + format!( + r#""# + ) + }); + assert!(main_seq_play[0].autoplay, "main-sequence automatic Play effect sets autoplay"); } diff --git a/docs/file-format-parity.md b/docs/file-format-parity.md index 70b74f7e1..67707cded 100644 --- a/docs/file-format-parity.md +++ b/docs/file-format-parity.md @@ -92,7 +92,7 @@ the round trip even though DeckCraft doesn't model it. | Charts (`cx:` chartex: waterfall, funnel, treemap, sunburst, histogram, Pareto, box) | yes | yes | Unchanged parts kept, with their embedded workbook (#335); edited and new ones written from the model with a plain-chart fallback. Treemap and sunburst hierarchy levels kept; region maps kept, drawn as tiles (no map geometry) | | SmartArt (`dgm`) | the drawing part as a group (text laid out on its `dsp:txXfrm` rectangle), and the outline from the data model (Text Pane) | as a diagram: data model from the outline; layout, quick style and colour parts (the file's own kept until the layout is switched); the group as the cached drawing | DeckCraft's layout definitions are simple; a diagram without a cached drawing isn't drawn. SmartArt kept only as XML, without its relationship parts, still loses the drawing and data parts (#203) | | Equations (OMML, `a14:m`) | no | no | PR #55 open | -| Audio / video (`p14:media`, trim, fades, bookmarks) | yes | partly | Playback options lost on save (#74) | +| Audio / video (`p14:media`, trim, fades, bookmarks, playback options) | yes | yes | Volume, mute, autoplay, loop, rewind, play across slides, hide while not playing and full screen round-trip (`media_timing.rs`) | | Ink (`contentPart`) | yes | as a group of freeform lines | PowerPoint sees shapes, not ink | | Transitions incl. p14/p15 (Vortex, Morph…) | all 48 | all 48 | `every_transition_kind_round_trips` | | Animations: timing tree, build paragraphs, triggers, motion paths | yes (flattened to the Animation Pane list) | yes (rebuilt) | `every_animation_effect_survives`; repeat, triggers, bounce end and rewind kept | From ea85b0412aa111b24fe484e3fe8cd6cb323bb04a Mon Sep 17 00:00:00 2001 From: Neil Cawse Date: Fri, 13 Nov 2026 18:55:05 +0000 Subject: [PATCH 5/6] pptx: preserve deferred ancestor conditions in command-based autoplay inference --- crates/pptx/src/read/timing.rs | 38 ++++++++++++++++++++-------- crates/pptx/tests/it/media_timing.rs | 9 +++++++ 2 files changed, 36 insertions(+), 11 deletions(-) diff --git a/crates/pptx/src/read/timing.rs b/crates/pptx/src/read/timing.rs index 69a1363c4..3106c039b 100644 --- a/crates/pptx/src/read/timing.rs +++ b/crates/pptx/src/read/timing.rs @@ -57,6 +57,8 @@ struct Found<'a> { group: usize, /// Sum of the start delays of the time nodes between the click group and the effect, in ms. offset: u32, + /// Whether any ancestor time node defers start until an event (`onClick`, `onEnd`, targeted `onBegin`) or nonzero delay. + deferred: bool, } /// Where `collect` is in the time tree: the click group and the delay accumulated inside it. @@ -65,6 +67,7 @@ struct At { trigger: Option, group: usize, offset: u32, + deferred: bool, } fn collect<'a>(e: &'a El, at: At, groups: &mut usize, out: &mut Vec>, depth: usize) { @@ -73,7 +76,8 @@ fn collect<'a>(e: &'a El, at: At, groups: &mut usize, out: &mut Vec>, } for c in e.elements() { if c.is("cTn") && c.has_attr("presetClass") { - out.push(Found { ctn: c, trigger: at.trigger, group: at.group, offset: at.offset }); + let deferred = at.deferred || c.child("stCondLst").is_some_and(defers_start); + out.push(Found { ctn: c, trigger: at.trigger, group: at.group, offset: at.offset, deferred }); continue; } let mut at = at; @@ -84,14 +88,26 @@ fn collect<'a>(e: &'a El, at: At, groups: &mut usize, out: &mut Vec>, at.trigger = ctn.path(&["stCondLst", "cond", "tgtEl", "spTgt"]).and_then(|s| s.u32("spid")); } if c.is("cTn") - && let Some(cond) = c.path(&["stCondLst", "cond"]) + && let Some(st) = c.child("stCondLst") { - if cond.attr("delay").map(str::trim) == Some("indefinite") { - *groups += 1; - at.group = *groups; - at.offset = 0; - } else { - at.offset = at.offset.saturating_add(ms_attr(cond, "delay").unwrap_or(0)); + let is_main_seq_auto = st.children_named("cond").any(|d| d.attr("delay").map(str::trim) == Some("indefinite")) + && st.children_named("cond").any(|d| { + d.attr("evt") == Some("onBegin") + && d.attr("delay").map(str::trim) == Some("0") + && d.child("tgtEl").is_none() + && d.child("rtn").is_none() + }); + if !is_main_seq_auto && defers_start(st) { + at.deferred = true; + } + if let Some(cond) = st.child("cond") { + if cond.attr("delay").map(str::trim) == Some("indefinite") { + *groups += 1; + at.group = *groups; + at.offset = 0; + } else { + at.offset = at.offset.saturating_add(ms_attr(cond, "delay").unwrap_or(0)); + } } } collect(c, at, groups, out, depth + 1); @@ -277,10 +293,10 @@ pub fn media_settings(timing: &El, ctx: &IdCtx, shapes: &mut [Shape]) { let root_deferred = timing.path(&["tnLst", "par", "cTn", "stCondLst"]).is_some_and(defers_start); if !root_deferred && let Some(tn) = timing.child("tnLst") { let mut found = vec![]; - collect(tn, At { trigger: None, group: 0, offset: 0 }, &mut 0, &mut found, 0); + collect(tn, At { trigger: None, group: 0, offset: 0, deferred: false }, &mut 0, &mut found, 0); for f in found.into_iter().filter(|f| f.trigger.is_none()) { let c = f.ctn; - if c.attr("nodeType") == Some("clickEffect") { + if c.attr("nodeType") == Some("clickEffect") || f.deferred { break; } let class = AnimClass::from_xml(c.attr("presetClass").unwrap_or("entr")); @@ -305,7 +321,7 @@ pub fn animations(imp: &mut Imp, part: &Part, ctx: &IdCtx, timing: &El, shapes: let _ = (imp, part); let Some(tn) = timing.child("tnLst") else { return vec![] }; let mut found = vec![]; - collect(tn, At { trigger: None, group: 0, offset: 0 }, &mut 0, &mut found, 0); + collect(tn, At { trigger: None, group: 0, offset: 0, deferred: false }, &mut 0, &mut found, 0); // Paragraph builds (`bldP`) by (spid, grpId) -> whether `build="p"`. let bld_p: std::collections::HashMap<(u32, String), bool> = timing .child("bldLst") diff --git a/crates/pptx/tests/it/media_timing.rs b/crates/pptx/tests/it/media_timing.rs index 1274d8dd5..644a64b6f 100644 --- a/crates/pptx/tests/it/media_timing.rs +++ b/crates/pptx/tests/it/media_timing.rs @@ -181,4 +181,13 @@ fn media_only_slide_exports_and_round_trips_video_and_audio_timing_nodes() { ) }); assert!(main_seq_play[0].autoplay, "main-sequence automatic Play effect sets autoplay"); + + // Command-based Play beneath an onClick or onEnd ancestor must not set autoplay. + let deferred_cmd_play = self::clips(|v, a| { + format!( + r#""# + ) + }); + assert!(!deferred_cmd_play[0].autoplay, "command-based Play under onClick ancestor is not autoplay"); + assert!(!deferred_cmd_play[1].autoplay, "command-based Play under onEnd ancestor is not autoplay"); } From fc6a3c4dbd3503ff41af805437f8939ad84ddaa8 Mon Sep 17 00:00:00 2001 From: Neil Cawse Date: Tue, 17 Nov 2026 02:30:00 +0000 Subject: [PATCH 6/6] pptx: validate enclosing mainSeq id on onBegin timing exemption --- crates/pptx/src/read/timing.rs | 67 +++++++++++++++++++--------- crates/pptx/tests/it/media_timing.rs | 9 ++++ 2 files changed, 54 insertions(+), 22 deletions(-) diff --git a/crates/pptx/src/read/timing.rs b/crates/pptx/src/read/timing.rs index 3106c039b..ca29f4592 100644 --- a/crates/pptx/src/read/timing.rs +++ b/crates/pptx/src/read/timing.rs @@ -65,11 +65,38 @@ struct Found<'a> { #[derive(Clone, Copy)] struct At { trigger: Option, + main_seq_id: Option, group: usize, offset: u32, deferred: bool, } +/// Returns whether `st` (`p:stCondLst`) is PowerPoint's automatic main-sequence step wrapper: +/// `` paired only with `` +/// referencing the immediate enclosing `mainSeq` ``. +fn is_main_seq_auto(st: &El, main_seq_id: Option) -> bool { + let Some(ms_id) = main_seq_id else { + return false; + }; + let is_indefinite = |d: &El| { + d.attr("evt").is_none() + && d.attr("delay").map(str::trim) == Some("indefinite") + && d.child("tgtEl").is_none() + && d.child("tn").is_none() + && d.child("rtn").is_none() + }; + let is_main_seq_on_begin = |d: &El| { + d.attr("evt") == Some("onBegin") + && d.attr("delay").map(str::trim) == Some("0") + && d.child("tgtEl").is_none() + && d.child("rtn").is_none() + && d.child("tn").and_then(|tn| tn.u32("val")) == Some(ms_id) + }; + st.children_named("cond").any(is_indefinite) + && st.children_named("cond").any(is_main_seq_on_begin) + && st.children_named("cond").all(|d| is_indefinite(d) || is_main_seq_on_begin(d)) +} + fn collect<'a>(e: &'a El, at: At, groups: &mut usize, out: &mut Vec>, depth: usize) { if depth > 64 || out.len() > 10_000 { return; @@ -86,29 +113,25 @@ fn collect<'a>(e: &'a El, at: At, groups: &mut usize, out: &mut Vec>, && ctn.attr("nodeType") == Some("interactiveSeq") { at.trigger = ctn.path(&["stCondLst", "cond", "tgtEl", "spTgt"]).and_then(|s| s.u32("spid")); + at.main_seq_id = None; } - if c.is("cTn") - && let Some(st) = c.child("stCondLst") - { - let is_main_seq_auto = st.children_named("cond").any(|d| d.attr("delay").map(str::trim) == Some("indefinite")) - && st.children_named("cond").any(|d| { - d.attr("evt") == Some("onBegin") - && d.attr("delay").map(str::trim) == Some("0") - && d.child("tgtEl").is_none() - && d.child("rtn").is_none() - }); - if !is_main_seq_auto && defers_start(st) { - at.deferred = true; - } - if let Some(cond) = st.child("cond") { - if cond.attr("delay").map(str::trim) == Some("indefinite") { - *groups += 1; - at.group = *groups; - at.offset = 0; - } else { - at.offset = at.offset.saturating_add(ms_attr(cond, "delay").unwrap_or(0)); + if c.is("cTn") { + let is_main_seq = c.attr("nodeType") == Some("mainSeq"); + if let Some(st) = c.child("stCondLst") { + if !(!is_main_seq && !at.deferred && is_main_seq_auto(st, at.main_seq_id)) && defers_start(st) { + at.deferred = true; + } + if let Some(cond) = st.child("cond") { + if cond.attr("delay").map(str::trim) == Some("indefinite") { + *groups += 1; + at.group = *groups; + at.offset = 0; + } else { + at.offset = at.offset.saturating_add(ms_attr(cond, "delay").unwrap_or(0)); + } } } + at.main_seq_id = if is_main_seq && !at.deferred { c.u32("id") } else { None }; } collect(c, at, groups, out, depth + 1); } @@ -293,7 +316,7 @@ pub fn media_settings(timing: &El, ctx: &IdCtx, shapes: &mut [Shape]) { let root_deferred = timing.path(&["tnLst", "par", "cTn", "stCondLst"]).is_some_and(defers_start); if !root_deferred && let Some(tn) = timing.child("tnLst") { let mut found = vec![]; - collect(tn, At { trigger: None, group: 0, offset: 0, deferred: false }, &mut 0, &mut found, 0); + collect(tn, At { trigger: None, main_seq_id: None, group: 0, offset: 0, deferred: false }, &mut 0, &mut found, 0); for f in found.into_iter().filter(|f| f.trigger.is_none()) { let c = f.ctn; if c.attr("nodeType") == Some("clickEffect") || f.deferred { @@ -321,7 +344,7 @@ pub fn animations(imp: &mut Imp, part: &Part, ctx: &IdCtx, timing: &El, shapes: let _ = (imp, part); let Some(tn) = timing.child("tnLst") else { return vec![] }; let mut found = vec![]; - collect(tn, At { trigger: None, group: 0, offset: 0, deferred: false }, &mut 0, &mut found, 0); + collect(tn, At { trigger: None, main_seq_id: None, group: 0, offset: 0, deferred: false }, &mut 0, &mut found, 0); // Paragraph builds (`bldP`) by (spid, grpId) -> whether `build="p"`. let bld_p: std::collections::HashMap<(u32, String), bool> = timing .child("bldLst") diff --git a/crates/pptx/tests/it/media_timing.rs b/crates/pptx/tests/it/media_timing.rs index 644a64b6f..43323500b 100644 --- a/crates/pptx/tests/it/media_timing.rs +++ b/crates/pptx/tests/it/media_timing.rs @@ -182,6 +182,15 @@ fn media_only_slide_exports_and_round_trips_video_and_audio_timing_nodes() { }); assert!(main_seq_play[0].autoplay, "main-sequence automatic Play effect sets autoplay"); + // An `indefinite` + `onBegin delay="0"` condition whose `` references a different + // click-dependent time node rather than the enclosing `mainSeq` must stay deferred. + let non_main_seq_tn_play = self::clips(|v, _| { + format!( + r#""# + ) + }); + assert!(!non_main_seq_tn_play[0].autoplay, "onBegin referencing non-mainSeq click-dependent node is not autoplay"); + // Command-based Play beneath an onClick or onEnd ancestor must not set autoplay. let deferred_cmd_play = self::clips(|v, a| { format!(