Skip to main content

style/values/specified/
ui.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//! Specified types for UI properties.
6
7use crate::derives::*;
8use crate::parser::{Parse, ParserContext};
9use crate::values::generics::ui as generics;
10use crate::values::specified::color::Color;
11use crate::values::specified::image::Image;
12use crate::values::specified::Number;
13use cssparser::Parser;
14use std::fmt::{self, Write};
15use style_traits::{
16    CssWriter, KeywordsCollectFn, ParseError, SpecifiedValueInfo, StyleParseErrorKind, ToCss,
17};
18
19/// A specified value for the `cursor` property.
20pub type Cursor = generics::GenericCursor<CursorImage>;
21
22/// A specified value for item of `image cursors`.
23pub type CursorImage = generics::GenericCursorImage<Image, Number>;
24
25impl Parse for Cursor {
26    /// cursor: [<url> [<number> <number>]?]# [auto | default | ...]
27    fn parse(context: &ParserContext, input: &mut Parser) -> Result<Self, ParseError> {
28        let mut images = vec![];
29        loop {
30            match input.try_parse(|input| CursorImage::parse(context, input)) {
31                Ok(image) => images.push(image),
32                Err(_) => break,
33            }
34            input.expect_comma()?;
35        }
36        Ok(Self {
37            images: images.into(),
38            keyword: CursorKind::parse(input)?,
39        })
40    }
41}
42
43impl Parse for CursorImage {
44    fn parse(context: &ParserContext, input: &mut Parser) -> Result<Self, ParseError> {
45        use crate::Zero;
46
47        let image = Image::parse_only_url(context, input)?;
48        let mut has_hotspot = false;
49        let mut hotspot_x = Number::zero();
50        let mut hotspot_y = Number::zero();
51
52        if let Ok(x) = input.try_parse(|input| Number::parse(context, input)) {
53            has_hotspot = true;
54            hotspot_x = x;
55            hotspot_y = Number::parse(context, input)?;
56        }
57
58        Ok(Self {
59            image,
60            has_hotspot,
61            hotspot_x,
62            hotspot_y,
63        })
64    }
65}
66
67// This trait is manually implemented because we don't support the whole <image>
68// syntax for cursors
69impl SpecifiedValueInfo for CursorImage {
70    fn collect_completion_keywords(f: KeywordsCollectFn) {
71        f(&["url", "image-set"]);
72    }
73}
74/// Specified value of `-moz-force-broken-image-icon`
75#[derive(
76    Clone,
77    Copy,
78    Debug,
79    MallocSizeOf,
80    PartialEq,
81    SpecifiedValueInfo,
82    ToComputedValue,
83    ToResolvedValue,
84    ToShmem,
85    ToTyped,
86)]
87#[repr(transparent)]
88#[typed(todo_derive_fields)]
89pub struct BoolInteger(pub bool);
90
91impl BoolInteger {
92    /// Returns 0
93    #[inline]
94    pub fn zero() -> Self {
95        Self(false)
96    }
97}
98
99impl Parse for BoolInteger {
100    fn parse(_context: &ParserContext, input: &mut Parser) -> Result<Self, ParseError> {
101        // We intentionally don't support calc values here.
102        match input.expect_integer()? {
103            0 => Ok(Self(false)),
104            1 => Ok(Self(true)),
105            _ => Err(ParseError::custom(StyleParseErrorKind::UnspecifiedError)),
106        }
107    }
108}
109
110impl ToCss for BoolInteger {
111    fn to_css<W>(&self, dest: &mut CssWriter<W>) -> fmt::Result
112    where
113        W: Write,
114    {
115        dest.write_str(if self.0 { "1" } else { "0" })
116    }
117}
118
119/// A specified value for `scrollbar-color` property
120pub type ScrollbarColor = generics::ScrollbarColor<Color>;
121
122impl Parse for ScrollbarColor {
123    fn parse(context: &ParserContext, input: &mut Parser) -> Result<Self, ParseError> {
124        if input.try_parse(|i| i.expect_ident_matching("auto")).is_ok() {
125            return Ok(generics::ScrollbarColor::Auto);
126        }
127        Ok(generics::ScrollbarColor::Colors {
128            thumb: Color::parse(context, input)?,
129            track: Color::parse(context, input)?,
130        })
131    }
132}
133
134/// The specified value for the `user-select` property.
135///
136/// https://drafts.csswg.org/css-ui-4/#propdef-user-select
137#[allow(missing_docs)]
138#[derive(
139    Clone,
140    Copy,
141    Debug,
142    Eq,
143    MallocSizeOf,
144    Parse,
145    PartialEq,
146    SpecifiedValueInfo,
147    ToComputedValue,
148    ToCss,
149    ToResolvedValue,
150    ToShmem,
151    ToTyped,
152)]
153#[repr(u8)]
154pub enum UserSelect {
155    Auto,
156    Text,
157    #[parse(aliases = "-moz-none")]
158    None,
159    #[cfg(feature = "servo")]
160    Contain,
161    /// Force selection of all children.
162    All,
163}
164
165/// The keywords allowed in the Cursor property.
166///
167/// https://drafts.csswg.org/css-ui-4/#propdef-cursor
168#[allow(missing_docs)]
169#[derive(
170    Clone,
171    Copy,
172    Debug,
173    Eq,
174    FromPrimitive,
175    MallocSizeOf,
176    Parse,
177    PartialEq,
178    SpecifiedValueInfo,
179    ToComputedValue,
180    ToCss,
181    ToResolvedValue,
182    ToShmem,
183    ToTyped,
184)]
185#[repr(u8)]
186pub enum CursorKind {
187    None,
188    Default,
189    Pointer,
190    ContextMenu,
191    Help,
192    Progress,
193    Wait,
194    Cell,
195    Crosshair,
196    Text,
197    VerticalText,
198    Alias,
199    Copy,
200    Move,
201    NoDrop,
202    NotAllowed,
203    #[parse(aliases = "-moz-grab")]
204    Grab,
205    #[parse(aliases = "-moz-grabbing")]
206    Grabbing,
207    EResize,
208    NResize,
209    NeResize,
210    NwResize,
211    SResize,
212    SeResize,
213    SwResize,
214    WResize,
215    EwResize,
216    NsResize,
217    NeswResize,
218    NwseResize,
219    ColResize,
220    RowResize,
221    AllScroll,
222    #[parse(aliases = "-moz-zoom-in")]
223    ZoomIn,
224    #[parse(aliases = "-moz-zoom-out")]
225    ZoomOut,
226    Auto,
227}
228
229/// The keywords allowed in the -moz-theme property.
230#[derive(
231    Clone,
232    Copy,
233    Debug,
234    Eq,
235    FromPrimitive,
236    MallocSizeOf,
237    Parse,
238    PartialEq,
239    SpecifiedValueInfo,
240    ToComputedValue,
241    ToCss,
242    ToResolvedValue,
243    ToShmem,
244    ToTyped,
245)]
246#[repr(u8)]
247pub enum MozTheme {
248    /// Choose the default (maybe native) rendering.
249    Auto,
250    /// Choose the non-native rendering.
251    NonNative,
252}
253
254/// The pointer-events property
255/// https://svgwg.org/svg2-draft/interact.html#PointerEventsProperty
256#[allow(missing_docs)]
257#[derive(
258    Clone,
259    Copy,
260    Debug,
261    Eq,
262    FromPrimitive,
263    MallocSizeOf,
264    Parse,
265    PartialEq,
266    SpecifiedValueInfo,
267    ToComputedValue,
268    ToCss,
269    ToResolvedValue,
270    ToShmem,
271    ToTyped,
272)]
273#[repr(u8)]
274pub enum PointerEvents {
275    Auto,
276    None,
277    #[cfg(feature = "gecko")]
278    Visiblepainted,
279    #[cfg(feature = "gecko")]
280    Visiblefill,
281    #[cfg(feature = "gecko")]
282    Visiblestroke,
283    #[cfg(feature = "gecko")]
284    Visible,
285    #[cfg(feature = "gecko")]
286    Painted,
287    #[cfg(feature = "gecko")]
288    Fill,
289    #[cfg(feature = "gecko")]
290    Stroke,
291    #[cfg(feature = "gecko")]
292    All,
293}
294
295/// Internal property to represent the inert attribute state:
296/// https://html.spec.whatwg.org/multipage/#inert-subtrees
297#[allow(missing_docs)]
298#[derive(
299    Clone,
300    Copy,
301    Debug,
302    Eq,
303    FromPrimitive,
304    MallocSizeOf,
305    Parse,
306    PartialEq,
307    SpecifiedValueInfo,
308    ToComputedValue,
309    ToCss,
310    ToResolvedValue,
311    ToShmem,
312    ToTyped,
313)]
314#[repr(u8)]
315pub enum Inert {
316    None,
317    Inert,
318}
319
320/// Internal -moz-user-focus property.
321/// https://developer.mozilla.org/en-US/docs/Web/CSS/-moz-user-focus
322#[allow(missing_docs)]
323#[derive(
324    Clone,
325    Copy,
326    Debug,
327    Eq,
328    FromPrimitive,
329    MallocSizeOf,
330    Parse,
331    PartialEq,
332    SpecifiedValueInfo,
333    ToComputedValue,
334    ToCss,
335    ToResolvedValue,
336    ToShmem,
337    ToTyped,
338)]
339#[repr(u8)]
340pub enum UserFocus {
341    Normal,
342    None,
343    Ignore,
344}