File: minify.t

package info (click to toggle)
libcss-minifier-xs-perl 0.13-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 216 kB
  • sloc: perl: 630; makefile: 3
file content (503 lines) | stat: -rw-r--r-- 16,133 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
#!/usr/bin/perl

use strict;
use warnings;
use if $ENV{AUTOMATED_TESTING}, 'Test::DiagINC'; use Test::More;
use CSS::Minifier::XS qw(minify);

###############################################################################
# Removal of leading whitespace, regardless of "space", "tab", "CR", "LF".
subtest 'leading whitespace can be removed' => sub {
  my $given  = "\n\n\r\t\n    \n    h1 { }";
  my $expect = 'h1{}';
  my $got    = minify($given);
  is $got, $expect;
};

###############################################################################
# Removal of trailing whitespace, regardless of "space", "tab", "CR", "LF".
subtest 'trailing whitespace can be removed' => sub {
  my $given  = "h1 { }  \t\n\r\n";
  my $expect = 'h1{}';
  my $got    = minify($given);
  is $got, $expect;
};

###############################################################################
# Whitespace that trails a ")" may be preserved.
subtest 'whitespace trailing a ")"' => sub {
  subtest 'end of statement; removed' => sub {
    my $given  = 'div { background: url(foo.gif) ; }';
    my $expect = 'div{background:url(foo.gif)}';
    my $got    = minify($given);
    is $got, $expect;
  };

  subtest 'inside of statement; preserved' => sub {
    my $given  = 'div { background: url(foo.gif) no-repeat; }';
    my $expect = 'div{background:url(foo.gif) no-repeat}';
    my $got    = minify($given);
    is $got, $expect;
  };
};

###############################################################################
# Removal of trailing semi-colons, at end of a group.
subtest 'trailing semi-colons' => sub {
  subtest 'at end of group; removed' => sub {
    my $given  = 'h1 { font-weight: bold; }';
    my $expect = 'h1{font-weight:bold}';
    my $got    = minify($given);
    is $got, $expect;
  };

  subtest 'inside of group; preserved' => sub {
    my $given  = 'h1 { font-weight: bold; color: red;}';
    my $expect = 'h1{font-weight:bold;color:red}';
    my $got    = minify($given);
    is $got, $expect;
  };
};

###############################################################################
# Comments get minified and removed, UNLESS they contain the word
# "copyright" in them.
subtest 'comments' => sub {
  subtest 'block comment removed' => sub {
    my $given  = '/* comment */ h1 { background: green }';
    my $expect = 'h1{background:green}';
    my $got    = minify($given);
    is $got, $expect;
  };

  subtest 'copyright comment remains' => sub {
    my $given  = '/* comment with copyright */ h1 { color: red }';
    my $expect = '/* comment with copyright */h1{color:red}';
    my $got    = minify($given);
    is $got, $expect;
  };

  subtest 'copyright is case in-sensitive' => sub {
    my $given  = '/* comment with CoPyRiGhT */ h1 { color: red }';
    my $expect = '/* comment with CoPyRiGhT */h1{color:red}';
    my $got    = minify($given);
    is $got, $expect;
  };
};

###############################################################################
# Comments with the "Mac/IE Comment Hack" are preserved, but minified.
subtest 'mac/ie comment hack' => sub {
  subtest 'comment hack is minified' => sub {
    my $given  = '/* start \*/ ul { color: red } /* end */';
    my $expect = '/*\*/ul{color:red}/**/';
    my $got    = minify($given);
    is $got, $expect;
  };

  subtest 'inner hacks are removed' => sub {
    my $given  = '/* start \*/ ul { /* inner \*/ color: red } /* end */';
    my $expect = '/*\*/ul{color:red}/**/';
    my $got    = minify($given);
    is $got, $expect;
  };
};

###############################################################################
# "!important" declarations can have preceeding whitespace removed.
subtest '!important' => sub {
  my $given  = 'a { box-shadow: none !important; }';
  my $expect = 'a{box-shadow:none!important}';
  my $got    = minify($given);
  is $got, $expect;
};

