3 class JavaScriptMinifierTest
extends PHPUnit_Framework_TestCase
{
5 use MediaWikiCoversValidator
;
7 public static function provideCases() {
10 // Basic whitespace and comments that should be stripped entirely
11 [ "\r\t\f \v\n\r", "" ],
12 [ "/* Foo *\n*bar\n*/", "" ],
15 * Slashes used inside block comments (T28931).
16 * At some point there was a bug that caused this comment to be ended at '* /',
17 * causing /M... to be left as the beginning of a regex.
20 "/**\n * Foo\n * {\n * 'bar' : {\n * "
21 . "//Multiple rules with configurable operators\n * 'baz' : false\n * }\n */",
29 "' Foo \\' bar \\\n baz \\' quox ' .length",
30 "' Foo \\' bar \\\n baz \\' quox '.length"
33 "\" Foo \\\" bar \\\n baz \\\" quox \" .length",
34 "\" Foo \\\" bar \\\n baz \\\" quox \".length"
36 [ "// Foo b/ar baz", "" ],
38 "/ Foo \\/ bar [ / \\] / ] baz / .length",
39 "/ Foo \\/ bar [ / \\] / ] baz /.length"
43 [ "<!-- Foo bar", "" ],
44 [ "<!-- Foo --> bar", "" ],
46 [ "x --> y", "x-->y" ],
48 // Semicolon insertion
49 [ "(function(){return\nx;})", "(function(){return\nx;})" ],
50 [ "throw\nx;", "throw\nx;" ],
51 [ "while(p){continue\nx;}", "while(p){continue\nx;}" ],
52 [ "while(p){break\nx;}", "while(p){break\nx;}" ],
53 [ "var\nx;", "var x;" ],
55 [ "x\n++y;", "x\n++y;" ],
56 [ "x\n!y;", "x\n!y;" ],
57 [ "x\n{y}", "x\n{y}" ],
59 [ "x\n(y);", "x(y);" ],
60 [ "5.\nx;", "5.\nx;" ],
61 [ "0xFF.\nx;", "0xFF.x;" ],
62 [ "5.3.\nx;", "5.3.x;" ],
64 // Cover failure case for incomplete hex literal
65 [ "0x;", false, false ],
67 // Cover failure case for number with no digits after E
68 [ "1.4E", false, false ],
70 // Cover failure case for number with several E
71 [ "1.4EE2", false, false ],
72 [ "1.4EE", false, false ],
74 // Cover failure case for number with several E (nonconsecutive)
75 // FIXME: This is invalid, but currently tolerated
76 [ "1.4E2E3", "1.4E2 E3", false ],
78 // Semicolon insertion between an expression having an inline
79 // comment after it, and a statement on the next line (T29046).
81 "var a = this //foo bar \n for ( b = 0; c < d; b++ ) {}",
82 "var a=this\nfor(b=0;c<d;b++){}"
85 // Cover failure case of incomplete regexp at end of file (T75556)
86 // FIXME: This is invalid, but currently tolerated
87 [ "*/", "*/", false ],
89 // Cover failure case of incomplete string at end of file (T75556)
90 // FIXME: This is invalid, but currently tolerated
91 [ "'a", "'a", false ],
94 [ "x in y", "x in y" ],
95 [ "/x/g in y", "/x/g in y" ],
96 [ "x in 30", "x in 30" ],
97 [ "x + ++ y", "x+ ++y" ],
98 [ "x ++ + y", "x++ +y" ],
99 [ "x / /y/.exec(z)", "x/ /y/.exec(z)" ],
102 [ "/ x/g", "/ x/g" ],
103 [ "(function(){return/ x/g})", "(function(){return/ x/g})" ],
104 [ "+/ x/g", "+/ x/g" ],
105 [ "++/ x/g", "++/ x/g" ],
106 [ "x/ x/g", "x/x/g" ],
107 [ "(/ x/g)", "(/ x/g)" ],
108 [ "if(/ x/g);", "if(/ x/g);" ],
109 [ "(x/ x/g)", "(x/x/g)" ],
110 [ "([/ x/g])", "([/ x/g])" ],
111 [ "+x/ x/g", "+x/x/g" ],
112 [ "{}/ x/g", "{}/ x/g" ],
113 [ "+{}/ x/g", "+{}/x/g" ],
114 [ "(x)/ x/g", "(x)/x/g" ],
115 [ "if(x)/ x/g", "if(x)/ x/g" ],
116 [ "for(x;x;{}/ x/g);", "for(x;x;{}/x/g);" ],
117 [ "x;x;{}/ x/g", "x;x;{}/ x/g" ],
118 [ "x:{}/ x/g", "x:{}/ x/g" ],
119 [ "switch(x){case y?z:{}/ x/g:{}/ x/g;}", "switch(x){case y?z:{}/x/g:{}/ x/g;}" ],
120 [ "function x(){}/ x/g", "function x(){}/ x/g" ],
121 [ "+function x(){}/ x/g", "+function x(){}/x/g" ],
123 // Multiline quoted string
124 [ "var foo=\"\\\nblah\\\n\";", "var foo=\"\\\nblah\\\n\";" ],
126 // Multiline quoted string followed by string with spaces
128 "var foo=\"\\\nblah\\\n\";\nvar baz = \" foo \";\n",
129 "var foo=\"\\\nblah\\\n\";var baz=\" foo \";"
132 // URL in quoted string ( // is not a comment)
134 "aNode.setAttribute('href','http://foo.bar.org/baz');",
135 "aNode.setAttribute('href','http://foo.bar.org/baz');"
138 // URL in quoted string after multiline quoted string
140 "var foo=\"\\\nblah\\\n\";\naNode.setAttribute('href','http://foo.bar.org/baz');",
141 "var foo=\"\\\nblah\\\n\";aNode.setAttribute('href','http://foo.bar.org/baz');"
144 // Division vs. regex nastiness
146 "alert( (10+10) / '/'.charCodeAt( 0 ) + '//' );",
147 "alert((10+10)/'/'.charCodeAt(0)+'//');"
149 [ "if(1)/a /g.exec('Pa ss');", "if(1)/a /g.exec('Pa ss');" ],
151 // newline insertion after 1000 chars: break after the "++", not before
152 [ str_repeat( ';', 996 ) . "if(x++);", str_repeat( ';', 996 ) . "if(x++\n);" ],
154 // Unicode letter characters should pass through ok in identifiers (T33187)
155 [ "var KaŝSkatolVal = {}", 'var KaŝSkatolVal={}' ],
157 // Per spec unicode char escape values should work in identifiers,
158 // as long as it's a valid char. In future it might get normalized.
159 [ "var Ka\\u015dSkatolVal = {}", 'var Ka\\u015dSkatolVal={}' ],
161 // Some structures that might look invalid at first sight
162 [ "var a = 5.;", "var a=5.;" ],
163 [ "5.0.toString();", "5.0.toString();" ],
164 [ "5..toString();", "5..toString();" ],
165 // Cover failure case for too many decimal points
166 [ "5...toString();", false ],
167 [ "5.\n.toString();", '5..toString();' ],
169 // Boolean minification (!0 / !1)
170 [ "var a = { b: true };", "var a={b:!0};" ],
171 [ "var a = { true: 12 };", "var a={true:12};", false ],
172 [ "a.true = 12;", "a.true=12;", false ],
173 [ "a.foo = true;", "a.foo=!0;" ],
174 [ "a.foo = false;", "a.foo=!1;" ],
179 * @dataProvider provideCases
180 * @covers JavaScriptMinifier::minify
181 * @covers JavaScriptMinifier::parseError
183 public function testMinifyOutput( $code, $expectedOutput, $expectedValid = true ) {
184 $minified = JavaScriptMinifier
::minify( $code );
186 // JSMin+'s parser will throw an exception if output is not valid JS.
187 // suppression of warnings needed for stupid crap
188 if ( $expectedValid ) {
189 MediaWiki\
suppressWarnings();
190 $parser = new JSParser();
191 MediaWiki\restoreWarnings
();
192 $parser->parse( $minified, 'minify-test.js', 1 );
198 "Minified output should be in the form expected."
202 public static function provideExponentLineBreaking() {
205 // This one gets interpreted all together by the prior code;
206 // no break at the 'E' happens.
210 // This one breaks under the bad code; splits between 'E' and '+'
214 // This one breaks under the bad code; splits between 'E' and '-'
221 * @dataProvider provideExponentLineBreaking
222 * @covers JavaScriptMinifier::minify
224 public function testExponentLineBreaking( $num ) {
225 // Long line breaking was being incorrectly done between the base and
226 // exponent part of a number, causing a syntax error. The line should
227 // instead break at the start of the number. (T34548)
228 $prefix = 'var longVarName' . str_repeat( '_', 973 ) . '=';
229 $suffix = ',shortVarName=0;';
231 $input = $prefix . $num . $suffix;
232 $expected = $prefix . "\n" . $num . $suffix;
234 $minified = JavaScriptMinifier
::minify( $input );
236 $this->assertEquals( $expected, $minified, "Line breaks must not occur in middle of exponent" );