forked from moodle/moodle
-
Notifications
You must be signed in to change notification settings - Fork 1
/
csslib.php
4789 lines (4383 loc) · 150 KB
/
csslib.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
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* This file contains CSS related class, and function for the CSS optimiser
*
* Please see the {@link css_optimiser} class for greater detail.
*
* @package core
* @category css
* @copyright 2012 Sam Hemelryk
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
// NOTE: do not verify MOODLE_INTERNAL here, this is used from themes too
/**
* Stores CSS in a file at the given path.
*
* This function either succeeds or throws an exception.
*
* @param theme_config $theme The theme that the CSS belongs to.
* @param string $csspath The path to store the CSS at.
* @param array $cssfiles The CSS files to store.
*/
function css_store_css(theme_config $theme, $csspath, array $cssfiles) {
global $CFG;
// Check if both the CSS optimiser is enabled and the theme supports it.
if (!empty($CFG->enablecssoptimiser) && $theme->supportscssoptimisation) {
// This is an experimental feature introduced in Moodle 2.3
// The CSS optimiser organises the CSS in order to reduce the overall number
// of rules and styles being sent to the client. It does this by collating
// the CSS before it is cached removing excess styles and rules and stripping
// out any extraneous content such as comments and empty rules.
$optimiser = new css_optimiser;
$css = '';
foreach ($cssfiles as $file) {
$css .= file_get_contents($file)."\n";
}
$css = $theme->post_process($css);
$css = $optimiser->process($css);
// If cssoptimisestats is set then stats from the optimisation are collected
// and output at the beginning of the CSS
if (!empty($CFG->cssoptimiserstats)) {
$css = $optimiser->output_stats_css().$css;
}
} else {
// This is the default behaviour.
// The cssoptimise setting was introduced in Moodle 2.3 and will hopefully
// in the future be changed from an experimental setting to the default.
// The css_minify_css will method will use the Minify library remove
// comments, additional whitespace and other minor measures to reduce the
// the overall CSS being sent.
// However it has the distinct disadvantage of having to minify the CSS
// before running the post process functions. Potentially things may break
// here if theme designers try to push things with CSS post processing.
$css = $theme->post_process(css_minify_css($cssfiles));
}
clearstatcache();
if (!file_exists(dirname($csspath))) {
@mkdir(dirname($csspath), $CFG->directorypermissions, true);
}
// Prevent serving of incomplete file from concurrent request,
// the rename() should be more atomic than fwrite().
ignore_user_abort(true);
if ($fp = fopen($csspath.'.tmp', 'xb')) {
fwrite($fp, $css);
fclose($fp);
rename($csspath.'.tmp', $csspath);
@chmod($csspath, $CFG->filepermissions);
@unlink($csspath.'.tmp'); // just in case anything fails
}
ignore_user_abort(false);
if (connection_aborted()) {
die;
}
}
/**
* Sends IE specific CSS
*
* In writing the CSS parser I have a theory that we could optimise the CSS
* then split it based upon the number of selectors to ensure we dont' break IE
* and that we include only as many sub-stylesheets as we require.
* Of course just a theory but may be fun to code.
*
* @param string $themename The name of the theme we are sending CSS for.
* @param string $rev The revision to ensure we utilise the cache.
* @param string $etag The revision to ensure we utilise the cache.
* @param bool $slasharguments
*/
function css_send_ie_css($themename, $rev, $etag, $slasharguments) {
global $CFG;
$lifetime = 60*60*24*60; // 60 days only - the revision may get incremented quite often
$relroot = preg_replace('|^http.?://[^/]+|', '', $CFG->wwwroot);
$css = "/** Unfortunately IE6-9 does not support more than 4096 selectors in one CSS file, which means we have to use some ugly hacks :-( **/";
if ($slasharguments) {
$css .= "\n@import url($relroot/styles.php/$themename/$rev/plugins);";
$css .= "\n@import url($relroot/styles.php/$themename/$rev/parents);";
$css .= "\n@import url($relroot/styles.php/$themename/$rev/theme);";
} else {
$css .= "\n@import url($relroot/styles.php?theme=$themename&rev=$rev&type=plugins);";
$css .= "\n@import url($relroot/styles.php?theme=$themename&rev=$rev&type=parents);";
$css .= "\n@import url($relroot/styles.php?theme=$themename&rev=$rev&type=theme);";
}
header('Etag: '.$etag);
header('Content-Disposition: inline; filename="styles.php"');
header('Last-Modified: '. gmdate('D, d M Y H:i:s', time()) .' GMT');
header('Expires: '. gmdate('D, d M Y H:i:s', time() + $lifetime) .' GMT');
header('Pragma: ');
header('Cache-Control: public, max-age='.$lifetime);
header('Accept-Ranges: none');
header('Content-Type: text/css; charset=utf-8');
header('Content-Length: '.strlen($css));
echo $css;
die;
}
/**
* Sends a cached CSS file
*
* This function sends the cached CSS file. Remember it is generated on the first
* request, then optimised/minified, and finally cached for serving.
*
* @param string $csspath The path to the CSS file we want to serve.
* @param string $etag The revision to make sure we utilise any caches.
*/
function css_send_cached_css($csspath, $etag) {
$lifetime = 60*60*24*60; // 60 days only - the revision may get incremented quite often
header('Etag: '.$etag);
header('Content-Disposition: inline; filename="styles.php"');
header('Last-Modified: '. gmdate('D, d M Y H:i:s', filemtime($csspath)) .' GMT');
header('Expires: '. gmdate('D, d M Y H:i:s', time() + $lifetime) .' GMT');
header('Pragma: ');
header('Cache-Control: public, max-age='.$lifetime);
header('Accept-Ranges: none');
header('Content-Type: text/css; charset=utf-8');
if (!min_enable_zlib_compression()) {
header('Content-Length: '.filesize($csspath));
}
readfile($csspath);
die;
}
/**
* Sends CSS directly without caching it.
*
* This function takes a raw CSS string, optimises it if required, and then
* serves it.
* Turning both themedesignermode and CSS optimiser on at the same time is awful
* for performance because of the optimiser running here. However it was done so
* that theme designers could utilise the optimised output during development to
* help them optimise their CSS... not that they should write lazy CSS.
*
* @param string $css
*/
function css_send_uncached_css($css, $themesupportsoptimisation = true) {
global $CFG;
header('Content-Disposition: inline; filename="styles_debug.php"');
header('Last-Modified: '. gmdate('D, d M Y H:i:s', time()) .' GMT');
header('Expires: '. gmdate('D, d M Y H:i:s', time() + THEME_DESIGNER_CACHE_LIFETIME) .' GMT');
header('Pragma: ');
header('Accept-Ranges: none');
header('Content-Type: text/css; charset=utf-8');
if (is_array($css)) {
$css = implode("\n\n", $css);
}
echo $css;
die;
}
/**
* Send file not modified headers
* @param int $lastmodified
* @param string $etag
*/
function css_send_unmodified($lastmodified, $etag) {
$lifetime = 60*60*24*60; // 60 days only - the revision may get incremented quite often
header('HTTP/1.1 304 Not Modified');
header('Expires: '. gmdate('D, d M Y H:i:s', time() + $lifetime) .' GMT');
header('Cache-Control: public, max-age='.$lifetime);
header('Content-Type: text/css; charset=utf-8');
header('Etag: '.$etag);
if ($lastmodified) {
header('Last-Modified: '. gmdate('D, d M Y H:i:s', $lastmodified) .' GMT');
}
die;
}
/**
* Sends a 404 message about CSS not being found.
*/
function css_send_css_not_found() {
header('HTTP/1.0 404 not found');
die('CSS was not found, sorry.');
}
/**
* Uses the minify library to compress CSS.
*
* This is used if $CFG->enablecssoptimiser has been turned off. This was
* the original CSS optimisation library.
* It removes whitespace and shrinks things but does no apparent optimisation.
* Note the minify library is still being used for JavaScript.
*
* @param array $files An array of files to minify
* @return string The minified CSS
*/
function css_minify_css($files) {
global $CFG;
if (empty($files)) {
return '';
}
set_include_path($CFG->libdir . '/minify/lib' . PATH_SEPARATOR . get_include_path());
require_once('Minify.php');
if (0 === stripos(PHP_OS, 'win')) {
Minify::setDocRoot(); // IIS may need help
}
// disable all caching, we do it in moodle
Minify::setCache(null, false);
$options = array(
// JSMin is not GNU GPL compatible, use the plus version instead.
'minifiers' => array(Minify::TYPE_JS => array('JSMinPlus', 'minify')),
'bubbleCssImports' => false,
// Don't gzip content we just want text for storage
'encodeOutput' => false,
// Maximum age to cache, not used but required
'maxAge' => (60*60*24*20),
// The files to minify
'files' => $files,
// Turn orr URI rewriting
'rewriteCssUris' => false,
// This returns the CSS rather than echoing it for display
'quiet' => true
);
$error = 'unknown';
try {
$result = Minify::serve('Files', $options);
if ($result['success']) {
return $result['content'];
}
} catch (Exception $e) {
$error = $e->getMessage();
$error = str_replace("\r", ' ', $error);
$error = str_replace("\n", ' ', $error);
}
// minification failed - try to inform the theme developer and include the non-minified version
$css = <<<EOD
/* Error: $error */
/* Problem detected during theme CSS minimisation, please review the following code */
/* ================================================================================ */
EOD;
foreach ($files as $cssfile) {
$css .= file_get_contents($cssfile)."\n";
}
return $css;
}
/**
* Determines if the given value is a valid CSS colour.
*
* A CSS colour can be one of the following:
* - Hex colour: #AA66BB
* - RGB colour: rgb(0-255, 0-255, 0-255)
* - RGBA colour: rgba(0-255, 0-255, 0-255, 0-1)
* - HSL colour: hsl(0-360, 0-100%, 0-100%)
* - HSLA colour: hsla(0-360, 0-100%, 0-100%, 0-1)
*
* Or a recognised browser colour mapping {@link css_optimiser::$htmlcolours}
*
* @param string $value The colour value to check
* @return bool
*/
function css_is_colour($value) {
$value = trim($value);
$hex = '/^#([a-fA-F0-9]{1,3}|[a-fA-F0-9]{6})$/';
$rgb = '#^rgb\s*\(\s*(\d{1,3})\s*,\s*(\d{1,3})\s*,\s*(\d{1,3})\s*\)$#i';
$rgba = '#^rgba\s*\(\s*(\d{1,3})\s*,\s*(\d{1,3})\s*,\s*(\d{1,3})\s*,\s*(\d{1}(\.\d+)?)\s*\)$#i';
$hsl = '#^hsl\s*\(\s*(\d{1,3})\s*,\s*(\d{1,3})\%\s*,\s*(\d{1,3})\%\s*\)$#i';
$hsla = '#^hsla\s*\(\s*(\d{1,3})\s*,\s*(\d{1,3})\%\s*,\s*(\d{1,3})\%\s*,\s*(\d{1}(\.\d+)?)\s*\)$#i';
if (in_array(strtolower($value), array('inherit'))) {
return true;
} else if (preg_match($hex, $value)) {
return true;
} else if (in_array(strtolower($value), array_keys(css_optimiser::$htmlcolours))) {
return true;
} else if (preg_match($rgb, $value, $m) && $m[1] < 256 && $m[2] < 256 && $m[3] < 256) {
// It is an RGB colour
return true;
} else if (preg_match($rgba, $value, $m) && $m[1] < 256 && $m[2] < 256 && $m[3] < 256) {
// It is an RGBA colour
return true;
} else if (preg_match($hsl, $value, $m) && $m[1] <= 360 && $m[2] <= 100 && $m[3] <= 100) {
// It is an HSL colour
return true;
} else if (preg_match($hsla, $value, $m) && $m[1] <= 360 && $m[2] <= 100 && $m[3] <= 100) {
// It is an HSLA colour
return true;
}
// Doesn't look like a colour.
return false;
}
/**
* Returns true is the passed value looks like a CSS width.
* In order to pass this test the value must be purely numerical or end with a
* valid CSS unit term.
*
* @param string|int $value
* @return boolean
*/
function css_is_width($value) {
$value = trim($value);
if (in_array(strtolower($value), array('auto', 'inherit'))) {
return true;
}
if ((string)$value === '0' || preg_match('#^(\-\s*)?(\d*\.)?(\d+)\s*(em|px|pt|\%|in|cm|mm|ex|pc)$#i', $value)) {
return true;
}
return false;
}
/**
* A simple sorting function to sort two array values on the number of items they contain
*
* @param array $a
* @param array $b
* @return int
*/
function css_sort_by_count(array $a, array $b) {
$a = count($a);
$b = count($b);
if ($a == $b) {
return 0;
}
return ($a > $b) ? -1 : 1;
}
/**
* A basic CSS optimiser that strips out unwanted things and then processing the
* CSS organising styles and moving duplicates and useless CSS.
*
* This CSS optimiser works by reading through a CSS string one character at a
* time and building an object structure of the CSS.
* As part of that processing styles are expanded out as much as they can be to
* ensure we collect all mappings, at the end of the processing those styles are
* then combined into an optimised form to keep them as short as possible.
*
* @package core
* @category css
* @copyright 2012 Sam Hemelryk
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class css_optimiser {
/**
* Used when the processor is about to start processing.
* Processing states. Used internally.
*/
const PROCESSING_START = 0;
/**
* Used when the processor is currently processing a selector.
* Processing states. Used internally.
*/
const PROCESSING_SELECTORS = 0;
/**
* Used when the processor is currently processing a style.
* Processing states. Used internally.
*/
const PROCESSING_STYLES = 1;
/**
* Used when the processor is currently processing a comment.
* Processing states. Used internally.
*/
const PROCESSING_COMMENT = 2;
/**
* Used when the processor is currently processing an @ rule.
* Processing states. Used internally.
*/
const PROCESSING_ATRULE = 3;
/**
* The raw string length before optimisation.
* Stats variables set during and after processing
* @var int
*/
protected $rawstrlen = 0;
/**
* The number of comments that were removed during optimisation.
* Stats variables set during and after processing
* @var int
*/
protected $commentsincss = 0;
/**
* The number of rules in the CSS before optimisation.
* Stats variables set during and after processing
* @var int
*/
protected $rawrules = 0;
/**
* The number of selectors using in CSS rules before optimisation.
* Stats variables set during and after processing
* @var int
*/
protected $rawselectors = 0;
/**
* The string length after optimisation.
* Stats variables set during and after processing
* @var int
*/
protected $optimisedstrlen = 0;
/**
* The number of rules after optimisation.
* Stats variables set during and after processing
* @var int
*/
protected $optimisedrules = 0;
/**
* The number of selectors used in rules after optimisation.
* Stats variables set during and after processing
* @var int
*/
protected $optimisedselectors = 0;
/**
* The start time of the optimisation.
* Stats variables set during and after processing
* @var int
*/
protected $timestart = 0;
/**
* The end time of the optimisation.
* Stats variables set during and after processing
* @var int
*/
protected $timecomplete = 0;
/**
* Will be set to any errors that may have occured during processing.
* This is updated only at the end of processing NOT during.
*
* @var array
*/
protected $errors = array();
/**
* Processes incoming CSS optimising it and then returning it.
*
* @param string $css The raw CSS to optimise
* @return string The optimised CSS
*/
public function process($css) {
global $CFG;
// Easiest win there is
$css = trim($css);
$this->reset_stats();
$this->timestart = microtime(true);
$this->rawstrlen = strlen($css);
// Don't try to process files with no content... it just doesn't make sense.
// But we should produce an error for them, an empty CSS file will lead to a
// useless request for those running theme designer mode.
if ($this->rawstrlen === 0) {
$this->errors[] = 'Skipping file as it has no content.';
return '';
}
// First up we need to remove all line breaks - this allows us to instantly
// reduce our processing requirements and as we will process everything
// into a new structure there's really nothing lost.
$css = preg_replace('#\r?\n#', ' ', $css);
// Next remove the comments... no need to them in an optimised world and
// knowing they're all gone allows us to REALLY make our processing simpler
$css = preg_replace('#/\*(.*?)\*/#m', '', $css, -1, $this->commentsincss);
$medias = array(
'all' => new css_media()
);
$imports = array();
$charset = false;
// Keyframes are used for CSS animation they will be processed right at the very end.
$keyframes = array();
$currentprocess = self::PROCESSING_START;
$currentrule = css_rule::init();
$currentselector = css_selector::init();
$inquotes = false; // ' or "
$inbraces = false; // {
$inbrackets = false; // [
$inparenthesis = false; // (
$currentmedia = $medias['all'];
$currentatrule = null;
$suspectatrule = false;
$buffer = '';
$char = null;
// Next we are going to iterate over every single character in $css.
// This is why we removed line breaks and comments!
for ($i = 0; $i < $this->rawstrlen; $i++) {
$lastchar = $char;
$char = substr($css, $i, 1);
if ($char == '@' && $buffer == '') {
$suspectatrule = true;
}
switch ($currentprocess) {
// Start processing an @ rule e.g. @media, @page, @keyframes
case self::PROCESSING_ATRULE:
switch ($char) {
case ';':
if (!$inbraces) {
$buffer .= $char;
if ($currentatrule == 'import') {
$imports[] = $buffer;
$currentprocess = self::PROCESSING_SELECTORS;
} else if ($currentatrule == 'charset') {
$charset = $buffer;
$currentprocess = self::PROCESSING_SELECTORS;
}
}
if ($currentatrule !== 'media') {
$buffer = '';
$currentatrule = false;
}
// continue 1: The switch processing chars
// continue 2: The switch processing the state
// continue 3: The for loop
continue 3;
case '{':
if ($currentatrule == 'media' && preg_match('#\s*@media\s*([a-zA-Z0-9]+(\s*,\s*[a-zA-Z0-9]+)*)\s*{#', $buffer, $matches)) {
// Basic media declaration
$mediatypes = str_replace(' ', '', $matches[1]);
if (!array_key_exists($mediatypes, $medias)) {
$medias[$mediatypes] = new css_media($mediatypes);
}
$currentmedia = $medias[$mediatypes];
$currentprocess = self::PROCESSING_SELECTORS;
$buffer = '';
} else if ($currentatrule == 'media' && preg_match('#\s*@media\s*([^{]+)#', $buffer, $matches)) {
// Advanced media query declaration http://www.w3.org/TR/css3-mediaqueries/
$mediatypes = $matches[1];
$hash = md5($mediatypes);
$medias[$hash] = new css_media($mediatypes);
$currentmedia = $medias[$hash];
$currentprocess = self::PROCESSING_SELECTORS;
$buffer = '';
} else if ($currentatrule == 'keyframes' && preg_match('#@((\-moz\-|\-webkit\-)?keyframes)\s*([^\s]+)#', $buffer, $matches)) {
// Keyframes declaration, we treat it exactly like a @media declaration except we don't allow
// them to be overridden to ensure we don't mess anything up. (means we keep everything in order)
$keyframefor = $matches[1];
$keyframename = $matches[3];
$keyframe = new css_keyframe($keyframefor, $keyframename);
$keyframes[] = $keyframe;
$currentmedia = $keyframe;
$currentprocess = self::PROCESSING_SELECTORS;
$buffer = '';
}
// continue 1: The switch processing chars
// continue 2: The switch processing the state
// continue 3: The for loop
continue 3;
}
break;
// Start processing selectors
case self::PROCESSING_START:
case self::PROCESSING_SELECTORS:
switch ($char) {
case '[':
$inbrackets ++;
$buffer .= $char;
// continue 1: The switch processing chars
// continue 2: The switch processing the state
// continue 3: The for loop
continue 3;
case ']':
$inbrackets --;
$buffer .= $char;
// continue 1: The switch processing chars
// continue 2: The switch processing the state
// continue 3: The for loop
continue 3;
case ' ':
if ($inbrackets) {
// continue 1: The switch processing chars
// continue 2: The switch processing the state
// continue 3: The for loop
continue 3;
}
if (!empty($buffer)) {
// Check for known @ rules
if ($suspectatrule && preg_match('#@(media|import|charset|(\-moz\-|\-webkit\-)?(keyframes))\s*#', $buffer, $matches)) {
$currentatrule = (!empty($matches[3]))?$matches[3]:$matches[1];
$currentprocess = self::PROCESSING_ATRULE;
$buffer .= $char;
} else {
$currentselector->add($buffer);
$buffer = '';
}
}
$suspectatrule = false;
// continue 1: The switch processing chars
// continue 2: The switch processing the state
// continue 3: The for loop
continue 3;
case '{':
if ($inbrackets) {
// continue 1: The switch processing chars
// continue 2: The switch processing the state
// continue 3: The for loop
continue 3;
}
if ($buffer !== '') {
$currentselector->add($buffer);
}
$currentrule->add_selector($currentselector);
$currentselector = css_selector::init();
$currentprocess = self::PROCESSING_STYLES;
$buffer = '';
// continue 1: The switch processing chars
// continue 2: The switch processing the state
// continue 3: The for loop
continue 3;
case '}':
if ($inbrackets) {
// continue 1: The switch processing chars
// continue 2: The switch processing the state
// continue 3: The for loop
continue 3;
}
if ($currentatrule == 'media') {
$currentmedia = $medias['all'];
$currentatrule = false;
$buffer = '';
} else if (strpos($currentatrule, 'keyframes') !== false) {
$currentmedia = $medias['all'];
$currentatrule = false;
$buffer = '';
}
// continue 1: The switch processing chars
// continue 2: The switch processing the state
// continue 3: The for loop
continue 3;
case ',':
if ($inbrackets) {
// continue 1: The switch processing chars
// continue 2: The switch processing the state
// continue 3: The for loop
continue 3;
}
$currentselector->add($buffer);
$currentrule->add_selector($currentselector);
$currentselector = css_selector::init();
$buffer = '';
// continue 1: The switch processing chars
// continue 2: The switch processing the state
// continue 3: The for loop
continue 3;
}
break;
// Start processing styles
case self::PROCESSING_STYLES:
if ($char == '"' || $char == "'") {
if ($inquotes === false) {
$inquotes = $char;
}
if ($inquotes === $char && $lastchar !== '\\') {
$inquotes = false;
}
}
if ($inquotes) {
$buffer .= $char;
continue 2;
}
switch ($char) {
case ';':
if ($inparenthesis) {
$buffer .= $char;
// continue 1: The switch processing chars
// continue 2: The switch processing the state
// continue 3: The for loop
continue 3;
}
$currentrule->add_style($buffer);
$buffer = '';
$inquotes = false;
// continue 1: The switch processing chars
// continue 2: The switch processing the state
// continue 3: The for loop
continue 3;
case '}':
$currentrule->add_style($buffer);
$this->rawselectors += $currentrule->get_selector_count();
$currentmedia->add_rule($currentrule);
$currentrule = css_rule::init();
$currentprocess = self::PROCESSING_SELECTORS;
$this->rawrules++;
$buffer = '';
$inquotes = false;
$inparenthesis = false;
// continue 1: The switch processing chars
// continue 2: The switch processing the state
// continue 3: The for loop
continue 3;
case '(':
$inparenthesis = true;
$buffer .= $char;
// continue 1: The switch processing chars
// continue 2: The switch processing the state
// continue 3: The for loop
continue 3;
case ')':
$inparenthesis = false;
$buffer .= $char;
// continue 1: The switch processing chars
// continue 2: The switch processing the state
// continue 3: The for loop
continue 3;
}
break;
}
$buffer .= $char;
}
foreach ($medias as $media) {
$this->optimise($media);
}
$css = $this->produce_css($charset, $imports, $medias, $keyframes);
$this->timecomplete = microtime(true);
return trim($css);
}
/**
* Produces CSS for the given charset, imports, media, and keyframes
* @param string $charset
* @param array $imports
* @param array $medias
* @param array $keyframes
* @return string
*/
protected function produce_css($charset, array $imports, array $medias, array $keyframes) {
$css = '';
if (!empty($charset)) {
$imports[] = $charset;
}
if (!empty($imports)) {
$css .= implode("\n", $imports);
$css .= "\n\n";
}
$cssreset = array();
$cssstandard = array();
$csskeyframes = array();
// Process each media declaration individually
foreach ($medias as $media) {
// If this declaration applies to all media types
if (in_array('all', $media->get_types())) {
// Collect all rules that represet reset rules and remove them from the media object at the same time.
// We do this because we prioritise reset rules to the top of a CSS output. This ensures that they
// can't end up out of order because of optimisation.
$resetrules = $media->get_reset_rules(true);
if (!empty($resetrules)) {
$cssreset[] = css_writer::media('all', $resetrules);
}
}
// Get the standard cSS
$cssstandard[] = $media->out();
}
// Finally if there are any keyframe declarations process them now.
if (count($keyframes) > 0) {
foreach ($keyframes as $keyframe) {
$this->optimisedrules += $keyframe->count_rules();
$this->optimisedselectors += $keyframe->count_selectors();
if ($keyframe->has_errors()) {
$this->errors += $keyframe->get_errors();
}
$csskeyframes[] = $keyframe->out();
}
}
// Join it all together
$css .= join('', $cssreset);
$css .= join('', $cssstandard);
$css .= join('', $csskeyframes);
// Record the strlenght of the now optimised CSS.
$this->optimisedstrlen = strlen($css);
// Return the now produced CSS
return $css;
}
/**
* Optimises the CSS rules within a rule collection of one form or another
*
* @param css_rule_collection $media
* @return void This function acts in reference
*/
protected function optimise(css_rule_collection $media) {
$media->organise_rules_by_selectors();
$this->optimisedrules += $media->count_rules();
$this->optimisedselectors += $media->count_selectors();
if ($media->has_errors()) {
$this->errors += $media->get_errors();
}
}
/**
* Returns an array of stats from the last processing run
* @return string
*/
public function get_stats() {
$stats = array(
'timestart' => $this->timestart,
'timecomplete' => $this->timecomplete,
'timetaken' => round($this->timecomplete - $this->timestart, 4),
'commentsincss' => $this->commentsincss,
'rawstrlen' => $this->rawstrlen,
'rawselectors' => $this->rawselectors,
'rawrules' => $this->rawrules,
'optimisedstrlen' => $this->optimisedstrlen,
'optimisedrules' => $this->optimisedrules,
'optimisedselectors' => $this->optimisedselectors,
'improvementstrlen' => '-',
'improvementrules' => '-',
'improvementselectors' => '-',
);
// Avoid division by 0 errors by checking we have valid raw values
if ($this->rawstrlen > 0) {
$stats['improvementstrlen'] = round(100 - ($this->optimisedstrlen / $this->rawstrlen) * 100, 1).'%';
}
if ($this->rawrules > 0) {
$stats['improvementrules'] = round(100 - ($this->optimisedrules / $this->rawrules) * 100, 1).'%';
}
if ($this->rawselectors > 0) {
$stats['improvementselectors'] = round(100 - ($this->optimisedselectors / $this->rawselectors) * 100, 1).'%';
}
return $stats;
}
/**
* Returns true if any errors have occured during processing
*
* @return bool
*/
public function has_errors() {
return !empty($this->errors);
}
/**
* Returns an array of errors that have occured
*
* @param bool $clear If set to true the errors will be cleared after being returned.
* @return array
*/
public function get_errors($clear = false) {
$errors = $this->errors;
if ($clear) {
// Reset the error array
$this->errors = array();
}
return $errors;
}
/**
* Returns any errors as a string that can be included in CSS.
*
* @return string
*/
public function output_errors_css() {
$computedcss = "/****************************************\n";
$computedcss .= " *--- Errors found during processing ----\n";
foreach ($this->errors as $error) {
$computedcss .= preg_replace('#^#m', '* ', $error);
}
$computedcss .= " ****************************************/\n\n";
return $computedcss;
}
/**
* Returns a string to display stats about the last generation within CSS output
*
* @return string
*/
public function output_stats_css() {
$computedcss = "/****************************************\n";
$computedcss .= " *------- CSS Optimisation stats --------\n";
if ($this->rawstrlen === 0) {
$computedcss .= " File not processed as it has no content /\n\n";
$computedcss .= " ****************************************/\n\n";
return $computedcss;
} else if ($this->rawrules === 0) {
$computedcss .= " File contained no rules to be processed /\n\n";
$computedcss .= " ****************************************/\n\n";
return $computedcss;
}
$stats = $this->get_stats();
$computedcss .= " * ".date('r')."\n";
$computedcss .= " * {$stats['commentsincss']} \t comments removed\n";
$computedcss .= " * Optimisation took {$stats['timetaken']} seconds\n";
$computedcss .= " *--------------- before ----------------\n";
$computedcss .= " * {$stats['rawstrlen']} \t chars read in\n";
$computedcss .= " * {$stats['rawrules']} \t rules read in\n";
$computedcss .= " * {$stats['rawselectors']} \t total selectors\n";
$computedcss .= " *---------------- after ----------------\n";
$computedcss .= " * {$stats['optimisedstrlen']} \t chars once optimized\n";
$computedcss .= " * {$stats['optimisedrules']} \t optimized rules\n";
$computedcss .= " * {$stats['optimisedselectors']} \t total selectors once optimized\n";
$computedcss .= " *---------------- stats ----------------\n";
$computedcss .= " * {$stats['improvementstrlen']} \t reduction in chars\n";
$computedcss .= " * {$stats['improvementrules']} \t reduction in rules\n";
$computedcss .= " * {$stats['improvementselectors']} \t reduction in selectors\n";
$computedcss .= " ****************************************/\n\n";
return $computedcss;
}
/**
* Resets the stats ready for another fresh processing
*/
public function reset_stats() {
$this->commentsincss = 0;
$this->optimisedrules = 0;
$this->optimisedselectors = 0;
$this->optimisedstrlen = 0;
$this->rawrules = 0;
$this->rawselectors = 0;
$this->rawstrlen = 0;
$this->timecomplete = 0;
$this->timestart = 0;
}
/**
* An array of the common HTML colours that are supported by most browsers.
*
* This reference table is used to allow us to unify colours, and will aid
* us in identifying buggy CSS using unsupported colours.
*
* @staticvar array