Skip to main content

rspack_lightningcss/properties/
animation.rs

1//! CSS properties related to keyframe animations.
2
3use std::borrow::Cow;
4
5use crate::context::PropertyHandlerContext;
6use crate::declaration::{DeclarationBlock, DeclarationList};
7use crate::error::{ParserError, PrinterError};
8use crate::macros::*;
9use crate::prefixes::Feature;
10use crate::properties::{Property, PropertyId, TokenOrValue, VendorPrefix};
11use crate::traits::{Parse, PropertyHandler, Shorthand, ToCss, Zero};
12use crate::values::ident::DashedIdent;
13use crate::values::number::{CSSNumber, NonNegative};
14use crate::values::percentage::Percentage;
15use crate::values::size::Size2D;
16use crate::values::string::CSSString;
17use crate::values::{easing::EasingFunction, ident::CustomIdent, time::Time};
18#[cfg(feature = "visitor")]
19use crate::visitor::Visit;
20use cssparser::*;
21use itertools::izip;
22use smallvec::SmallVec;
23
24use super::{LengthPercentage, LengthPercentageOrAuto};
25
26/// A value for the [animation-name](https://drafts.csswg.org/css-animations/#animation-name) property.
27#[derive(Debug, Clone, PartialEq, Parse)]
28#[cfg_attr(feature = "visitor", derive(Visit))]
29#[cfg_attr(feature = "into_owned", derive(rspack_lightningcss_static_self::IntoOwned))]
30#[cfg_attr(
31  feature = "serde",
32  derive(serde::Serialize, serde::Deserialize),
33  serde(tag = "type", content = "value", rename_all = "kebab-case")
34)]
35#[cfg_attr(feature = "jsonschema", derive(schemars::JsonSchema))]
36pub enum AnimationName<'i> {
37  /// The `none` keyword.
38  None,
39  /// An identifier of a `@keyframes` rule.
40  #[cfg_attr(feature = "serde", serde(borrow))]
41  Ident(CustomIdent<'i>),
42  /// A `<string>` name of a `@keyframes` rule.
43  #[cfg_attr(feature = "serde", serde(borrow))]
44  String(CSSString<'i>),
45}
46
47impl<'i> ToCss for AnimationName<'i> {
48  fn to_css<PrinterT: crate::printer::PrinterTrait>(&self, dest: &mut PrinterT) -> Result<(), PrinterError> {
49    let css_module_animation_enabled = dest
50      .state()
51      .css_module
52      .as_ref()
53      .map_or(false, |css_module| css_module.config.animation);
54    let source_index = dest.state().loc.source_index;
55
56    match self {
57      AnimationName::None => {
58        dest.write_str("none")?;
59        Ok(())
60      }
61      AnimationName::Ident(s) => {
62        if css_module_animation_enabled {
63          if let Some(css_module) = dest.state_mut().css_module.as_mut() {
64            css_module.reference(&s.0, source_index)
65          }
66        }
67        s.to_css_with_options(dest, css_module_animation_enabled)
68      }
69      AnimationName::String(s) => {
70        if css_module_animation_enabled {
71          if let Some(css_module) = dest.state_mut().css_module.as_mut() {
72            css_module.reference(&s, source_index)
73          }
74        }
75
76        // CSS-wide keywords and `none` cannot remove quotes.
77        match_ignore_ascii_case! { &*s,
78          "none" | "initial" | "inherit" | "unset" | "default" | "revert" | "revert-layer" => {
79            serialize_string(&s, dest)?;
80            Ok(())
81          },
82          _ => {
83            dest.write_ident(s.as_ref(), css_module_animation_enabled)
84          }
85        }
86      }
87    }
88  }
89}
90
91/// A list of animation names.
92pub type AnimationNameList<'i> = SmallVec<[AnimationName<'i>; 1]>;
93
94/// A value for the [animation-iteration-count](https://drafts.csswg.org/css-animations/#animation-iteration-count) property.
95#[derive(Debug, Clone, PartialEq, Parse, ToCss)]
96#[cfg_attr(feature = "visitor", derive(Visit))]
97#[cfg_attr(
98  feature = "serde",
99  derive(serde::Serialize, serde::Deserialize),
100  serde(tag = "type", content = "value", rename_all = "kebab-case")
101)]
102#[cfg_attr(feature = "jsonschema", derive(schemars::JsonSchema))]
103#[cfg_attr(feature = "into_owned", derive(rspack_lightningcss_static_self::IntoOwned))]
104pub enum AnimationIterationCount {
105  /// The animation will repeat the specified number of times.
106  Number(NonNegative<CSSNumber>),
107  /// The animation will repeat forever.
108  Infinite,
109}
110
111impl Default for AnimationIterationCount {
112  fn default() -> Self {
113    AnimationIterationCount::Number(NonNegative(1.0))
114  }
115}
116
117enum_property! {
118  /// A value for the [animation-direction](https://drafts.csswg.org/css-animations/#animation-direction) property.
119  pub enum AnimationDirection {
120    /// The animation is played as specified
121    Normal,
122    /// The animation is played in reverse.
123    Reverse,
124    /// The animation iterations alternate between forward and reverse.
125    Alternate,
126    /// The animation iterations alternate between forward and reverse, with reverse occurring first.
127    AlternateReverse,
128  }
129}
130
131impl Default for AnimationDirection {
132  fn default() -> Self {
133    AnimationDirection::Normal
134  }
135}
136
137enum_property! {
138  /// A value for the [animation-play-state](https://drafts.csswg.org/css-animations/#animation-play-state) property.
139  pub enum AnimationPlayState {
140    /// The animation is playing.
141    Running,
142    /// The animation is paused.
143    Paused,
144  }
145}
146
147impl Default for AnimationPlayState {
148  fn default() -> Self {
149    AnimationPlayState::Running
150  }
151}
152
153enum_property! {
154  /// A value for the [animation-fill-mode](https://drafts.csswg.org/css-animations/#animation-fill-mode) property.
155  pub enum AnimationFillMode {
156    /// The animation has no effect while not playing.
157    None,
158    /// After the animation, the ending values are applied.
159    Forwards,
160    /// Before the animation, the starting values are applied.
161    Backwards,
162    /// Both forwards and backwards apply.
163    Both,
164  }
165}
166
167impl Default for AnimationFillMode {
168  fn default() -> Self {
169    AnimationFillMode::None
170  }
171}
172
173enum_property! {
174  /// A value for the [animation-composition](https://drafts.csswg.org/css-animations-2/#animation-composition) property.
175  pub enum AnimationComposition {
176    /// The result of compositing the effect value with the underlying value is simply the effect value.
177    Replace,
178    /// The effect value is added to the underlying value.
179    Add,
180    /// The effect value is accumulated onto the underlying value.
181    Accumulate,
182  }
183}
184
185/// A value for the [animation-timeline](https://drafts.csswg.org/css-animations-2/#animation-timeline) property.
186#[derive(Debug, Clone, PartialEq, Parse, ToCss)]
187#[cfg_attr(feature = "visitor", derive(Visit))]
188#[cfg_attr(
189  feature = "serde",
190  derive(serde::Serialize, serde::Deserialize),
191  serde(tag = "type", content = "value", rename_all = "kebab-case")
192)]
193#[cfg_attr(feature = "jsonschema", derive(schemars::JsonSchema))]
194#[cfg_attr(feature = "into_owned", derive(rspack_lightningcss_static_self::IntoOwned))]
195pub enum AnimationTimeline<'i> {
196  /// The animation’s timeline is a DocumentTimeline, more specifically the default document timeline.
197  Auto,
198  /// The animation is not associated with a timeline.
199  None,
200  /// A timeline referenced by name.
201  #[cfg_attr(feature = "serde", serde(borrow))]
202  DashedIdent(DashedIdent<'i>),
203  /// The scroll() function.
204  Scroll(ScrollTimeline),
205  /// The view() function.
206  View(ViewTimeline),
207}
208
209impl<'i> Default for AnimationTimeline<'i> {
210  fn default() -> Self {
211    AnimationTimeline::Auto
212  }
213}
214
215/// The [scroll()](https://drafts.csswg.org/scroll-animations-1/#scroll-notation) function.
216#[derive(Debug, Clone, PartialEq)]
217#[cfg_attr(feature = "visitor", derive(Visit))]
218#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
219#[cfg_attr(feature = "jsonschema", derive(schemars::JsonSchema))]
220#[cfg_attr(feature = "into_owned", derive(rspack_lightningcss_static_self::IntoOwned))]
221pub struct ScrollTimeline {
222  /// Specifies which element to use as the scroll container.
223  pub scroller: Scroller,
224  /// Specifies which axis of the scroll container to use as the progress for the timeline.
225  pub axis: ScrollAxis,
226}
227
228impl<'i> Parse<'i> for ScrollTimeline {
229  fn parse<'t>(input: &mut Parser<'i, 't>) -> Result<Self, ParseError<'i, ParserError<'i>>> {
230    input.expect_function_matching("scroll")?;
231    input.parse_nested_block(|input| {
232      let mut scroller = None;
233      let mut axis = None;
234      loop {
235        if scroller.is_none() {
236          scroller = input.try_parse(Scroller::parse).ok();
237        }
238
239        if axis.is_none() {
240          axis = input.try_parse(ScrollAxis::parse).ok();
241          if axis.is_some() {
242            continue;
243          }
244        }
245        break;
246      }
247
248      Ok(ScrollTimeline {
249        scroller: scroller.unwrap_or_default(),
250        axis: axis.unwrap_or_default(),
251      })
252    })
253  }
254}
255
256impl ToCss for ScrollTimeline {
257  fn to_css<PrinterT: crate::printer::PrinterTrait>(&self, dest: &mut PrinterT) -> Result<(), PrinterError> {
258    dest.write_str("scroll(")?;
259
260    let mut needs_space = false;
261    if self.scroller != Scroller::default() {
262      self.scroller.to_css(dest)?;
263      needs_space = true;
264    }
265
266    if self.axis != ScrollAxis::default() {
267      if needs_space {
268        dest.write_char(' ')?;
269      }
270      self.axis.to_css(dest)?;
271    }
272
273    dest.write_char(')')?;
274
275    Ok(())
276  }
277}
278
279enum_property! {
280  /// A scroller, used in the `scroll()` function.
281  pub enum Scroller {
282    /// Specifies to use the document viewport as the scroll container.
283    "root": Root,
284    /// Specifies to use the nearest ancestor scroll container.
285    "nearest": Nearest,
286    /// Specifies to use the element’s own principal box as the scroll container.
287    "self": SelfElement,
288  }
289}
290
291impl Default for Scroller {
292  fn default() -> Self {
293    Scroller::Nearest
294  }
295}
296
297enum_property! {
298  /// A scroll axis, used in the `scroll()` function.
299  pub enum ScrollAxis {
300    /// Specifies to use the measure of progress along the block axis of the scroll container.
301    Block,
302    /// Specifies to use the measure of progress along the inline axis of the scroll container.
303    Inline,
304    /// Specifies to use the measure of progress along the horizontal axis of the scroll container.
305    X,
306    /// Specifies to use the measure of progress along the vertical axis of the scroll container.
307    Y,
308  }
309}
310
311impl Default for ScrollAxis {
312  fn default() -> Self {
313    ScrollAxis::Block
314  }
315}
316
317/// The [view()](https://drafts.csswg.org/scroll-animations-1/#view-notation) function.
318#[derive(Debug, Clone, PartialEq)]
319#[cfg_attr(feature = "visitor", derive(Visit))]
320#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
321#[cfg_attr(feature = "jsonschema", derive(schemars::JsonSchema))]
322#[cfg_attr(feature = "into_owned", derive(rspack_lightningcss_static_self::IntoOwned))]
323pub struct ViewTimeline {
324  /// Specifies which axis of the scroll container to use as the progress for the timeline.
325  pub axis: ScrollAxis,
326  /// Provides an adjustment of the view progress visibility range.
327  pub inset: Size2D<LengthPercentageOrAuto>,
328}
329
330impl<'i> Parse<'i> for ViewTimeline {
331  fn parse<'t>(input: &mut Parser<'i, 't>) -> Result<Self, ParseError<'i, ParserError<'i>>> {
332    input.expect_function_matching("view")?;
333    input.parse_nested_block(|input| {
334      let mut axis = None;
335      let mut inset = None;
336      loop {
337        if axis.is_none() {
338          axis = input.try_parse(ScrollAxis::parse).ok();
339        }
340
341        if inset.is_none() {
342          inset = input.try_parse(Size2D::parse).ok();
343          if inset.is_some() {
344            continue;
345          }
346        }
347        break;
348      }
349
350      Ok(ViewTimeline {
351        axis: axis.unwrap_or_default(),
352        inset: inset.unwrap_or(Size2D(LengthPercentageOrAuto::Auto, LengthPercentageOrAuto::Auto)),
353      })
354    })
355  }
356}
357
358impl ToCss for ViewTimeline {
359  fn to_css<PrinterT: crate::printer::PrinterTrait>(&self, dest: &mut PrinterT) -> Result<(), PrinterError> {
360    dest.write_str("view(")?;
361    let mut needs_space = false;
362    if self.axis != ScrollAxis::default() {
363      self.axis.to_css(dest)?;
364      needs_space = true;
365    }
366
367    if self.inset.0 != LengthPercentageOrAuto::Auto || self.inset.1 != LengthPercentageOrAuto::Auto {
368      if needs_space {
369        dest.write_char(' ')?;
370      }
371      self.inset.to_css(dest)?;
372    }
373
374    dest.write_char(')')?;
375
376    Ok(())
377  }
378}
379
380/// A [view progress timeline range](https://drafts.csswg.org/scroll-animations/#view-timelines-ranges)
381#[derive(Debug, Clone, PartialEq, Parse, ToCss)]
382#[cfg_attr(feature = "visitor", derive(Visit))]
383#[cfg_attr(
384  feature = "serde",
385  derive(serde::Serialize, serde::Deserialize),
386  serde(rename_all = "kebab-case")
387)]
388#[cfg_attr(feature = "jsonschema", derive(schemars::JsonSchema))]
389#[cfg_attr(feature = "into_owned", derive(rspack_lightningcss_static_self::IntoOwned))]
390pub enum TimelineRangeName {
391  /// Represents the full range of the view progress timeline.
392  Cover,
393  /// Represents the range during which the principal box is either fully contained by,
394  /// or fully covers, its view progress visibility range within the scrollport.
395  Contain,
396  /// Represents the range during which the principal box is entering the view progress visibility range.
397  Entry,
398  /// Represents the range during which the principal box is exiting the view progress visibility range.
399  Exit,
400  /// Represents the range during which the principal box crosses the end border edge.
401  EntryCrossing,
402  /// Represents the range during which the principal box crosses the start border edge.
403  ExitCrossing,
404}
405
406/// A value for the [animation-range-start](https://drafts.csswg.org/scroll-animations/#animation-range-start)
407/// or [animation-range-end](https://drafts.csswg.org/scroll-animations/#animation-range-end) property.
408#[derive(Debug, Clone, PartialEq)]
409#[cfg_attr(feature = "visitor", derive(Visit))]
410#[cfg_attr(
411  feature = "serde",
412  derive(serde::Serialize, serde::Deserialize),
413  serde(rename_all = "lowercase")
414)]
415#[cfg_attr(feature = "jsonschema", derive(schemars::JsonSchema))]
416#[cfg_attr(feature = "into_owned", derive(rspack_lightningcss_static_self::IntoOwned))]
417pub enum AnimationAttachmentRange {
418  /// The start of the animation’s attachment range is the start of its associated timeline.
419  Normal,
420  /// The animation attachment range starts at the specified point on the timeline measuring from the start of the timeline.
421  #[cfg_attr(feature = "serde", serde(untagged))]
422  LengthPercentage(LengthPercentage),
423  /// The animation attachment range starts at the specified point on the timeline measuring from the start of the specified named timeline range.
424  #[cfg_attr(feature = "serde", serde(untagged))]
425  TimelineRange {
426    /// The name of the timeline range.
427    name: TimelineRangeName,
428    /// The offset from the start of the named timeline range.
429    offset: LengthPercentage,
430  },
431}
432
433impl<'i> AnimationAttachmentRange {
434  fn parse<'t>(input: &mut Parser<'i, 't>, default: f32) -> Result<Self, ParseError<'i, ParserError<'i>>> {
435    if input.try_parse(|input| input.expect_ident_matching("normal")).is_ok() {
436      return Ok(AnimationAttachmentRange::Normal);
437    }
438
439    if let Ok(val) = input.try_parse(LengthPercentage::parse) {
440      return Ok(AnimationAttachmentRange::LengthPercentage(val));
441    }
442
443    let name = TimelineRangeName::parse(input)?;
444    let offset = input
445      .try_parse(LengthPercentage::parse)
446      .unwrap_or(LengthPercentage::Percentage(Percentage(default)));
447    Ok(AnimationAttachmentRange::TimelineRange { name, offset })
448  }
449
450  fn to_css<PrinterT: crate::printer::PrinterTrait>(
451    &self,
452    dest: &mut PrinterT,
453    default: f32,
454  ) -> Result<(), PrinterError> {
455    match self {
456      Self::Normal => {
457        dest.write_str("normal")?;
458        Ok(())
459      }
460      Self::LengthPercentage(val) => val.to_css(dest),
461      Self::TimelineRange { name, offset } => {
462        name.to_css(dest)?;
463        if *offset != LengthPercentage::Percentage(Percentage(default)) {
464          dest.write_char(' ')?;
465          offset.to_css(dest)?;
466        }
467        Ok(())
468      }
469    }
470  }
471}
472
473impl Default for AnimationAttachmentRange {
474  fn default() -> Self {
475    AnimationAttachmentRange::Normal
476  }
477}
478
479/// A value for the [animation-range-start](https://drafts.csswg.org/scroll-animations/#animation-range-start) property.
480#[derive(Debug, Clone, PartialEq)]
481#[cfg_attr(feature = "visitor", derive(Visit))]
482#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
483#[cfg_attr(feature = "jsonschema", derive(schemars::JsonSchema))]
484#[cfg_attr(feature = "into_owned", derive(rspack_lightningcss_static_self::IntoOwned))]
485pub struct AnimationRangeStart(pub AnimationAttachmentRange);
486
487impl<'i> Parse<'i> for AnimationRangeStart {
488  fn parse<'t>(input: &mut Parser<'i, 't>) -> Result<Self, ParseError<'i, ParserError<'i>>> {
489    let range = AnimationAttachmentRange::parse(input, 0.0)?;
490    Ok(Self(range))
491  }
492}
493
494impl ToCss for AnimationRangeStart {
495  fn to_css<PrinterT: crate::printer::PrinterTrait>(&self, dest: &mut PrinterT) -> Result<(), PrinterError> {
496    self.0.to_css(dest, 0.0)
497  }
498}
499
500/// A value for the [animation-range-end](https://drafts.csswg.org/scroll-animations/#animation-range-end) property.
501#[derive(Debug, Clone, PartialEq)]
502#[cfg_attr(feature = "visitor", derive(Visit))]
503#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
504#[cfg_attr(feature = "jsonschema", derive(schemars::JsonSchema))]
505#[cfg_attr(feature = "into_owned", derive(rspack_lightningcss_static_self::IntoOwned))]
506pub struct AnimationRangeEnd(pub AnimationAttachmentRange);
507
508impl<'i> Parse<'i> for AnimationRangeEnd {
509  fn parse<'t>(input: &mut Parser<'i, 't>) -> Result<Self, ParseError<'i, ParserError<'i>>> {
510    let range = AnimationAttachmentRange::parse(input, 1.0)?;
511    Ok(Self(range))
512  }
513}
514
515impl ToCss for AnimationRangeEnd {
516  fn to_css<PrinterT: crate::printer::PrinterTrait>(&self, dest: &mut PrinterT) -> Result<(), PrinterError> {
517    self.0.to_css(dest, 1.0)
518  }
519}
520
521/// A value for the [animation-range](https://drafts.csswg.org/scroll-animations/#animation-range) shorthand property.
522#[derive(Debug, Clone, PartialEq)]
523#[cfg_attr(feature = "visitor", derive(Visit))]
524#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
525#[cfg_attr(feature = "jsonschema", derive(schemars::JsonSchema))]
526#[cfg_attr(feature = "into_owned", derive(rspack_lightningcss_static_self::IntoOwned))]
527pub struct AnimationRange {
528  /// The start of the animation's attachment range.
529  pub start: AnimationRangeStart,
530  /// The end of the animation's attachment range.
531  pub end: AnimationRangeEnd,
532}
533
534impl<'i> Parse<'i> for AnimationRange {
535  fn parse<'t>(input: &mut Parser<'i, 't>) -> Result<Self, ParseError<'i, ParserError<'i>>> {
536    let start = AnimationRangeStart::parse(input)?;
537    let end = input
538      .try_parse(AnimationRangeStart::parse)
539      .map(|r| AnimationRangeEnd(r.0))
540      .unwrap_or_else(|_| {
541        // If <'animation-range-end'> is omitted and <'animation-range-start'> includes a <timeline-range-name> component, then
542        // animation-range-end is set to that same <timeline-range-name> and 100%. Otherwise, any omitted longhand is set to its initial value.
543        match &start.0 {
544          AnimationAttachmentRange::TimelineRange { name, .. } => {
545            AnimationRangeEnd(AnimationAttachmentRange::TimelineRange {
546              name: name.clone(),
547              offset: LengthPercentage::Percentage(Percentage(1.0)),
548            })
549          }
550          _ => AnimationRangeEnd(AnimationAttachmentRange::default()),
551        }
552      });
553    Ok(AnimationRange { start, end })
554  }
555}
556
557impl ToCss for AnimationRange {
558  fn to_css<PrinterT: crate::printer::PrinterTrait>(&self, dest: &mut PrinterT) -> Result<(), PrinterError> {
559    self.start.to_css(dest)?;
560
561    let omit_end = match (&self.start.0, &self.end.0) {
562      (
563        AnimationAttachmentRange::TimelineRange { name: start_name, .. },
564        AnimationAttachmentRange::TimelineRange {
565          name: end_name,
566          offset: end_offset,
567        },
568      ) => start_name == end_name && *end_offset == LengthPercentage::Percentage(Percentage(1.0)),
569      (_, end) => *end == AnimationAttachmentRange::default(),
570    };
571
572    if !omit_end {
573      dest.write_char(' ')?;
574      self.end.to_css(dest)?;
575    }
576    Ok(())
577  }
578}
579
580define_list_shorthand! {
581  /// A value for the [animation](https://drafts.csswg.org/css-animations/#animation) shorthand property.
582  pub struct Animation<'i>(VendorPrefix) {
583    /// The animation name.
584    #[cfg_attr(feature = "serde", serde(borrow))]
585    name: AnimationName(AnimationName<'i>, VendorPrefix),
586    /// The animation duration.
587    duration: AnimationDuration(NonNegative<Time>, VendorPrefix),
588    /// The easing function for the animation.
589    timing_function: AnimationTimingFunction(EasingFunction, VendorPrefix),
590    /// The number of times the animation will run.
591    iteration_count: AnimationIterationCount(AnimationIterationCount, VendorPrefix),
592    /// The direction of the animation.
593    direction: AnimationDirection(AnimationDirection, VendorPrefix),
594    /// The current play state of the animation.
595    play_state: AnimationPlayState(AnimationPlayState, VendorPrefix),
596    /// The animation delay.
597    delay: AnimationDelay(Time, VendorPrefix),
598    /// The animation fill mode.
599    fill_mode: AnimationFillMode(AnimationFillMode, VendorPrefix),
600    /// The animation timeline.
601    timeline: AnimationTimeline(AnimationTimeline<'i>),
602  }
603}
604
605impl<'i> Parse<'i> for Animation<'i> {
606  fn parse<'t>(input: &mut Parser<'i, 't>) -> Result<Self, ParseError<'i, ParserError<'i>>> {
607    let mut name = None;
608    let mut duration = None;
609    let mut timing_function = None;
610    let mut iteration_count = None;
611    let mut direction = None;
612    let mut play_state = None;
613    let mut delay = None;
614    let mut fill_mode = None;
615    let mut timeline = None;
616
617    macro_rules! parse_prop {
618      ($var: ident, $type: ty) => {
619        if $var.is_none() {
620          if let Ok(value) = input.try_parse(<$type>::parse) {
621            $var = Some(value);
622            continue;
623          }
624        }
625      };
626    }
627
628    loop {
629      parse_prop!(duration, NonNegative<Time>);
630      parse_prop!(timing_function, EasingFunction);
631      // The first time must be a duration, even if it is invalid.
632      if duration.is_some() {
633        parse_prop!(delay, Time);
634      }
635      parse_prop!(iteration_count, AnimationIterationCount);
636      parse_prop!(direction, AnimationDirection);
637      parse_prop!(fill_mode, AnimationFillMode);
638      parse_prop!(play_state, AnimationPlayState);
639      parse_prop!(name, AnimationName);
640      parse_prop!(timeline, AnimationTimeline);
641      break;
642    }
643
644    Ok(Animation {
645      name: name.unwrap_or(AnimationName::None),
646      duration: duration.unwrap_or(NonNegative(Time::Seconds(0.0))),
647      timing_function: timing_function.unwrap_or(EasingFunction::Ease),
648      iteration_count: iteration_count.unwrap_or(AnimationIterationCount::Number(NonNegative(1.0))),
649      direction: direction.unwrap_or(AnimationDirection::Normal),
650      play_state: play_state.unwrap_or(AnimationPlayState::Running),
651      delay: delay.unwrap_or(Time::Seconds(0.0)),
652      fill_mode: fill_mode.unwrap_or(AnimationFillMode::None),
653      timeline: timeline.unwrap_or(AnimationTimeline::Auto),
654    })
655  }
656}
657
658impl<'i> ToCss for Animation<'i> {
659  fn to_css<PrinterT: crate::printer::PrinterTrait>(&self, dest: &mut PrinterT) -> Result<(), PrinterError> {
660    match &self.name {
661      AnimationName::None => {}
662      AnimationName::Ident(CustomIdent(name)) | AnimationName::String(CSSString(name)) => {
663        if !self.duration.is_zero() || !self.delay.is_zero() {
664          self.duration.to_css(dest)?;
665          dest.write_char(' ')?;
666        }
667
668        if !self.timing_function.is_ease() || EasingFunction::is_ident(&name) {
669          self.timing_function.to_css(dest)?;
670          dest.write_char(' ')?;
671        }
672
673        if !self.delay.is_zero() {
674          self.delay.to_css(dest)?;
675          dest.write_char(' ')?;
676        }
677
678        if self.iteration_count != AnimationIterationCount::default() || name.as_ref() == "infinite" {
679          self.iteration_count.to_css(dest)?;
680          dest.write_char(' ')?;
681        }
682
683        if self.direction != AnimationDirection::default() || AnimationDirection::parse_string(&name).is_ok() {
684          self.direction.to_css(dest)?;
685          dest.write_char(' ')?;
686        }
687
688        if self.fill_mode != AnimationFillMode::default()
689          || (!name.eq_ignore_ascii_case("none") && AnimationFillMode::parse_string(&name).is_ok())
690        {
691          self.fill_mode.to_css(dest)?;
692          dest.write_char(' ')?;
693        }
694
695        if self.play_state != AnimationPlayState::default() || AnimationPlayState::parse_string(&name).is_ok() {
696          self.play_state.to_css(dest)?;
697          dest.write_char(' ')?;
698        }
699      }
700    }
701
702    // Eventually we could output a string here to avoid duplicating some properties above.
703    // Chrome does not yet support strings, however.
704    self.name.to_css(dest)?;
705
706    if self.name != AnimationName::None && self.timeline != AnimationTimeline::default() {
707      dest.write_char(' ')?;
708      self.timeline.to_css(dest)?;
709    }
710
711    Ok(())
712  }
713}
714
715/// A list of animations.
716pub type AnimationList<'i> = SmallVec<[Animation<'i>; 1]>;
717
718#[derive(Default)]
719pub(crate) struct AnimationHandler<'i> {
720  names: Option<(SmallVec<[AnimationName<'i>; 1]>, VendorPrefix)>,
721  durations: Option<(SmallVec<[NonNegative<Time>; 1]>, VendorPrefix)>,
722  timing_functions: Option<(SmallVec<[EasingFunction; 1]>, VendorPrefix)>,
723  iteration_counts: Option<(SmallVec<[AnimationIterationCount; 1]>, VendorPrefix)>,
724  directions: Option<(SmallVec<[AnimationDirection; 1]>, VendorPrefix)>,
725  play_states: Option<(SmallVec<[AnimationPlayState; 1]>, VendorPrefix)>,
726  delays: Option<(SmallVec<[Time; 1]>, VendorPrefix)>,
727  fill_modes: Option<(SmallVec<[AnimationFillMode; 1]>, VendorPrefix)>,
728  timelines: Option<SmallVec<[AnimationTimeline<'i>; 1]>>,
729  range_starts: Option<SmallVec<[AnimationRangeStart; 1]>>,
730  range_ends: Option<SmallVec<[AnimationRangeEnd; 1]>>,
731  has_any: bool,
732}
733
734impl<'i> PropertyHandler<'i> for AnimationHandler<'i> {
735  fn handle_property(
736    &mut self,
737    property: &Property<'i>,
738    dest: &mut DeclarationList<'i>,
739    context: &mut PropertyHandlerContext<'i, '_>,
740  ) -> bool {
741    macro_rules! maybe_flush {
742      ($prop: ident, $val: expr, $vp: ident) => {{
743        // If two vendor prefixes for the same property have different
744        // values, we need to flush what we have immediately to preserve order.
745        if let Some((val, prefixes)) = &self.$prop {
746          if val != $val && !prefixes.contains(*$vp) {
747            self.flush(dest, context);
748          }
749        }
750      }};
751    }
752
753    macro_rules! property {
754      ($prop: ident, $val: expr, $vp: ident) => {{
755        maybe_flush!($prop, $val, $vp);
756
757        // Otherwise, update the value and add the prefix.
758        if let Some((val, prefixes)) = &mut self.$prop {
759          *val = $val.clone();
760          *prefixes |= *$vp;
761        } else {
762          self.$prop = Some(($val.clone(), *$vp));
763          self.has_any = true;
764        }
765      }};
766    }
767
768    match property {
769      Property::AnimationName(val, vp) => property!(names, val, vp),
770      Property::AnimationDuration(val, vp) => property!(durations, val, vp),
771      Property::AnimationTimingFunction(val, vp) => property!(timing_functions, val, vp),
772      Property::AnimationIterationCount(val, vp) => property!(iteration_counts, val, vp),
773      Property::AnimationDirection(val, vp) => property!(directions, val, vp),
774      Property::AnimationPlayState(val, vp) => property!(play_states, val, vp),
775      Property::AnimationDelay(val, vp) => property!(delays, val, vp),
776      Property::AnimationFillMode(val, vp) => property!(fill_modes, val, vp),
777      Property::AnimationTimeline(val) => {
778        self.timelines = Some(val.clone());
779        self.has_any = true;
780      }
781      Property::AnimationRangeStart(val) => {
782        self.range_starts = Some(val.clone());
783        self.has_any = true;
784      }
785      Property::AnimationRangeEnd(val) => {
786        self.range_ends = Some(val.clone());
787        self.has_any = true;
788      }
789      Property::AnimationRange(val) => {
790        self.range_starts = Some(val.iter().map(|v| v.start.clone()).collect());
791        self.range_ends = Some(val.iter().map(|v| v.end.clone()).collect());
792        self.has_any = true;
793      }
794      Property::Animation(val, vp) => {
795        let names = val.iter().map(|b| b.name.clone()).collect();
796        maybe_flush!(names, &names, vp);
797
798        let durations = val.iter().map(|b| b.duration.clone()).collect();
799        maybe_flush!(durations, &durations, vp);
800
801        let timing_functions = val.iter().map(|b| b.timing_function.clone()).collect();
802        maybe_flush!(timing_functions, &timing_functions, vp);
803
804        let iteration_counts = val.iter().map(|b| b.iteration_count.clone()).collect();
805        maybe_flush!(iteration_counts, &iteration_counts, vp);
806
807        let directions = val.iter().map(|b| b.direction.clone()).collect();
808        maybe_flush!(directions, &directions, vp);
809
810        let play_states = val.iter().map(|b| b.play_state.clone()).collect();
811        maybe_flush!(play_states, &play_states, vp);
812
813        let delays = val.iter().map(|b| b.delay.clone()).collect();
814        maybe_flush!(delays, &delays, vp);
815
816        let fill_modes = val.iter().map(|b| b.fill_mode.clone()).collect();
817        maybe_flush!(fill_modes, &fill_modes, vp);
818
819        self.timelines = Some(val.iter().map(|b| b.timeline.clone()).collect());
820
821        property!(names, &names, vp);
822        property!(durations, &durations, vp);
823        property!(timing_functions, &timing_functions, vp);
824        property!(iteration_counts, &iteration_counts, vp);
825        property!(directions, &directions, vp);
826        property!(play_states, &play_states, vp);
827        property!(delays, &delays, vp);
828        property!(fill_modes, &fill_modes, vp);
829
830        // The animation shorthand resets animation-range
831        // https://drafts.csswg.org/scroll-animations/#named-range-animation-declaration
832        self.range_starts = None;
833        self.range_ends = None;
834      }
835      Property::Unparsed(val) if is_animation_property(&val.property_id) => {
836        let mut val = Cow::Borrowed(val);
837        if matches!(val.property_id, PropertyId::Animation(_)) {
838          use crate::properties::custom::Token;
839
840          // Find an identifier that isn't a keyword and replace it with an
841          // AnimationName token so it is scoped in CSS modules.
842          for token in &mut val.to_mut().value.0 {
843            match token {
844              TokenOrValue::Token(Token::Ident(id)) => {
845                if AnimationDirection::parse_string(&id).is_err()
846                  && AnimationPlayState::parse_string(&id).is_err()
847                  && AnimationFillMode::parse_string(&id).is_err()
848                  && !EasingFunction::is_ident(&id)
849                  && id.as_ref() != "infinite"
850                  && id.as_ref() != "auto"
851                {
852                  *token = TokenOrValue::AnimationName(AnimationName::Ident(CustomIdent(id.clone())));
853                }
854              }
855              TokenOrValue::Token(Token::String(s)) => {
856                *token = TokenOrValue::AnimationName(AnimationName::String(CSSString(s.clone())));
857              }
858              _ => {}
859            }
860          }
861
862          self.range_starts = None;
863          self.range_ends = None;
864        }
865
866        self.flush(dest, context);
867        dest.push(Property::Unparsed(
868          val.get_prefixed(context.targets, Feature::Animation),
869        ));
870      }
871      _ => return false,
872    }
873
874    true
875  }
876
877  fn finalize(&mut self, dest: &mut DeclarationList<'i>, context: &mut PropertyHandlerContext<'i, '_>) {
878    self.flush(dest, context);
879  }
880}
881
882impl<'i> AnimationHandler<'i> {
883  fn flush(&mut self, dest: &mut DeclarationList<'i>, context: &mut PropertyHandlerContext<'i, '_>) {
884    if !self.has_any {
885      return;
886    }
887
888    self.has_any = false;
889
890    let mut names = std::mem::take(&mut self.names);
891    let mut durations = std::mem::take(&mut self.durations);
892    let mut timing_functions = std::mem::take(&mut self.timing_functions);
893    let mut iteration_counts = std::mem::take(&mut self.iteration_counts);
894    let mut directions = std::mem::take(&mut self.directions);
895    let mut play_states = std::mem::take(&mut self.play_states);
896    let mut delays = std::mem::take(&mut self.delays);
897    let mut fill_modes = std::mem::take(&mut self.fill_modes);
898    let mut timelines_value = std::mem::take(&mut self.timelines);
899    let range_starts = std::mem::take(&mut self.range_starts);
900    let range_ends = std::mem::take(&mut self.range_ends);
901
902    if let (
903      Some((names, names_vp)),
904      Some((durations, durations_vp)),
905      Some((timing_functions, timing_functions_vp)),
906      Some((iteration_counts, iteration_counts_vp)),
907      Some((directions, directions_vp)),
908      Some((play_states, play_states_vp)),
909      Some((delays, delays_vp)),
910      Some((fill_modes, fill_modes_vp)),
911    ) = (
912      &mut names,
913      &mut durations,
914      &mut timing_functions,
915      &mut iteration_counts,
916      &mut directions,
917      &mut play_states,
918      &mut delays,
919      &mut fill_modes,
920    ) {
921      // Only use shorthand syntax if the number of animations matches on all properties.
922      let len = names.len();
923      let intersection = *names_vp
924        & *durations_vp
925        & *timing_functions_vp
926        & *iteration_counts_vp
927        & *directions_vp
928        & *play_states_vp
929        & *delays_vp
930        & *fill_modes_vp;
931      let mut timelines = if let Some(timelines) = &mut timelines_value {
932        Cow::Borrowed(timelines)
933      } else if !intersection.contains(VendorPrefix::None) {
934        // Prefixed animation shorthand does not support animation-timeline
935        Cow::Owned(std::iter::repeat(AnimationTimeline::Auto).take(len).collect())
936      } else {
937        Cow::Owned(SmallVec::new())
938      };
939
940      if !intersection.is_empty()
941        && durations.len() == len
942        && timing_functions.len() == len
943        && iteration_counts.len() == len
944        && directions.len() == len
945        && play_states.len() == len
946        && delays.len() == len
947        && fill_modes.len() == len
948        && timelines.len() == len
949      {
950        let timeline_property = if timelines.iter().any(|t| *t != AnimationTimeline::Auto)
951          && (intersection != VendorPrefix::None
952            || !context
953              .targets
954              .is_compatible(crate::compat::Feature::AnimationTimelineShorthand))
955        {
956          Some(Property::AnimationTimeline(timelines.clone().into_owned()))
957        } else {
958          None
959        };
960
961        let animations = izip!(
962          names.drain(..),
963          durations.drain(..),
964          timing_functions.drain(..),
965          iteration_counts.drain(..),
966          directions.drain(..),
967          play_states.drain(..),
968          delays.drain(..),
969          fill_modes.drain(..),
970          timelines.to_mut().drain(..)
971        )
972        .map(
973          |(
974            name,
975            duration,
976            timing_function,
977            iteration_count,
978            direction,
979            play_state,
980            delay,
981            fill_mode,
982            timeline,
983          )| {
984            Animation {
985              name,
986              duration,
987              timing_function,
988              iteration_count,
989              direction,
990              play_state,
991              delay,
992              fill_mode,
993              timeline: if timeline_property.is_some() {
994                AnimationTimeline::Auto
995              } else {
996                timeline
997              },
998            }
999          },
1000        )
1001        .collect();
1002        let prefix = context.targets.prefixes(intersection, Feature::Animation);
1003        dest.push(Property::Animation(animations, prefix));
1004        names_vp.remove(intersection);
1005        durations_vp.remove(intersection);
1006        timing_functions_vp.remove(intersection);
1007        iteration_counts_vp.remove(intersection);
1008        directions_vp.remove(intersection);
1009        play_states_vp.remove(intersection);
1010        delays_vp.remove(intersection);
1011        fill_modes_vp.remove(intersection);
1012
1013        if let Some(p) = timeline_property {
1014          dest.push(p);
1015        }
1016        timelines_value = None;
1017      }
1018    }
1019
1020    macro_rules! prop {
1021      ($var: ident, $property: ident) => {
1022        if let Some((val, vp)) = $var {
1023          if !vp.is_empty() {
1024            let prefix = context.targets.prefixes(vp, Feature::$property);
1025            dest.push(Property::$property(val, prefix))
1026          }
1027        }
1028      };
1029    }
1030
1031    prop!(names, AnimationName);
1032    prop!(durations, AnimationDuration);
1033    prop!(timing_functions, AnimationTimingFunction);
1034    prop!(iteration_counts, AnimationIterationCount);
1035    prop!(directions, AnimationDirection);
1036    prop!(play_states, AnimationPlayState);
1037    prop!(delays, AnimationDelay);
1038    prop!(fill_modes, AnimationFillMode);
1039
1040    if let Some(val) = timelines_value {
1041      dest.push(Property::AnimationTimeline(val));
1042    }
1043
1044    match (range_starts, range_ends) {
1045      (Some(range_starts), Some(range_ends)) => {
1046        if range_starts.len() == range_ends.len() {
1047          dest.push(Property::AnimationRange(
1048            range_starts
1049              .into_iter()
1050              .zip(range_ends.into_iter())
1051              .map(|(start, end)| AnimationRange { start, end })
1052              .collect(),
1053          ));
1054        } else {
1055          dest.push(Property::AnimationRangeStart(range_starts));
1056          dest.push(Property::AnimationRangeEnd(range_ends));
1057        }
1058      }
1059      (range_starts, range_ends) => {
1060        if let Some(range_starts) = range_starts {
1061          dest.push(Property::AnimationRangeStart(range_starts));
1062        }
1063
1064        if let Some(range_ends) = range_ends {
1065          dest.push(Property::AnimationRangeEnd(range_ends));
1066        }
1067      }
1068    }
1069  }
1070}
1071
1072#[inline]
1073fn is_animation_property(property_id: &PropertyId) -> bool {
1074  match property_id {
1075    PropertyId::AnimationName(_)
1076    | PropertyId::AnimationDuration(_)
1077    | PropertyId::AnimationTimingFunction(_)
1078    | PropertyId::AnimationIterationCount(_)
1079    | PropertyId::AnimationDirection(_)
1080    | PropertyId::AnimationPlayState(_)
1081    | PropertyId::AnimationDelay(_)
1082    | PropertyId::AnimationFillMode(_)
1083    | PropertyId::AnimationComposition
1084    | PropertyId::AnimationTimeline
1085    | PropertyId::AnimationRange
1086    | PropertyId::AnimationRangeStart
1087    | PropertyId::AnimationRangeEnd
1088    | PropertyId::Animation(_) => true,
1089    _ => false,
1090  }
1091}