1#[cfg(test)]
2mod normalizers {
3 use swc_core::{
4 common::{BytePos, DUMMY_SP, Span},
5 css::parser::error::{Error, ErrorKind},
6 };
7
8 use crate::shared::utils::css::common::{stringify, swc_parse_css};
9 use crate::shared::utils::css::normalizers::base::base_normalizer;
10 use stylex_css::css::normalizers::whitespace_normalizer::whitespace_normalizer;
11
12 #[test]
13 fn should_normalize_transition_property() {
14 let (stylesheet, errors) = swc_parse_css("* {{ transitionProperty: opacity, margin-top; }}");
15
16 assert_eq!(
17 stringify(&base_normalizer(
18 stylesheet.unwrap(),
19 false,
20 Some("transitionProperty")
21 )),
22 "*{{transitionproperty:opacity,margin-top}}"
23 );
24
25 assert_eq!(
26 errors,
27 vec![Error::new(DUMMY_SP, ErrorKind::InvalidSelector)]
28 );
29 }
30
31 #[test]
32 fn should_normalize_box_shadow() {
33 let (stylesheet, errors) = swc_parse_css("* {{ boxShadow: 0px 2px 4px var(--shadow-1); }}");
34
35 assert_eq!(
36 stringify(&base_normalizer(
37 stylesheet.unwrap(),
38 false,
39 Some("boxShadow")
40 )),
41 "*{{boxshadow:0 2px 4px var(--shadow-1)}}"
42 );
43
44 assert_eq!(
45 errors,
46 vec![Error::new(DUMMY_SP, ErrorKind::InvalidSelector)]
47 );
48
49 let (stylesheet2, errors2) = swc_parse_css("* {{ boxShadow: 1px 1px #000; }}");
50
51 assert_eq!(
52 stringify(&base_normalizer(
53 stylesheet2.unwrap(),
54 false,
55 Some("boxShadow")
56 )),
57 "*{{boxshadow:1px 1px#000}}"
58 );
59
60 assert_eq!(
61 errors2,
62 vec![Error::new(DUMMY_SP, ErrorKind::InvalidSelector)]
63 );
64 }
65
66 #[test]
67 fn should_normalize_opacity() {
68 let (stylesheet, errors) = swc_parse_css("* {{ opacity: 0.5; }}");
69
70 assert_eq!(
71 stringify(&base_normalizer(
72 stylesheet.unwrap(),
73 false,
74 Some("opacity")
75 )),
76 "*{{opacity:.5}}"
77 );
78
79 assert_eq!(
80 errors,
81 vec![Error::new(DUMMY_SP, ErrorKind::InvalidSelector)]
82 );
83 }
84
85 #[test]
86 fn should_normalize_transition_duration() {
87 let (stylesheet, errors) = swc_parse_css("* {{ transitionDuration: 500ms; }}");
88
89 assert_eq!(
90 stringify(&base_normalizer(
91 stylesheet.unwrap(),
92 false,
93 Some("transitionDuration")
94 )),
95 "*{{transitionduration:.5s}}"
96 );
97
98 assert_eq!(
99 errors,
100 vec![Error::new(DUMMY_SP, ErrorKind::InvalidSelector)]
101 );
102 }
103
104 #[test]
105 fn should_normalize_quotes() {
106 let (stylesheet, errors) = swc_parse_css(r#"* {{ quotes: '""'; }}"#);
107
108 assert_eq!(
109 stringify(&base_normalizer(stylesheet.unwrap(), false, Some("quotes"))),
110 r#"*{{quotes:""}}"#
111 );
112
113 assert_eq!(
114 errors,
115 vec![Error::new(DUMMY_SP, ErrorKind::InvalidSelector)]
116 );
117
118 let (stylesheet2, errors2) = swc_parse_css(r#"* {{ quotes: '"123"'; }}"#);
119
120 assert_eq!(
121 stringify(&base_normalizer(
122 stylesheet2.unwrap(),
123 false,
124 Some("quotes")
125 )),
126 r#"*{{quotes:"123"}}"#
127 );
128
129 assert_eq!(
130 errors2,
131 vec![Error::new(DUMMY_SP, ErrorKind::InvalidSelector)]
132 );
133 }
134
135 #[test]
136 fn should_normalize_grid_template_areas() {
137 let (stylesheet, errors) = swc_parse_css(r#"* {{ gridTemplateAreas: '"content"'; }}"#);
138
139 assert_eq!(
140 stringify(&base_normalizer(
141 stylesheet.unwrap(),
142 false,
143 Some("gridTemplateAreas")
144 )),
145 r#"*{{gridtemplateareas:"content"}}"#
146 );
147
148 assert_eq!(
149 errors,
150 vec![Error::new(DUMMY_SP, ErrorKind::InvalidSelector)]
151 );
152
153 let (stylesheet2, errors2) =
154 swc_parse_css(r#"* {{ gridTemplateAreas: '"content" "sidebar"'; }}"#);
155
156 assert_eq!(
157 stringify(&base_normalizer(
158 stylesheet2.unwrap(),
159 false,
160 Some("gridTemplateAreas")
161 )),
162 r#"*{{gridtemplateareas:"content" "sidebar"}}"#
163 );
164
165 assert_eq!(
166 errors2,
167 vec![Error::new(DUMMY_SP, ErrorKind::InvalidSelector)]
168 );
169 }
170
171 #[test]
172 fn should_normalize_color_oklch() {
173 let (stylesheet, errors) =
174 swc_parse_css("* {{ color: oklch(from var(--xs74gcj) l c h / 0.5) }}");
175
176 assert_eq!(
177 stringify(&base_normalizer(stylesheet.unwrap(), false, Some("color"))),
178 "*{{color:oklch(from var(--xs74gcj) l c h / 0.5)}}"
179 );
180
181 assert_eq!(
184 errors,
185 vec![
186 Error::new(DUMMY_SP, ErrorKind::InvalidSelector),
187 Error::new(
188 Span::new(BytePos(38), BytePos(39)),
189 ErrorKind::Expected("'none' value of an ident token")
190 )
191 ]
192 );
193 }
194
195 #[test]
196 fn should_normalize_color_oklab() {
197 let (stylesheet, errors) = swc_parse_css(r#"* {{ color: oklab(40.101% 0.1147 0.0453) }}"#);
198
199 assert_eq!(
200 whitespace_normalizer(stringify(&base_normalizer(
201 stylesheet.unwrap(),
202 false,
203 Some("color")
204 ))),
205 r#"oklab(40.101% .1147 .0453)"#
206 );
207
208 assert_eq!(
209 errors,
210 vec![Error::new(DUMMY_SP, ErrorKind::InvalidSelector)]
211 );
212
213 let (stylesheet2, errors2) = swc_parse_css(r#"* {{ color: var(--a) var(--b) var(--c) }}"#);
214
215 assert_eq!(
216 stringify(&base_normalizer(stylesheet2.unwrap(), false, Some("color"))),
217 r#"*{{color:var(--a)var(--b)var(--c)}}"#
218 );
219
220 assert_eq!(
221 errors2,
222 vec![Error::new(DUMMY_SP, ErrorKind::InvalidSelector)]
223 );
224
225 let (stylesheet3, errors3) =
226 swc_parse_css("* {{ color: oklab(from #0000FF calc(l + 0.1) a b / calc(alpha * 0.9)) }}");
227
228 assert_eq!(
229 stringify(&base_normalizer(stylesheet3.unwrap(), false, Some("color"))),
230 "*{{color:oklab(from #0000FF calc(l + 0.1) a b / calc(alpha * 0.9))}}"
231 );
232
233 assert_eq!(
236 errors3,
237 vec![
238 Error::new(DUMMY_SP, ErrorKind::InvalidSelector),
239 Error::new(
240 Span::new(BytePos(36), BytePos(37)),
241 ErrorKind::Expected("'e', 'pi', 'infinity', '-infinity' or 'NaN', ident tokens")
242 ),
243 Error::new(
244 Span::new(BytePos(45), BytePos(46)),
245 ErrorKind::Expected("'none' value of an ident token")
246 )
247 ]
248 );
249
250 let (stylesheet4, errors4) =
251 swc_parse_css("* {{ color: oklab(from hsl(180 100% 50%) calc(l - 0.1) a b) }}");
252
253 assert_eq!(
254 stringify(&base_normalizer(stylesheet4.unwrap(), false, Some("color"))),
255 "*{{color:oklab(from hsl(180 100% 50%) calc(l - 0.1) a b)}}"
256 );
257
258 assert_eq!(
261 errors4,
262 vec![
263 Error::new(DUMMY_SP, ErrorKind::InvalidSelector),
264 Error::new(
265 Span::new(BytePos(46), BytePos(47)),
266 ErrorKind::Expected("'e', 'pi', 'infinity', '-infinity' or 'NaN', ident tokens")
267 ),
268 Error::new(
269 Span::new(BytePos(55), BytePos(56)),
270 ErrorKind::Expected("'none' value of an ident token")
271 )
272 ]
273 );
274
275 let (stylesheet5, errors5) = swc_parse_css("* {{ color: oklab(from green l a b / 0.5) }}");
276
277 assert_eq!(
278 stringify(&base_normalizer(stylesheet5.unwrap(), false, Some("color"))),
279 "*{{color:oklab(from green l a b / 0.5)}}"
280 );
281
282 assert_eq!(
285 errors5,
286 vec![
287 Error::new(DUMMY_SP, ErrorKind::InvalidSelector),
288 Error::new(
289 Span::new(BytePos(29), BytePos(30)),
290 ErrorKind::Expected("'none' value of an ident token")
291 )
292 ]
293 );
294 }
295
296 #[test]
297 fn should_normalize_color_clamp() {
298 let (stylesheet, errors) = swc_parse_css(r#"* {{ color: clamp(200px, 40%, 400px) }}"#);
299
300 assert_eq!(
301 whitespace_normalizer(stringify(&base_normalizer(
302 stylesheet.unwrap(),
303 false,
304 Some("color")
305 ))),
306 r#"clamp(200px,40%,400px)"#
307 );
308
309 assert_eq!(
310 errors,
311 vec![Error::new(DUMMY_SP, ErrorKind::InvalidSelector)]
312 );
313
314 let (stylesheet2, errors2) = swc_parse_css(
315 r#"* {{ color: clamp(min(10vw, 20rem), 300px, max(90vw, 55rem)) }}"#,
316 );
317
318 assert_eq!(
319 stringify(&base_normalizer(stylesheet2.unwrap(), false, Some("color"))),
320 r#"*{{color:clamp(min(10vw,20rem),300px,max(90vw,55rem))}}"#
321 );
322
323 assert_eq!(
324 errors2,
325 vec![Error::new(DUMMY_SP, ErrorKind::InvalidSelector)]
326 );
327
328 let (stylesheet3, errors3) = swc_parse_css(
329 "* {{ color: clamp(0, (var(--l-threshold, 0.623) / l - 1) * infinity, 1) }}",
330 );
331
332 assert_eq!(
333 stringify(&base_normalizer(stylesheet3.unwrap(), false, Some("color"))),
334 "*{{color:clamp(0, (var(--l-threshold, 0.623) / l - 1) * infinity, 1)}}"
335 );
336
337 assert_eq!(
340 errors3,
341 vec![
342 Error::new(DUMMY_SP, ErrorKind::InvalidSelector),
343 Error::new(
344 Span::new(BytePos(53), BytePos(54)),
345 ErrorKind::Expected("'e', 'pi', 'infinity', '-infinity' or 'NaN', ident tokens")
346 ),
347 ]
348 );
349 }
350
351 #[test]
352 fn should_normalize_function_arg_dimensions() {
353 let (stylesheet, errors) = swc_parse_css("* {{ color: calc(0 - var(--someVar)) }}");
354
355 assert_eq!(
356 stringify(&base_normalizer(stylesheet.unwrap(), false, Some("color"))),
357 "*{{color:calc(0 - var(--someVar))}}"
358 );
359
360 assert_eq!(
361 errors,
362 vec![Error::new(DUMMY_SP, ErrorKind::InvalidSelector)]
363 );
364
365 let (stylesheet2, errors2) = swc_parse_css("* {{ color: calc(0px - var(--someVar) + 10px) }}");
366
367 assert_eq!(
368 stringify(&base_normalizer(stylesheet2.unwrap(), false, Some("color"))),
369 "*{{color:calc(0px - var(--someVar) + 10px)}}"
370 );
371
372 assert_eq!(
373 errors2,
374 vec![Error::new(DUMMY_SP, ErrorKind::InvalidSelector)]
375 );
376
377 let (stylesheet, errors) = swc_parse_css("* {{ grid-column-start: -1 }}");
378
379 assert_eq!(
380 stringify(&base_normalizer(
381 stylesheet.unwrap(),
382 false,
383 Some("grid-column-start")
384 )),
385 "*{{grid-column-start:-1}}"
386 );
387
388 assert_eq!(
389 errors,
390 vec![Error::new(DUMMY_SP, ErrorKind::InvalidSelector)]
391 );
392 }
393}