/
gzipit.php
2592 lines (2469 loc) · 87.6 KB
/
gzipit.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
/**
*
* GzipIt 1.2
*
* Single file solution for CSS and JavaScript combination,
* minimization, gzipping and caching.
*
* For documentation, requirements, updates and support please visit:
* http://code.google.com/p/gzipit/
*
* Inspired by CSS and Javascript Combinator by Niels Leenheer
* (http://rakaz.nl/code/combine)
*
* See copyright and licences below for bundled components.
*
* --
* Copyright (c) 2010-2012 Artem Volk (www.artvolk.sumy.ua)
*
* 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 gzipit
* @author Artem Volk <artvolk@gmail.com>
* @license http://opensource.org/licenses/mit-license.php MIT License
* @version 1.0 ($Id$)
* @link http://code.google.com/p/gzipit/
*/
/**
* Configuration section
* *****************************************************************************************************************
*/
// Use gzip compression
if (!defined('GZIPIT_COMPRESSION'))
define('GZIPIT_COMPRESSION', true);
// IE6 is buggy with gzip, you can turn gzip for this browser completely using this parameter
if (!defined('GZIPIT_COMPRESSION_FOR_IE6'))
define('GZIPIT_COMPRESSION_FOR_IE6', true);
// Compresion level (from 0 to 9)
if (!defined('GZIPIT_GZIP_LEVEL'))
define('GZIPIT_GZIP_LEVEL', 9);
// Cache files on disk (with minimizing enabled this should be enabled)
if (!defined('GZIPIT_DISK_CACHE'))
define('GZIPIT_DISK_CACHE', true);
// Minimize CSS files
if (!defined('GZIPIT_CSSMIN'))
define('GZIPIT_CSSMIN', true);
// Minimize JavaScript files
if (!defined('GZIPIT_JSMIN'))
define('GZIPIT_JSMIN', true);
// Include filename into combined output (useful for debug)
if (!defined('GZIPIT_INCLUDE_FILENAME'))
define('GZIPIT_INCLUDE_FILENAME', true);
// Directory where output files will be cached (can be placed outside of document root)
if (!defined('GZIPIT_DIR_CACHE'))
define('GZIPIT_DIR_CACHE', dirname(__FILE__) . '/tmp');
// Directory where original CSS files are stored (sub directories are accessible too)
if (!defined('GZIPIT_DIR_CSS'))
define('GZIPIT_DIR_CSS', dirname(__FILE__) . '/css');
// Directory where original CSS files are stored (sub directories are accessible too)
if (!defined('GZIPIT_DIR_JS'))
define('GZIPIT_DIR_JS', dirname(__FILE__) . '/js');
// Send 'ETag' header (calculated automatically)
if (!defined('GZIPIT_HEADER_ETAG'))
define('GZIPIT_HEADER_ETAG', true);
// Send 'Last-Modified' header (calculated automatically)
if (!defined('GZIPIT_HEADER_LAST_MODIFIED'))
define('GZIPIT_HEADER_LAST_MODIFIED', true);
// Send 'Cache-Control' header
if (!defined('GZIPIT_HEADER_CACHE_CONTROL'))
define('GZIPIT_HEADER_CACHE_CONTROL', true);
// Value for the 'Cache-Control' header
if (!defined('GZIPIT_HEADER_CACHE_CONTROL_VALUE'))
define('GZIPIT_HEADER_CACHE_CONTROL_VALUE', 'max-age=315360000');
// Send 'Expires' header
if (!defined('GZIPIT_HEADER_EXPIRES'))
define('GZIPIT_HEADER_EXPIRES', true);
// Value for the 'Expires' header
if (!defined('GZIPIT_HEADER_EXPIRES_VALUE'))
define('GZIPIT_HEADER_EXPIRES_VALUE', 'Thu, 31 Dec 2037 23:55:55 GMT');
// NOTE: Specify name of the asset file OR assets array, but not the two at the same time
if (!defined('GZIPIT_ASSETS_FILE'))
define('GZIPIT_ASSETS_FILE', 'assets.php');
/*
Example of $GZIPIT_ASSETS
$GZIPIT_ASSETS = array(
'css-default' => array(
'type' => 'css',
'files' => array(
'file1.css',
'file2.css',
...
)
),
'js-default' => array(
'type' => 'javascript',
'files' => array(
'file1.js',
'file2.js',
...
)
),
);
*/
$GZIPIT_ASSETS = array(
);
/**
* Just code below
* No user-serviceable parts inside :)
* *****************************************************************************************************************
*/
// Other constants and parameters
define('GZIPIT_FILELIST_DELIMITER', ',');
define('GZIPIT_ENCODING_NONE', 'none');
define('GZIPIT_ENCODING_GZIP', 'gzip');
$GZIPIT_ENCODING_TYPES = array(
GZIPIT_ENCODING_NONE,
GZIPIT_ENCODING_GZIP
);
define('GZIPIT_TYPE_CSS', 'css');
define('GZIPIT_TYPE_JS', 'javascript');
$GZIPIT_TYPES = array(
GZIPIT_TYPE_CSS,
GZIPIT_TYPE_JS
);
$GZIPIT_CONTENT_TYPES = array(
GZIPIT_TYPE_CSS => 'text/css',
GZIPIT_TYPE_JS => 'text/javascript'
);
$GZIPIT_EXTENSIONS = array(
GZIPIT_TYPE_CSS => 'css',
GZIPIT_TYPE_JS => 'js'
);
$GZIPIT_PATHES = array(
GZIPIT_TYPE_CSS => GZIPIT_DIR_CSS,
GZIPIT_TYPE_JS => GZIPIT_DIR_JS
);
if (GZIPIT_ASSETS_FILE != NULL && GZIPIT_ASSETS_FILE != '' && GZIPIT_ASSETS_FILE !== false)
{
require_once(GZIPIT_ASSETS_FILE);
}
ob_start();
/**
* Parse GET parameters
*/
$type = get_param('type', true);
$files = get_param('files');
$asset = get_param('asset');
// Check if asset name specified
if ($asset != NULL)
{
if (isset($GZIPIT_ASSETS[$asset]))
{
$files = $GZIPIT_ASSETS[$asset]['files'];
$type = $GZIPIT_ASSETS[$asset]['type'];
}
else
{
give_404('Incorrect asset name');
exit;
}
}
// Get files list and type
if ($files !== NULL && $type !== NULL)
{
if (in_array($type, $GZIPIT_TYPES))
{
if ($asset === NULL)
{
$elements = explode(GZIPIT_FILELIST_DELIMITER, $files);
}
else
{
$elements = $GZIPIT_ASSETS[$asset]['files'];
}
}
else
{
give_404('Incorrect type specified');
exit;
}
}
else
{
if ($asset == NULL)
{
give_404('Incorrect files and type parameters');
}
else
{
give_404('Incorrect asset definition');
}
exit;
}
/**
* Determine supported compression
*
*/
if (GZIPIT_COMPRESSION)
{
$temp = getAcceptedEncoding();
if ($temp[0] == GZIPIT_ENCODING_GZIP)
{
$encoding = GZIPIT_ENCODING_GZIP;
$encoding_header = $temp[1];
}
else
{
$encoding = GZIPIT_ENCODING_NONE;
$encoding_header = NULL;
}
}
else
{
$encoding = GZIPIT_ENCODING_NONE;
$encoding_header = NULL;
}
/**
* Find last date and time of last modification of files
*/
$last_modified = 0;
$base_path = realpath($GZIPIT_PATHES[$type]);
$ext = $GZIPIT_EXTENSIONS[$type];
foreach ($elements as $element)
{
$path = realpath($base_path . DIRECTORY_SEPARATOR . $element);
if ($path === false ||
substr($path, -1 * strlen($ext)) != $ext ||
substr($path, 0, strlen($base_path)) != $base_path ||
!file_exists($path))
{
$message = sprintf('File "%s" not found', htmlspecialchars($element));
give_404($message);
exit;
}
$last_modified = max($last_modified, filemtime($path));
}
/**
* Construct and send ETag if enabled
*/
$etag = sprintf('%s-%s', $last_modified, md5(implode(GZIPIT_FILELIST_DELIMITER, $elements) . $type . (string)(GZIPIT_CSSMIN || GZIPIT_JSMIN) . $encoding_header));
if (GZIPIT_HEADER_ETAG)
{
header ('Etag: "' . $etag . '"');
}
/**
* Let's do it!
*/
// Check Etag
if (GZIPIT_HEADER_ETAG && isset($_SERVER['HTTP_IF_NONE_MATCH']) &&
stripslashes($_SERVER['HTTP_IF_NONE_MATCH']) == '"' . $etag . '"')
{
header ("HTTP/1.0 304 Not Modified");
ob_end_clean();
exit;
}
else // No Etag specified
{
// Send headers
header('Content-Type: ' . $GZIPIT_CONTENT_TYPES[$type]);
if (GZIPIT_HEADER_LAST_MODIFIED)
{
header('Last-Modified: ' . gmdate("D, d M Y H:i:s", $last_modified)." GMT");
}
if (GZIPIT_HEADER_EXPIRES)
{
header('Expires: ' . GZIPIT_HEADER_EXPIRES_VALUE);
}
if (GZIPIT_HEADER_CACHE_CONTROL)
{
header('Cache-Control: ' . GZIPIT_HEADER_CACHE_CONTROL_VALUE);
}
$cached_file =
realpath(GZIPIT_DIR_CACHE) .
DIRECTORY_SEPARATOR .
sprintf('cache-%s%s.%s%s',
$etag,
(($type == GZIPIT_TYPE_CSS && GZIPIT_CSSMIN) || ($type == GZIPIT_TYPE_JS && GZIPIT_JSMIN)) ? '-min' : '',
$GZIPIT_EXTENSIONS[$type],
($encoding != GZIPIT_ENCODING_NONE) ? '.' . $encoding : ''
);
// If we have cached file, return it to the client
if (GZIPIT_DISK_CACHE && file_exists($cached_file))
{
if ($fp = fopen($cached_file, 'rb'))
{
if ($encoding_header != NULL)
header('Content-Encoding: ' . $encoding);
header('Content-Length: ' . filesize($cached_file));
fpassthru($fp);
fclose($fp);
ob_end_flush();
exit;
}
else
{
give_404('Error reading cached file');
exit;
}
}
// Perform combining, minimization and compression
$content = '';
foreach ($elements as $element)
{
$path = realpath($base_path . DIRECTORY_SEPARATOR . $element);
$temp = file_get_contents($path);
$content .= "\n\n";
if (GZIPIT_INCLUDE_FILENAME)
{
$content .= sprintf("/* %s */\n", $element);
}
if ($type == GZIPIT_TYPE_CSS && GZIPIT_CSSMIN)
{
$temp = CssMin::minify($temp);
}
if ($type == GZIPIT_TYPE_JS && GZIPIT_JSMIN)
{
$temp = JSMin::minify($temp);
}
$content .= $temp;
}
if ($encoding != GZIPIT_ENCODING_NONE)
{
$content = gzencode($content, GZIPIT_GZIP_LEVEL, FORCE_GZIP);
header ('Content-Encoding: ' . $encoding_header);
}
header ('Content-Length: ' . strlen($content));
echo $content;
if (GZIPIT_DISK_CACHE)
{
if ($fp = fopen($cached_file, 'wb'))
{
fwrite($fp, $content);
fclose($fp);
}
}
} //else (no Etag)
/**
* The End
*/
ob_end_flush();
exit;
/**
* Utility functions
*/
/**
* Renders 404 error to client
*
* @param string $message Detailed error message
* @return void
*/
function give_404($message)
{
printf('
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html>
<head>
<title>404 Not Found</title>
</head>
<body>
<h1>Not Found</h1>
<p>%s</p>
</body>
</html>
', $message);
header("HTTP/1.0 404 Not Found");
ob_end_flush();
}
/**
* Parses HTTP GET params
*
* @param string $param Parameter name
* @param bool $trim Convert parameter value to lowercase and trim it
* @return string|NULL Returns NULL if parameter doesn't exist
*/
function get_param($param, $trim = false)
{
return
isset($_GET[$param]) ?
($trim ? strtolower(trim($_GET[$param])) : $_GET[$param]) :
NULL;
}
/**
* Returns client's accepted encoding
* Code taken from Minify (http://code.google.com/p/minify/)
*
* @return void bool If client supports gzip
*/
function getAcceptedEncoding()
{
// @link http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html
if (! isset($_SERVER['HTTP_ACCEPT_ENCODING'])
|| isBuggyIe())
{
return array('', '');
}
$ae = $_SERVER['HTTP_ACCEPT_ENCODING'];
// gzip checks (quick)
if (0 === strpos($ae, 'gzip,') // most browsers
|| 0 === strpos($ae, 'deflate, gzip,') // opera
) {
return array('gzip', 'gzip');
}
// gzip checks (slow)
if (preg_match(
'@(?:^|,)\\s*((?:x-)?gzip)\\s*(?:$|,|;\\s*q=(?:0\\.|1))@'
,$ae
,$m)) {
return array('gzip', $m[1]);
}
}
/**
* Detect IE with buggy compression support (version earlier than 6 SP2)
* Code taken from Minify (http://code.google.com/p/minify/)
*
* @link http://code.google.com/p/minify/
* @return bool If client uses IE with buggy gzip support
*/
function isBuggyIe()
{
$ua = $_SERVER['HTTP_USER_AGENT'];
// quick escape for non-IEs
if (0 !== strpos($ua, 'Mozilla/4.0 (compatible; MSIE ')
|| false !== strpos($ua, 'Opera')) {
return false;
}
// no regex = faaast
$version = (float)substr($ua, 30);
return GZIPIT_COMPRESSION_FOR_IE6
? ($version < 6 || ($version == 6 && false === strpos($ua, 'SV1')))
: ($version < 7);
}
/**
* JSmin
* http://github.com/rgrove/jsmin-php/
* *****************************************************************************************************************
*/
/**
* jsmin.php - PHP implementation of Douglas Crockford's JSMin.
*
* This is pretty much a direct port of jsmin.c to PHP with just a few
* PHP-specific performance tweaks. Also, whereas jsmin.c reads from stdin and
* outputs to stdout, this library accepts a string as input and returns another
* string as output.
*
* PHP 5 or higher is required.
*
* Permission is hereby granted to use this version of the library under the
* same terms as jsmin.c, which has the following license:
*
* --
* Copyright (c) 2002 Douglas Crockford (www.crockford.com)
*
* 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 shall be used for Good, not Evil.
*
* 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 JSMin
* @author Ryan Grove <ryan@wonko.com>
* @copyright 2002 Douglas Crockford <douglas@crockford.com> (jsmin.c)
* @copyright 2008 Ryan Grove <ryan@wonko.com> (PHP port)
* @license http://opensource.org/licenses/mit-license.php MIT License
* @version 1.1.1 (2008-03-02)
* @link http://code.google.com/p/jsmin-php/
*/
class JSMin {
const ORD_LF = 10;
const ORD_SPACE = 32;
protected $a = '';
protected $b = '';
protected $input = '';
protected $inputIndex = 0;
protected $inputLength = 0;
protected $lookAhead = null;
protected $output = '';
// -- Public Static Methods --------------------------------------------------
public static function minify($js) {
$jsmin = new JSMin($js);
return $jsmin->min();
}
// -- Public Instance Methods ------------------------------------------------
public function __construct($input) {
$this->input = str_replace("\r\n", "\n", $input);
$this->inputLength = strlen($this->input);
}
// -- Protected Instance Methods ---------------------------------------------
/* action -- do something! What you do is determined by the argument:
1 Output A. Copy B to A. Get the next B.
2 Copy B to A. Get the next B. (Delete A).
3 Get the next B. (Delete B).
action treats a string as a single character. Wow!
action recognizes a regular expression if it is preceded by ( or , or =.
*/
protected function action($d) {
switch($d) {
case 1:
$this->output .= $this->a;
case 2:
$this->a = $this->b;
if ($this->a === "'" || $this->a === '"') {
for (;;) {
$this->output .= $this->a;
$this->a = $this->get();
if ($this->a === $this->b) {
break;
}
if (ord($this->a) <= self::ORD_LF) {
throw new JSMinException('Unterminated string literal.');
}
if ($this->a === '\\') {
$this->output .= $this->a;
$this->a = $this->get();
}
}
}
case 3:
$this->b = $this->next();
if ($this->b === '/' && (
$this->a === '(' || $this->a === ',' || $this->a === '=' ||
$this->a === ':' || $this->a === '[' || $this->a === '!' ||
$this->a === '&' || $this->a === '|' || $this->a === '?' ||
$this->a === '{' || $this->a === '}' || $this->a === ';' ||
$this->a === "\n" )) {
$this->output .= $this->a . $this->b;
for (;;) {
$this->a = $this->get();
if ($this->a === '[') {
/*
inside a regex [...] set, which MAY contain a '/' itself. Example: mootools Form.Validator near line 460:
return Form.Validator.getValidator('IsEmpty').test(element) || (/^(?:[a-z0-9!#$%&'*+/=?^_`{|}~-]\.?){0,63}[a-z0-9!#$%&'*+/=?^_`{|}~-]@(?:(?:[a-z0-9](?:[a-z0-9-]{0,61}[a-z0-9])?\.)*[a-z0-9](?:[a-z0-9-]{0,61}[a-z0-9])?|\[(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\])$/i).test(element.get('value'));
*/
for (;;) {
$this->output .= $this->a;
$this->a = $this->get();
if ($this->a === ']') {
break;
} elseif ($this->a === '\\') {
$this->output .= $this->a;
$this->a = $this->get();
} elseif (ord($this->a) <= self::ORD_LF) {
throw new JSMinException('Unterminated regular expression set in regex literal.');
}
}
} elseif ($this->a === '/') {
break;
} elseif ($this->a === '\\') {
$this->output .= $this->a;
$this->a = $this->get();
} elseif (ord($this->a) <= self::ORD_LF) {
throw new JSMinException('Unterminated regular expression literal.');
}
$this->output .= $this->a;
}
$this->b = $this->next();
}
}
}
protected function get() {
$c = $this->lookAhead;
$this->lookAhead = null;
if ($c === null) {
if ($this->inputIndex < $this->inputLength) {
$c = substr($this->input, $this->inputIndex, 1);
$this->inputIndex += 1;
} else {
$c = null;
}
}
if ($c === "\r") {
return "\n";
}
if ($c === null || $c === "\n" || ord($c) >= self::ORD_SPACE) {
return $c;
}
return ' ';
}
/* isAlphanum -- return true if the character is a letter, digit, underscore,
dollar sign, or non-ASCII character.
*/
protected function isAlphaNum($c) {
return ord($c) > 126 || $c === '\\' || preg_match('/^[\w\$]$/', $c) === 1;
}
protected function min() {
$this->a = "\n";
$this->action(3);
while ($this->a !== null) {
switch ($this->a) {
case ' ':
if ($this->isAlphaNum($this->b)) {
$this->action(1);
} else {
$this->action(2);
}
break;
case "\n":
switch ($this->b) {
case '{':
case '[':
case '(':
case '+':
case '-':
$this->action(1);
break;
case ' ':
$this->action(3);
break;
default:
if ($this->isAlphaNum($this->b)) {
$this->action(1);
}
else {
$this->action(2);
}
}
break;
default:
switch ($this->b) {
case ' ':
if ($this->isAlphaNum($this->a)) {
$this->action(1);
break;
}
$this->action(3);
break;
case "\n":
switch ($this->a) {
case '}':
case ']':
case ')':
case '+':
case '-':
case '"':
case "'":
$this->action(1);
break;
default:
if ($this->isAlphaNum($this->a)) {
$this->action(1);
}
else {
$this->action(3);
}
}
break;
default:
$this->action(1);
break;
}
}
}
return $this->output;
}
/* next -- get the next character, excluding comments. peek() is used to see
if a '/' is followed by a '/' or '*'.
*/
protected function next() {
$c = $this->get();
if ($c === '/') {
switch($this->peek()) {
case '/':
for (;;) {
$c = $this->get();
if (ord($c) <= self::ORD_LF) {
return $c;
}
}
case '*':
$this->get();
for (;;) {
switch($this->get()) {
case '*':
if ($this->peek() === '/') {
$this->get();
return ' ';
}
break;
case null:
throw new JSMinException('Unterminated comment.');
}
}
default:
return $c;
}
}
return $c;
}
protected function peek() {
$this->lookAhead = $this->get();
return $this->lookAhead;
}
}
// -- Exceptions ---------------------------------------------------------------
class JSMinException extends Exception {}
/**
* CSSMin
* http://code.google.com/p/cssmin/
* *****************************************************************************************************************
*/
/**
* CssMin - A (simple) css minifier with benefits
*
* --
*
* 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 <joe.scylla@gmail.com>
* @copyright 2008 - 2011 Joe Scylla <joe.scylla@gmail.com>
* @license http://opensource.org/licenses/mit-license.php MIT License
* @version 2.0.2.1 (2011-02-18)
*/
class CssMin
{
/**
* Null-Token.
*
* @var integer
*/
const T_NULL = 0;
/**
* State: Is in document
*
* @var integer
*/
const T_DOCUMENT = 1;
/**
* Token: Comment
*
* @var integer
*/
const T_COMMENT = 2;
/**
* Token: Generic at-rule
*
* @var integer
*/
const T_AT_RULE = 3;
/**
* Token: Generic at-rule
*
* @var integer
*/
const T_AT_IMPORT = 22;
/**
* Token: Start of @media block
*
* @var integer
*/
const T_AT_MEDIA_START = 4;
/**
* State: Is in @media block
*
* @var integer
*/
const T_AT_MEDIA = 5;
/**
* Token: End of @media block
*
* @var integer
*/
const T_AT_MEDIA_END = 6;
/**
* Token: Start of @font-face block
*
* @var integer
*/
const T_AT_FONT_FACE_START = 7;
/**
* State: Is in @font-face block
*
* @var integer
*/
const T_AT_FONT_FACE = 8;
/**
* Token: @font-face declaration
*
* @var integer
*/
const T_FONT_FACE_DECLARATION = 9;
/**
* Token: End of @font-face block
*
* @var integer
*/
const T_AT_FONT_FACE_END = 10;
/**
* Token: Start of @page block
*
* @var integer
*/
const T_AT_PAGE_START = 11;
/**
* State: Is in @page block
*
* @var integer
*/
const T_AT_PAGE = 12;
/**
* Token: @page declaration
*
* @var integer
*/
const T_PAGE_DECLARATION = 13;
/**
* Token: End of @page block
*
* @var integer
*/
const T_AT_PAGE_END = 14;
/**
* Token: Start of ruleset
*
* @var integer
*/
const T_RULESET_START = 15;
/**
* Token: Ruleset selectors
*
* @var integer
*/
const T_SELECTORS = 16;
/**
* Token: Start of declarations
*
* @var integer
*/
const T_DECLARATIONS_START = 17;
/**
* State: Is in declarations
*
* @var integer
*/
const T_DECLARATIONS = 18;
/**
* Token: Declaration
*
* @var integer
*/