swc_css_minifier/compressor/
easing_function.rs

1use swc_atoms::atom;
2use swc_css_ast::*;
3
4use super::Compressor;
5
6impl Compressor {
7    pub(super) fn compress_easing_function(&mut self, component_value: &mut ComponentValue) {
8        match component_value {
9            ComponentValue::Function(function)
10                if function.name == "cubic-bezier" && function.value.len() == 7 =>
11            {
12                if let (first, second, third, ComponentValue::Integer(fourth)) = (
13                    &function.value[0],
14                    &function.value[2],
15                    &function.value[4],
16                    &function.value[6],
17                ) {
18                    if matches!(first, ComponentValue::Integer(integer) if integer.value == 0)
19                        && matches!(second, ComponentValue::Integer(integer) if integer.value == 0)
20                        && matches!(third, ComponentValue::Integer(integer) if integer.value == 1)
21                        && fourth.value == 1
22                    {
23                        *component_value = ComponentValue::Ident(Box::new(Ident {
24                            span: function.span,
25                            value: atom!("linear"),
26                            raw: None,
27                        }))
28                    } else if matches!(first, ComponentValue::Number(number) if number.value == 0.25)
29                        && matches!(second, ComponentValue::Number(number) if number.value == 0.1)
30                        && matches!(third, ComponentValue::Number(number) if number.value == 0.25)
31                        && fourth.value == 1
32                    {
33                        *component_value = ComponentValue::Ident(Box::new(Ident {
34                            span: function.span,
35                            value: atom!("ease"),
36                            raw: None,
37                        }))
38                    } else if matches!(first, ComponentValue::Number(number) if number.value == 0.42)
39                        && matches!(second, ComponentValue::Integer(integer) if integer.value == 0)
40                        && matches!(third, ComponentValue::Integer(integer) if integer.value == 1)
41                        && fourth.value == 1
42                    {
43                        *component_value = ComponentValue::Ident(Box::new(Ident {
44                            span: function.span,
45                            value: atom!("ease-in"),
46                            raw: None,
47                        }))
48                    } else if matches!(first, ComponentValue::Integer(integer) if integer.value == 0)
49                        && matches!(second, ComponentValue::Integer(integer) if integer.value == 0)
50                        && matches!(third, ComponentValue::Number(number) if number.value == 0.58)
51                        && fourth.value == 1
52                    {
53                        *component_value = ComponentValue::Ident(Box::new(Ident {
54                            span: function.span,
55                            value: atom!("ease-out"),
56                            raw: None,
57                        }))
58                    } else if matches!(first, ComponentValue::Number(number) if number.value == 0.42)
59                        && matches!(second, ComponentValue::Integer(integer) if integer.value == 0)
60                        && matches!(third, ComponentValue::Number(number) if number.value == 0.58)
61                        && fourth.value == 1
62                    {
63                        *component_value = ComponentValue::Ident(Box::new(Ident {
64                            span: function.span,
65                            value: atom!("ease-in-out"),
66                            raw: None,
67                        }))
68                    }
69                }
70            }
71            ComponentValue::Function(function)
72                if function.name == "steps" && function.value.len() == 3 =>
73            {
74                match (&function.value[0], &function.value[2]) {
75                    (ComponentValue::Integer(integer), ComponentValue::Ident(ident))
76                        if integer.value == 1 =>
77                    {
78                        match &*ident.value.to_ascii_lowercase() {
79                            "start" | "jump-start" => {
80                                *component_value = ComponentValue::Ident(Box::new(Ident {
81                                    span: function.span,
82                                    value: atom!("step-start"),
83                                    raw: None,
84                                }))
85                            }
86                            "end" | "jump-end" => {
87                                *component_value = ComponentValue::Ident(Box::new(Ident {
88                                    span: function.span,
89                                    value: atom!("step-end"),
90                                    raw: None,
91                                }))
92                            }
93                            _ => {}
94                        }
95                    }
96                    (ComponentValue::Integer(..), ComponentValue::Ident(ident))
97                        if ident.value.eq_ignore_ascii_case("jump-start") =>
98                    {
99                        function.value[2] = ComponentValue::Ident(Box::new(Ident {
100                            span: function.span,
101                            value: atom!("start"),
102                            raw: None,
103                        }))
104                    }
105                    (ComponentValue::Integer(number), ComponentValue::Ident(ident)) => {
106                        match &*ident.value.to_ascii_lowercase() {
107                            "end" | "jump-end" => {
108                                function.value = vec![ComponentValue::Integer(number.clone())];
109                            }
110                            _ => {}
111                        }
112                    }
113                    _ => {}
114                }
115            }
116            _ => {}
117        }
118    }
119}