###############################################################################
# CSS Selector Combinators get whitespace removed
subtest 'css selector combinators' => sub {
  subtest 'child-of' => sub {
    my $given  = 'h1 > p { background: green }';
    my $expect = 'h1>p{background:green}';
    my $got    = minify($given);
    is $got, $expect;
  };

  subtest 'adjacent-sibling' => sub {
    # adjacent siblings don't get whitespace removed, as the "+" could also
    # be seen as a mathematical operator
    my $given  = 'h1 + p { background: green }';
    my $expect = 'h1 + p{background:green}';
    my $got    = minify($given);
    is $got, $expect;
  };

  subtest 'general-sibling' => sub {
    my $given  = 'h1 ~ p { background: green }';
    my $expect = 'h1~p{background:green}';
    my $got    = minify($given);
    is $got, $expect;
  };
};

###############################################################################
# CSS pseudo-selectors get leading whitespace preserved
# - which (unfortunately) means that "whitespace before a ':' is significant",
#   as otherwise we need to truly understand the context that we are in when
#   we see a ":" to determine if we can eliminate the whitespace or not
subtest 'pseudo-selectors' => sub {
  my $given  = '#test :link { color: red; }';
  my $expect = '#test :link{color:red}';
  my $got    = minify($given);
  is $got, $expect;
};

###############################################################################
# Media selectors require "(" to retain leading whitespace, which is
# minified.
subtest 'media selectors' => sub {
  subtest 'whitespace is preserved' => sub {
    my $given  = '@media all and (max-width: 100px) { }';
    my $expect = '@media all and (max-width:100px){}';
    my $got    = minify($given);
    is $got, $expect;
  };

  subtest 'whitespace is minified' => sub {
    my $given  = '@media all and       (max-width: 100px) { }';
    my $expect = '@media all and (max-width:100px){}';
    my $got    = minify($given);
    is $got, $expect;
  };
};

###############################################################################
# "zero" can be expressed without a unit, in *MOST* cases.
subtest 'zero without units' => sub {
  subtest 'px' => sub {
    my $given  = 'p { width: 0px }';
    my $expect = 'p{width:0}';
    my $got    = minify($given);
    is $got, $expect;
  };

  subtest 'no units' => sub {
    my $given  = 'p { width: 0 }';
    my $expect = 'p{width:0}';
    my $got    = minify($given);
    is $got, $expect;
  };

  # Percent is special, and may need to be preserved for CSS animations
  subtest 'percent' => sub {
    my $given  = 'p { width: 0% }';
    my $expect = 'p{width:0%}';
    my $got    = minify($given);
    is $got, $expect;
  };

  # Inside of a function, units/zeros are preserved.
  subtest 'inside a function' => sub {
    my $given  = 'p { width: calc(300px - 0px) }';
    my $expect = 'p{width:calc(300px - 0px)}';
    my $got    = minify($given);
    is $got, $expect;
  };
};

###############################################################################
# "point zero" can be expressed without a unit, in *MOST* cases.
subtest 'point-zero without units' => sub {
  subtest 'px' => sub {
    my $given  = 'p { width: .0px }';
    my $expect = 'p{width:0}';
    my $got    = minify($given);
    is $got, $expect;
  };

  subtest 'no units' => sub {
    my $given  = 'p { width: .0 }';
    my $expect = 'p{width:0}';
    my $got    = minify($given);
    is $got, $expect;
  };

  # Percent is special, and may need to be preserved for CSS animations,
  # but will be minified from "point-zero" to just "zero".
  subtest 'percent' => sub {
    my $given  = 'p { width: .0% }';
    my $expect = 'p{width:0%}';
    my $got    = minify($given);
    is $got, $expect;
  };

  # Inside of a function, units/zeros are preserved.
  subtest 'inside a function' => sub {
    my $given  = 'p { width: calc(300px - .0px) }';
    my $expect = 'p{width:calc(300px - 0px)}';
    my $got    = minify($given);
    is $got, $expect;
  };
};

