Merge "Widgets: Allow titles with name of Object.prototypes"
[lhc/web/wiklou.git] / tests / phpunit / includes / libs / JavaScriptMinifierTest.php
1 <?php
2
3 class JavaScriptMinifierTest extends PHPUnit_Framework_TestCase {
4
5 use MediaWikiCoversValidator;
6
7 public static function provideCases() {
8 return [
9
10 // Basic whitespace and comments that should be stripped entirely
11 [ "\r\t\f \v\n\r", "" ],
12 [ "/* Foo *\n*bar\n*/", "" ],
13
14 /**
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.
18 */
19 [
20 "/**\n * Foo\n * {\n * 'bar' : {\n * "
21 . "//Multiple rules with configurable operators\n * 'baz' : false\n * }\n */",
22 "" ],
23
24 /**
25 * ' Foo \' bar \
26 * baz \' quox ' .
27 */
28 [
29 "' Foo \\' bar \\\n baz \\' quox ' .length",
30 "' Foo \\' bar \\\n baz \\' quox '.length"
31 ],
32 [
33 "\" Foo \\\" bar \\\n baz \\\" quox \" .length",
34 "\" Foo \\\" bar \\\n baz \\\" quox \".length"
35 ],
36 [ "// Foo b/ar baz", "" ],
37 [
38 "/ Foo \\/ bar [ / \\] / ] baz / .length",
39 "/ Foo \\/ bar [ / \\] / ] baz /.length"
40 ],
41
42 // HTML comments
43 [ "<!-- Foo bar", "" ],
44 [ "<!-- Foo --> bar", "" ],
45 [ "--> Foo", "" ],
46 [ "x --> y", "x-->y" ],
47
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;" ],
54 [ "x\ny;", "x\ny;" ],
55 [ "x\n++y;", "x\n++y;" ],
56 [ "x\n!y;", "x\n!y;" ],
57 [ "x\n{y}", "x\n{y}" ],
58 [ "x\n+y;", "x+y;" ],
59 [ "x\n(y);", "x(y);" ],
60 [ "5.\nx;", "5.\nx;" ],
61 [ "0xFF.\nx;", "0xFF.x;" ],
62 [ "5.3.\nx;", "5.3.x;" ],
63
64 // Cover failure case for incomplete hex literal
65 [ "0x;", false, false ],
66
67 // Cover failure case for number with no digits after E
68 [ "1.4E", false, false ],
69
70 // Cover failure case for number with several E
71 [ "1.4EE2", false, false ],
72 [ "1.4EE", false, false ],
73
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 ],
77
78 // Semicolon insertion between an expression having an inline
79 // comment after it, and a statement on the next line (T29046).
80 [
81 "var a = this //foo bar \n for ( b = 0; c < d; b++ ) {}",
82 "var a=this\nfor(b=0;c<d;b++){}"
83 ],
84
85 // Cover failure case of incomplete regexp at end of file (T75556)
86 // FIXME: This is invalid, but currently tolerated
87 [ "*/", "*/", false ],
88
89 // Cover failure case of incomplete string at end of file (T75556)
90 // FIXME: This is invalid, but currently tolerated
91 [ "'a", "'a", false ],
92
93 // Token separation
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)" ],
100
101 // State machine
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" ],
122
123 // Multiline quoted string
124 [ "var foo=\"\\\nblah\\\n\";", "var foo=\"\\\nblah\\\n\";" ],
125
126 // Multiline quoted string followed by string with spaces
127 [
128 "var foo=\"\\\nblah\\\n\";\nvar baz = \" foo \";\n",
129 "var foo=\"\\\nblah\\\n\";var baz=\" foo \";"
130 ],
131
132 // URL in quoted string ( // is not a comment)
133 [
134 "aNode.setAttribute('href','http://foo.bar.org/baz');",
135 "aNode.setAttribute('href','http://foo.bar.org/baz');"
136 ],
137
138 // URL in quoted string after multiline quoted string
139 [
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');"
142 ],
143
144 // Division vs. regex nastiness
145 [
146 "alert( (10+10) / '/'.charCodeAt( 0 ) + '//' );",
147 "alert((10+10)/'/'.charCodeAt(0)+'//');"
148 ],
149 [ "if(1)/a /g.exec('Pa ss');", "if(1)/a /g.exec('Pa ss');" ],
150
151 // newline insertion after 1000 chars: break after the "++", not before
152 [ str_repeat( ';', 996 ) . "if(x++);", str_repeat( ';', 996 ) . "if(x++\n);" ],
153
154 // Unicode letter characters should pass through ok in identifiers (T33187)
155 [ "var KaŝSkatolVal = {}", 'var KaŝSkatolVal={}' ],
156
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={}' ],
160
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();' ],
168
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;" ],
175 ];
176 }
177
178 /**
179 * @dataProvider provideCases
180 * @covers JavaScriptMinifier::minify
181 * @covers JavaScriptMinifier::parseError
182 */
183 public function testMinifyOutput( $code, $expectedOutput, $expectedValid = true ) {
184 $minified = JavaScriptMinifier::minify( $code );
185
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 );
193 }
194
195 $this->assertEquals(
196 $expectedOutput,
197 $minified,
198 "Minified output should be in the form expected."
199 );
200 }
201
202 public static function provideExponentLineBreaking() {
203 return [
204 [
205 // This one gets interpreted all together by the prior code;
206 // no break at the 'E' happens.
207 '1.23456789E55',
208 ],
209 [
210 // This one breaks under the bad code; splits between 'E' and '+'
211 '1.23456789E+5',
212 ],
213 [
214 // This one breaks under the bad code; splits between 'E' and '-'
215 '1.23456789E-5',
216 ],
217 ];
218 }
219
220 /**
221 * @dataProvider provideExponentLineBreaking
222 * @covers JavaScriptMinifier::minify
223 */
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;';
230
231 $input = $prefix . $num . $suffix;
232 $expected = $prefix . "\n" . $num . $suffix;
233
234 $minified = JavaScriptMinifier::minify( $input );
235
236 $this->assertEquals( $expected, $minified, "Line breaks must not occur in middle of exponent" );
237 }
238 }