lewp_css/domain/at_rules/keyframes/keyframes_at_rule.rs
1// This file is part of css. It is subject to the license terms in the COPYRIGHT file found in the top-level directory of this distribution and at https://raw.githubusercontent.com/lemonrock/css/master/COPYRIGHT. No part of predicator, including this file, may be copied, modified, propagated, or distributed except according to the terms contained in the COPYRIGHT file.
2// Copyright © 2017 The developers of css. See the COPYRIGHT file in the top-level directory of this distribution and at https://raw.githubusercontent.com/lemonrock/css/master/COPYRIGHT.
3
4use {
5 super::{Keyframe, KeyframeSelector, KeyframesName},
6 crate::domain::{
7 at_rules::VendorPrefixedAtRule,
8 HasVendorPrefix,
9 VendorPrefix,
10 },
11 cssparser::ToCss,
12 std::fmt,
13};
14
15/// A [`@keyframes`](crate::domain::CssRule::Keyframes) rule.
16/// Keyframes: <https://drafts.csswg.org/css-animations/#keyframes>
17#[derive(Debug, Clone)]
18pub struct KeyframesAtRule {
19 /// Vendor prefix type the @keyframes has.
20 pub vendor_prefix: Option<VendorPrefix>,
21
22 /// The name of the current animation.
23 pub name: KeyframesName,
24
25 /// The keyframes specified for this CSS rule.
26 pub keyframes: Vec<Keyframe>,
27}
28
29impl ToCss for KeyframesAtRule {
30 fn to_css<W: fmt::Write>(&self, dest: &mut W) -> fmt::Result {
31 dest.write_str("@")?;
32 if let Some(ref vendor_prefix) = self.vendor_prefix {
33 vendor_prefix.to_css(dest)?;
34 }
35 dest.write_str("keyframes ")?;
36 self.name.to_css(dest)?;
37 dest.write_char('{')?;
38 for keyframe in self.keyframes.iter() {
39 keyframe.to_css(dest)?;
40 }
41 dest.write_char('}')
42 }
43}
44
45impl HasVendorPrefix for KeyframesAtRule {
46 #[inline(always)]
47 fn isNotVendorPrefixed(&self) -> bool {
48 self.vendor_prefix.is_none()
49 }
50}
51
52impl VendorPrefixedAtRule for KeyframesAtRule {}
53
54impl KeyframesAtRule {
55 /// Returns the index of the last keyframe that matches the given selector.
56 /// If the selector is not valid, or no keyframe is found, returns None.
57 ///
58 /// Related spec: <https://drafts.csswg.org/css-animations-1/#interface-csskeyframesrule-findrule>
59 pub fn find_rule(&self, selector: KeyframeSelector) -> Option<usize> {
60 for (index, keyframe) in self.keyframes.iter().enumerate().rev() {
61 if keyframe.selector == selector {
62 return Some(index);
63 }
64 }
65 None
66 }
67}