style_traits/
lib.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//! This module contains shared types and messages for use by devtools/script.
6//! The traits are here instead of in script so that the devtools crate can be
7//! modified independently of the rest of Servo.
8
9#![crate_name = "style_traits"]
10#![crate_type = "rlib"]
11#![deny(unsafe_code, missing_docs)]
12
13#[macro_use]
14extern crate malloc_size_of_derive;
15#[macro_use]
16extern crate serde;
17#[macro_use]
18extern crate to_shmem_derive;
19#[cfg(feature = "servo")]
20extern crate url;
21
22use bitflags::bitflags;
23use cssparser::{CowRcStr, Token};
24use selectors::parser::SelectorParseErrorKind;
25#[cfg(feature = "servo")]
26use stylo_atoms::Atom;
27
28/// One hardware pixel.
29///
30/// This unit corresponds to the smallest addressable element of the display hardware.
31#[derive(Clone, Copy, Debug)]
32pub enum DevicePixel {}
33
34/// Represents a mobile style pinch zoom factor.
35#[derive(Clone, Copy, Debug, PartialEq)]
36#[cfg_attr(feature = "servo", derive(Deserialize, Serialize, MallocSizeOf))]
37pub struct PinchZoomFactor(f32);
38
39impl PinchZoomFactor {
40    /// Construct a new pinch zoom factor.
41    pub fn new(scale: f32) -> PinchZoomFactor {
42        PinchZoomFactor(scale)
43    }
44
45    /// Get the pinch zoom factor as an untyped float.
46    pub fn get(&self) -> f32 {
47        self.0
48    }
49}
50
51/// One CSS "px" in the coordinate system of the "initial viewport":
52/// <http://www.w3.org/TR/css-device-adapt/#initial-viewport>
53///
54/// `CSSPixel` is equal to `DeviceIndependentPixel` times a "page zoom" factor controlled by the user.  This is
55/// the desktop-style "full page" zoom that enlarges content but then reflows the layout viewport
56/// so it still exactly fits the visible area.
57///
58/// At the default zoom level of 100%, one `CSSPixel` is equal to one `DeviceIndependentPixel`.  However, if the
59/// document is zoomed in or out then this scale may be larger or smaller.
60#[derive(Clone, Copy, Debug)]
61pub enum CSSPixel {}
62
63// In summary, the hierarchy of pixel units and the factors to convert from one to the next:
64//
65// DevicePixel
66//   / hidpi_ratio => DeviceIndependentPixel
67//     / desktop_zoom => CSSPixel
68
69pub mod arc_slice;
70pub mod dom;
71pub mod specified_value_info;
72#[macro_use]
73pub mod values;
74pub mod owned_slice;
75pub mod owned_str;
76
77pub use crate::specified_value_info::{CssType, KeywordsCollectFn, SpecifiedValueInfo};
78pub use crate::values::{
79    Comma, CommaWithSpace, CssString, CssStringWriter, CssWriter, KeywordValue, MathSum,
80    NumericValue, OneOrMoreSeparated, Separator, Space, ToCss, ToTyped, TypedValue, TypedValueList,
81    UnitValue,
82};
83
84/// The error type for all CSS parsing routines.
85pub type ParseError<'i> = cssparser::ParseError<'i, StyleParseErrorKind<'i>>;
86
87/// Error in property value parsing
88pub type ValueParseError<'i> = cssparser::ParseError<'i, ValueParseErrorKind<'i>>;
89
90#[derive(Clone, Debug, PartialEq)]
91/// Errors that can be encountered while parsing CSS values.
92pub enum StyleParseErrorKind<'i> {
93    /// A bad URL token in a DVB.
94    BadUrlInDeclarationValueBlock(CowRcStr<'i>),
95    /// A bad string token in a DVB.
96    BadStringInDeclarationValueBlock(CowRcStr<'i>),
97    /// Unexpected closing parenthesis in a DVB.
98    UnbalancedCloseParenthesisInDeclarationValueBlock,
99    /// Unexpected closing bracket in a DVB.
100    UnbalancedCloseSquareBracketInDeclarationValueBlock,
101    /// Unexpected closing curly bracket in a DVB.
102    UnbalancedCloseCurlyBracketInDeclarationValueBlock,
103    /// A property declaration value had input remaining after successfully parsing.
104    PropertyDeclarationValueNotExhausted,
105    /// An unexpected dimension token was encountered.
106    UnexpectedDimension(CowRcStr<'i>),
107    /// Missing or invalid media feature name.
108    MediaQueryExpectedFeatureName(CowRcStr<'i>),
109    /// Missing or invalid media feature value.
110    MediaQueryExpectedFeatureValue,
111    /// A media feature range operator was not expected.
112    MediaQueryUnexpectedOperator,
113    /// min- or max- properties must have a value.
114    RangedExpressionWithNoValue,
115    /// A function was encountered that was not expected.
116    UnexpectedFunction(CowRcStr<'i>),
117    /// Error encountered parsing a @property's `syntax` descriptor
118    PropertySyntaxField(PropertySyntaxParseError),
119    /// Error encountered parsing a @property's `inherits` descriptor.
120    ///
121    /// TODO(zrhoffman, bug 1920365): Include the custom property name in error messages.
122    PropertyInheritsField(PropertyInheritsParseError),
123    /// @namespace must be before any rule but @charset and @import
124    UnexpectedNamespaceRule,
125    /// @import must be before any rule but @charset
126    UnexpectedImportRule,
127    /// @import rules are disallowed in the parser.
128    DisallowedImportRule,
129    /// Unexpected @charset rule encountered.
130    UnexpectedCharsetRule,
131    /// The @property `<custom-property-name>` must start with `--`
132    UnexpectedIdent(CowRcStr<'i>),
133    /// A placeholder for many sources of errors that require more specific variants.
134    UnspecifiedError,
135    /// An unexpected token was found within a namespace rule.
136    UnexpectedTokenWithinNamespace(Token<'i>),
137    /// An error was encountered while parsing a property value.
138    ValueError(ValueParseErrorKind<'i>),
139    /// An error was encountered while parsing a selector
140    SelectorError(SelectorParseErrorKind<'i>),
141    /// The property declaration was for an unknown property.
142    UnknownProperty(CowRcStr<'i>),
143    /// The property declaration was for a disabled experimental property.
144    ExperimentalProperty,
145    /// The property declaration contained an invalid color value.
146    InvalidColor(CowRcStr<'i>, Token<'i>),
147    /// The property declaration contained an invalid filter value.
148    InvalidFilter(CowRcStr<'i>, Token<'i>),
149    /// The property declaration contained an invalid value.
150    OtherInvalidValue(CowRcStr<'i>),
151    /// `!important` declarations are disallowed in `@position-try` or keyframes.
152    UnexpectedImportantDeclaration,
153}
154
155impl<'i> From<ValueParseErrorKind<'i>> for StyleParseErrorKind<'i> {
156    fn from(this: ValueParseErrorKind<'i>) -> Self {
157        StyleParseErrorKind::ValueError(this)
158    }
159}
160
161impl<'i> From<SelectorParseErrorKind<'i>> for StyleParseErrorKind<'i> {
162    fn from(this: SelectorParseErrorKind<'i>) -> Self {
163        StyleParseErrorKind::SelectorError(this)
164    }
165}
166
167/// Specific errors that can be encountered while parsing property values.
168#[derive(Clone, Debug, PartialEq)]
169pub enum ValueParseErrorKind<'i> {
170    /// An invalid token was encountered while parsing a color value.
171    InvalidColor(Token<'i>),
172    /// An invalid filter value was encountered.
173    InvalidFilter(Token<'i>),
174}
175
176impl<'i> StyleParseErrorKind<'i> {
177    /// Create an InvalidValue parse error
178    pub fn new_invalid<S>(name: S, value_error: ParseError<'i>) -> ParseError<'i>
179    where
180        S: Into<CowRcStr<'i>>,
181    {
182        let name = name.into();
183        let variant = match value_error.kind {
184            cssparser::ParseErrorKind::Custom(StyleParseErrorKind::ValueError(e)) => match e {
185                ValueParseErrorKind::InvalidColor(token) => {
186                    StyleParseErrorKind::InvalidColor(name, token)
187                },
188                ValueParseErrorKind::InvalidFilter(token) => {
189                    StyleParseErrorKind::InvalidFilter(name, token)
190                },
191            },
192            _ => StyleParseErrorKind::OtherInvalidValue(name),
193        };
194        cssparser::ParseError {
195            kind: cssparser::ParseErrorKind::Custom(variant),
196            location: value_error.location,
197        }
198    }
199}
200
201/// Errors that can be encountered while parsing the @property rule's syntax descriptor.
202#[derive(Clone, Debug, PartialEq)]
203pub enum PropertySyntaxParseError {
204    /// The syntax descriptor is required for the @property rule to be valid; if it’s missing, the
205    /// @property rule is invalid.
206    ///
207    /// <https://drafts.css-houdini.org/css-properties-values-api-1/#ref-for-descdef-property-syntax②>
208    NoSyntax,
209    /// The string's length was 0.
210    EmptyInput,
211    /// A non-whitespace, non-pipe character was fount after parsing a component.
212    ExpectedPipeBetweenComponents,
213    /// The start of an identifier was expected but not found.
214    ///
215    /// <https://drafts.csswg.org/css-syntax-3/#name-start-code-point>
216    InvalidNameStart,
217    /// The name is not a valid `<ident>`.
218    InvalidName,
219    /// The data type name was not closed.
220    ///
221    /// <https://drafts.css-houdini.org/css-properties-values-api-1/#consume-data-type-name>
222    UnclosedDataTypeName,
223    /// The next byte was expected while parsing, but EOF was found instead.
224    UnexpectedEOF,
225    /// The data type is not a supported syntax component name.
226    ///
227    /// <https://drafts.css-houdini.org/css-properties-values-api-1/#supported-names>
228    UnknownDataTypeName,
229}
230
231/// Errors that can be encountered while parsing the @property rule's inherits descriptor.
232#[derive(Clone, Debug, PartialEq)]
233pub enum PropertyInheritsParseError {
234    /// The inherits descriptor is required for the @property rule to be valid; if it’s missing,
235    /// the @property rule is invalid.
236    ///
237    /// <https://drafts.css-houdini.org/css-properties-values-api-1/#ref-for-descdef-property-inherits②>
238    NoInherits,
239
240    /// The inherits descriptor must successfully parse as `true` or `false`.
241    InvalidInherits,
242}
243
244bitflags! {
245    /// The mode to use when parsing values.
246    #[derive(Clone, Copy, Eq, PartialEq)]
247    #[repr(C)]
248    pub struct ParsingMode: u8 {
249        /// In CSS; lengths must have units, except for zero values, where the unit can be omitted.
250        /// <https://www.w3.org/TR/css3-values/#lengths>
251        const DEFAULT = 0;
252        /// In SVG; a coordinate or length value without a unit identifier (e.g., "25") is assumed
253        /// to be in user units (px).
254        /// <https://www.w3.org/TR/SVG/coords.html#Units>
255        const ALLOW_UNITLESS_LENGTH = 1;
256        /// In SVG; out-of-range values are not treated as an error in parsing.
257        /// <https://www.w3.org/TR/SVG/implnote.html#RangeClamping>
258        const ALLOW_ALL_NUMERIC_VALUES = 1 << 1;
259        /// In CSS Properties and Values, the initial value must be computationally
260        /// independent.
261        /// <https://drafts.css-houdini.org/css-properties-values-api-1/#ref-for-computationally-independent%E2%91%A0>
262        const DISALLOW_COMPUTATIONALLY_DEPENDENT = 1 << 2;
263        /// In CSS Values and Units, values produced by `attr()` are considered attr()-tainted, as are
264        /// functions that contain an attr()-tainted value. Using an attr()-tainted value as or in a <url>
265        /// makes a declaration invalid at computed-value time.
266        /// https://drafts.csswg.org/css-values-5/#attr-security
267        /// TODO(bug1997583): Remove this mode in favor of a more robust range-based attr tracking.
268        const DISALLOW_URLS = 1 << 3;
269    }
270}
271
272impl ParsingMode {
273    /// Whether the parsing mode allows unitless lengths for non-zero values to be intpreted as px.
274    #[inline]
275    pub fn allows_unitless_lengths(&self) -> bool {
276        self.intersects(ParsingMode::ALLOW_UNITLESS_LENGTH)
277    }
278
279    /// Whether the parsing mode allows all numeric values.
280    #[inline]
281    pub fn allows_all_numeric_values(&self) -> bool {
282        self.intersects(ParsingMode::ALLOW_ALL_NUMERIC_VALUES)
283    }
284
285    /// Whether the parsing mode allows units or functions that are not computationally independent.
286    #[inline]
287    pub fn allows_computational_dependence(&self) -> bool {
288        !self.intersects(ParsingMode::DISALLOW_COMPUTATIONALLY_DEPENDENT)
289    }
290
291    /// Whether the parsing mode disallows <url> to be resolved.
292    pub fn disallows_urls(&self) -> bool {
293        self.intersects(ParsingMode::DISALLOW_URLS)
294    }
295}
296
297#[cfg(feature = "servo")]
298/// Speculatively execute paint code in the worklet thread pool.
299pub trait SpeculativePainter: Send + Sync {
300    /// <https://drafts.css-houdini.org/css-paint-api/#draw-a-paint-image>
301    fn speculatively_draw_a_paint_image(
302        &self,
303        properties: Vec<(Atom, String)>,
304        arguments: Vec<String>,
305    );
306}