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