3 class JavaScriptMinifierTest
extends MediaWikiTestCase
{
5 public static function provideCases() {
8 // Basic whitespace and comments that should be stripped entirely
9 array( "\r\t\f \v\n\r", "" ),
10 array( "/* Foo *\n*bar\n*/", "" ),
13 * Slashes used inside block comments (bug 26931).
14 * At some point there was a bug that caused this comment to be ended at '* /',
15 * causing /M... to be left as the beginning of a regex.
17 array( "/**\n * Foo\n * {\n * 'bar' : {\n * //Multiple rules with configurable operators\n * 'baz' : false\n * }\n */", "" ),
23 array( "' Foo \\' bar \\\n baz \\' quox ' .length", "' Foo \\' bar \\\n baz \\' quox '.length" ),
24 array( "\" Foo \\\" bar \\\n baz \\\" quox \" .length", "\" Foo \\\" bar \\\n baz \\\" quox \".length" ),
25 array( "// Foo b/ar baz", "" ),
26 array( "/ Foo \\/ bar [ / \\] / ] baz / .length", "/ Foo \\/ bar [ / \\] / ] baz /.length" ),
29 array( "<!-- Foo bar", "" ),
30 array( "<!-- Foo --> bar", "" ),
31 array( "--> Foo", "" ),
32 array( "x --> y", "x-->y" ),
34 // Semicolon insertion
35 array( "(function(){return\nx;})", "(function(){return\nx;})" ),
36 array( "throw\nx;", "throw\nx;" ),
37 array( "while(p){continue\nx;}", "while(p){continue\nx;}" ),
38 array( "while(p){break\nx;}", "while(p){break\nx;}" ),
39 array( "var\nx;", "var x;" ),
40 array( "x\ny;", "x\ny;" ),
41 array( "x\n++y;", "x\n++y;" ),
42 array( "x\n!y;", "x\n!y;" ),
43 array( "x\n{y}", "x\n{y}" ),
44 array( "x\n+y;", "x+y;" ),
45 array( "x\n(y);", "x(y);" ),
46 array( "5.\nx;", "5.\nx;" ),
47 array( "0xFF.\nx;", "0xFF.x;" ),
48 array( "5.3.\nx;", "5.3.x;" ),
50 // Semicolon insertion between an expression having an inline
51 // comment after it, and a statement on the next line (bug 27046).
52 array( "var a = this //foo bar \n for ( b = 0; c < d; b++ ) {}", "var a=this\nfor(b=0;c<d;b++){}" ),
55 array( "x in y", "x in y" ),
56 array( "/x/g in y", "/x/g in y" ),
57 array( "x in 30", "x in 30" ),
58 array( "x + ++ y", "x+ ++y" ),
59 array( "x ++ + y", "x++ +y" ),
60 array( "x / /y/.exec(z)", "x/ /y/.exec(z)" ),
63 array( "/ x/g", "/ x/g" ),
64 array( "(function(){return/ x/g})", "(function(){return/ x/g})" ),
65 array( "+/ x/g", "+/ x/g" ),
66 array( "++/ x/g", "++/ x/g" ),
67 array( "x/ x/g", "x/x/g" ),
68 array( "(/ x/g)", "(/ x/g)" ),
69 array( "if(/ x/g);", "if(/ x/g);" ),
70 array( "(x/ x/g)", "(x/x/g)" ),
71 array( "([/ x/g])", "([/ x/g])" ),
72 array( "+x/ x/g", "+x/x/g" ),
73 array( "{}/ x/g", "{}/ x/g" ),
74 array( "+{}/ x/g", "+{}/x/g" ),
75 array( "(x)/ x/g", "(x)/x/g" ),
76 array( "if(x)/ x/g", "if(x)/ x/g" ),
77 array( "for(x;x;{}/ x/g);", "for(x;x;{}/x/g);" ),
78 array( "x;x;{}/ x/g", "x;x;{}/ x/g" ),
79 array( "x:{}/ x/g", "x:{}/ x/g" ),
80 array( "switch(x){case y?z:{}/ x/g:{}/ x/g;}", "switch(x){case y?z:{}/x/g:{}/ x/g;}" ),
81 array( "function x(){}/ x/g", "function x(){}/ x/g" ),
82 array( "+function x(){}/ x/g", "+function x(){}/x/g" ),
84 // Multiline quoted string
85 array( "var foo=\"\\\nblah\\\n\";", "var foo=\"\\\nblah\\\n\";" ),
87 // Multiline quoted string followed by string with spaces
88 array( "var foo=\"\\\nblah\\\n\";\nvar baz = \" foo \";\n", "var foo=\"\\\nblah\\\n\";var baz=\" foo \";" ),
90 // URL in quoted string ( // is not a comment)
91 array( "aNode.setAttribute('href','http://foo.bar.org/baz');", "aNode.setAttribute('href','http://foo.bar.org/baz');" ),
93 // URL in quoted string after multiline quoted string
94 array( "var foo=\"\\\nblah\\\n\";\naNode.setAttribute('href','http://foo.bar.org/baz');", "var foo=\"\\\nblah\\\n\";aNode.setAttribute('href','http://foo.bar.org/baz');" ),
96 // Division vs. regex nastiness
97 array( "alert( (10+10) / '/'.charCodeAt( 0 ) + '//' );", "alert((10+10)/'/'.charCodeAt(0)+'//');" ),
98 array( "if(1)/a /g.exec('Pa ss');", "if(1)/a /g.exec('Pa ss');" ),
100 // newline insertion after 1000 chars: break after the "++", not before
101 array( str_repeat( ';', 996 ) . "if(x++);", str_repeat( ';', 996 ) . "if(x++\n);" ),
103 // Unicode letter characters should pass through ok in identifiers (bug 31187)
104 array( "var KaŝSkatolVal = {}", 'var KaŝSkatolVal={}' ),
106 // Per spec unicode char escape values should work in identifiers,
107 // as long as it's a valid char. In future it might get normalized.
108 array( "var Ka\\u015dSkatolVal = {}", 'var Ka\\u015dSkatolVal={}' ),
110 // Some structures that might look invalid at first sight
111 array( "var a = 5.;", "var a=5.;" ),
112 array( "5.0.toString();", "5.0.toString();" ),
113 array( "5..toString();", "5..toString();" ),
114 array( "5...toString();", false ),
115 array( "5.\n.toString();", '5..toString();' ),
120 * @dataProvider provideCases
122 function testJavaScriptMinifierOutput( $code, $expectedOutput ) {
123 $minified = JavaScriptMinifier
::minify( $code );
125 // JSMin+'s parser will throw an exception if output is not valid JS.
126 // suppression of warnings needed for stupid crap
127 wfSuppressWarnings();
128 $parser = new JSParser();
130 $parser->parse( $minified, 'minify-test.js', 1 );
132 $this->assertEquals( $expectedOutput, $minified, "Minified output should be in the form expected." );
135 public static function provideBug32548() {
138 // This one gets interpreted all together by the prior code;
139 // no break at the 'E' happens.
143 // This one breaks under the bad code; splits between 'E' and '+'
147 // This one breaks under the bad code; splits between 'E' and '-'
154 * @dataProvider provideBug32548
156 function testBug32548Exponent( $num ) {
157 // Long line breaking was being incorrectly done between the base and
158 // exponent part of a number, causing a syntax error. The line should
159 // instead break at the start of the number.
160 $prefix = 'var longVarName' . str_repeat( '_', 973 ) . '=';
161 $suffix = ',shortVarName=0;';
163 $input = $prefix . $num . $suffix;
164 $expected = $prefix . "\n" . $num . $suffix;
166 $minified = JavaScriptMinifier
::minify( $input );
168 $this->assertEquals( $expected, $minified, "Line breaks must not occur in middle of exponent" );