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