###############################################################################
# "point zero something" requires preservation
subtest 'point-zero without units' => sub {
  subtest 'px' => sub {
    my $given  = 'p { width: .001px }';
    my $expect = 'p{width:.001px}';
    my $got    = minify($given);
    is $got, $expect;
  };

  subtest 'no units' => sub {
    my $given  = 'p { width: .001 }';
    my $expect = 'p{width:.001}';
    my $got    = minify($given);
    is $got, $expect;
  };

  # Percent is special, and may need to be preserved for CSS animations
  subtest 'percent' => sub {
    my $given  = 'p { width: .001% }';
    my $expect = 'p{width:.001%}';
    my $got    = minify($given);
    is $got, $expect;
  };

  # Inside of a function, units/zeros are preserved.
  subtest 'inside a function' => sub {
    my $given  = 'p { width: calc(300px - .001px) }';
    my $expect = 'p{width:calc(300px - .001px)}';
    my $got    = minify($given);
    is $got, $expect;
  };
};

###############################################################################
# "something point zero" requires preservation of the unit
subtest 'something-point-zero preserves units' => sub {
  subtest 'px' => sub {
    my $given  = 'p { width: 1.0px }';
    my $expect = 'p{width:1.0px}';
    my $got    = minify($given);
    is $got, $expect;
  };

  # Percent is special, and may need to be preserved for CSS animations
  subtest 'percent' => sub {
    my $given  = 'p { width: 1.0% }';
    my $expect = 'p{width:1.0%}';
    my $got    = minify($given);
    is $got, $expect;
  };

  # Inside of a function, units/zeros are preserved.
  subtest 'inside a function' => sub {
    my $given  = 'p { width: calc(300px - 1.0px) }';
    my $expect = 'p{width:calc(300px - 1.0px)}';
    my $got    = minify($given);
    is $got, $expect;
  };
};

###############################################################################
# "zero point zero" can be expressed without a unit, in *MOST* cases.
subtest 'zero-point-zero without units' => sub {
  subtest 'px' => sub {
    my $given  = 'p { width: 0.0px }';
    my $expect = 'p{width:0}';
    my $got    = minify($given);
    is $got, $expect;
  };

  subtest 'no units' => sub {
    my $given  = 'p { width: 0.0 }';
    my $expect = 'p{width:0}';
    my $got    = minify($given);
    is $got, $expect;
  };

  # Percent is special, and may need to be preserved for CSS animations,
  # but will be minified from "zero-point-zero" to just "zero".
  subtest 'percent' => sub {
    my $given  = 'p { width: 0.0% }';
    my $expect = 'p{width:0%}';
    my $got    = minify($given);
    is $got, $expect;
  };

  # Inside of a function, units/zeros are preserved.
  subtest 'inside a function' => sub {
    my $given  = 'p { width: calc(300px - 0.0px) }';
    my $expect = 'p{width:calc(300px - 0px)}';
    my $got    = minify($given);
    is $got, $expect;
  };
};

###############################################################################
# "zero point zero somethihg" requires preservation
subtest 'zero-point-zero without units' => sub {
  subtest 'px' => sub {
    my $given  = 'p { width: 0.001px }';
    my $expect = 'p{width:.001px}';
    my $got    = minify($given);
    is $got, $expect;
  };

  subtest 'no units' => sub {
    my $given  = 'p { width: 0.001 }';
    my $expect = 'p{width:.001}';
    my $got    = minify($given);
    is $got, $expect;
  };

  # Percent is special, and may need to be preserved for CSS animations,
  # but will be minified from "zero-point-zero-something" to just
  # "point-zero-something"
  subtest 'percent' => sub {
    my $given  = 'p { width: 0.001% }';
    my $expect = 'p{width:.001%}';
    my $got    = minify($given);
    is $got, $expect;
  };

  # Inside of a function, units/zeros are preserved.
  subtest 'inside a function' => sub {
    my $given  = 'p { width: calc(300px - 0.001px) }';
    my $expect = 'p{width:calc(300px - .001px)}';
    my $got    = minify($given);
    is $got, $expect;
  };
};

