Skip to main content

style/values/
mod.rs

1/* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at https://mozilla.org/MPL/2.0/. */
4
5//! Common [values][values] used in CSS.
6//!
7//! [values]: https://drafts.csswg.org/css-values/
8
9#![deny(missing_docs)]
10
11use crate::derives::*;
12use crate::parser::{Parse, ParserContext};
13use crate::typed_om::{KeywordValue, MathSum, NumericValue, ToTyped, TypedValue, UnitValue};
14use crate::values::distance::{ComputeSquaredDistance, SquaredDistance};
15use crate::values::generics::position::IsTreeScoped;
16use crate::Atom;
17pub use cssparser::{serialize_identifier, serialize_name, CowRcStr, Parser};
18pub use cssparser::{SourceLocation, Token};
19use precomputed_hash::PrecomputedHash;
20use selectors::parser::SelectorParseErrorKind;
21use std::fmt::{self, Debug, Write};
22use style_traits::{CssString, CssWriter, ParseError, StyleParseErrorKind, ToCss};
23use thin_vec::ThinVec;
24use to_shmem::impl_trivial_to_shmem;
25
26pub use crate::url::CssUrl;
27
28pub mod animated;
29pub mod computed;
30pub mod distance;
31pub mod generics;
32pub mod resolved;
33pub mod specified;
34pub mod tagged_numeric;
35
36/// A CSS float value.
37pub type CSSFloat = f32;
38
39/// Normalizes a float value to zero after a set of operations that might turn
40/// it into NaN.
41#[inline]
42pub fn normalize(v: CSSFloat) -> CSSFloat {
43    if v.is_nan() {
44        0.0
45    } else {
46        v
47    }
48}
49
50/// A CSS integer value.
51pub type CSSInteger = i32;
52
53/// Serialize an identifier which is represented as an atom.
54#[cfg(feature = "gecko")]
55pub fn serialize_atom_identifier<W>(ident: &Atom, dest: &mut W) -> fmt::Result
56where
57    W: Write,
58{
59    ident.with_str(|s| serialize_identifier(s, dest))
60}
61
62/// Serialize an identifier which is represented as an atom.
63#[cfg(feature = "servo")]
64pub fn serialize_atom_identifier<Static, W>(
65    ident: &::string_cache::Atom<Static>,
66    dest: &mut W,
67) -> fmt::Result
68where
69    Static: string_cache::StaticAtomSet,
70    W: Write,
71{
72    serialize_identifier(&ident, dest)
73}
74
75/// Serialize a name which is represented as an Atom.
76#[cfg(feature = "gecko")]
77pub fn serialize_atom_name<W>(ident: &Atom, dest: &mut W) -> fmt::Result
78where
79    W: Write,
80{
81    ident.with_str(|s| serialize_name(s, dest))
82}
83
84/// Serialize a name which is represented as an Atom.
85#[cfg(feature = "servo")]
86pub fn serialize_atom_name<Static, W>(
87    ident: &::string_cache::Atom<Static>,
88    dest: &mut W,
89) -> fmt::Result
90where
91    Static: string_cache::StaticAtomSet,
92    W: Write,
93{
94    serialize_name(&ident, dest)
95}
96
97/// Serialize a number with calc, and NaN/infinity handling (if enabled)
98pub fn serialize_number<W>(v: f32, dest: &mut CssWriter<W>) -> fmt::Result
99where
100    W: Write,
101{
102    serialize_specified_dimension(v, "", /* was_calc = */ false, dest)
103}
104
105/// Serialize a specified dimension with unit, calc, and NaN/infinity handling (if enabled)
106pub fn serialize_specified_dimension<W>(
107    v: f32,
108    unit: &str,
109    was_calc: bool,
110    dest: &mut CssWriter<W>,
111) -> fmt::Result
112where
113    W: Write,
114{
115    if was_calc {
116        dest.write_str("calc(")?;
117    }
118
119    if !v.is_finite() {
120        // https://drafts.csswg.org/css-values/#calc-error-constants:
121        // "While not technically numbers, these keywords act as numeric values,
122        // similar to e and pi. Thus to get an infinite length, for example,
123        // requires an expression like calc(infinity * 1px)."
124
125        if v.is_nan() {
126            dest.write_str("NaN")?;
127        } else if v == f32::INFINITY {
128            dest.write_str("infinity")?;
129        } else if v == f32::NEG_INFINITY {
130            dest.write_str("-infinity")?;
131        }
132
133        if !unit.is_empty() {
134            dest.write_str(" * 1")?;
135        }
136    } else {
137        v.to_css(dest)?;
138    }
139
140    dest.write_str(unit)?;
141
142    if was_calc {
143        dest.write_char(')')?;
144    }
145    Ok(())
146}
147
148/// A CSS string stored as an `Atom`.
149#[repr(transparent)]
150#[derive(
151    Clone,
152    Debug,
153    Default,
154    Deref,
155    Eq,
156    Hash,
157    MallocSizeOf,
158    PartialEq,
159    SpecifiedValueInfo,
160    ToComputedValue,
161    ToResolvedValue,
162    ToShmem,
163)]
164pub struct AtomString(pub Atom);
165
166#[cfg(feature = "servo")]
167impl AsRef<str> for AtomString {
168    fn as_ref(&self) -> &str {
169        &*self.0
170    }
171}
172
173impl Parse for AtomString {
174    fn parse<'i>(_: &ParserContext, input: &mut Parser<'i, '_>) -> Result<Self, ParseError<'i>> {
175        Ok(Self(Atom::from(input.expect_string()?.as_ref())))
176    }
177}
178
179impl cssparser::ToCss for AtomString {
180    fn to_css<W>(&self, dest: &mut W) -> fmt::Result
181    where
182        W: Write,
183    {
184        // Wrap in quotes to form a string literal
185        dest.write_char('"')?;
186        #[cfg(feature = "servo")]
187        {
188            cssparser::CssStringWriter::new(dest).write_str(self.as_ref())?;
189        }
190        #[cfg(feature = "gecko")]
191        {
192            self.0
193                .with_str(|s| cssparser::CssStringWriter::new(dest).write_str(s))?;
194        }
195        dest.write_char('"')
196    }
197}
198
199impl style_traits::ToCss for AtomString {
200    fn to_css<W>(&self, dest: &mut CssWriter<W>) -> fmt::Result
201    where
202        W: Write,
203    {
204        cssparser::ToCss::to_css(self, dest)
205    }
206}
207
208impl PrecomputedHash for AtomString {
209    #[inline]
210    fn precomputed_hash(&self) -> u32 {
211        self.0.precomputed_hash()
212    }
213}
214
215impl<'a> From<&'a str> for AtomString {
216    #[inline]
217    fn from(string: &str) -> Self {
218        Self(Atom::from(string))
219    }
220}
221
222/// A generic CSS `<ident>` stored as an `Atom`.
223#[cfg(feature = "servo")]
224#[repr(transparent)]
225#[derive(Deref)]
226pub struct GenericAtomIdent<Set>(pub string_cache::Atom<Set>)
227where
228    Set: string_cache::StaticAtomSet;
229
230/// A generic CSS `<ident>` stored as an `Atom`, for the default atom set.
231#[cfg(feature = "servo")]
232pub type AtomIdent = GenericAtomIdent<stylo_atoms::AtomStaticSet>;
233
234#[cfg(feature = "servo")]
235impl<Set: string_cache::StaticAtomSet> style_traits::SpecifiedValueInfo for GenericAtomIdent<Set> {}
236
237#[cfg(feature = "servo")]
238impl<Set: string_cache::StaticAtomSet> Default for GenericAtomIdent<Set> {
239    fn default() -> Self {
240        Self(string_cache::Atom::default())
241    }
242}
243
244#[cfg(feature = "servo")]
245impl<Set: string_cache::StaticAtomSet> std::fmt::Debug for GenericAtomIdent<Set> {
246    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
247        self.0.fmt(f)
248    }
249}
250
251#[cfg(feature = "servo")]
252impl<Set: string_cache::StaticAtomSet> std::hash::Hash for GenericAtomIdent<Set> {
253    fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
254        self.0.hash(state)
255    }
256}
257
258#[cfg(feature = "servo")]
259impl<Set: string_cache::StaticAtomSet> Eq for GenericAtomIdent<Set> {}
260
261#[cfg(feature = "servo")]
262impl<Set: string_cache::StaticAtomSet> PartialEq for GenericAtomIdent<Set> {
263    fn eq(&self, other: &Self) -> bool {
264        self.0 == other.0
265    }
266}
267
268#[cfg(feature = "servo")]
269impl<Set: string_cache::StaticAtomSet> Clone for GenericAtomIdent<Set> {
270    fn clone(&self) -> Self {
271        Self(self.0.clone())
272    }
273}
274
275#[cfg(feature = "servo")]
276impl<Set: string_cache::StaticAtomSet> to_shmem::ToShmem for GenericAtomIdent<Set> {
277    fn to_shmem(&self, builder: &mut to_shmem::SharedMemoryBuilder) -> to_shmem::Result<Self> {
278        use std::mem::ManuallyDrop;
279
280        let atom = self.0.to_shmem(builder)?;
281        Ok(ManuallyDrop::new(Self(ManuallyDrop::into_inner(atom))))
282    }
283}
284
285#[cfg(feature = "servo")]
286impl<Set: string_cache::StaticAtomSet> malloc_size_of::MallocSizeOf for GenericAtomIdent<Set> {
287    fn size_of(&self, ops: &mut malloc_size_of::MallocSizeOfOps) -> usize {
288        self.0.size_of(ops)
289    }
290}
291
292#[cfg(feature = "servo")]
293impl<Set: string_cache::StaticAtomSet> cssparser::ToCss for GenericAtomIdent<Set> {
294    fn to_css<W>(&self, dest: &mut W) -> fmt::Result
295    where
296        W: Write,
297    {
298        serialize_atom_identifier(&self.0, dest)
299    }
300}
301
302#[cfg(feature = "servo")]
303impl<Set: string_cache::StaticAtomSet> style_traits::ToCss for GenericAtomIdent<Set> {
304    fn to_css<W>(&self, dest: &mut CssWriter<W>) -> fmt::Result
305    where
306        W: Write,
307    {
308        serialize_atom_identifier(&self.0, dest)
309    }
310}
311
312#[cfg(feature = "servo")]
313impl<Set: string_cache::StaticAtomSet> PrecomputedHash for GenericAtomIdent<Set> {
314    #[inline]
315    fn precomputed_hash(&self) -> u32 {
316        self.0.precomputed_hash()
317    }
318}
319
320#[cfg(feature = "servo")]
321impl<'a, Set: string_cache::StaticAtomSet> From<&'a str> for GenericAtomIdent<Set> {
322    #[inline]
323    fn from(string: &str) -> Self {
324        Self(string_cache::Atom::from(string))
325    }
326}
327
328#[cfg(feature = "servo")]
329impl<Set: string_cache::StaticAtomSet> std::borrow::Borrow<string_cache::Atom<Set>>
330    for GenericAtomIdent<Set>
331{
332    #[inline]
333    fn borrow(&self) -> &string_cache::Atom<Set> {
334        &self.0
335    }
336}
337
338#[cfg(feature = "servo")]
339impl<Set: string_cache::StaticAtomSet> GenericAtomIdent<Set> {
340    /// Constructs a new GenericAtomIdent.
341    #[inline]
342    pub fn new(atom: string_cache::Atom<Set>) -> Self {
343        Self(atom)
344    }
345
346    /// Cast an atom ref to an AtomIdent ref.
347    #[inline]
348    pub fn cast<'a>(atom: &'a string_cache::Atom<Set>) -> &'a Self {
349        let ptr = atom as *const _ as *const Self;
350        // safety: repr(transparent)
351        unsafe { &*ptr }
352    }
353}
354
355/// A CSS `<ident>` stored as an `Atom`.
356#[cfg(feature = "gecko")]
357#[repr(transparent)]
358#[derive(
359    Clone, Debug, Default, Deref, Eq, Hash, MallocSizeOf, PartialEq, SpecifiedValueInfo, ToShmem,
360)]
361pub struct AtomIdent(pub Atom);
362
363#[cfg(feature = "gecko")]
364impl cssparser::ToCss for AtomIdent {
365    fn to_css<W>(&self, dest: &mut W) -> fmt::Result
366    where
367        W: Write,
368    {
369        serialize_atom_identifier(&self.0, dest)
370    }
371}
372
373#[cfg(feature = "gecko")]
374impl style_traits::ToCss for AtomIdent {
375    fn to_css<W>(&self, dest: &mut CssWriter<W>) -> fmt::Result
376    where
377        W: Write,
378    {
379        cssparser::ToCss::to_css(self, dest)
380    }
381}
382
383#[cfg(feature = "gecko")]
384impl PrecomputedHash for AtomIdent {
385    #[inline]
386    fn precomputed_hash(&self) -> u32 {
387        self.0.precomputed_hash()
388    }
389}
390
391#[cfg(feature = "gecko")]
392impl<'a> From<&'a str> for AtomIdent {
393    #[inline]
394    fn from(string: &str) -> Self {
395        Self(Atom::from(string))
396    }
397}
398
399#[cfg(feature = "gecko")]
400impl AtomIdent {
401    /// Constructs a new AtomIdent.
402    #[inline]
403    pub fn new(atom: Atom) -> Self {
404        Self(atom)
405    }
406
407    /// Like `Atom::with` but for `AtomIdent`.
408    pub unsafe fn with<F, R>(ptr: *const crate::gecko_bindings::structs::nsAtom, callback: F) -> R
409    where
410        F: FnOnce(&Self) -> R,
411    {
412        Atom::with(ptr, |atom: &Atom| {
413            // safety: repr(transparent)
414            let atom = atom as *const Atom as *const AtomIdent;
415            callback(&*atom)
416        })
417    }
418
419    /// Cast an atom ref to an AtomIdent ref.
420    #[inline]
421    pub fn cast<'a>(atom: &'a Atom) -> &'a Self {
422        let ptr = atom as *const _ as *const Self;
423        // safety: repr(transparent)
424        unsafe { &*ptr }
425    }
426}
427
428#[cfg(feature = "gecko")]
429impl std::borrow::Borrow<crate::gecko_string_cache::WeakAtom> for AtomIdent {
430    #[inline]
431    fn borrow(&self) -> &crate::gecko_string_cache::WeakAtom {
432        self.0.borrow()
433    }
434}
435
436/// Serialize a value into percentage.
437pub fn serialize_percentage<W>(value: CSSFloat, dest: &mut CssWriter<W>) -> fmt::Result
438where
439    W: Write,
440{
441    serialize_specified_dimension(value * 100., "%", /* was_calc = */ false, dest)
442}
443
444/// Serialize a value into normalized (no NaN/inf serialization) percentage.
445pub fn serialize_normalized_percentage<W>(value: CSSFloat, dest: &mut CssWriter<W>) -> fmt::Result
446where
447    W: Write,
448{
449    (value * 100.).to_css(dest)?;
450    dest.write_char('%')
451}
452
453/// Reify a percentage with calc.
454pub fn reify_percentage(
455    value: CSSFloat,
456    was_calc: bool,
457    dest: &mut ThinVec<TypedValue>,
458) -> Result<(), ()> {
459    let numeric_value = NumericValue::Unit(UnitValue {
460        value: value * 100.,
461        unit: CssString::from("percent"),
462    });
463
464    // https://drafts.css-houdini.org/css-typed-om-1/#reify-a-math-expression
465    if was_calc {
466        dest.push(TypedValue::Numeric(NumericValue::Sum(MathSum {
467            values: ThinVec::from([numeric_value]),
468        })));
469    } else {
470        dest.push(TypedValue::Numeric(numeric_value));
471    }
472
473    Ok(())
474}
475
476/// Convenience void type to disable some properties and values through types.
477#[cfg_attr(feature = "servo", derive(Deserialize, MallocSizeOf, Serialize))]
478#[derive(
479    Clone,
480    Copy,
481    Debug,
482    PartialEq,
483    SpecifiedValueInfo,
484    ToAnimatedValue,
485    ToComputedValue,
486    ToCss,
487    ToResolvedValue,
488)]
489pub enum Impossible {}
490
491// FIXME(nox): This should be derived but the derive code cannot cope
492// with uninhabited enums.
493impl ComputeSquaredDistance for Impossible {
494    #[inline]
495    fn compute_squared_distance(&self, _other: &Self) -> Result<SquaredDistance, ()> {
496        match *self {}
497    }
498}
499
500impl_trivial_to_shmem!(Impossible);
501
502impl Parse for Impossible {
503    fn parse<'i, 't>(
504        _context: &ParserContext,
505        input: &mut Parser<'i, 't>,
506    ) -> Result<Self, ParseError<'i>> {
507        Err(input.new_custom_error(StyleParseErrorKind::UnspecifiedError))
508    }
509}
510
511/// A struct representing one of two kinds of values.
512#[derive(
513    Animate,
514    Clone,
515    ComputeSquaredDistance,
516    Copy,
517    MallocSizeOf,
518    PartialEq,
519    Parse,
520    SpecifiedValueInfo,
521    ToAnimatedValue,
522    ToAnimatedZero,
523    ToComputedValue,
524    ToCss,
525    ToResolvedValue,
526    ToShmem,
527)]
528pub enum Either<A, B> {
529    /// The first value.
530    First(A),
531    /// The second kind of value.
532    Second(B),
533}
534
535impl<A: Debug, B: Debug> Debug for Either<A, B> {
536    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
537        match *self {
538            Either::First(ref v) => v.fmt(f),
539            Either::Second(ref v) => v.fmt(f),
540        }
541    }
542}
543
544/// <https://drafts.csswg.org/css-values-4/#custom-idents>
545#[derive(
546    Clone,
547    Debug,
548    Default,
549    Eq,
550    Hash,
551    MallocSizeOf,
552    PartialEq,
553    SpecifiedValueInfo,
554    ToAnimatedValue,
555    ToComputedValue,
556    ToResolvedValue,
557    ToShmem,
558)]
559#[repr(C)]
560pub struct CustomIdent(pub Atom);
561
562impl CustomIdent {
563    /// Parse a <custom-ident>
564    ///
565    /// TODO(zrhoffman, bug 1844501): Use CustomIdent::parse in more places instead of
566    /// CustomIdent::from_ident.
567    pub fn parse<'i, 't>(
568        input: &mut Parser<'i, 't>,
569        invalid: &[&str],
570    ) -> Result<Self, ParseError<'i>> {
571        let location = input.current_source_location();
572        let ident = input.expect_ident()?;
573        CustomIdent::from_ident(location, ident, invalid)
574    }
575
576    /// Parse an already-tokenizer identifier
577    pub fn from_ident<'i>(
578        location: SourceLocation,
579        ident: &CowRcStr<'i>,
580        excluding: &[&str],
581    ) -> Result<Self, ParseError<'i>> {
582        if !Self::is_valid(ident, excluding) {
583            return Err(
584                location.new_custom_error(SelectorParseErrorKind::UnexpectedIdent(ident.clone()))
585            );
586        }
587        if excluding.iter().any(|s| ident.eq_ignore_ascii_case(s)) {
588            Err(location.new_custom_error(StyleParseErrorKind::UnspecifiedError))
589        } else {
590            Ok(CustomIdent(Atom::from(ident.as_ref())))
591        }
592    }
593
594    fn is_valid(ident: &str, excluding: &[&str]) -> bool {
595        use crate::properties::CSSWideKeyword;
596        // https://drafts.csswg.org/css-values-4/#custom-idents:
597        //
598        //     The CSS-wide keywords are not valid <custom-ident>s. The default
599        //     keyword is reserved and is also not a valid <custom-ident>.
600        if CSSWideKeyword::from_ident(ident).is_ok() || ident.eq_ignore_ascii_case("default") {
601            return false;
602        }
603
604        // https://drafts.csswg.org/css-values-4/#custom-idents:
605        //
606        //     Excluded keywords are excluded in all ASCII case permutations.
607        !excluding.iter().any(|s| ident.eq_ignore_ascii_case(s))
608    }
609}
610
611impl ToCss for CustomIdent {
612    fn to_css<W>(&self, dest: &mut CssWriter<W>) -> fmt::Result
613    where
614        W: Write,
615    {
616        serialize_atom_identifier(&self.0, dest)
617    }
618}
619
620impl ToTyped for CustomIdent {
621    fn to_typed(&self, dest: &mut ThinVec<TypedValue>) -> Result<(), ()> {
622        // This shouldn't escape identifiers. See bug 2023533.
623        let s = ToCss::to_css_cssstring(self);
624        dest.push(TypedValue::Keyword(KeywordValue(s)));
625        Ok(())
626    }
627}
628
629/// <https://www.w3.org/TR/css-values-4/#dashed-idents>
630/// This is simply an Atom, but will only parse if the identifier starts with "--".
631#[repr(transparent)]
632#[derive(
633    Clone,
634    Debug,
635    Eq,
636    Hash,
637    MallocSizeOf,
638    PartialEq,
639    SpecifiedValueInfo,
640    ToAnimatedValue,
641    ToComputedValue,
642    ToResolvedValue,
643    ToShmem,
644    Serialize,
645    Deserialize,
646)]
647pub struct DashedIdent(pub Atom);
648
649impl DashedIdent {
650    /// Parse an already-tokenizer identifier
651    pub fn from_ident<'i>(
652        location: SourceLocation,
653        ident: &CowRcStr<'i>,
654    ) -> Result<Self, ParseError<'i>> {
655        if !ident.starts_with("--") {
656            return Err(
657                location.new_custom_error(SelectorParseErrorKind::UnexpectedIdent(ident.clone()))
658            );
659        }
660        Ok(Self(Atom::from(ident.as_ref())))
661    }
662
663    /// Special value for internal use. Useful where we can't use Option<>.
664    pub fn empty() -> Self {
665        Self(atom!(""))
666    }
667
668    /// Check for special internal value.
669    pub fn is_empty(&self) -> bool {
670        self.0 == atom!("")
671    }
672
673    /// Returns an atom with the same value, but without the starting "--".
674    ///
675    /// # Panics
676    ///
677    /// Panics when used on the special `DashedIdent::empty()`.
678    pub(crate) fn undashed(&self) -> Atom {
679        assert!(!self.is_empty(), "Can't undash the empty DashedIdent");
680        #[cfg(feature = "gecko")]
681        let name = &self.0.as_slice()[2..];
682        #[cfg(feature = "servo")]
683        let name = &self.0[2..];
684        Atom::from(name)
685    }
686}
687
688impl IsTreeScoped for DashedIdent {
689    fn is_tree_scoped(&self) -> bool {
690        !self.is_empty()
691    }
692}
693
694impl Parse for DashedIdent {
695    fn parse<'i, 't>(
696        _: &ParserContext,
697        input: &mut Parser<'i, 't>,
698    ) -> Result<Self, ParseError<'i>> {
699        let location = input.current_source_location();
700        let ident = input.expect_ident()?;
701        Self::from_ident(location, ident)
702    }
703}
704
705impl ToCss for DashedIdent {
706    fn to_css<W>(&self, dest: &mut CssWriter<W>) -> fmt::Result
707    where
708        W: Write,
709    {
710        serialize_atom_identifier(&self.0, dest)
711    }
712}
713
714/// The <keyframes-name>.
715///
716/// <https://drafts.csswg.org/css-animations/#typedef-keyframes-name>
717///
718/// We use a single atom for this. Empty atom represents `none` animation.
719#[repr(transparent)]
720#[derive(
721    Clone,
722    Debug,
723    Eq,
724    Hash,
725    PartialEq,
726    MallocSizeOf,
727    SpecifiedValueInfo,
728    ToComputedValue,
729    ToResolvedValue,
730    ToShmem,
731)]
732pub struct KeyframesName(Atom);
733
734impl KeyframesName {
735    /// <https://drafts.csswg.org/css-animations/#dom-csskeyframesrule-name>
736    pub fn from_ident(value: &str) -> Self {
737        Self(Atom::from(value))
738    }
739
740    /// Returns the `none` value.
741    pub fn none() -> Self {
742        Self(atom!(""))
743    }
744
745    /// Returns whether this is the special `none` value.
746    pub fn is_none(&self) -> bool {
747        self.0 == atom!("")
748    }
749
750    /// Create a new KeyframesName from Atom.
751    #[cfg(feature = "gecko")]
752    pub fn from_atom(atom: Atom) -> Self {
753        Self(atom)
754    }
755
756    /// The name as an Atom
757    pub fn as_atom(&self) -> &Atom {
758        &self.0
759    }
760}
761
762impl Parse for KeyframesName {
763    fn parse<'i, 't>(
764        _: &ParserContext,
765        input: &mut Parser<'i, 't>,
766    ) -> Result<Self, ParseError<'i>> {
767        let location = input.current_source_location();
768        Ok(match *input.next()? {
769            Token::Ident(ref s) => Self(CustomIdent::from_ident(location, s, &["none"])?.0),
770            // Note that empty <string> should be rejected.
771            Token::QuotedString(ref s) if !s.as_ref().is_empty() => Self(Atom::from(s.as_ref())),
772            ref t => return Err(location.new_unexpected_token_error(t.clone())),
773        })
774    }
775}
776
777impl ToCss for KeyframesName {
778    fn to_css<W>(&self, dest: &mut CssWriter<W>) -> fmt::Result
779    where
780        W: Write,
781    {
782        if self.is_none() {
783            return dest.write_str("none");
784        }
785
786        fn serialize<W: Write>(string: &str, dest: &mut CssWriter<W>) -> fmt::Result {
787            if CustomIdent::is_valid(string, &["none"]) {
788                serialize_identifier(string, dest)
789            } else {
790                string.to_css(dest)
791            }
792        }
793
794        #[cfg(feature = "gecko")]
795        return self.0.with_str(|s| serialize(s, dest));
796
797        #[cfg(feature = "servo")]
798        return serialize(self.0.as_ref(), dest);
799    }
800}
801
802impl ToTyped for KeyframesName {
803    fn to_typed(&self, dest: &mut ThinVec<TypedValue>) -> Result<(), ()> {
804        let s = ToCss::to_css_cssstring(self);
805        dest.push(TypedValue::Keyword(KeywordValue(s)));
806        Ok(())
807    }
808}