3 class JavaScriptMinifierTest
extends PHPUnit_Framework_TestCase
{
5 public static function provideCases() {
8 // Basic whitespace and comments that should be stripped entirely
9 [ "\r\t\f \v\n\r", "" ],
10 [ "/* Foo *\n*bar\n*/", "" ],
13 * Slashes used inside block comments (T28931).
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.
18 "/**\n * Foo\n * {\n * 'bar' : {\n * "
19 . "//Multiple rules with configurable operators\n * 'baz' : false\n * }\n */",
27 "' Foo \\' bar \\\n baz \\' quox ' .length",
28 "' Foo \\' bar \\\n baz \\' quox '.length"
31 "\" Foo \\\" bar \\\n baz \\\" quox \" .length",
32 "\" Foo \\\" bar \\\n baz \\\" quox \".length"
34 [ "// Foo b/ar baz", "" ],
36 "/ Foo \\/ bar [ / \\] / ] baz / .length",
37 "/ Foo \\/ bar [ / \\] / ] baz /.length"
41 [ "<!-- Foo bar", "" ],
42 [ "<!-- Foo --> bar", "" ],
44 [ "x --> y", "x-->y" ],
46 // Semicolon insertion
47 [ "(function(){return\nx;})", "(function(){return\nx;})" ],
48 [ "throw\nx;", "throw\nx;" ],
49 [ "while(p){continue\nx;}", "while(p){continue\nx;}" ],
50 [ "while(p){break\nx;}", "while(p){break\nx;}" ],
51 [ "var\nx;", "var x;" ],
53 [ "x\n++y;", "x\n++y;" ],
54 [ "x\n!y;", "x\n!y;" ],
55 [ "x\n{y}", "x\n{y}" ],
57 [ "x\n(y);", "x(y);" ],
58 [ "5.\nx;", "5.\nx;" ],
59 [ "0xFF.\nx;", "0xFF.x;" ],
60 [ "5.3.\nx;", "5.3.x;" ],
62 // Cover failure case for incomplete hex literal
63 [ "0x;", false, false ],
65 // Cover failure case for number with no digits after E
66 [ "1.4E", false, false ],
68 // Cover failure case for number with several E
69 [ "1.4EE2", false, false ],
70 [ "1.4EE", false, false ],
72 // Cover failure case for number with several E (nonconsecutive)
73 // FIXME: This is invalid, but currently tolerated
74 [ "1.4E2E3", "1.4E2 E3", false ],
76 // Semicolon insertion between an expression having an inline
77 // comment after it, and a statement on the next line (T29046).
79 "var a = this //foo bar \n for ( b = 0; c < d; b++ ) {}",
80 "var a=this\nfor(b=0;c<d;b++){}"
84 [ "x in y", "x in y" ],
85 [ "/x/g in y", "/x/g in y" ],
86 [ "x in 30", "x in 30" ],
87 [ "x + ++ y", "x+ ++y" ],
88 [ "x ++ + y", "x++ +y" ],
89 [ "x / /y/.exec(z)", "x/ /y/.exec(z)" ],
93 [ "(function(){return/ x/g})", "(function(){return/ x/g})" ],
94 [ "+/ x/g", "+/ x/g" ],
95 [ "++/ x/g", "++/ x/g" ],
96 [ "x/ x/g", "x/x/g" ],
97 [ "(/ x/g)", "(/ x/g)" ],
98 [ "if(/ x/g);", "if(/ x/g);" ],
99 [ "(x/ x/g)", "(x/x/g)" ],
100 [ "([/ x/g])", "([/ x/g])" ],
101 [ "+x/ x/g", "+x/x/g" ],
102 [ "{}/ x/g", "{}/ x/g" ],
103 [ "+{}/ x/g", "+{}/x/g" ],
104 [ "(x)/ x/g", "(x)/x/g" ],
105 [ "if(x)/ x/g", "if(x)/ x/g" ],
106 [ "for(x;x;{}/ x/g);", "for(x;x;{}/x/g);" ],
107 [ "x;x;{}/ x/g", "x;x;{}/ x/g" ],
108 [ "x:{}/ x/g", "x:{}/ x/g" ],
109 [ "switch(x){case y?z:{}/ x/g:{}/ x/g;}", "switch(x){case y?z:{}/x/g:{}/ x/g;}" ],
110 [ "function x(){}/ x/g", "function x(){}/ x/g" ],
111 [ "+function x(){}/ x/g", "+function x(){}/x/g" ],
113 // Multiline quoted string
114 [ "var foo=\"\\\nblah\\\n\";", "var foo=\"\\\nblah\\\n\";" ],
116 // Multiline quoted string followed by string with spaces
118 "var foo=\"\\\nblah\\\n\";\nvar baz = \" foo \";\n",
119 "var foo=\"\\\nblah\\\n\";var baz=\" foo \";"
122 // URL in quoted string ( // is not a comment)
124 "aNode.setAttribute('href','http://foo.bar.org/baz');",
125 "aNode.setAttribute('href','http://foo.bar.org/baz');"
128 // URL in quoted string after multiline quoted string
130 "var foo=\"\\\nblah\\\n\";\naNode.setAttribute('href','http://foo.bar.org/baz');",
131 "var foo=\"\\\nblah\\\n\";aNode.setAttribute('href','http://foo.bar.org/baz');"
134 // Division vs. regex nastiness
136 "alert( (10+10) / '/'.charCodeAt( 0 ) + '//' );",
137 "alert((10+10)/'/'.charCodeAt(0)+'//');"
139 [ "if(1)/a /g.exec('Pa ss');", "if(1)/a /g.exec('Pa ss');" ],
141 // newline insertion after 1000 chars: break after the "++", not before
142 [ str_repeat( ';', 996 ) . "if(x++);", str_repeat( ';', 996 ) . "if(x++\n);" ],
144 // Unicode letter characters should pass through ok in identifiers (T33187)
145 [ "var KaŝSkatolVal = {}", 'var KaŝSkatolVal={}' ],
147 // Per spec unicode char escape values should work in identifiers,
148 // as long as it's a valid char. In future it might get normalized.
149 [ "var Ka\\u015dSkatolVal = {}", 'var Ka\\u015dSkatolVal={}' ],
151 // Some structures that might look invalid at first sight
152 [ "var a = 5.;", "var a=5.;" ],
153 [ "5.0.toString();", "5.0.toString();" ],
154 [ "5..toString();", "5..toString();" ],
155 // Cover failure case for too many decimal points
156 [ "5...toString();", false ],
157 [ "5.\n.toString();", '5..toString();' ],
159 // Boolean minification (!0 / !1)
160 [ "var a = { b: true };", "var a={b:!0};" ],
161 [ "var a = { true: 12 };", "var a={true:12};", false ],
162 [ "a.true = 12;", "a.true=12;", false ],
163 [ "a.foo = true;", "a.foo=!0;" ],
164 [ "a.foo = false;", "a.foo=!1;" ],
169 * @dataProvider provideCases
170 * @covers JavaScriptMinifier::minify
171 * @covers JavaScriptMinifier::parseError
173 public function testMinifyOutput( $code, $expectedOutput, $expectedValid = true ) {
174 $minified = JavaScriptMinifier
::minify( $code );
176 // JSMin+'s parser will throw an exception if output is not valid JS.
177 // suppression of warnings needed for stupid crap
178 if ( $expectedValid ) {
179 MediaWiki\
suppressWarnings();
180 $parser = new JSParser();
181 MediaWiki\restoreWarnings
();
182 $parser->parse( $minified, 'minify-test.js', 1 );
188 "Minified output should be in the form expected."
192 public static function provideExponentLineBreaking() {
195 // This one gets interpreted all together by the prior code;
196 // no break at the 'E' happens.
200 // This one breaks under the bad code; splits between 'E' and '+'
204 // This one breaks under the bad code; splits between 'E' and '-'
211 * @dataProvider provideExponentLineBreaking
212 * @covers JavaScriptMinifier::minify
214 public function testExponentLineBreaking( $num ) {
215 // Long line breaking was being incorrectly done between the base and
216 // exponent part of a number, causing a syntax error. The line should
217 // instead break at the start of the number. (T34548)
218 $prefix = 'var longVarName' . str_repeat( '_', 973 ) . '=';
219 $suffix = ',shortVarName=0;';
221 $input = $prefix . $num . $suffix;
222 $expected = $prefix . "\n" . $num . $suffix;
224 $minified = JavaScriptMinifier
::minify( $input );
226 $this->assertEquals( $expected, $minified, "Line breaks must not occur in middle of exponent" );