Skip to main content

whisker_css/keyword/
transform.rs

1//! Transform-related keyword enums.
2//!
3//! References:
4//! - <https://lynxjs.org/api/css/properties/transform-box>
5//! - <https://lynxjs.org/api/css/properties/transform-style>
6//! - <https://lynxjs.org/api/css/properties/backface-visibility>
7
8use core::fmt;
9
10use crate::to_css::ToCss;
11
12/// The `transform-box` keyword.
13#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
14pub enum TransformBox {
15    /// `content-box` — reference box is the content box.
16    ContentBox,
17    /// `border-box` — reference box is the border box.
18    BorderBox,
19    /// `fill-box` — reference box is the object's bounding box.
20    FillBox,
21    /// `stroke-box` — reference box is the stroke bounding box.
22    StrokeBox,
23    /// `view-box` — reference box is the viewport.
24    ViewBox,
25}
26
27impl ToCss for TransformBox {
28    fn to_css(&self, dest: &mut dyn fmt::Write) -> fmt::Result {
29        dest.write_str(match self {
30            TransformBox::ContentBox => "content-box",
31            TransformBox::BorderBox => "border-box",
32            TransformBox::FillBox => "fill-box",
33            TransformBox::StrokeBox => "stroke-box",
34            TransformBox::ViewBox => "view-box",
35        })
36    }
37}
38
39/// The `transform-style` keyword. Determines whether children of a
40/// 3-D-transformed element live in the same 3-D rendering context.
41#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
42pub enum TransformStyle {
43    /// `flat` — children are flattened into the element's plane. Default.
44    Flat,
45    /// `preserve-3d` — children live in 3-D space.
46    Preserve3d,
47}
48
49impl ToCss for TransformStyle {
50    fn to_css(&self, dest: &mut dyn fmt::Write) -> fmt::Result {
51        dest.write_str(match self {
52            TransformStyle::Flat => "flat",
53            TransformStyle::Preserve3d => "preserve-3d",
54        })
55    }
56}
57
58/// The `backface-visibility` keyword. Controls whether the back face
59/// of a 3-D-transformed element is visible when facing the viewer.
60#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
61pub enum BackfaceVisibility {
62    /// `visible` — back face is rendered. Default.
63    Visible,
64    /// `hidden` — back face is not rendered.
65    Hidden,
66}
67
68impl ToCss for BackfaceVisibility {
69    fn to_css(&self, dest: &mut dyn fmt::Write) -> fmt::Result {
70        dest.write_str(match self {
71            BackfaceVisibility::Visible => "visible",
72            BackfaceVisibility::Hidden => "hidden",
73        })
74    }
75}
76
77#[cfg(test)]
78mod tests {
79    use super::*;
80
81    #[test]
82    fn transform_box_all() {
83        let cases = [
84            (TransformBox::ContentBox, "content-box"),
85            (TransformBox::BorderBox, "border-box"),
86            (TransformBox::FillBox, "fill-box"),
87            (TransformBox::StrokeBox, "stroke-box"),
88            (TransformBox::ViewBox, "view-box"),
89        ];
90        for (k, expected) in cases {
91            assert_eq!(k.to_css_string(), expected);
92        }
93    }
94
95    #[test]
96    fn transform_style_all() {
97        assert_eq!(TransformStyle::Flat.to_css_string(), "flat");
98        assert_eq!(TransformStyle::Preserve3d.to_css_string(), "preserve-3d");
99    }
100
101    #[test]
102    fn backface_visibility_all() {
103        assert_eq!(BackfaceVisibility::Visible.to_css_string(), "visible");
104        assert_eq!(BackfaceVisibility::Hidden.to_css_string(), "hidden");
105    }
106}