style/properties_and_values/syntax/
data_type.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//! Used for parsing and serializing component names from the syntax string.
6
7use super::{Component, ComponentName, Multiplier};
8use crate::derives::*;
9use std::fmt::{self, Debug, Write};
10use style_traits::{CssWriter, ToCss};
11
12/// Some types (lengths and colors) depend on other properties to resolve correctly.
13#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, MallocSizeOf, ToShmem)]
14pub struct DependentDataTypes(u8);
15bitflags! {
16    impl DependentDataTypes: u8 {
17        /// <length> values depend on font-size/line-height/zoom...
18        const LENGTH = 1 << 0;
19        /// <color> values depend on color-scheme, etc..
20        const COLOR= 1 << 1;
21    }
22}
23
24/// <https://drafts.css-houdini.org/css-properties-values-api-1/#supported-names>
25#[derive(Clone, Copy, Debug, MallocSizeOf, PartialEq, ToShmem)]
26pub enum DataType {
27    /// Any valid `<length>` value
28    Length,
29    /// `<number>` values
30    Number,
31    /// Any valid <percentage> value
32    Percentage,
33    /// Any valid `<length>` or `<percentage>` value, any valid `<calc()>` expression combining
34    /// `<length>` and `<percentage>` components.
35    LengthPercentage,
36    /// Any valid `<color>` value
37    Color,
38    /// Any valid `<image>` value
39    Image,
40    /// Any valid `<url>` value
41    Url,
42    /// Any valid `<integer>` value
43    Integer,
44    /// Any valid `<angle>` value
45    Angle,
46    /// Any valid `<time>` value
47    Time,
48    /// Any valid `<resolution>` value
49    Resolution,
50    /// Any valid `<transform-function>` value
51    TransformFunction,
52    /// Any valid `<custom-ident>` value
53    CustomIdent,
54    /// A list of valid `<transform-function>` values. Note that "<transform-list>" is a pre-multiplied
55    /// data type name equivalent to "<transform-function>+"
56    TransformList,
57    /// Any valid `<string>` value
58    ///
59    /// <https://github.com/w3c/css-houdini-drafts/issues/1103>
60    String,
61}
62
63impl DataType {
64    /// Converts a component name from a pre-multiplied data type to its un-pre-multiplied equivalent.
65    ///
66    /// <https://drafts.css-houdini.org/css-properties-values-api-1/#pre-multiplied-data-type-name>
67    pub fn unpremultiply(&self) -> Option<Component> {
68        match *self {
69            DataType::TransformList => Some(Component {
70                name: ComponentName::DataType(DataType::TransformFunction),
71                multiplier: Some(Multiplier::Space),
72            }),
73            _ => None,
74        }
75    }
76
77    /// Parses a syntax component name.
78    pub fn from_str(ty: &str) -> Option<Self> {
79        Some(match ty.as_bytes() {
80            b"length" => DataType::Length,
81            b"number" => DataType::Number,
82            b"percentage" => DataType::Percentage,
83            b"length-percentage" => DataType::LengthPercentage,
84            b"color" => DataType::Color,
85            b"image" => DataType::Image,
86            b"url" => DataType::Url,
87            b"integer" => DataType::Integer,
88            b"angle" => DataType::Angle,
89            b"time" => DataType::Time,
90            b"resolution" => DataType::Resolution,
91            b"transform-function" => DataType::TransformFunction,
92            b"custom-ident" => DataType::CustomIdent,
93            b"transform-list" => DataType::TransformList,
94            b"string" => DataType::String,
95            _ => return None,
96        })
97    }
98
99    /// Returns which kinds of dependent data types this property might contain.
100    pub fn dependent_types(&self) -> DependentDataTypes {
101        match self {
102            DataType::Length
103            | DataType::LengthPercentage
104            | DataType::TransformFunction
105            | DataType::TransformList => DependentDataTypes::LENGTH,
106            DataType::Color => DependentDataTypes::COLOR,
107            DataType::Number
108            | DataType::Percentage
109            | DataType::Image
110            | DataType::Url
111            | DataType::Integer
112            | DataType::Angle
113            | DataType::Time
114            | DataType::Resolution
115            | DataType::CustomIdent
116            | DataType::String => DependentDataTypes::empty(),
117        }
118    }
119}
120
121impl ToCss for DataType {
122    fn to_css<W>(&self, dest: &mut CssWriter<W>) -> fmt::Result
123    where
124        W: Write,
125    {
126        dest.write_char('<')?;
127        dest.write_str(match *self {
128            DataType::Length => "length",
129            DataType::Number => "number",
130            DataType::Percentage => "percentage",
131            DataType::LengthPercentage => "length-percentage",
132            DataType::Color => "color",
133            DataType::Image => "image",
134            DataType::Url => "url",
135            DataType::Integer => "integer",
136            DataType::Angle => "angle",
137            DataType::Time => "time",
138            DataType::Resolution => "resolution",
139            DataType::TransformFunction => "transform-function",
140            DataType::CustomIdent => "custom-ident",
141            DataType::TransformList => "transform-list",
142            DataType::String => "string",
143        })?;
144        dest.write_char('>')
145    }
146}