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