###############################################################################
# "zerooooooo" can be expressed without a unit, in *MOST* cases.
subtest 'zerooooooo without units' => sub {
  subtest 'px' => sub {
    my $given  = 'p { width: 0000px }';
    my $expect = 'p{width:0}';
    my $got    = minify($given);
    is $got, $expect;
  };

  subtest 'no units' => sub {
    my $given  = 'p { width: 0000 }';
    my $expect = 'p{width:0}';
    my $got    = minify($given);
    is $got, $expect;
  };

  # Percent is special, and may need to be preserved for CSS animations,
  # but will be minified from "zerooooo" to just "zero".
  subtest 'percent' => sub {
    my $given  = 'p { width: 000% }';
    my $expect = 'p{width:0%}';
    my $got    = minify($given);
    is $got, $expect;
  };

  # Inside of a function, units/zeros are preserved.
  subtest 'inside a function' => sub {
    my $given  = 'p { width: calc(300px - 0000px) }';
    my $expect = 'p{width:calc(300px - 0px)}';
    my $got    = minify($given);
    is $got, $expect;
  };
};

###############################################################################
# "zerooooo-point-zeroooo" can be expressed without a unit, in *MOST* cases.
subtest 'zerooooo-point-zeroooo without units' => sub {
  subtest 'px' => sub {
    my $given  = 'p { width: 000.0000px }';
    my $expect = 'p{width:0}';
    my $got    = minify($given);
    is $got, $expect;
  };

  # Percent is special, and may need to be preserved for CSS animations,
  # but will be minified from "zerooo-point-zerooo" to just "zero".
  subtest 'percent' => sub {
    my $given  = 'p { width: 000.000% }';
    my $expect = 'p{width:0%}';
    my $got    = minify($given);
    is $got, $expect;
  };

  # Inside of a function, units/zeros are preserved.
  subtest 'inside a function' => sub {
    my $given  = 'p { width: calc(300px - 000.0000px) }';
    my $expect = 'p{width:calc(300px - 0px)}';
    my $got    = minify($given);
    is $got, $expect;
  };
};

###############################################################################
# Inside of "calc()", whitespace surrounding operators needs to be preserved.
subtest 'calc() method' => sub {
  subtest 'plus' => sub {
    my $given  = 'h1   { width: calc( 100px + 10% ) }';
    my $expect = 'h1{width:calc(100px + 10%)}';
    my $got    = minify($given);
    is $got, $expect;
  };

  subtest 'minus' => sub {
    my $given  = 'h2 { width: calc(100% - 30px ) }';
    my $expect = 'h2{width:calc(100% - 30px)}';
    my $got    = minify($given);
    is $got, $expect;
  };

  subtest 'times' => sub {
    my $given  = 'h3 { width: calc( 60px * 2 ) }';
    my $expect = 'h3{width:calc(60px * 2)}';
    my $got    = minify($given);
    is $got, $expect;
  };

  subtest 'divide' => sub {
    my $given  = 'h4 { width: calc( 100% / 2 ) }';
    my $expect = 'h4{width:calc(100% / 2)}';
    my $got    = minify($given);
    is $got, $expect;
  };
};

###############################################################################
# RT #36557: Nasty segfault on minifying comment-only css input
#
# Actually turns out to be that *anything* that minifies to "nothing" causes
# a segfault in Perl-5.8.  Perl-5.10 seems immune.
subtest 'minifies to nothing' => sub {
  my $given  = '/* */';
  my $expect = undef;
  my $got    = minify($given);
  is $got, $expect;
};

###############################################################################
# General/broad test
subtest 'general/broad test' => sub {
  my $given
    = "/* comment to be removed */\n"
    . "\@import    url(more.css   );\n\n"
    . "\tbody, td, th {\nfont-family: Verdana, 'Bitstream Vera Sans';}\n"
    . ".nav {\n\tmargin-left:20%\n}"
    . "#main-nav { background-color: red; border: 1px solid yellow; }\n"
    . "div#content h1    + p\t{padding-top: 000;}";
  my $expect
    = ""
    . "\@import url(more.css);"
    . "body,td,th{font-family:Verdana,'Bitstream Vera Sans'}"
    . ".nav{margin-left:20%}"
    . "#main-nav{background-color:red;border:1px solid yellow}"
    . "div#content h1 + p{padding-top:0}";
  my $got = minify($given);
  is $got, $expect;
};

###############################################################################
done_testing();