style/values/generics/
box.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//! Generic types for box properties.
6
7use crate::values::animated::ToAnimatedZero;
8use std::fmt::{self, Write};
9use style_traits::{CssWriter, ToCss};
10
11#[derive(
12    Animate,
13    Clone,
14    ComputeSquaredDistance,
15    Copy,
16    Debug,
17    FromPrimitive,
18    MallocSizeOf,
19    Parse,
20    PartialEq,
21    SpecifiedValueInfo,
22    ToAnimatedValue,
23    ToComputedValue,
24    ToCss,
25    ToResolvedValue,
26    ToShmem,
27)]
28#[repr(u8)]
29#[allow(missing_docs)]
30pub enum VerticalAlignKeyword {
31    Baseline,
32    Sub,
33    Super,
34    Top,
35    TextTop,
36    Middle,
37    Bottom,
38    TextBottom,
39    #[cfg(feature = "gecko")]
40    MozMiddleWithBaseline,
41}
42
43/// A generic value for the `vertical-align` property.
44#[derive(
45    Animate,
46    Clone,
47    ComputeSquaredDistance,
48    Copy,
49    Debug,
50    MallocSizeOf,
51    PartialEq,
52    SpecifiedValueInfo,
53    ToAnimatedValue,
54    ToComputedValue,
55    ToCss,
56    ToResolvedValue,
57    ToShmem,
58)]
59#[repr(C, u8)]
60pub enum GenericVerticalAlign<LengthPercentage> {
61    /// One of the vertical-align keywords.
62    Keyword(VerticalAlignKeyword),
63    /// `<length-percentage>`
64    Length(LengthPercentage),
65}
66
67pub use self::GenericVerticalAlign as VerticalAlign;
68
69impl<L> VerticalAlign<L> {
70    /// Returns `baseline`.
71    #[inline]
72    pub fn baseline() -> Self {
73        VerticalAlign::Keyword(VerticalAlignKeyword::Baseline)
74    }
75}
76
77impl<L> ToAnimatedZero for VerticalAlign<L> {
78    fn to_animated_zero(&self) -> Result<Self, ()> {
79        Err(())
80    }
81}
82
83/// https://drafts.csswg.org/css-sizing-4/#intrinsic-size-override
84#[derive(
85    Animate,
86    Clone,
87    ComputeSquaredDistance,
88    Debug,
89    MallocSizeOf,
90    PartialEq,
91    SpecifiedValueInfo,
92    ToComputedValue,
93    ToAnimatedValue,
94    ToAnimatedZero,
95    ToResolvedValue,
96    ToShmem,
97)]
98#[value_info(other_values = "auto")]
99#[repr(C, u8)]
100pub enum GenericContainIntrinsicSize<L> {
101    /// The keyword `none`.
102    None,
103    /// The keywords 'auto none',
104    AutoNone,
105    /// A non-negative length.
106    Length(L),
107    /// "auto <Length>"
108    AutoLength(L),
109}
110
111pub use self::GenericContainIntrinsicSize as ContainIntrinsicSize;
112
113impl<L: ToCss> ToCss for ContainIntrinsicSize<L> {
114    fn to_css<W>(&self, dest: &mut CssWriter<W>) -> fmt::Result
115    where
116        W: Write,
117    {
118        match *self {
119            Self::None => dest.write_str("none"),
120            Self::AutoNone => dest.write_str("auto none"),
121            Self::Length(ref l) => l.to_css(dest),
122            Self::AutoLength(ref l) => {
123                dest.write_str("auto ")?;
124                l.to_css(dest)
125            },
126        }
127    }
128}
129
130/// Note that we only implement -webkit-line-clamp as a single, longhand
131/// property for now, but the spec defines line-clamp as a shorthand for
132/// separate max-lines, block-ellipsis, and continue properties.
133///
134/// https://drafts.csswg.org/css-overflow-3/#line-clamp
135#[derive(
136    Clone,
137    ComputeSquaredDistance,
138    Copy,
139    Debug,
140    MallocSizeOf,
141    PartialEq,
142    SpecifiedValueInfo,
143    ToComputedValue,
144    ToAnimatedValue,
145    ToAnimatedZero,
146    ToResolvedValue,
147    ToShmem,
148)]
149#[repr(transparent)]
150#[value_info(other_values = "none")]
151pub struct GenericLineClamp<I>(pub I);
152
153pub use self::GenericLineClamp as LineClamp;
154
155impl<I: crate::Zero> LineClamp<I> {
156    /// Returns the `none` value.
157    pub fn none() -> Self {
158        Self(crate::Zero::zero())
159    }
160
161    /// Returns whether we're the `none` value.
162    pub fn is_none(&self) -> bool {
163        self.0.is_zero()
164    }
165}
166
167impl<I: crate::Zero + ToCss> ToCss for LineClamp<I> {
168    fn to_css<W>(&self, dest: &mut CssWriter<W>) -> fmt::Result
169    where
170        W: Write,
171    {
172        if self.is_none() {
173            return dest.write_str("none");
174        }
175        self.0.to_css(dest)
176    }
177}
178
179/// A generic value for the `perspective` property.
180#[derive(
181    Animate,
182    Clone,
183    ComputeSquaredDistance,
184    Copy,
185    Debug,
186    MallocSizeOf,
187    Parse,
188    PartialEq,
189    SpecifiedValueInfo,
190    ToAnimatedValue,
191    ToAnimatedZero,
192    ToComputedValue,
193    ToCss,
194    ToResolvedValue,
195    ToShmem,
196)]
197#[repr(C, u8)]
198pub enum GenericPerspective<NonNegativeLength> {
199    /// A non-negative length.
200    Length(NonNegativeLength),
201    /// The keyword `none`.
202    None,
203}
204
205pub use self::GenericPerspective as Perspective;
206
207impl<L> Perspective<L> {
208    /// Returns `none`.
209    #[inline]
210    pub fn none() -> Self {
211        Perspective::None
212    }
213}
214
215#[derive(
216    Clone,
217    Copy,
218    Debug,
219    MallocSizeOf,
220    Parse,
221    PartialEq,
222    SpecifiedValueInfo,
223    ToComputedValue,
224    ToCss,
225    ToResolvedValue,
226    ToShmem,
227)]
228#[repr(u8)]
229#[allow(missing_docs)]
230pub enum PositionProperty {
231    Static = 0,
232    Relative,
233    Absolute,
234    Fixed,
235    Sticky,
236}
237
238impl PositionProperty {
239    /// Is the box absolutely positioned?
240    pub fn is_absolutely_positioned(self) -> bool {
241        matches!(self, Self::Absolute | Self::Fixed)
242    }
243}