Skip to content

The headless browser for AI agents and web scraping

Retain styles across Web Animation updates

SGavrlb627d4c7985d
diff --git a/crates/obscura-js/src/ops.rs b/crates/obscura-js/src/ops.rs
index 13fed3b0..a262fa99 100644
--- a/crates/obscura-js/src/ops.rs
+++ b/crates/obscura-js/src/ops.rs
@@ -690,6 +690,17 @@ fn queue_retained_style_mutation(
pending: &mut Vec<obscura_render::RetainedStyleMutation>,
mutation: obscura_render::RetainedStyleMutation,
) -> bool {
+ if let obscura_render::RetainedStyleMutation::Animation { node } = &mutation {
+ if pending.iter().any(|queued| {
+ matches!(
+ queued,
+ obscura_render::RetainedStyleMutation::Animation { node: current }
+ if current == node
+ )
+ }) {
+ return true;
+ }
+ }
if let obscura_render::RetainedStyleMutation::Attribute(next) = &mutation {
if next.name.eq_ignore_ascii_case("style")
&& pending.iter().any(|queued| {
@@ -2872,6 +2883,31 @@ mod tests {
assert_eq!(pending.len(), 256);
}
+ #[cfg(feature = "render")]
+ #[test]
+ fn repeated_animation_changes_share_one_retained_dirty_marker_per_node() {
+ let mut pending = Vec::new();
+ let first = obscura_dom::tree::NodeId::new(1);
+ let second = obscura_dom::tree::NodeId::new(2);
+ for _ in 0..300 {
+ assert!(queue_retained_style_mutation(
+ &mut pending,
+ obscura_render::RetainedStyleMutation::Animation { node: first },
+ ));
+ }
+ assert!(queue_retained_style_mutation(
+ &mut pending,
+ obscura_render::RetainedStyleMutation::Animation { node: second },
+ ));
+ assert_eq!(
+ pending,
+ vec![
+ obscura_render::RetainedStyleMutation::Animation { node: first },
+ obscura_render::RetainedStyleMutation::Animation { node: second },
+ ]
+ );
+ }
+
#[cfg(feature = "render")]
#[test]
fn geometry_consumer_defers_paint_only_sample_until_exact_consumer() {
@@ -3826,8 +3862,21 @@ fn waapi_document_time_ms(state: &ObscuraState) -> f32 {
}
#[cfg(feature = "render")]
-fn invalidate_waapi_render(state: &mut ObscuraState) {
- state.prepared_render = None;
+fn invalidate_waapi_render(state: &mut ObscuraState, node: NodeId) {
+ // Adding or controlling one effect changes the animation cascade only for
+ // its target subtree. Keep the previous style graph available to the
+ // retained planner instead of turning every animation setup into a full
+ // document cascade. The bounded mutation queue remains the safety valve
+ // for genuinely broad animation bursts.
+ if state.prepared_render.is_some()
+ && !queue_retained_style_mutation(
+ &mut state.pending_style_mutations,
+ obscura_render::RetainedStyleMutation::Animation { node },
+ )
+ {
+ state.prepared_render = None;
+ state.pending_style_mutations.clear();
+ }
state.resolved_scroll = None;
state.activity_generation = state.activity_generation.wrapping_add(1);
}
@@ -3893,7 +3942,7 @@ fn op_waapi_create(state: &OpState, #[string] input: &str) -> bool {
hold_time_ms: None,
play_state: obscura_render::WaapiPlayState::Running,
});
- invalidate_waapi_render(&mut state);
+ invalidate_waapi_render(&mut state, node);
true
}
@@ -3911,6 +3960,9 @@ fn op_waapi_control(
let shared = state.borrow::<SharedState>().clone();
let mut state = shared.borrow_mut();
let id = id as u64;
+ let Some(node) = state.animation_timeline.waapi_node(id) else {
+ return false;
+ };
let document_time = waapi_document_time_ms(&state);
let changed = match action {
"cancel" => state.animation_timeline.cancel_waapi(id),
@@ -3933,7 +3985,7 @@ fn op_waapi_control(
_ => false,
};
if changed {
- invalidate_waapi_render(&mut state);
+ invalidate_waapi_render(&mut state, node);
}
changed
}
@@ -4089,6 +4141,7 @@ pub(crate) fn sample_live_document_animations(state: &mut ObscuraState) {
}
if sample.time.milliseconds > state.animation_sample.time.milliseconds
&& state.animation_sample.mode == obscura_render::AnimationSampleMode::DocumentTime
+ && state.pending_style_mutations.is_empty()
&& state.prepared_render.as_mut().is_some_and(|prepared| {
prepared.advance_inactive_animation_sample_time(sample.time)
})
diff --git a/crates/obscura-js/src/runtime.rs b/crates/obscura-js/src/runtime.rs
index 01d28fdc..57342a98 100644
--- a/crates/obscura-js/src/runtime.rs
+++ b/crates/obscura-js/src/runtime.rs
@@ -525,6 +525,7 @@ impl ObscuraJsRuntime {
&& state.animation_sample.mode == obscura_render::AnimationSampleMode::DocumentTime
&& sample.time.milliseconds > state.animation_sample.time.milliseconds;
if forward_document_sample
+ && state.pending_style_mutations.is_empty()
&& state.prepared_render.as_mut().is_some_and(|prepared| {
prepared.advance_inactive_animation_sample_time(sample.time)
})
@@ -7943,6 +7944,8 @@ mod tests {
rt.set_viewport(120.0, 40.0);
rt.state.borrow_mut().animation_timeline_origin = std::time::Instant::now();
assert!(rt.set_animation_sample(obscura_render::AnimationSample::document(0.0)));
+ rt.screenshot_prepared((120.0, 40.0), Some("http://example.com/test"))
+ .expect("static frame before WAAPI registration");
rt.execute_script(
"waapi-retained-frame",
r#"document.getElementById('box').animate(
@@ -7952,6 +7955,23 @@ mod tests {
)"#,
)
.unwrap();
+ {
+ let state = rt.state.borrow();
+ let box_node = state
+ .dom
+ .as_ref()
+ .unwrap()
+ .get_element_by_id("box")
+ .unwrap();
+ assert!(
+ state.prepared_render.is_some(),
+ "registering one WAAPI effect must retain the previous style graph"
+ );
+ assert_eq!(
+ state.pending_style_mutations,
+ vec![obscura_render::RetainedStyleMutation::Animation { node: box_node }]
+ );
+ }
let initial = rt
.screenshot_prepared((120.0, 40.0), Some("http://example.com/test"))
.expect("initial WAAPI frame");
@@ -7980,6 +8000,41 @@ mod tests {
assert_ne!(initial, midpoint, "WAAPI paint output must advance");
}
+ #[cfg(feature = "render")]
+ #[test]
+ fn waapi_cancel_retains_static_style_graph_and_restores_authored_style() {
+ let mut rt = setup_runtime(
+ r#"<html><body><div id="box" style="opacity:.25;width:20px;height:20px"></div></body></html>"#,
+ );
+ rt.set_viewport(40.0, 40.0);
+ rt.screenshot_prepared((40.0, 40.0), Some("http://example.com/test"))
+ .expect("static frame");
+ rt.execute_script(
+ "waapi-retained-cancel",
+ r#"globalThis.__cancelAnimation = document.getElementById('box').animate(
+ [{opacity:1}, {opacity:0}], {duration:1000, fill:'both'}
+ )"#,
+ )
+ .unwrap();
+ let animated_opacity = rt
+ .evaluate("Number(getComputedStyle(document.getElementById('box')).opacity)")
+ .unwrap()
+ .as_f64()
+ .unwrap();
+ assert!(animated_opacity > 0.9, "animated opacity={animated_opacity}");
+
+ rt.evaluate("__cancelAnimation.cancel()").unwrap();
+ assert!(
+ rt.state.borrow().prepared_render.is_some(),
+ "canceling one WAAPI effect must retain the previous style graph until recascade"
+ );
+ assert_eq!(
+ rt.evaluate("getComputedStyle(document.getElementById('box')).opacity")
+ .unwrap(),
+ serde_json::json!("0.25")
+ );
+ }
+
#[cfg(feature = "render")]
#[test]
fn completed_animation_retains_forward_frame_but_backward_seek_rebuilds() {
diff --git a/crates/obscura-render/src/lib.rs b/crates/obscura-render/src/lib.rs
index e1a269c7..a71ad7aa 100644
--- a/crates/obscura-render/src/lib.rs
+++ b/crates/obscura-render/src/lib.rs
@@ -1910,6 +1910,13 @@ impl AnimationTimelineState {
self.waapi.insert(animation.id, animation);
}
+ /// Target node for a registered Web Animation. Control operations use
+ /// this before mutating/removing the record so retained style damage can
+ /// stay scoped to the affected element.
+ pub fn waapi_node(&self, id: u64) -> Option<obscura_dom::tree::NodeId> {
+ self.waapi.get(&id).map(|animation| animation.node)
+ }
+
/// Exact set of nodes currently targeted by registered Web Animations.
/// The render preparation boundary additionally filters disconnected
/// targets against the current DOM before producing restyle damage.