Truly independent web browser
/*
* Copyright (c) 2026-present, the Ladybird developers.
*
* SPDX-License-Identifier: BSD-2-Clause
*/
//! The before-change styles a style stabilization epoch decides CSS transitions against.
use super::boundary::StyleChange;
use super::pipeline::Record;
use super::tree::StyleNodeID;
use super::{RetainedState, ViewLonghands};
use crate::css::animated_overlay::FfiAnimatedOverlayEntry;
use crate::css::style_value::StyleValueData;
use smallvec::SmallVec;
use std::sync::Arc;
/// https://drafts.csswg.org/css-transitions-2/#defining-before-change-style
/// Per transition target, by element and then pseudo-element kind, the before-change style its transitions are decided
/// against for the rest of the style stabilization epoch: the first record named for the target, which the baseline
/// holds until the epoch commits. The host follows every baseline it records for the engine, the commit that releases
/// them, and every identity a transaction releases, as the engine drops a retired node's baselines, so it knows each
/// without asking.
#[derive(Clone, Default)]
pub(crate) struct TransitionBaselines {
/// Shared with a fork until either side writes it.
baselines: crate::cow_map::CowMap<StyleNodeID, SmallVec<[TargetBaseline; 1]>>,
}
/// The baseline of a transition target, by its pseudo-element kind.
type TargetBaseline = (u8, Arc<Record>);
impl TransitionBaselines {
/// The bytes the baselines hold on the heap.
pub(crate) fn heap_size(&self) -> usize {
use crate::heap_size::small_vec_bytes;
self.baselines.heap_size() + self.baselines.values().map(small_vec_bytes).sum::<usize>()
}
/// Keeps `record` as the target's baseline where it has none.
fn insert(&mut self, node: StyleNodeID, pseudo_kind: u8, record: &Arc<Record>) {
if self.get(node, pseudo_kind).is_some() {
return;
}
self.baselines
.entry(node)
.or_default()
.push((pseudo_kind, record.clone()));
}
/// The baseline of the target, or none before a pass has recorded one.
pub(crate) fn get(&self, node: StyleNodeID, pseudo_kind: u8) -> Option<&Arc<Record>> {
self.baselines
.get(&node)
.and_then(|baselines| baselines.iter().find(|(kind, _)| *kind == pseudo_kind))
.map(|(_, record)| record)
}
/// Lets go of the baselines of `node`'s element and pseudo-elements.
pub(crate) fn remove(&mut self, node: StyleNodeID) {
if self.baselines.contains_key(&node) {
self.baselines.remove(&node);
}
}
/// Follows `change`, a write the host queued for the engine, as the engine applies it.
pub(crate) fn follow(&mut self, change: &StyleChange) {
match *change {
StyleChange::RecordTransitionBaseline {
node: Some(node),
pseudo_kind,
style_record: Some(ref record),
} => self.insert(node, pseudo_kind, record),
StyleChange::ReleaseTransitionBaselines if !self.baselines.is_empty() => {
self.baselines = Default::default();
}
_ => {}
}
}
/// Forgets the baselines of the nodes a transaction retired, whose identities it `released`.
pub(crate) fn forget(&mut self, released: &[u32]) {
for node in released.iter().filter_map(|&raw| StyleNodeID::from_raw(raw)) {
self.remove(node);
}
}
}
/// What a transition decides an inherited, animated value against.
#[derive(Clone, Copy)]
pub(crate) enum InheritedAnimatedValue<'a> {
/// An ancestor's animation, other than a transition, sets the value.
Animation(&'a FfiAnimatedOverlayEntry),
/// Ancestors transition the value; this is the base value beneath their transitions.
BeneathTransitions(&'a StyleValueData),
}
impl RetainedState {
/// Style, layout or animation feedback can give a target a transition in any later pass of
/// the epoch, and that transition starts from the style the target held before the epoch's
/// first pass. The first record named for a target is that style: it is kept until the epoch
/// commits.
pub(crate) fn record_transition_baseline(&mut self, node: StyleNodeID, pseudo_kind: u8, record: Arc<Record>) {
self.transition_baselines.insert(node, pseudo_kind, &record);
}
/// The baseline of `node`'s element or pseudo-element of kind `pseudo_kind`, or none before a pass has recorded
/// one.
pub(crate) fn transition_baseline(&self, node: StyleNodeID, pseudo_kind: u8) -> Option<&Arc<Record>> {
self.transition_baselines.get(node, pseudo_kind)
}
/// Whether an ancestor `node` inherits from holds animated values, which is the only way it inherits one.
pub(crate) fn ancestors_hold_animated_values(&self, node: StyleNodeID) -> bool {
let mut ancestor = self.projection.inheritance_parent(node);
while let Some(current) = ancestor {
if let Some(record) = self.computed_group_sets.assigned_style_record(current)
&& self
.computed_group_sets
.style_record_view(record.raw())
.is_some_and(|view| view.animated_overlay.is_some())
{
return true;
}
ancestor = self.projection.inheritance_parent(current);
}
false
}
/// Where an inherited value of `property` in `longhands`, a record of `node`'s, comes from when an
/// ancestor animates it. A record the engine derives holds the animated value its parent passed
/// on in its table, where a transition decides against the value beneath the animations: an
/// animation's value, where an ancestor's animation other than a transition sets it, or else the
/// base value of the nearest ancestor that does not inherit the property, above one that
/// transitions it. None when no ancestor along that chain animates the property.
pub(crate) fn inherited_animated_value(
&self,
node: StyleNodeID,
longhands: ViewLonghands<'_>,
property: u16,
) -> Option<InheritedAnimatedValue<'_>> {
if !longhands.is_inherited(property) {
return None;
}
let mut transition_entry = None;
let mut ancestor = self.projection.inheritance_parent(node);
while let Some(current) = ancestor {
let record = self.computed_group_sets.assigned_style_record(current)?;
let view = self.computed_group_sets.style_record_view(record.raw())?;
let ancestor_longhands = view.longhands();
if let Some(entry) = view.animated_overlay.and_then(|overlay| overlay.get(property)) {
if !entry.result_of_transition {
return Some(InheritedAnimatedValue::Animation(entry));
}
transition_entry = Some(entry);
}
if !ancestor_longhands.is_inherited(property) {
transition_entry?;
return Some(InheritedAnimatedValue::BeneathTransitions(
ancestor_longhands.value(property)?,
));
}
ancestor = self.projection.inheritance_parent(current);
}
None
}
/// The epoch committed: no later pass decides against these styles.
pub(crate) fn release_transition_baselines(&mut self) {
self.transition_baselines.baselines.clear();
}
}