forked from matthiasmullie/minify
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJSTest.php
More file actions
972 lines (890 loc) · 30.4 KB
/
Copy pathJSTest.php
File metadata and controls
972 lines (890 loc) · 30.4 KB
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
<?php
use MatthiasMullie\Minify;
/**
* JS minifier test case.
*/
class JSTest extends PHPUnit_Framework_TestCase
{
/**
* @var Minify\JS
*/
private $minifier;
/**
* Prepares the environment before running a test.
*/
protected function setUp()
{
parent::setUp();
// override save method, there's no point in writing the result out here
$this->minifier = $this->getMockBuilder('\MatthiasMullie\Minify\JS')
->setMethods(array('save'))
->getMock();
}
/**
* Cleans up the environment after running a test.
*/
protected function tearDown()
{
$this->minifier = null;
parent::tearDown();
}
/**
* Test JS minifier rules, provided by dataProvider.
*
* @test
* @dataProvider dataProvider
*/
public function minify($input, $expected)
{
$this->minifier->add($input);
$result = $this->minifier->minify();
$this->assertEquals($expected, $result);
}
/**
* @return array [input, expected result]
*/
public function dataProvider()
{
$tests = array();
// adding multiple files
$tests[] = array(
[
__DIR__.'/sample/source/script1.js',
__DIR__.'/sample/source/script2.js',
],
'var test=1;var test=2',
);
// adding multiple files and string
$tests[] = array(
[
__DIR__.'/sample/source/script1.js',
'console.log(test)',
__DIR__.'/sample/source/script2.js',
],
'var test=1;console.log(test);var test=2',
);
// escaped quotes should not terminate string
$tests[] = array(
'alert("Escaped quote which is same as string quotes: \"; should not match")',
'alert("Escaped quote which is same as string quotes: \"; should not match")',
);
// backtick string (allow string interpolation)
$tests[] = array(
'var str=`Hi, ${name}`',
'var str=`Hi, ${name}`',
);
// regex delimiters need to be treated as strings
// (two forward slashes could look like a comment)
$tests[] = array(
'/abc\/def\//.test("abc")',
'/abc\/def\//.test("abc")',
);
$tests[] = array(
'/abc\/def\//.test("abc\/def\/")',
'/abc\/def\//.test("abc\/def\/")',
);
$tests[] = array(
// there's an escape mess here; below regex represent this JS line:
// /abc\/def\\\//.test("abc/def\\/")
'/abc\/def\\\\\//.test("abc/def\\\/")',
'/abc\/def\\\\\//.test("abc/def\\\/")',
);
$tests[] = array(
// escape mess, this represents:
// /abc\/def\\\\\//.test("abc/def\\\\/")
'/abc\/def\\\\\\\\\//.test("abc/def\\\\\\\\/")',
'/abc\/def\\\\\\\\\//.test("abc/def\\\\\\\\/")',
);
$tests[] = array(
'var a = /abc\/def\//.test("abc")',
'var a=/abc\/def\//.test("abc")',
);
// don't confuse multiple slashes for regexes
$tests[] = array(
'a = b / c; d = e / f',
'a=b/c;d=e/f',
);
$tests[] = array(
'(2 + 4) / 3 + 5 / 1',
'(2+4)/3+5/1',
);
$tests[] = array(
'a=4/
2',
'a=4/2',
);
// mixture of quotes starting in comment/regex, to make sure strings are
// matched correctly, not inside comment/regex
// additionally test catching of empty strings as well
$tests[] = array(
'/abc"def/.test("abc")',
'/abc"def/.test("abc")',
);
$tests[] = array(
'/abc"def/.test(\'\')',
'/abc"def/.test(\'\')',
);
$tests[] = array(
'/* Bogus " */var test="test";',
'var test="test"',
);
// replace comments
$tests[] = array(
'/* This is a JS comment */',
'',
);
// make sure no ; is added in places it shouldn't
$tests[] = array(
'if(true){}else{}',
'if(!0){}else{}',
);
$tests[] = array(
'do{i++}while(i<1)',
'do{i++}while(i<1)',
);
$tests[] = array(
'if(true)statement;else statement',
'if(!0)statement;else statement',
);
$tests[] = array(
'for ( i = 0; ; i++ ) statement',
'for(i=0;;i++)statement',
);
$tests[] = array(
'for (i = 0; (i < 10); i++) statement',
'for(i=0;(i<10);i++)statement',
);
$tests[] = array(
'alert("test");;alert("test2")',
'alert("test");alert("test2")',
);
$tests[] = array(
'-1
+2',
'-1+2',
);
$tests[] = array(
'-1+
2',
'-1+2',
);
$tests[] = array(
'alert("this is a test");',
'alert("this is a test")',
);
// test where newline should be preserved (for ASI) or semicolon added
$tests[] = array(
'function(){console.log("this is a test");}',
'function(){console.log("this is a test")}',
);
$tests[] = array(
'alert("this is a test")
alert("this is another test")',
'alert("this is a test")
alert("this is another test")',
);
$tests[] = array(
'a=b+c
d=e+f',
'a=b+c
d=e+f',
);
$tests[] = array(
'a++
++b',
'a++
++b',
);
$tests[] = array(
'!a
!b',
'!a
!b',
);
$tests[] = array(
// don't confuse with 'if'
'digestif
(true)
statement',
'digestif(!0)
statement',
);
$tests[] = array(
'if
(
(
true
)
&&
(
true
)
)
statement',
'if((!0)&&(!0))
statement',
);
$tests[] = array(
'if
(
true
)
{
}
else
{
}',
'if(!0)
{}
else{}',
);
$tests[] = array(
'do
{
i++
}
while
(
i<1
)',
'do{i++}
while(i<1)',
);
$tests[] = array(
'if ( true )
statement
else
statement',
'if(!0)
statement
else statement',
);
// test if whitespace around keywords is properly collapsed
$tests[] = array(
'var
variable
=
"value";',
'var variable="value"',
);
$tests[] = array(
'var variable = {
test:
{
}
}',
'var variable={test:{}}',
);
$tests[] = array(
'if ( true ) {
} else {
}',
'if(!0){}else{}',
);
$tests[] = array(
'53 instanceof String',
'53 instanceof String',
);
// remove whitespace around operators
$tests[] = array(
'a = 1 + 2',
'a=1+2',
);
$tests[] = array(
'object . property',
'object.property',
);
$tests[] = array(
'object
.property',
'object.property',
);
$tests[] = array(
'alert ( "this is a test" );',
'alert("this is a test")',
);
// mix of ++ and +: three consecutive +es will be interpreted as ++ +
$tests[] = array(
'a++ +b',
'a++ +b',
);
$tests[] = array(
'a+ ++b',
'a+ ++b', // +++ would actually be allowed as well
);
// SyntaxError: identifier starts immediately after numeric literal
$tests[] = array(
'42 .toString()',
'42 .toString()',
);
// add comment in between whitespace that needs to be stripped
$tests[] = array(
'object
// haha, some comment, just to make things harder!
.property',
'object.property',
);
// add comment in between whitespace that needs to be stripped
$tests[] = array(
'var test=true,test2=false',
'var test=!0,test2=!1',
);
$tests[] = array(
'var testtrue="testing if true as part of varname is ignored as it should"',
'var testtrue="testing if true as part of varname is ignored as it should"',
);
// random bits of code that tripped errors during development
$tests[] = array(
'
// check if it isn\'t a text-element
if(currentElement.attr(\'type\') != \'text\')
{
// remove the current one
currentElement.remove();
}
// already a text element
else newElement = currentElement;
',
'if(currentElement.attr(\'type\')!=\'text\')
{currentElement.remove()}
else newElement=currentElement',
);
$tests[] = array(
'var jsBackend =
{
debug: false,
current: {}
}',
'var jsBackend={debug:!1,current:{}}',
);
$tests[] = array(
'var utils =
{
debug: false
}
utils.array =
{
}',
'var utils={debug:!1}
utils.array={}',
);
$tests[] = array(
'rescape = /\'|\\\\/g,
// blablabla here was some more code but the point was that somewhere
// down below, there would be a closing quote which would cause the
// regex (confused for escaped closing tag) not to be recognized,
// taking the opening single quote & looking for a string.
// So here\'s <-- the closing quote
runescape = \'blabla\'',
'rescape=/\'|\\\\/g,runescape=\'blabla\'',
);
$tests[] = array(
'var rsingleTag = (/^<(\w+)\s*\/?>(?:<\/\1>|)$/)',
'var rsingleTag=(/^<(\w+)\s*\/?>(?:<\/\1>|)$/)',
);
$tests[] = array(
'if (this.sliding) return this.$element.one(\'slid.bs.carousel\', function () { that.to(pos) }) // yes, "slid"
if (activeIndex == pos) return this.pause().cycle()',
'if(this.sliding)return this.$element.one(\'slid.bs.carousel\',function(){that.to(pos)})
if(activeIndex==pos)return this.pause().cycle()',
);
$tests[] = array(
'if (e.which == 38 && index > 0) index-- // up
if (e.which == 40 && index < $items.length - 1) index++ // down',
'if(e.which==38&&index>0)index--
if(e.which==40&&index<$items.length-1)index++',
);
// replace associative array key references by property notation
$tests[] = array(
'array["key"][\'key2\']',
'array.key.key2',
);
$tests[] = array(
'array[ "key" ][ \'key2\' ]',
'array.key.key2',
);
$tests[] = array(
'array["a","b","c"]',
'array["a","b","c"]',
);
$tests[] = array(
"['loader']",
"['loader']",
);
$tests[] = array(
'array["dont-replace"][\'key2\']',
'array["dont-replace"].key2',
);
// shorten bools
$tests[] = array(
'while(true){break}',
'for(;;){break}',
);
// make sure we don't get "missing while after do-loop body"
$tests[] = array(
'do{break}while(true)',
'do{break}while(!0)',
);
$tests[] = array(
"do break\nwhile(true)",
"do break\nwhile(!0)",
);
$tests[] = array(
"do{break}while(true){alert('test')}",
"do{break}while(!0){alert('test')}",
);
$tests[] = array(
"do break\nwhile(true){alert('test')}",
"do break\nwhile(!0){alert('test')}",
);
// nested do-while & while
$tests[] = array(
"do{while(true){break}break}while(true){alert('test')}",
"do{for(;;){break}break}while(!0){alert('test')}",
);
$tests[] = array(
"do{while(true){break}break}while(true){alert('test')}while(true){break}",
"do{for(;;){break}break}while(!0){alert('test')}for(;;){break}",
);
$tests[] = array(
"do{while(true){break}break}while(true){alert('test')}while(true){break}do{while(true){break}break}while(true){alert('test')}while(true){break}",
"do{for(;;){break}break}while(!0){alert('test')}for(;;){break}do{for(;;){break}break}while(!0){alert('test')}for(;;){break}",
);
// https://github.com/matthiasmullie/minify/issues/10
$tests[] = array(
'// first mutation patch
// second mutation patch
// third mutation patch
// fourth mutation patch',
'',
);
$tests[] = array(
'/////////////////////////
// first mutation patch
// second mutation patch
// third mutation patch
// fourth mutation patch
/////////////////////////',
'',
);
// https://github.com/matthiasmullie/minify/issues/14
$tests[] = array(
'function foo (a, b)
{
return a / b;
}
function foo (a, b)
{
return a / b;
}',
'function foo(a,b)
{return a/b}
function foo(a,b)
{return a/b}',
);
// https://github.com/matthiasmullie/minify/issues/15
$tests[] = array(
'if ( !data.success )
deferred.reject(); else
deferred.resolve(data);',
'if(!data.success)
deferred.reject();else deferred.resolve(data)',
);
$tests[] = array(
"if ( typeof jQuery === 'undefined' )
throw new Error('.editManager.js: jQuery is required and must be loaded first');",
"if(typeof jQuery==='undefined')
throw new Error('.editManager.js: jQuery is required and must be loaded first')",
);
// https://github.com/matthiasmullie/minify/issues/27
$tests[] = array(
'$.expr[":"]',
'$.expr[":"]',
);
// https://github.com/matthiasmullie/minify/issues/31
$tests[] = array(
"$(_this).attr('src',this.src).trigger('adapt',['loader'])",
"$(_this).attr('src',this.src).trigger('adapt',['loader'])",
);
// https://github.com/matthiasmullie/minify/issues/33
$tests[] = array(
'$.fn.alert = Plugin
$.fn.alert.Constructor = Alert',
'$.fn.alert=Plugin
$.fn.alert.Constructor=Alert',
);
// https://github.com/matthiasmullie/minify/issues/34
$tests[] = array(
'a.replace("\\\\","");hi="This is a string"',
'a.replace("\\\\","");hi="This is a string"',
);
// https://github.com/matthiasmullie/minify/issues/35
$tests[] = array(
array(
'// script that ends with comment',
'var test=1',
),
'var test=1',
);
// https://github.com/matthiasmullie/minify/issues/37
$tests[] = array(
'function () { ;;;;;;;; }',
'function(){}',
);
// https://github.com/matthiasmullie/minify/issues/40
$tests[] = array(
'for(v=1,_=b;;){}',
'for(v=1,_=b;;){}',
);
// https://github.com/matthiasmullie/minify/issues/41
$tests[] = array(
"conf.zoomHoverIcons['default']",
"conf.zoomHoverIcons['default']",
);
// https://github.com/matthiasmullie/minify/issues/42
$tests[] = array(
'for(i=1;i<2;i++);',
'for(i=1;i<2;i++);',
);
$tests[] = array(
'if(1){for(i=1;i<2;i++);}',
'if(1){for(i=1;i<2;i++);}',
);
$tests[] = array(
'for(i in list);',
'for(i in list);',
);
$tests[] = array(
'if(1){for(i in list);}',
'if(1){for(i in list);}',
);
// https://github.com/matthiasmullie/minify/issues/43
$tests[] = array(
'{"key":"3","key2":"value","key3":"3"}',
'{"key":"3","key2":"value","key3":"3"}',
);
// https://github.com/matthiasmullie/minify/issues/44
$tests[] = array(
'return ["x"]',
'return["x"]',
);
// https://github.com/matthiasmullie/minify/issues/50
$tests[] = array(
'do{var dim=this._getDaysInMonth(year,month-1);if(day<=dim){break}month++;day-=dim}while(true)}',
'do{var dim=this._getDaysInMonth(year,month-1);if(day<=dim){break}month++;day-=dim}while(!0)}',
);
// https://github.com/matthiasmullie/minify/issues/53
$tests[] = array(
'a.validator.addMethod("accept", function (b, c, d) {
var e, f, g = "string" == typeof d ?
d.replace(/\s/g, "").replace(/,/g, "|") :
"image/*", h = this.optional(c);
if (h)return h;
if ("file" === a(c).attr("type") && (g = g.replace(/\*/g, ".*"), c.files && c.files.length))
for (e = 0; e < c.files.length; e++)
if (f = c.files[e], !f.type.match(new RegExp(".?(" + g + ")$", "i")))
return !1;
return !0
}',
'a.validator.addMethod("accept",function(b,c,d){var e,f,g="string"==typeof d?d.replace(/\s/g,"").replace(/,/g,"|"):"image/*",h=this.optional(c);if(h)return h;if("file"===a(c).attr("type")&&(g=g.replace(/\*/g,".*"),c.files&&c.files.length))
for(e=0;e<c.files.length;e++)
if(f=c.files[e],!f.type.match(new RegExp(".?("+g+")$","i")))
return!1;return!0}',
);
// https://github.com/matthiasmullie/minify/issues/54
$tests[] = array(
'function a() {
if (true)
return
if (false)
return
}',
'function a(){if(!0)
return
if(!1)
return}',
);
// https://github.com/matthiasmullie/minify/issues/56
$tests[] = array(
'var timeRegex = /^([2][0-3]|[01]?[0-9])(:[0-5][0-9])?$/
if (start_time.match(timeRegex) == null) {}',
'var timeRegex=/^([2][0-3]|[01]?[0-9])(:[0-5][0-9])?$/
if(start_time.match(timeRegex)==null){}',
);
// https://github.com/matthiasmullie/minify/issues/58
// stripped of redundant code to expose problem case
$tests[] = array(
<<<'BUG'
function inspect() {
escapedString.replace(/abc/g, '\\\'');
}
function isJSON() {
str.replace(/"[^"\\\n\r]*"|true|false|null|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?/g, ']');
}
BUG
,
<<<'BUG'
function inspect(){escapedString.replace(/abc/g,'\\\'')}
function isJSON(){str.replace(/"[^"\\\n\r]*"|true|false|null|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?/g,']')}
BUG
);
// https://github.com/matthiasmullie/minify/issues/59
$tests[] = array(
'isPath:function(e) {
return /\//.test(e);
}',
'isPath:function(e){return/\//.test(e)}',
);
// https://github.com/matthiasmullie/minify/issues/64
$tests[] = array(
' var d3_nsPrefix = {
svg: "http://www.w3.org/2000/svg",
xhtml: "http://www.w3.org/1999/xhtml",
xlink: "http://www.w3.org/1999/xlink",
xml: "http://www.w3.org/XML/1998/namespace",
xmlns: "http://www.w3.org/2000/xmlns/"
};',
'var d3_nsPrefix={svg:"http://www.w3.org/2000/svg",xhtml:"http://www.w3.org/1999/xhtml",xlink:"http://www.w3.org/1999/xlink",xml:"http://www.w3.org/XML/1998/namespace",xmlns:"http://www.w3.org/2000/xmlns/"}',
);
// https://github.com/matthiasmullie/minify/issues/66
$tests[] = array(
"$(coming.wrap).bind('onReset', function () {
try {
$(this).find('iframe').hide().attr('src', '//about:blank').end().empty();
} catch (e) {}
});",
"$(coming.wrap).bind('onReset',function(){try{\$(this).find('iframe').hide().attr('src','//about:blank').end().empty()}catch(e){}})",
);
// https://github.com/matthiasmullie/minify/issues/89
$tests[] = array(
'for(;;ja||(ja=true)){}',
'for(;;ja||(ja=!0)){}',
);
// https://github.com/matthiasmullie/minify/issues/91
$tests[] = array(
'if(true){if(true)console.log("test")else;}',
'if(!0){if(!0)console.log("test")}',
);
// https://github.com/matthiasmullie/minify/issues/99
$tests[] = array(
'"object";"object2";"0";"1"',
'"object";"object2";"0";"1"',
);
// https://github.com/matthiasmullie/minify/issues/102
$tests[] = array(
'var pb = {};',
'var pb={}',
);
$tests[] = array(
'pb.Initialize = function(settings) {};',
'pb.Initialize=function(settings){}',
);
// https://github.com/matthiasmullie/minify/issues/108
$tests[] = array(
'function isHtmlNamespace(node) {
var ns;
return typeof node.namespaceURI == UNDEF || ((ns = node.namespaceURI) === null || ns == "http://www.w3.org/1999/xhtml");
}',
'function isHtmlNamespace(node){var ns;return typeof node.namespaceURI==UNDEF||((ns=node.namespaceURI)===null||ns=="http://www.w3.org/1999/xhtml")}',
);
// https://github.com/matthiasmullie/minify/issues/115
$tests[] = array(
'if(typeof i[s].token=="string")/keyword|support|storage/.test(i[s].token)&&n.push(i[s].regex);else if(typeof i[s].token=="object")for(var u=0,a=i[s].token.length;u<a;u++)if(/keyword|support|storage/.test(i[s].token[u])){}',
'if(typeof i[s].token=="string")/keyword|support|storage/.test(i[s].token)&&n.push(i[s].regex);else if(typeof i[s].token=="object")for(var u=0,a=i[s].token.length;u<a;u++)if(/keyword|support|storage/.test(i[s].token[u])){}',
);
// https://github.com/matthiasmullie/minify/issues/120
$tests[] = array(
'function myFuncName() {
function otherFuncName() {
if (condition) {
a = b / 1; // comment 1
} else if (condition) {
a = c / 2; // comment 2
} else if (condition) {
a = d / 3; // comment 3
} else {
a = 0;
}
}
};',
'function myFuncName(){function otherFuncName(){if(condition){a=b/1}else if(condition){a=c/2}else if(condition){a=d/3}else{a=0}}}',
);
// https://github.com/matthiasmullie/minify/issues/128
$tests[] = array(
'angle = (i - 3) * (Math.PI * 2) / 12; // THE ANGLE TO MARK.',
'angle=(i-3)*(Math.PI*2)/12',
);
// https://github.com/matthiasmullie/minify/issues/124
$tests[] = array(
'return cond ? document._getElementsByXPath(\'.//*\' + cond, element) : [];',
'return cond?document._getElementsByXPath(\'.//*\'+cond,element):[]',
);
$tests[] = array(
'Sizzle.selectors = {
match: {
PSEUDO: /:((?:[\w\u00c0-\uFFFF-]|\\.)+)(?:\(([\'"]*)((?:\([^\)]+\)|[^\2\(\)]*)+)\2\))?/
},
attrMap: {
"class": "className"
}
}',
'Sizzle.selectors={match:{PSEUDO:/:((?:[\w\u00c0-\uFFFF-]|\\.)+)(?:\(([\'"]*)((?:\([^\)]+\)|[^\2\(\)]*)+)\2\))?/},attrMap:{"class":"className"}}',
);
// https://github.com/matthiasmullie/minify/issues/130
$tests[] = array(
'function func(){}
func()
{ alert(\'hey\'); }',
'function func(){}
func()
{alert(\'hey\')}',
);
// https://github.com/matthiasmullie/minify/issues/133
$tests[] = array(
'if ( args[\'message\'] instanceof Array ) { args[\'message\'] = args[\'message\'].join( \' \' );}',
'if(args.message instanceof Array){args.message=args.message.join(\' \')}',
);
// https://github.com/matthiasmullie/minify/issues/134
$tests[] = array(
'e={true:!0,false:!1}',
'e={true:!0,false:!1}',
);
// https://github.com/matthiasmullie/minify/issues/134
$tests[] = array(
'if (\'x\'+a in foo && \'y\'+b[a].z in bar)',
'if(\'x\'+a in foo&&\'y\'+b[a].z in bar)',
);
// https://github.com/matthiasmullie/minify/issues/136
$tests[] = array(
'XPRSHelper.isManagable = function(presetId){ if (presetId in XPRSHelper.presetTypes){ return (XPRSHelper.presetTypes[presetId]["GROUP"] in {"FEATURES":true,"SLIDESHOWS":true,"GALLERIES":true}); } return false; };',
'XPRSHelper.isManagable=function(presetId){if(presetId in XPRSHelper.presetTypes){return(XPRSHelper.presetTypes[presetId].GROUP in{"FEATURES":!0,"SLIDESHOWS":!0,"GALLERIES":!0})}return!1}',
);
// https://github.com/matthiasmullie/minify/issues/138
$tests[] = array(
'matchers.push(/^[0-9]*$/.source);',
'matchers.push(/^[0-9]*$/.source)',
);
$tests[] = array(
'matchers.push(/^[0-9]*$/.source);
String(dateString).match(/^[0-9]*$/);',
'matchers.push(/^[0-9]*$/.source);String(dateString).match(/^[0-9]*$/)',
);
// https://github.com/matthiasmullie/minify/issues/139
$tests[] = array(
__DIR__.'/sample/line_endings/lf/script.js',
'var a=1',
);
$tests[] = array(
__DIR__.'/sample/line_endings/cr/script.js',
'var a=1',
);
$tests[] = array(
__DIR__.'/sample/line_endings/crlf/script.js',
'var a=1',
);
// https://github.com/matthiasmullie/minify/issues/142
$tests[] = array(
'return {
l: ((116 * y) - 16) / 100, // [0,100]
a: ((500 * (x - y)) + 128) / 255, // [-128,127]
b: ((200 * (y - z)) + 128) / 255 // [-128,127]
};',
'return{l:((116*y)-16)/100,a:((500*(x-y))+128)/255,b:((200*(y-z))+128)/255}',
);
// https://github.com/matthiasmullie/minify/issues/143
$tests[] = array(
"if(nutritionalPortionWeightUnit == 'lbs' && blockUnit == 'oz'){
itemFat = (qty * (fat/nutritionalPortionWeight))/16;
itemProtein = (qty * (protein/nutritionalPortionWeight))/16;
itemCarbs = (qty * (carbs/nutritionalPortionWeight))/16;
itemKcal = (qty * (kcal/nutritionalPortionWeight))/16;
}",
"if(nutritionalPortionWeightUnit=='lbs'&&blockUnit=='oz'){itemFat=(qty*(fat/nutritionalPortionWeight))/16;itemProtein=(qty*(protein/nutritionalPortionWeight))/16;itemCarbs=(qty*(carbs/nutritionalPortionWeight))/16;itemKcal=(qty*(kcal/nutritionalPortionWeight))/16}",
);
$tests[] = array(
'itemFat = (qty * (fat/nutritionalPortionWeight))/16;
itemFat = (qty * (fat/nutritionalPortionWeight))/(28.3495*16);',
'itemFat=(qty*(fat/nutritionalPortionWeight))/16;itemFat=(qty*(fat/nutritionalPortionWeight))/(28.3495*16)',
);
// https://github.com/matthiasmullie/minify/issues/146
$tests[] = array(
'rnoContent = /^(?:GET|HEAD)$/,
rprotocol = /^\/\//,
/* ...
*/
prefilters = {};',
'rnoContent=/^(?:GET|HEAD)$/,rprotocol=/^\/\//,prefilters={}',
);
$tests[] = array(
'elem.getAttribute("type")!==null)+"/"+elem.type
var rprotocol=/^\/\//,prefilters={}',
'elem.getAttribute("type")!==null)+"/"+elem.type
var rprotocol=/^\/\//,prefilters={}',
);
$tests[] = array(
'map: function( elems, callback, arg ) {
for ( i in elems ) {
value = callback( elems[ i ], i, arg );
if ( value != null ) {
ret.push( value );
}
}
return concat.apply( [], ret );
}',
'map:function(elems,callback,arg){for(i in elems){value=callback(elems[i],i,arg);if(value!=null){ret.push(value)}}
return concat.apply([],ret)}',
);
// https://github.com/matthiasmullie/minify/issues/167
$tests[] = array(
'this.valueMap.false',
'this.valueMap.false',
);
$tests[] = array(
'this.valueMap . false',
'this.valueMap.false',
);
$tests[] = array(
'false!==true',
'!1!==!0',
);
// https://github.com/matthiasmullie/minify/issues/164
$tests[] = array(
'Calendar.createElement = function(type, parent) {
var el = null;
if (document.createElementNS) {
// use the XHTML namespace; IE won\'t normally get here unless
// _they_ "fix" the DOM2 implementation.
el = document.createElementNS("http://www.w3.org/1999/xhtml", type);
} else {
el = document.createElement(type);
}
if (typeof parent != "undefined") {
parent.appendChild(el);
}
return el;
};',
'Calendar.createElement=function(type,parent){var el=null;if(document.createElementNS){el=document.createElementNS("http://www.w3.org/1999/xhtml",type)}else{el=document.createElement(type)}
if(typeof parent!="undefined"){parent.appendChild(el)}
return el}',
);
// https://github.com/matthiasmullie/minify/issues/163
$tests[] = array(
'q = d / 4 / b.width()',
'q=d/4/b.width()',
);
// known minified files to help doublecheck changes in places not yet
// anticipated in these tests
$files = glob(__DIR__.'/sample/minified/*.js');
foreach ($files as $file) {
$content = trim(file_get_contents($file));
$tests[] = array($content, $content);
}
// update tests' expected results for cross-system compatibility
foreach ($tests as &$test) {
if (!empty($test[1])) {
$test[1] = str_replace("\r", '', $test[1]);
}
}
return $tests;
}
}