swc_css_minifier/compressor/
transform_function.rs

1use swc_atoms::atom;
2use swc_common::Spanned;
3use swc_css_ast::*;
4
5use super::Compressor;
6
7impl Compressor {
8    pub(super) fn compress_transform_function(&self, component_value: &mut ComponentValue) {
9        match component_value {
10            ComponentValue::Function(function)
11                if function.name == "translate" && function.value.len() == 3 =>
12            {
13                match (function.value.first(), function.value.get(2)) {
14                    (Some(first), Some(ComponentValue::Integer(second))) if second.value == 0 => {
15                        function.value = vec![first.clone()];
16                    }
17                    (Some(ComponentValue::Integer(first)), Some(second)) if first.value == 0 => {
18                        function.name = FunctionName::Ident(Ident {
19                            span: function.name.span(),
20                            value: atom!("translatey"),
21                            raw: None,
22                        });
23                        function.value = vec![second.clone()];
24                    }
25                    _ => {}
26                }
27            }
28            ComponentValue::Function(function)
29                if function.name == "translate3d" && function.value.len() == 5 =>
30            {
31                match (
32                    function.value.first(),
33                    function.value.get(2),
34                    function.value.get(4),
35                ) {
36                    (
37                        Some(ComponentValue::Integer(first)),
38                        Some(ComponentValue::Integer(second)),
39                        Some(third),
40                    ) if first.value == 0 && second.value == 0 => {
41                        function.name = FunctionName::Ident(Ident {
42                            span: function.name.span(),
43                            value: atom!("translatez"),
44                            raw: None,
45                        });
46                        function.value = vec![third.clone()];
47                    }
48                    _ => {}
49                }
50            }
51            ComponentValue::Function(function)
52                if function.name == "scale" && function.value.len() == 3 =>
53            {
54                match (function.value.first(), function.value.get(2)) {
55                    (
56                        Some(first @ ComponentValue::Integer(first_number)),
57                        Some(ComponentValue::Integer(second_number)),
58                    ) if first_number.value == second_number.value => {
59                        function.value = vec![first.clone()];
60                    }
61                    (Some(first), Some(ComponentValue::Integer(second_number)))
62                        if second_number.value == 1 =>
63                    {
64                        function.name = FunctionName::Ident(Ident {
65                            span: function.name.span(),
66                            value: atom!("scalex"),
67                            raw: None,
68                        });
69                        function.value = vec![first.clone()];
70                    }
71                    (Some(ComponentValue::Integer(first_number)), Some(second))
72                        if first_number.value == 1 =>
73                    {
74                        function.name = FunctionName::Ident(Ident {
75                            span: function.name.span(),
76                            value: atom!("scaley"),
77                            raw: None,
78                        });
79                        function.value = vec![second.clone()];
80                    }
81                    _ => {}
82                }
83            }
84            ComponentValue::Function(function)
85                if function.name == "scale3d" && function.value.len() == 5 =>
86            {
87                match (
88                    function.value.first(),
89                    function.value.get(2),
90                    function.value.get(4),
91                ) {
92                    (
93                        Some(first),
94                        Some(ComponentValue::Integer(second_number)),
95                        Some(ComponentValue::Integer(third_number)),
96                    ) if second_number.value == 1 && third_number.value == 1 => {
97                        function.name = FunctionName::Ident(Ident {
98                            span: function.name.span(),
99                            value: atom!("scalex"),
100                            raw: None,
101                        });
102                        function.value = vec![first.clone()];
103                    }
104                    (
105                        Some(ComponentValue::Integer(first_number)),
106                        Some(second),
107                        Some(ComponentValue::Integer(third_number)),
108                    ) if first_number.value == 1 && third_number.value == 1 => {
109                        function.name = FunctionName::Ident(Ident {
110                            span: function.name.span(),
111                            value: atom!("scaley"),
112                            raw: None,
113                        });
114                        function.value = vec![second.clone()];
115                    }
116                    (
117                        Some(ComponentValue::Integer(first_number)),
118                        Some(ComponentValue::Integer(second_number)),
119                        Some(third),
120                    ) if first_number.value == 1 && second_number.value == 1 => {
121                        function.name = FunctionName::Ident(Ident {
122                            span: function.name.span(),
123                            value: atom!("scalez"),
124                            raw: None,
125                        });
126                        function.value = vec![third.clone()];
127                    }
128                    _ => {}
129                }
130            }
131            ComponentValue::Function(function)
132                if function.name == "matrix3d" && function.value.len() == 31 =>
133            {
134                match (
135                    function.value.first(),
136                    function.value.get(1),
137                    function.value.get(2),
138                    function.value.get(3),
139                    function.value.get(4),
140                    function.value.get(6),
141                    function.value.get(8),
142                    function.value.get(9),
143                    function.value.get(10),
144                    function.value.get(11),
145                    function.value.get(12),
146                    function.value.get(14),
147                    function.value.get(16),
148                    function.value.get(18),
149                    function.value.get(20),
150                    function.value.get(22),
151                    function.value.get(24),
152                    function.value.get(25),
153                    function.value.get(26),
154                    function.value.get(28),
155                    function.value.get(30),
156                ) {
157                    (
158                        Some(first),
159                        Some(first_comma),
160                        Some(second),
161                        Some(second_comma),
162                        Some(ComponentValue::Integer(third_number)),
163                        Some(ComponentValue::Integer(fourth_number)),
164                        Some(fifth),
165                        Some(fifth_comma),
166                        Some(sixth),
167                        Some(sixth_comma),
168                        Some(ComponentValue::Integer(seventh_number)),
169                        Some(ComponentValue::Integer(eighth_number)),
170                        Some(ComponentValue::Integer(ninth_number)),
171                        Some(ComponentValue::Integer(tenth_number)),
172                        Some(ComponentValue::Integer(eleventh_number)),
173                        Some(ComponentValue::Integer(twelfth_number)),
174                        Some(thirteenth),
175                        Some(thirteenth_comma),
176                        Some(fourteenth),
177                        Some(ComponentValue::Integer(fifteenth_number)),
178                        Some(ComponentValue::Integer(sixteenth_number)),
179                    ) if third_number.value == 0
180                        && fourth_number.value == 0
181                        && seventh_number.value == 0
182                        && eighth_number.value == 0
183                        && ninth_number.value == 0
184                        && tenth_number.value == 0
185                        && eleventh_number.value == 1
186                        && twelfth_number.value == 0
187                        && fifteenth_number.value == 0
188                        && sixteenth_number.value == 1 =>
189                    {
190                        function.name = FunctionName::Ident(Ident {
191                            span: function.name.span(),
192                            value: atom!("matrix"),
193                            raw: None,
194                        });
195                        function.value = vec![
196                            first.clone(),
197                            first_comma.clone(),
198                            second.clone(),
199                            second_comma.clone(),
200                            fifth.clone(),
201                            fifth_comma.clone(),
202                            sixth.clone(),
203                            sixth_comma.clone(),
204                            thirteenth.clone(),
205                            thirteenth_comma.clone(),
206                            fourteenth.clone(),
207                        ];
208                    }
209                    _ => {}
210                }
211            }
212            ComponentValue::Function(function)
213                if function.name == "rotate3d" && function.value.len() == 7 =>
214            {
215                match (
216                    function.value.first(),
217                    function.value.get(2),
218                    function.value.get(4),
219                    function.value.get(6),
220                ) {
221                    (
222                        Some(ComponentValue::Integer(first_number)),
223                        Some(ComponentValue::Integer(second_number)),
224                        Some(ComponentValue::Integer(third_number)),
225                        Some(fourth_value),
226                    ) if first_number.value == 1
227                        && second_number.value == 0
228                        && third_number.value == 0 =>
229                    {
230                        function.name = FunctionName::Ident(Ident {
231                            span: function.name.span(),
232                            value: atom!("rotatex"),
233                            raw: None,
234                        });
235                        function.value = vec![fourth_value.clone()];
236                    }
237                    (
238                        Some(ComponentValue::Integer(first_number)),
239                        Some(ComponentValue::Integer(second_number)),
240                        Some(ComponentValue::Integer(third_number)),
241                        Some(fourth_value),
242                    ) if first_number.value == 0
243                        && second_number.value == 1
244                        && third_number.value == 0 =>
245                    {
246                        function.name = FunctionName::Ident(Ident {
247                            span: function.name.span(),
248                            value: atom!("rotatey"),
249                            raw: None,
250                        });
251                        function.value = vec![fourth_value.clone()];
252                    }
253                    (
254                        Some(ComponentValue::Integer(first_number)),
255                        Some(ComponentValue::Integer(second_number)),
256                        Some(ComponentValue::Integer(third_number)),
257                        Some(fourth_value),
258                    ) if first_number.value == 0
259                        && second_number.value == 0
260                        && third_number.value == 1 =>
261                    {
262                        function.name = FunctionName::Ident(Ident {
263                            span: function.name.span(),
264                            value: atom!("rotate"),
265                            raw: None,
266                        });
267                        function.value = vec![fourth_value.clone()];
268                    }
269                    _ => {}
270                }
271            }
272            ComponentValue::Function(function)
273                if function.name == "rotatez" && function.value.len() == 1 =>
274            {
275                function.name = FunctionName::Ident(Ident {
276                    span: function.name.span(),
277                    value: atom!("rotate"),
278                    raw: None,
279                });
280            }
281
282            ComponentValue::Function(function)
283                if function.name == "skew" && function.value.len() == 3 =>
284            {
285                match (function.value.first(), function.value.get(2)) {
286                    (Some(first), Some(ComponentValue::Integer(second_number)))
287                        if second_number.value == 0 =>
288                    {
289                        function.name = FunctionName::Ident(Ident {
290                            span: function.name.span(),
291                            value: atom!("skewx"),
292                            raw: None,
293                        });
294                        function.value = vec![first.clone()];
295                    }
296
297                    (Some(ComponentValue::Integer(first_number)), Some(second))
298                        if first_number.value == 0 =>
299                    {
300                        function.name = FunctionName::Ident(Ident {
301                            span: function.name.span(),
302                            value: atom!("skewy"),
303                            raw: None,
304                        });
305                        function.value = vec![second.clone()];
306                    }
307                    _ => {}
308                }
309            }
310            _ => {}
311        }
312    }
313}