From ea85b0412aa111b24fe484e3fe8cd6cb323bb04a Mon Sep 17 00:00:00 2001 From: Neil Cawse Date: Fri, 13 Nov 2026 18:55:05 +0000 Subject: [PATCH] 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"); }