-
Notifications
You must be signed in to change notification settings - Fork 2
/
cssmin.php
5090 lines (5009 loc) · 157 KB
/
cssmin.php
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
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
/**
* CssMin - A (simple) css minifier with benefits
*
* --
* Copyright (c) 2011 Joe Scylla <[email protected]>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
* --
*
* @package CssMin
* @link http://code.google.com/p/cssmin/
* @author Joe Scylla <[email protected]>
* @copyright 2008 - 2011 Joe Scylla <[email protected]>
* @license http://opensource.org/licenses/mit-license.php MIT License
* @version 3.0.1-$Id$
*/
/**
* Abstract definition of a CSS token class.
*
* Every token has to extend this class.
*
* @package CssMin/Tokens
* @link http://code.google.com/p/cssmin/
* @author Joe Scylla <[email protected]>
* @copyright 2008 - 2011 Joe Scylla <[email protected]>
* @license http://opensource.org/licenses/mit-license.php MIT License
* @version 3.0.1-$Id$
*/
abstract class aCssToken
{
/**
* Returns the token as string.
*
* @return string
*/
abstract public function __toString();
}
/**
* Abstract definition of a for a ruleset start token.
*
* @package CssMin/Tokens
* @link http://code.google.com/p/cssmin/
* @author Joe Scylla <[email protected]>
* @copyright 2008 - 2011 Joe Scylla <[email protected]>
* @license http://opensource.org/licenses/mit-license.php MIT License
* @version 3.0.1
*/
abstract class aCssRulesetStartToken extends aCssToken
{
}
/**
* Abstract definition of a for ruleset end token.
*
* @package CssMin/Tokens
* @link http://code.google.com/p/cssmin/
* @author Joe Scylla <[email protected]>
* @copyright 2008 - 2011 Joe Scylla <[email protected]>
* @license http://opensource.org/licenses/mit-license.php MIT License
* @version 3.0.1
*/
abstract class aCssRulesetEndToken extends aCssToken
{
/**
* Implements {@link aCssToken::__toString()}.
*
* @return string
*/
public function __toString()
{
return "}";
}
}
/**
* Abstract definition of a parser plugin.
*
* Every parser plugin have to extend this class. A parser plugin contains the logic to parse one or aspects of a
* stylesheet.
*
* @package CssMin/Parser/Plugins
* @link http://code.google.com/p/cssmin/
* @author Joe Scylla <[email protected]>
* @copyright 2008 - 2011 Joe Scylla <[email protected]>
* @license http://opensource.org/licenses/mit-license.php MIT License
* @version 3.0.1
*/
abstract class aCssParserPlugin
{
/**
* Plugin configuration.
*
* @var array
*/
protected $configuration = array();
/**
* The CssParser of the plugin.
*
* @var CssParser
*/
protected $parser = null;
/**
* Plugin buffer.
*
* @var string
*/
protected $buffer = "";
/**
* Constructor.
*
* @param CssParser $parser The CssParser object of this plugin.
* @param array $configuration Plugin configuration [optional]
* @return void
*/
public function __construct(CssParser $parser, array $configuration = null)
{
$this->configuration = $configuration;
$this->parser = $parser;
}
/**
* Returns the array of chars triggering the parser plugin.
*
* @return array
*/
abstract public function getTriggerChars();
/**
* Returns the array of states triggering the parser plugin or FALSE if every state will trigger the parser plugin.
*
* @return array
*/
abstract public function getTriggerStates();
/**
* Parser routine of the plugin.
*
* @param integer $index Current index
* @param string $char Current char
* @param string $previousChar Previous char
* @return mixed TRUE will break the processing; FALSE continue with the next plugin; integer set a new index and break the processing
*/
abstract public function parse($index, $char, $previousChar, $state);
}
/**
* Abstract definition of a minifier plugin class.
*
* Minifier plugin process the parsed tokens one by one to apply changes to the token. Every minifier plugin has to
* extend this class.
*
* @package CssMin/Minifier/Plugins
* @link http://code.google.com/p/cssmin/
* @author Joe Scylla <[email protected]>
* @copyright 2008 - 2011 Joe Scylla <[email protected]>
* @license http://opensource.org/licenses/mit-license.php MIT License
* @version 3.0.1
*/
abstract class aCssMinifierPlugin
{
/**
* Plugin configuration.
*
* @var array
*/
protected $configuration = array();
/**
* The CssMinifier of the plugin.
*
* @var CssMinifier
*/
protected $minifier = null;
/**
* Constructor.
*
* @param CssMinifier $minifier The CssMinifier object of this plugin.
* @param array $configuration Plugin configuration [optional]
* @return void
*/
public function __construct(CssMinifier $minifier, array $configuration = array())
{
$this->configuration = $configuration;
$this->minifier = $minifier;
}
/**
* Apply the plugin to the token.
*
* @param aCssToken $token Token to process
* @return boolean Return TRUE to break the processing of this token; FALSE to continue
*/
abstract public function apply(aCssToken &$token);
/**
* --
*
* @return array
*/
abstract public function getTriggerTokens();
}
/**
* Abstract definition of a minifier filter class.
*
* Minifier filters allows a pre-processing of the parsed token to add, edit or delete tokens. Every minifier filter
* has to extend this class.
*
* @package CssMin/Minifier/Filters
* @link http://code.google.com/p/cssmin/
* @author Joe Scylla <[email protected]>
* @copyright 2008 - 2011 Joe Scylla <[email protected]>
* @license http://opensource.org/licenses/mit-license.php MIT License
* @version 3.0.1
*/
abstract class aCssMinifierFilter
{
/**
* Filter configuration.
*
* @var array
*/
protected $configuration = array();
/**
* The CssMinifier of the filter.
*
* @var CssMinifier
*/
protected $minifier = null;
/**
* Constructor.
*
* @param CssMinifier $minifier The CssMinifier object of this plugin.
* @param array $configuration Filter configuration [optional]
* @return void
*/
public function __construct(CssMinifier $minifier, array $configuration = array())
{
$this->configuration = $configuration;
$this->minifier = $minifier;
}
/**
* Filter the tokens.
*
* @param array $tokens Array of objects of type aCssToken
* @return integer Count of added, changed or removed tokens; a return value large than 0 will rebuild the array
*/
abstract public function apply(array &$tokens);
}
/**
* Abstract formatter definition.
*
* Every formatter have to extend this class.
*
* @package CssMin/Formatter
* @link http://code.google.com/p/cssmin/
* @author Joe Scylla <[email protected]>
* @copyright 2008 - 2011 Joe Scylla <[email protected]>
* @license http://opensource.org/licenses/mit-license.php MIT License
* @version 3.0.1
*/
abstract class aCssFormatter
{
/**
* Indent string.
*
* @var string
*/
protected $indent = " ";
/**
* Declaration padding.
*
* @var integer
*/
protected $padding = 0;
/**
* Tokens.
*
* @var array
*/
protected $tokens = array();
/**
* Constructor.
*
* @param array $tokens Array of CssToken
* @param string $indent Indent string [optional]
* @param integer $padding Declaration value padding [optional]
*/
public function __construct(array $tokens, $indent = null, $padding = null)
{
$this->tokens = $tokens;
$this->indent = !is_null($indent) ? $indent : $this->indent;
$this->padding = !is_null($padding) ? $padding : $this->padding;
}
/**
* Returns the array of aCssToken as formatted string.
*
* @return string
*/
abstract public function __toString();
}
/**
* Abstract definition of a ruleset declaration token.
*
* @package CssMin/Tokens
* @link http://code.google.com/p/cssmin/
* @author Joe Scylla <[email protected]>
* @copyright 2008 - 2011 Joe Scylla <[email protected]>
* @license http://opensource.org/licenses/mit-license.php MIT License
* @version 3.0.1
*/
abstract class aCssDeclarationToken extends aCssToken
{
/**
* Is the declaration flagged as important?
*
* @var boolean
*/
public $IsImportant = false;
/**
* Is the declaration flagged as last one of the ruleset?
*
* @var boolean
*/
public $IsLast = false;
/**
* Property name of the declaration.
*
* @var string
*/
public $Property = "";
/**
* Value of the declaration.
*
* @var string
*/
public $Value = "";
/**
* Set the properties of the @font-face declaration.
*
* @param string $property Property of the declaration
* @param string $value Value of the declaration
* @param boolean $isImportant Is the !important flag is set?
* @param boolean $IsLast Is the declaration the last one of the block?
* @return void
*/
public function __construct($property, $value, $isImportant = false, $isLast = false)
{
$this->Property = $property;
$this->Value = $value;
$this->IsImportant = $isImportant;
$this->IsLast = $isLast;
}
/**
* Implements {@link aCssToken::__toString()}.
*
* @return string
*/
public function __toString()
{
return $this->Property . ":" . $this->Value . ($this->IsImportant ? " !important" : "") . ($this->IsLast ? "" : ";");
}
}
/**
* Abstract definition of a for at-rule block start token.
*
* @package CssMin/Tokens
* @link http://code.google.com/p/cssmin/
* @author Joe Scylla <[email protected]>
* @copyright 2008 - 2011 Joe Scylla <[email protected]>
* @license http://opensource.org/licenses/mit-license.php MIT License
* @version 3.0.1
*/
abstract class aCssAtBlockStartToken extends aCssToken
{
}
/**
* Abstract definition of a for at-rule block end token.
*
* @package CssMin/Tokens
* @link http://code.google.com/p/cssmin/
* @author Joe Scylla <[email protected]>
* @copyright 2008 - 2011 Joe Scylla <[email protected]>
* @license http://opensource.org/licenses/mit-license.php MIT License
* @version 3.0.1
*/
abstract class aCssAtBlockEndToken extends aCssToken
{
/**
* Implements {@link aCssToken::__toString()}.
*
* @return string
*/
public function __toString()
{
return "}";
}
}
/**
* {@link aCssFromatter Formatter} returning the CSS source in {@link http://goo.gl/etzLs Whitesmiths indent style}.
*
* @package CssMin/Formatter
* @link http://code.google.com/p/cssmin/
* @author Joe Scylla <[email protected]>
* @copyright 2008 - 2011 Joe Scylla <[email protected]>
* @license http://opensource.org/licenses/mit-license.php MIT License
* @version 3.0.1
*/
class CssWhitesmithsFormatter extends aCssFormatter
{
/**
* Implements {@link aCssFormatter::__toString()}.
*
* @return string
*/
public function __toString()
{
$r = array();
$level = 0;
for ($i = 0, $l = count($this->tokens); $i < $l; $i++)
{
$token = $this->tokens[$i];
$class = get_class($token);
$indent = str_repeat($this->indent, $level);
if ($class === "CssCommentToken")
{
$lines = array_map("trim", explode("\n", $token->Comment));
for ($ii = 0, $ll = count($lines); $ii < $ll; $ii++)
{
$r[] = $indent . (substr($lines[$ii], 0, 1) == "*" ? " " : "") . $lines[$ii];
}
}
elseif ($class === "CssAtCharsetToken")
{
$r[] = $indent . "@charset " . $token->Charset . ";";
}
elseif ($class === "CssAtFontFaceStartToken")
{
$r[] = $indent . "@font-face";
$r[] = $this->indent . $indent . "{";
$level++;
}
elseif ($class === "CssAtImportToken")
{
$r[] = $indent . "@import " . $token->Import . " " . implode(", ", $token->MediaTypes) . ";";
}
elseif ($class === "CssAtKeyframesStartToken")
{
$r[] = $indent . "@keyframes " . $token->Name;
$r[] = $this->indent . $indent . "{";
$level++;
}
elseif ($class === "CssAtMediaStartToken")
{
$r[] = $indent . "@media " . implode(", ", $token->MediaTypes);
$r[] = $this->indent . $indent . "{";
$level++;
}
elseif ($class === "CssAtPageStartToken")
{
$r[] = $indent . "@page";
$r[] = $this->indent . $indent . "{";
$level++;
}
elseif ($class === "CssAtVariablesStartToken")
{
$r[] = $indent . "@variables " . implode(", ", $token->MediaTypes);
$r[] = $this->indent . $indent . "{";
$level++;
}
elseif ($class === "CssRulesetStartToken" || $class === "CssAtKeyframesRulesetStartToken")
{
$r[] = $indent . implode(", ", $token->Selectors);
$r[] = $this->indent . $indent . "{";
$level++;
}
elseif ($class == "CssAtFontFaceDeclarationToken"
|| $class === "CssAtKeyframesRulesetDeclarationToken"
|| $class === "CssAtPageDeclarationToken"
|| $class == "CssAtVariablesDeclarationToken"
|| $class === "CssRulesetDeclarationToken"
)
{
$declaration = $indent . $token->Property . ": ";
if ($this->padding)
{
$declaration = str_pad($declaration, $this->padding, " ", STR_PAD_RIGHT);
}
$r[] = $declaration . $token->Value . ($token->IsImportant ? " !important" : "") . ";";
}
elseif ($class === "CssAtFontFaceEndToken"
|| $class === "CssAtMediaEndToken"
|| $class === "CssAtKeyframesEndToken"
|| $class === "CssAtKeyframesRulesetEndToken"
|| $class === "CssAtPageEndToken"
|| $class === "CssAtVariablesEndToken"
|| $class === "CssRulesetEndToken"
)
{
$r[] = $indent . "}";
$level--;
}
}
return implode("\n", $r);
}
}
/**
* This {@link aCssMinifierPlugin} will process var-statement and sets the declaration value to the variable value.
*
* This plugin only apply the variable values. The variable values itself will get parsed by the
* {@link CssVariablesMinifierFilter}.
*
* Example:
* <code>
* @variables
* {
* defaultColor: black;
* }
* color: var(defaultColor);
* </code>
*
* Will get converted to:
* <code>
* color:black;
* </code>
*
* @package CssMin/Minifier/Plugins
* @link http://code.google.com/p/cssmin/
* @author Joe Scylla <[email protected]>
* @copyright 2008 - 2011 Joe Scylla <[email protected]>
* @license http://opensource.org/licenses/mit-license.php MIT License
* @version 3.0.1
*/
class CssVariablesMinifierPlugin extends aCssMinifierPlugin
{
/**
* Regular expression matching a value.
*
* @var string
*/
private $reMatch = "/var\((.+)\)/iSU";
/**
* Parsed variables.
*
* @var array
*/
private $variables = null;
/**
* Returns the variables.
*
* @return array
*/
public function getVariables()
{
return $this->variables;
}
/**
* Implements {@link aCssMinifierPlugin::minify()}.
*
* @param aCssToken $token Token to process
* @return boolean Return TRUE to break the processing of this token; FALSE to continue
*/
public function apply(aCssToken &$token)
{
if (stripos($token->Value, "var") !== false && preg_match_all($this->reMatch, $token->Value, $m))
{
$mediaTypes = $token->MediaTypes;
if (!in_array("all", $mediaTypes))
{
$mediaTypes[] = "all";
}
for ($i = 0, $l = count($m[0]); $i < $l; $i++)
{
$variable = trim($m[1][$i]);
foreach ($mediaTypes as $mediaType)
{
if (isset($this->variables[$mediaType], $this->variables[$mediaType][$variable]))
{
// Variable value found => set the declaration value to the variable value and return
$token->Value = str_replace($m[0][$i], $this->variables[$mediaType][$variable], $token->Value);
continue 2;
}
}
// If no value was found trigger an error and replace the token with a CssNullToken
CssMin::triggerError(new CssError(__FILE__, __LINE__, __METHOD__ . ": No value found for variable <code>" . $variable . "</code> in media types <code>" . implode(", ", $mediaTypes) . "</code>", (string) $token));
$token = new CssNullToken();
return true;
}
}
return false;
}
/**
* Implements {@link aMinifierPlugin::getTriggerTokens()}
*
* @return array
*/
public function getTriggerTokens()
{
return array
(
"CssAtFontFaceDeclarationToken",
"CssAtPageDeclarationToken",
"CssRulesetDeclarationToken"
);
}
/**
* Sets the variables.
*
* @param array $variables Variables to set
* @return void
*/
public function setVariables(array $variables)
{
$this->variables = $variables;
}
}
/**
* This {@link aCssMinifierFilter minifier filter} will parse the variable declarations out of @variables at-rule
* blocks. The variables will get store in the {@link CssVariablesMinifierPlugin} that will apply the variables to
* declaration.
*
* @package CssMin/Minifier/Filters
* @link http://code.google.com/p/cssmin/
* @author Joe Scylla <[email protected]>
* @copyright 2008 - 2011 Joe Scylla <[email protected]>
* @license http://opensource.org/licenses/mit-license.php MIT License
* @version 3.0.1
*/
class CssVariablesMinifierFilter extends aCssMinifierFilter
{
/**
* Implements {@link aCssMinifierFilter::filter()}.
*
* @param array $tokens Array of objects of type aCssToken
* @return integer Count of added, changed or removed tokens; a return value large than 0 will rebuild the array
*/
public function apply(array &$tokens)
{
$variables = array();
$defaultMediaTypes = array("all");
$mediaTypes = array();
$remove = array();
for($i = 0, $l = count($tokens); $i < $l; $i++)
{
// @variables at-rule block found
if (get_class($tokens[$i]) === "CssAtVariablesStartToken")
{
$remove[] = $i;
$mediaTypes = (count($tokens[$i]->MediaTypes) == 0 ? $defaultMediaTypes : $tokens[$i]->MediaTypes);
foreach ($mediaTypes as $mediaType)
{
if (!isset($variables[$mediaType]))
{
$variables[$mediaType] = array();
}
}
// Read the variable declaration tokens
for($i = $i; $i < $l; $i++)
{
// Found a variable declaration => read the variable values
if (get_class($tokens[$i]) === "CssAtVariablesDeclarationToken")
{
foreach ($mediaTypes as $mediaType)
{
$variables[$mediaType][$tokens[$i]->Property] = $tokens[$i]->Value;
}
$remove[] = $i;
}
// Found the variables end token => break;
elseif (get_class($tokens[$i]) === "CssAtVariablesEndToken")
{
$remove[] = $i;
break;
}
}
}
}
// Variables in @variables at-rule blocks
foreach($variables as $mediaType => $null)
{
foreach($variables[$mediaType] as $variable => $value)
{
// If a var() statement in a variable value found...
if (stripos($value, "var") !== false && preg_match_all("/var\((.+)\)/iSU", $value, $m))
{
// ... then replace the var() statement with the variable values.
for ($i = 0, $l = count($m[0]); $i < $l; $i++)
{
$variables[$mediaType][$variable] = str_replace($m[0][$i], (isset($variables[$mediaType][$m[1][$i]]) ? $variables[$mediaType][$m[1][$i]] : ""), $variables[$mediaType][$variable]);
}
}
}
}
// Remove the complete @variables at-rule block
foreach ($remove as $i)
{
$tokens[$i] = null;
}
if (!($plugin = $this->minifier->getPlugin("CssVariablesMinifierPlugin")))
{
CssMin::triggerError(new CssError(__FILE__, __LINE__, __METHOD__ . ": The plugin <code>CssVariablesMinifierPlugin</code> was not found but is required for <code>" . __CLASS__ . "</code>"));
}
else
{
$plugin->setVariables($variables);
}
return count($remove);
}
}
/**
* {@link aCssParserPlugin Parser plugin} for preserve parsing url() values.
*
* This plugin return no {@link aCssToken CssToken} but ensures that url() values will get parsed properly.
*
* @package CssMin/Parser/Plugins
* @link http://code.google.com/p/cssmin/
* @author Joe Scylla <[email protected]>
* @copyright 2008 - 2011 Joe Scylla <[email protected]>
* @license http://opensource.org/licenses/mit-license.php MIT License
* @version 3.0.1
*/
class CssUrlParserPlugin extends aCssParserPlugin
{
/**
* Implements {@link aCssParserPlugin::getTriggerChars()}.
*
* @return array
*/
public function getTriggerChars()
{
return array("(", ")");
}
/**
* Implements {@link aCssParserPlugin::getTriggerStates()}.
*
* @return array
*/
public function getTriggerStates()
{
return false;
}
/**
* Implements {@link aCssParserPlugin::parse()}.
*
* @param integer $index Current index
* @param string $char Current char
* @param string $previousChar Previous char
* @return mixed TRUE will break the processing; FALSE continue with the next plugin; integer set a new index and break the processing
*/
public function parse($index, $char, $previousChar, $state)
{
// Start of string
if ($char === "(" && strtolower(substr($this->parser->getSource(), $index - 3, 4)) === "url(" && $state !== "T_URL")
{
$this->parser->pushState("T_URL");
$this->parser->setExclusive(__CLASS__);
}
// Escaped LF in url => remove escape backslash and LF
elseif ($char === "\n" && $previousChar === "\\" && $state === "T_URL")
{
$this->parser->setBuffer(substr($this->parser->getBuffer(), 0, -2));
}
// Parse error: Unescaped LF in string literal
elseif ($char === "\n" && $previousChar !== "\\" && $state === "T_URL")
{
$line = $this->parser->getBuffer();
$this->parser->setBuffer(substr($this->parser->getBuffer(), 0, -1) . ")"); // Replace the LF with the url string delimiter
$this->parser->popState();
$this->parser->unsetExclusive();
CssMin::triggerError(new CssError(__FILE__, __LINE__, __METHOD__ . ": Unterminated string literal", $line . "_"));
}
// End of string
elseif ($char === ")" && $state === "T_URL")
{
$this->parser->popState();
$this->parser->unsetExclusive();
}
else
{
return false;
}
return true;
}
}
/**
* {@link aCssParserPlugin Parser plugin} for preserve parsing string values.
*
* This plugin return no {@link aCssToken CssToken} but ensures that string values will get parsed properly.
*
* @package CssMin/Parser/Plugins
* @link http://code.google.com/p/cssmin/
* @author Joe Scylla <[email protected]>
* @copyright 2008 - 2011 Joe Scylla <[email protected]>
* @license http://opensource.org/licenses/mit-license.php MIT License
* @version 3.0.1
*/
class CssStringParserPlugin extends aCssParserPlugin
{
/**
* Current string delimiter char.
*
* @var string
*/
private $delimiterChar = null;
/**
* Implements {@link aCssParserPlugin::getTriggerChars()}.
*
* @return array
*/
public function getTriggerChars()
{
return array("\"", "'", "\n");
}
/**
* Implements {@link aCssParserPlugin::getTriggerStates()}.
*
* @return array
*/
public function getTriggerStates()
{
return false;
}
/**
* Implements {@link aCssParserPlugin::parse()}.
*
* @param integer $index Current index
* @param string $char Current char
* @param string $previousChar Previous char
* @return mixed TRUE will break the processing; FALSE continue with the next plugin; integer set a new index and break the processing
*/
public function parse($index, $char, $previousChar, $state)
{
// Start of string
if (($char === "\"" || $char === "'") && $state !== "T_STRING")
{
$this->delimiterChar = $char;
$this->parser->pushState("T_STRING");
$this->parser->setExclusive(__CLASS__);
}
// Escaped LF in string => remove escape backslash and LF
elseif ($char === "\n" && $previousChar === "\\" && $state === "T_STRING")
{
$this->parser->setBuffer(substr($this->parser->getBuffer(), 0, -2));
}
// Parse error: Unescaped LF in string literal
elseif ($char === "\n" && $previousChar !== "\\" && $state === "T_STRING")
{
$line = $this->parser->getBuffer();
$this->parser->popState();
$this->parser->unsetExclusive();
$this->parser->setBuffer(substr($this->parser->getBuffer(), 0, -1) . $this->delimiterChar); // Replace the LF with the current string char
CssMin::triggerError(new CssError(__FILE__, __LINE__, __METHOD__ . ": Unterminated string literal", $line . "_"));
$this->delimiterChar = null;
}
// End of string
elseif ($char === $this->delimiterChar && $state === "T_STRING")
{
// If the Previous char is a escape char count the amount of the previous escape chars. If the amount of
// escape chars is uneven do not end the string
if ($previousChar == "\\")
{
$source = $this->parser->getSource();
$c = 1;
$i = $index - 2;
while (substr($source, $i, 1) === "\\")
{
$c++; $i--;
}
if ($c % 2)
{
return false;
}
}
$this->parser->popState();
$this->parser->unsetExclusive();
$this->delimiterChar = null;
}
else
{
return false;
}
return true;
}
}
/**
* This {@link aCssMinifierFilter minifier filter} sorts the ruleset declarations of a ruleset by name.
*
* @package CssMin/Minifier/Filters
* @link http://code.google.com/p/cssmin/
* @author Rowan Beentje <http://assanka.net>
* @copyright Rowan Beentje <http://assanka.net>
* @license http://opensource.org/licenses/mit-license.php MIT License
* @version 3.0.1
*/
class CssSortRulesetPropertiesMinifierFilter extends aCssMinifierFilter
{
/**
* Implements {@link aCssMinifierFilter::filter()}.
*
* @param array $tokens Array of objects of type aCssToken
* @return integer Count of added, changed or removed tokens; a return value larger than 0 will rebuild the array
*/
public function apply(array &$tokens)
{
$r = 0;
for ($i = 0, $l = count($tokens); $i < $l; $i++)
{
// Only look for ruleset start rules
if (get_class($tokens[$i]) !== "CssRulesetStartToken") { continue; }
// Look for the corresponding ruleset end
$endIndex = false;
for ($ii = $i + 1; $ii < $l; $ii++)
{
if (get_class($tokens[$ii]) !== "CssRulesetEndToken") { continue; }
$endIndex = $ii;
break;
}
if (!$endIndex) { break; }
$startIndex = $i;
$i = $endIndex;
// Skip if there's only one token in this ruleset
if ($endIndex - $startIndex <= 2) { continue; }
// Ensure that everything between the start and end is a declaration token, for safety
for ($ii = $startIndex + 1; $ii < $endIndex; $ii++)
{
if (get_class($tokens[$ii]) !== "CssRulesetDeclarationToken") { continue(2); }
}
$declarations = array_slice($tokens, $startIndex + 1, $endIndex - $startIndex - 1);
// Check whether a sort is required
$sortRequired = $lastPropertyName = false;
foreach ($declarations as $declaration)
{
if ($lastPropertyName)
{
if (strcmp($lastPropertyName, $declaration->Property) > 0)
{
$sortRequired = true;
break;
}
}
$lastPropertyName = $declaration->Property;
}
if (!$sortRequired) { continue; }
// Arrange the declarations alphabetically by name
usort($declarations, array(__CLASS__, "userDefinedSort1"));
// Update "IsLast" property
for ($ii = 0, $ll = count($declarations) - 1; $ii <= $ll; $ii++)
{
if ($ii == $ll)
{
$declarations[$ii]->IsLast = true;
}
else
{
$declarations[$ii]->IsLast = false;
}
}
// Splice back into the array.
array_splice($tokens, $startIndex + 1, $endIndex - $startIndex - 1, $declarations);
$r += $endIndex - $startIndex - 1;
}
return $r;
}
/**
* User defined sort function.
*
* @return integer
*/
public static function userDefinedSort1($a, $b)
{
return strcmp($a->Property, $b->Property);
}
}
/**
* This {@link aCssToken CSS token} represents the start of a ruleset.