Skip to content

The headless browser for AI agents and web scraping

Retain opacity animation paint state

SGavrl740186e88a14
diff --git a/crates/obscura-render/src/css.rs b/crates/obscura-render/src/css.rs
index 52741a37..6e2d6c5c 100644
--- a/crates/obscura-render/src/css.rs
+++ b/crates/obscura-render/src/css.rs
@@ -1605,6 +1605,7 @@ struct DeclarationStreamFlags {
has_color_scheme: bool,
has_animation: bool,
has_transform: bool,
+ has_opacity: bool,
}
/// Cache the declaration features which otherwise require an extra stream
@@ -1633,6 +1634,9 @@ fn declaration_stream_flags(css: &str) -> DeclarationStreamFlags {
if name.eq_ignore_ascii_case("transform") || name.eq_ignore_ascii_case("all") {
flags.has_transform = true;
}
+ if name.eq_ignore_ascii_case("opacity") || name.eq_ignore_ascii_case("all") {
+ flags.has_opacity = true;
+ }
}
flags
}
@@ -4084,6 +4088,7 @@ impl Stylesheet {
.next()
.is_some();
let waapi_underlying_transform_ops = has_waapi.then(|| style.transform_ops.clone());
+ let waapi_underlying_opacity = style.opacity;
sample_waapi_properties(animation_timeline, nid, style, animation_sample);
let important_has_transform = shadow_important_flags.has_transform
@@ -4091,6 +4096,11 @@ impl Stylesheet {
|| important_matched
.iter()
.any(|&(_, _, i)| self.rules[i].important_flags.has_transform);
+ let important_has_opacity = shadow_important_flags.has_opacity
+ || inline_important_flags.has_opacity
+ || important_matched
+ .iter()
+ .any(|&(_, _, i)| self.rules[i].important_flags.has_opacity);
for &(_, _, i) in &important_matched {
let rule = &self.rules[i];
@@ -4107,11 +4117,13 @@ impl Stylesheet {
shadow_important_flags.has_var,
);
crate::style::apply_declarations_with_locked_color_scheme(style, &expanded);
- style.waapi_transform_sample_state =
+ style.waapi_sample_state =
waapi_underlying_transform_ops.map(|underlying_transform_ops| {
- Box::new(crate::WaapiTransformSampleState {
+ Box::new(crate::WaapiSampleState {
underlying_transform_ops,
- fast_path: !important_has_transform,
+ underlying_opacity: waapi_underlying_opacity,
+ transform_fast_path: !important_has_transform,
+ opacity_fast_path: !important_has_opacity,
})
});
effective
@@ -4121,35 +4133,48 @@ impl Stylesheet {
/// Replay transform-only Web Animations from their exact underlying cascade
/// value. Returns the sampled operations and transform containing-block bit;
/// callers apply them only after every target has passed preflight.
-pub(crate) fn resample_transform_only_waapi(
+pub(crate) struct ResampledVisualWaapi {
+ pub transform_ops: Vec<crate::TransformOp>,
+ pub opacity: Option<f32>,
+ pub has_transform_effect: bool,
+ pub has_opacity_effect: bool,
+ pub establishes_transform_cb: bool,
+}
+
+pub(crate) fn resample_visual_waapi(
timeline: &crate::AnimationTimelineState,
node: NodeId,
style: &LayoutStyle,
sample: crate::AnimationSample,
-) -> Option<(Vec<crate::TransformOp>, bool)> {
- let retained = style.waapi_transform_sample_state.as_deref()?;
- if !retained.fast_path {
- return None;
- }
+) -> Option<ResampledVisualWaapi> {
+ let retained = style.waapi_sample_state.as_deref()?;
let animations = timeline
.waapi_for_node(node, sample.time)
.collect::<Vec<_>>();
- if animations.is_empty()
- || animations.iter().any(|(animation, _)| {
- animation
- .keyframes
- .iter()
- .any(|frame| frame.opacity.is_some())
- || !animation
- .keyframes
- .iter()
- .any(|frame| frame.transform.is_some())
- })
+ if animations.is_empty() {
+ return None;
+ }
+ let has_transform_effect = animations.iter().any(|(animation, _)| {
+ animation.keyframes.iter().any(|frame| frame.transform.is_some())
+ });
+ let has_opacity_effect = animations.iter().any(|(animation, _)| {
+ animation.keyframes.iter().any(|frame| frame.opacity.is_some())
+ });
+ let all_effects_supported = animations.iter().all(|(animation, _)| {
+ animation
+ .keyframes
+ .iter()
+ .any(|frame| frame.transform.is_some() || frame.opacity.is_some())
+ });
+ if !all_effects_supported
+ || (has_transform_effect && !retained.transform_fast_path)
+ || (has_opacity_effect && !retained.opacity_fast_path)
{
return None;
}
let mut sampled = style.clone();
sampled.transform_ops = retained.underlying_transform_ops.clone();
+ sampled.opacity = retained.underlying_opacity;
let has_underlying_transform = !sampled.transform_ops.is_empty();
set_animation_containing_block_trigger(
&mut sampled,
@@ -4157,10 +4182,15 @@ pub(crate) fn resample_transform_only_waapi(
has_underlying_transform,
);
sample_waapi_properties(timeline, node, &mut sampled, sample);
- Some((
- sampled.transform_ops,
- sampled.containing_block_triggers & crate::CB_TRIGGER_TRANSFORM != 0,
- ))
+ Some(ResampledVisualWaapi {
+ transform_ops: sampled.transform_ops,
+ opacity: sampled.opacity,
+ has_transform_effect,
+ has_opacity_effect,
+ establishes_transform_cb: sampled.containing_block_triggers
+ & crate::CB_TRIGGER_TRANSFORM
+ != 0,
+ })
}
fn sample_waapi_properties(
@@ -8649,13 +8679,14 @@ mod tests {
let mut timeline = make_timeline();
let retained = apply(0.0, &mut timeline);
for time in [500.0, 750.0] {
- let (resampled, _) = resample_transform_only_waapi(
+ let resampled = resample_visual_waapi(
&timeline,
target,
&retained,
crate::AnimationSample::document(time),
)
- .expect("sparse transform target is eligible");
+ .expect("sparse transform target is eligible")
+ .transform_ops;
let mut fresh_timeline = make_timeline();
let fresh = apply(time, &mut fresh_timeline);
assert_eq!(format!("{resampled:?}"), format!("{:?}", fresh.transform_ops));
@@ -8703,7 +8734,7 @@ mod tests {
crate::AnimationSample::document(0.0),
&mut important_timeline,
);
- assert!(resample_transform_only_waapi(
+ assert!(resample_visual_waapi(
&important_timeline,
important_target,
&important_style,
diff --git a/crates/obscura-render/src/dom.rs b/crates/obscura-render/src/dom.rs
index 19b35616..070d7d9e 100644
--- a/crates/obscura-render/src/dom.rs
+++ b/crates/obscura-render/src/dom.rs
@@ -777,6 +777,33 @@ fn sticky_axis_position(
}
impl DomLayout {
+ /// Refresh the derived paint-culling bit after an opacity-only sample.
+ /// Keep opacity as a binary zero ancestor flag instead of multiplying the
+ /// chain, which avoids underflow making a deep fractional-opacity subtree
+ /// incorrectly disappear.
+ pub(crate) fn refresh_effective_visibility(&mut self, tree: &DomTree) {
+ let mut inherited: HashMap<NodeId, (bool, bool)> = HashMap::new();
+ for id in rendered_descendants(tree, tree.document()) {
+ let parent_state = rendered_parent(tree, id)
+ .and_then(|parent| inherited.get(&parent).copied())
+ .unwrap_or((false, false));
+ let Some(style) = self.styles.get_mut(&id) else {
+ inherited.insert(id, parent_state);
+ continue;
+ };
+ let visibility_hidden = style.visibility_hidden.unwrap_or(parent_state.0);
+ let opacity_zero = parent_state.1 || style.opacity.is_some_and(|value| value <= 0.0);
+ style.effectively_invisible = visibility_hidden || opacity_zero;
+ if let Some(pseudo) = style.before_pseudo.as_deref_mut() {
+ pseudo.effectively_invisible = style.effectively_invisible;
+ }
+ if let Some(pseudo) = style.after_pseudo.as_deref_mut() {
+ pseudo.effectively_invisible = style.effectively_invisible;
+ }
+ inherited.insert(id, (visibility_hidden, opacity_zero));
+ }
+ }
+
/// Rebuild visual geometry after a transform-only animation sample while
/// retaining normal-flow boxes and shaped text. This is deliberately a
/// complete top-down visual walk: ancestor transforms affect descendant
@@ -4302,7 +4329,7 @@ fn layout_dom_once(
text_indent: crate::Dimension,
legacy_center: bool,
visibility_hidden: bool,
- opacity_product: f32,
+ has_zero_opacity: bool,
list_style: crate::ListStyle,
line_height: crate::LineHeight,
white_space: crate::WhiteSpace,
@@ -4354,7 +4381,7 @@ fn layout_dom_once(
text_indent: crate::Dimension::Px(0.0),
legacy_center: false,
visibility_hidden: false,
- opacity_product: 1.0,
+ has_zero_opacity: false,
// CSS initial value of list-style-type.
list_style: crate::ListStyle::Disc,
line_height: crate::LineHeight::Normal,
@@ -4471,7 +4498,7 @@ fn layout_dom_once(
inh.visibility_hidden = style
.visibility_hidden
.unwrap_or(inh.visibility_hidden);
- inh.opacity_product *= style.opacity.unwrap_or(1.0);
+ inh.has_zero_opacity |= style.opacity.is_some_and(|value| value <= 0.0);
if let Some(value) = style.list_style {
inh.list_style = value;
}
@@ -4921,8 +4948,8 @@ fn layout_dom_once(
None => style.text_indent = Some(inh.text_indent),
}
inh.visibility_hidden = style.visibility_hidden.unwrap_or(inh.visibility_hidden);
- inh.opacity_product *= style.opacity.unwrap_or(1.0);
- style.effectively_invisible = inh.visibility_hidden || inh.opacity_product <= 0.0;
+ inh.has_zero_opacity |= style.opacity.is_some_and(|value| value <= 0.0);
+ style.effectively_invisible = inh.visibility_hidden || inh.has_zero_opacity;
match style.list_style {
Some(v) => inh.list_style = v,
None => style.list_style = Some(inh.list_style),
diff --git a/crates/obscura-render/src/lib.rs b/crates/obscura-render/src/lib.rs
index 32fae2c5..f80a7796 100644
--- a/crates/obscura-render/src/lib.rs
+++ b/crates/obscura-render/src/lib.rs
@@ -776,9 +776,11 @@ pub enum GeneratedContentItem {
/// Kept behind one pointer so ordinary, non-animated elements do not pay for
/// another `Vec` in every `LayoutStyle`.
#[derive(Debug, Clone)]
-pub(crate) struct WaapiTransformSampleState {
+pub(crate) struct WaapiSampleState {
pub underlying_transform_ops: Vec<TransformOp>,
- pub fast_path: bool,
+ pub underlying_opacity: Option<f32>,
+ pub transform_fast_path: bool,
+ pub opacity_fast_path: bool,
}
/// The subset of CSS that influences box layout. Expanded in later phases.
@@ -1399,7 +1401,7 @@ pub struct LayoutStyle {
/// A retained compositor-style sample restores this value before replaying
/// the registered effects, so sparse keyframes never compound on the
/// previously sampled transform.
- pub(crate) waapi_transform_sample_state: Option<Box<WaapiTransformSampleState>>,
+ pub(crate) waapi_sample_state: Option<Box<WaapiSampleState>>,
/// Individual CSS `translate` property. This composes independently with
/// the legacy `transform` property, so `transform:none` must not clear it.
/// Functional values are retained separately until the final border box
diff --git a/crates/obscura-render/src/paint.rs b/crates/obscura-render/src/paint.rs
index 820c30e1..b5a58392 100644
--- a/crates/obscura-render/src/paint.rs
+++ b/crates/obscura-render/src/paint.rs
@@ -873,11 +873,11 @@ impl PreparedRender {
self.layout.styles.values().any(css_animation_is_active)
}
- /// Advance a pure transform Web Animation without rebuilding normal-flow
+ /// Advance opacity/transform Web Animations without rebuilding normal-flow
/// layout. Eligibility is intentionally strict: every target is sampled
/// from retained pre-WAAPI cascade provenance, and every update passes
/// preflight before the prepared render is mutated.
- fn try_advance_transform_only_waapi_sample(
+ fn try_advance_visual_waapi_sample(
&mut self,
tree: &DomTree,
sample: crate::AnimationSample,
@@ -891,42 +891,52 @@ impl PreparedRender {
&& (node == tree.document() || tree.ancestors(node).contains(&tree.document()))
};
let mut updates = Vec::new();
+ let mut has_transform_effect = false;
+ let mut has_opacity_effect = false;
for node in timeline.waapi_nodes().into_iter().filter(|node| connected(*node)) {
let Some(style) = self.layout.styles.get(&node) else {
return false;
};
- let Some((transform_ops, establishes_transform_cb)) =
- crate::css::resample_transform_only_waapi(timeline, node, style, sample)
+ let Some(sampled) =
+ crate::css::resample_visual_waapi(timeline, node, style, sample)
else {
return false;
};
let established_transform_cb =
style.containing_block_triggers & crate::CB_TRIGGER_TRANSFORM != 0;
- if establishes_transform_cb != established_transform_cb {
+ if sampled.establishes_transform_cb != established_transform_cb {
return false;
}
- updates.push((node, transform_ops));
+ has_transform_effect |= sampled.has_transform_effect;
+ has_opacity_effect |= sampled.has_opacity_effect;
+ updates.push((node, sampled));
}
if updates.is_empty() {
return false;
}
- for (node, transform_ops) in updates {
- self.layout
+ for (node, sampled) in updates {
+ let style = self.layout
.styles
.get_mut(&node)
- .expect("WAAPI target passed preflight")
- .transform_ops = transform_ops;
- }
- self.layout.refresh_visual_geometry(tree, self.viewport);
- self.content_size = self.layout.scrolling_content_size(tree, self.viewport);
- self.viewport_fixed = self.layout.viewport_fixed_nodes(tree);
- self.sticky = self.layout.root_sticky_layout(tree, self.viewport);
- self.scroll_tree = self.layout.scroll_tree(
- tree,
- self.viewport,
- self.content_size,
- &self.viewport_fixed,
- );
+ .expect("WAAPI target passed preflight");
+ style.transform_ops = sampled.transform_ops;
+ style.opacity = sampled.opacity;
+ }
+ if has_opacity_effect {
+ self.layout.refresh_effective_visibility(tree);
+ }
+ if has_transform_effect {
+ self.layout.refresh_visual_geometry(tree, self.viewport);
+ self.content_size = self.layout.scrolling_content_size(tree, self.viewport);
+ self.viewport_fixed = self.layout.viewport_fixed_nodes(tree);
+ self.sticky = self.layout.root_sticky_layout(tree, self.viewport);
+ self.scroll_tree = self.layout.scroll_tree(
+ tree,
+ self.viewport,
+ self.content_size,
+ &self.viewport_fixed,
+ );
+ }
self.animation_sample = sample;
self.has_active_waapi_animations = timeline.has_active_waapi(sample.time);
self.active_animation_impact = timeline.active_waapi_effect_impact(sample.time);
@@ -2343,7 +2353,7 @@ pub fn prepare_dom_with_retained_styles_with_animation_state(
&& !previous.has_dynamic_fonts
&& dynamic_fonts.is_empty()
&& mutations.is_empty()
- && previous.try_advance_transform_only_waapi_sample(tree, animation_sample, animation_timeline)
+ && previous.try_advance_visual_waapi_sample(tree, animation_sample, animation_timeline)
{
return Some(previous);
}
@@ -15761,7 +15771,7 @@ mod tests {
}
#[test]
- fn transform_only_waapi_refresh_matches_forced_full_geometry_and_pixels() {
+ fn visual_waapi_refresh_matches_forced_full_geometry_and_pixels() {
let tree = parse_html(
r#"<html style="margin:0;background:white"><body style="margin:0">
<div id="moving" style="position:absolute;left:20px;top:20px;width:80px;height:60px;
@@ -15813,6 +15823,32 @@ mod tests {
play_state: crate::WaapiPlayState::Running,
});
}
+ timeline.register_waapi(crate::WaapiAnimation {
+ id: 4,
+ node: moving,
+ keyframes: vec![
+ crate::WaapiKeyframe {
+ offset: 0.0,
+ opacity: Some(0.0),
+ transform: None,
+ },
+ crate::WaapiKeyframe {
+ offset: 1.0,
+ opacity: Some(1.0),
+ transform: None,
+ },
+ ],
+ timing: crate::AnimationTiming {
+ duration_ms: 1_000.0,
+ fill_mode: crate::AnimationFillMode::Both,
+ ..Default::default()
+ },
+ easing: None,
+ linear_easing: None,
+ start_time_ms: 0.0,
+ hold_time_ms: None,
+ play_state: crate::WaapiPlayState::Running,
+ });
timeline
};
let viewport = (240.0, 120.0);
@@ -15832,7 +15868,7 @@ mod tests {
)
.expect("initial candidate");
let initial_transforms = candidate.layout.transforms.clone();
- assert!(candidate.try_advance_transform_only_waapi_sample(
+ assert!(candidate.try_advance_visual_waapi_sample(
&tree,
crate::AnimationSample::document(500.0),
&candidate_timeline,
@@ -15890,7 +15926,7 @@ mod tests {
}
#[test]
- fn transform_only_waapi_refresh_rejects_mixed_effect_atomically() {
+ fn visual_waapi_refresh_rejects_unsupported_effect_atomically() {
let tree = parse_html(
r#"<div id="pure" style="transform:translateX(0px)"></div>
<div id="mixed" style="transform:translateX(0px)"></div>"#,
@@ -15898,14 +15934,17 @@ mod tests {
let pure = tree.get_element_by_id("pure").unwrap();
let mixed = tree.get_element_by_id("mixed").unwrap();
let mut timeline = crate::AnimationTimelineState::default();
- for (id, node, opacity) in [(1, pure, None), (2, mixed, Some(0.5))] {
+ for (id, node, opacity, transform) in [
+ (1, pure, None, Some("translateX(40px)".to_string())),
+ (2, mixed, None, None),
+ ] {
timeline.register_waapi(crate::WaapiAnimation {
id,
node,
keyframes: vec![crate::WaapiKeyframe {
offset: 1.0,
opacity,
- transform: Some("translateX(40px)".into()),
+ transform,
}],
timing: crate::AnimationTiming {
duration_ms: 1_000.0,
@@ -15935,7 +15974,7 @@ mod tests {
.expect("initial render");
let sample_before = prepared.animation_sample;
let pure_before = prepared.layout.styles[&pure].transform_ops.clone();
- assert!(!prepared.try_advance_transform_only_waapi_sample(
+ assert!(!prepared.try_advance_visual_waapi_sample(
&tree,
crate::AnimationSample::document(500.0),
&timeline,
@@ -15948,7 +15987,7 @@ mod tests {
}
#[test]
- fn transform_only_waapi_refresh_rejects_containing_block_topology_change() {
+ fn visual_waapi_refresh_rejects_containing_block_topology_change() {
let tree = parse_html(
r#"<div id="target"><div style="position:fixed;left:10px;top:10px"></div></div>"#,
);
@@ -15992,7 +16031,7 @@ mod tests {
& crate::CB_TRIGGER_TRANSFORM,
0,
);
- assert!(!prepared.try_advance_transform_only_waapi_sample(
+ assert!(!prepared.try_advance_visual_waapi_sample(
&tree,
crate::AnimationSample::document(500.0),
&timeline,