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 char class in regexp (T75556)
90 // FIXME: This is invalid, but currently tolerated
91 [ "/a[b/.test", "/a[b/.test", false ],
93 // Cover failure case of incomplete string at end of file (T75556)
94 // FIXME: This is invalid, but currently tolerated
95 [ "'a", "'a", false ],
98 [ "x in y", "x in y" ],
99 [ "/x/g in y", "/x/g in y" ],
100 [ "x in 30", "x in 30" ],
101 [ "x + ++ y", "x+ ++y" ],
102 [ "x ++ + y", "x++ +y" ],
103 [ "x / /y/.exec(z)", "x/ /y/.exec(z)" ],
106 [ "/ x/g", "/ x/g" ],
107 [ "(function(){return/ x/g})", "(function(){return/ x/g})" ],
108 [ "+/ x/g", "+/ x/g" ],
109 [ "++/ x/g", "++/ x/g" ],
110 [ "x/ x/g", "x/x/g" ],
111 [ "(/ x/g)", "(/ x/g)" ],
112 [ "if(/ x/g);", "if(/ x/g);" ],
113 [ "(x/ x/g)", "(x/x/g)" ],
114 [ "([/ x/g])", "([/ x/g])" ],
115 [ "+x/ x/g", "+x/x/g" ],
116 [ "{}/ x/g", "{}/ x/g" ],
117 [ "+{}/ x/g", "+{}/x/g" ],
118 [ "(x)/ x/g", "(x)/x/g" ],
119 [ "if(x)/ x/g", "if(x)/ x/g" ],
120 [ "for(x;x;{}/ x/g);", "for(x;x;{}/x/g);" ],
121 [ "x;x;{}/ x/g", "x;x;{}/ x/g" ],
122 [ "x:{}/ x/g", "x:{}/ x/g" ],
123 [ "switch(x){case y?z:{}/ x/g:{}/ x/g;}", "switch(x){case y?z:{}/x/g:{}/ x/g;}" ],
124 [ "function x(){}/ x/g", "function x(){}/ x/g" ],
125 [ "+function x(){}/ x/g", "+function x(){}/x/g" ],
127 // Multiline quoted string
128 [ "var foo=\"\\\nblah\\\n\";", "var foo=\"\\\nblah\\\n\";" ],
130 // Multiline quoted string followed by string with spaces
132 "var foo=\"\\\nblah\\\n\";\nvar baz = \" foo \";\n",
133 "var foo=\"\\\nblah\\\n\";var baz=\" foo \";"
136 // URL in quoted string ( // is not a comment)
138 "aNode.setAttribute('href','http://foo.bar.org/baz');",
139 "aNode.setAttribute('href','http://foo.bar.org/baz');"
142 // URL in quoted string after multiline quoted string
144 "var foo=\"\\\nblah\\\n\";\naNode.setAttribute('href','http://foo.bar.org/baz');",
145 "var foo=\"\\\nblah\\\n\";aNode.setAttribute('href','http://foo.bar.org/baz');"
148 // Division vs. regex nastiness
150 "alert( (10+10) / '/'.charCodeAt( 0 ) + '//' );",
151 "alert((10+10)/'/'.charCodeAt(0)+'//');"
153 [ "if(1)/a /g.exec('Pa ss');", "if(1)/a /g.exec('Pa ss');" ],
155 // newline insertion after 1000 chars: break after the "++", not before
156 [ str_repeat( ';', 996 ) . "if(x++);", str_repeat( ';', 996 ) . "if(x++\n);" ],
158 // Unicode letter characters should pass through ok in identifiers (T33187)
159 [ "var KaŝSkatolVal = {}", 'var KaŝSkatolVal={}' ],
161 // Per spec unicode char escape values should work in identifiers,
162 // as long as it's a valid char. In future it might get normalized.
163 [ "var Ka\\u015dSkatolVal = {}", 'var Ka\\u015dSkatolVal={}' ],
165 // Some structures that might look invalid at first sight
166 [ "var a = 5.;", "var a=5.;" ],
167 [ "5.0.toString();", "5.0.toString();" ],
168 [ "5..toString();", "5..toString();" ],
169 // Cover failure case for too many decimal points
170 [ "5...toString();", false ],
171 [ "5.\n.toString();", '5..toString();' ],
173 // Boolean minification (!0 / !1)
174 [ "var a = { b: true };", "var a={b:!0};" ],
175 [ "var a = { true: 12 };", "var a={true:12};", false ],
176 [ "a.true = 12;", "a.true=12;", false ],
177 [ "a.foo = true;", "a.foo=!0;" ],
178 [ "a.foo = false;", "a.foo=!1;" ],
183 * @dataProvider provideCases
184 * @covers JavaScriptMinifier::minify
185 * @covers JavaScriptMinifier::parseError
187 public function testMinifyOutput( $code, $expectedOutput, $expectedValid = true ) {
188 $minified = JavaScriptMinifier
::minify( $code );
190 // JSMin+'s parser will throw an exception if output is not valid JS.
191 // suppression of warnings needed for stupid crap
192 if ( $expectedValid ) {
193 Wikimedia\
suppressWarnings();
194 $parser = new JSParser();
195 Wikimedia\restoreWarnings
();
196 $parser->parse( $minified, 'minify-test.js', 1 );
202 "Minified output should be in the form expected."
207 * @covers JavaScriptMinifier::minify
209 public function testReturnLineBreak() {
210 // Regression test for T201606.
211 $lineFill = str_repeat( 'x', 993 );
212 $code = <<<JAVASCRIPT
221 return name === 'input';
225 "call(function(){try{}catch(e){push={apply:1?0:{}};}"
226 // FIXME: Token `name` must be on line 2 instead of line 3
227 . "\n$lineFill return"
228 . "\nname==='input';});",
229 JavaScriptMinifier
::minify( $code )
233 public static function provideExponentLineBreaking() {
236 // This one gets interpreted all together by the prior code;
237 // no break at the 'E' happens.
241 // This one breaks under the bad code; splits between 'E' and '+'
245 // This one breaks under the bad code; splits between 'E' and '-'
252 * @dataProvider provideExponentLineBreaking
253 * @covers JavaScriptMinifier::minify
255 public function testExponentLineBreaking( $num ) {
256 // Long line breaking was being incorrectly done between the base and
257 // exponent part of a number, causing a syntax error. The line should
258 // instead break at the start of the number. (T34548)
259 $prefix = 'var longVarName' . str_repeat( '_', 973 ) . '=';
260 $suffix = ',shortVarName=0;';
262 $input = $prefix . $num . $suffix;
263 $expected = $prefix . "\n" . $num . $suffix;
265 $minified = JavaScriptMinifier
::minify( $input );
267 $this->assertEquals( $expected, $minified, "Line breaks must not occur in middle of exponent" );