Merge "Chinese Conversion Table Update 2017-6"
[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 // Token separation
86 [ "x in y", "x in y" ],
87 [ "/x/g in y", "/x/g in y" ],
88 [ "x in 30", "x in 30" ],
89 [ "x + ++ y", "x+ ++y" ],
90 [ "x ++ + y", "x++ +y" ],
91 [ "x / /y/.exec(z)", "x/ /y/.exec(z)" ],
92
93 // State machine
94 [ "/ x/g", "/ x/g" ],
95 [ "(function(){return/ x/g})", "(function(){return/ x/g})" ],
96 [ "+/ x/g", "+/ x/g" ],
97 [ "++/ x/g", "++/ x/g" ],
98 [ "x/ x/g", "x/x/g" ],
99 [ "(/ x/g)", "(/ x/g)" ],
100 [ "if(/ x/g);", "if(/ x/g);" ],
101 [ "(x/ x/g)", "(x/x/g)" ],
102 [ "([/ x/g])", "([/ x/g])" ],
103 [ "+x/ x/g", "+x/x/g" ],
104 [ "{}/ x/g", "{}/ x/g" ],
105 [ "+{}/ x/g", "+{}/x/g" ],
106 [ "(x)/ x/g", "(x)/x/g" ],
107 [ "if(x)/ x/g", "if(x)/ x/g" ],
108 [ "for(x;x;{}/ x/g);", "for(x;x;{}/x/g);" ],
109 [ "x;x;{}/ x/g", "x;x;{}/ x/g" ],
110 [ "x:{}/ x/g", "x:{}/ x/g" ],
111 [ "switch(x){case y?z:{}/ x/g:{}/ x/g;}", "switch(x){case y?z:{}/x/g:{}/ x/g;}" ],
112 [ "function x(){}/ x/g", "function x(){}/ x/g" ],
113 [ "+function x(){}/ x/g", "+function x(){}/x/g" ],
114
115 // Multiline quoted string
116 [ "var foo=\"\\\nblah\\\n\";", "var foo=\"\\\nblah\\\n\";" ],
117
118 // Multiline quoted string followed by string with spaces
119 [
120 "var foo=\"\\\nblah\\\n\";\nvar baz = \" foo \";\n",
121 "var foo=\"\\\nblah\\\n\";var baz=\" foo \";"
122 ],
123
124 // URL in quoted string ( // is not a comment)
125 [
126 "aNode.setAttribute('href','http://foo.bar.org/baz');",
127 "aNode.setAttribute('href','http://foo.bar.org/baz');"
128 ],
129
130 // URL in quoted string after multiline quoted string
131 [
132 "var foo=\"\\\nblah\\\n\";\naNode.setAttribute('href','http://foo.bar.org/baz');",
133 "var foo=\"\\\nblah\\\n\";aNode.setAttribute('href','http://foo.bar.org/baz');"
134 ],
135
136 // Division vs. regex nastiness
137 [
138 "alert( (10+10) / '/'.charCodeAt( 0 ) + '//' );",
139 "alert((10+10)/'/'.charCodeAt(0)+'//');"
140 ],
141 [ "if(1)/a /g.exec('Pa ss');", "if(1)/a /g.exec('Pa ss');" ],
142
143 // newline insertion after 1000 chars: break after the "++", not before
144 [ str_repeat( ';', 996 ) . "if(x++);", str_repeat( ';', 996 ) . "if(x++\n);" ],
145
146 // Unicode letter characters should pass through ok in identifiers (T33187)
147 [ "var KaŝSkatolVal = {}", 'var KaŝSkatolVal={}' ],
148
149 // Per spec unicode char escape values should work in identifiers,
150 // as long as it's a valid char. In future it might get normalized.
151 [ "var Ka\\u015dSkatolVal = {}", 'var Ka\\u015dSkatolVal={}' ],
152
153 // Some structures that might look invalid at first sight
154 [ "var a = 5.;", "var a=5.;" ],
155 [ "5.0.toString();", "5.0.toString();" ],
156 [ "5..toString();", "5..toString();" ],
157 // Cover failure case for too many decimal points
158 [ "5...toString();", false ],
159 [ "5.\n.toString();", '5..toString();' ],
160
161 // Boolean minification (!0 / !1)
162 [ "var a = { b: true };", "var a={b:!0};" ],
163 [ "var a = { true: 12 };", "var a={true:12};", false ],
164 [ "a.true = 12;", "a.true=12;", false ],
165 [ "a.foo = true;", "a.foo=!0;" ],
166 [ "a.foo = false;", "a.foo=!1;" ],
167 ];
168 }
169
170 /**
171 * @dataProvider provideCases
172 * @covers JavaScriptMinifier::minify
173 * @covers JavaScriptMinifier::parseError
174 */
175 public function testMinifyOutput( $code, $expectedOutput, $expectedValid = true ) {
176 $minified = JavaScriptMinifier::minify( $code );
177
178 // JSMin+'s parser will throw an exception if output is not valid JS.
179 // suppression of warnings needed for stupid crap
180 if ( $expectedValid ) {
181 MediaWiki\suppressWarnings();
182 $parser = new JSParser();
183 MediaWiki\restoreWarnings();
184 $parser->parse( $minified, 'minify-test.js', 1 );
185 }
186
187 $this->assertEquals(
188 $expectedOutput,
189 $minified,
190 "Minified output should be in the form expected."
191 );
192 }
193
194 public static function provideExponentLineBreaking() {
195 return [
196 [
197 // This one gets interpreted all together by the prior code;
198 // no break at the 'E' happens.
199 '1.23456789E55',
200 ],
201 [
202 // This one breaks under the bad code; splits between 'E' and '+'
203 '1.23456789E+5',
204 ],
205 [
206 // This one breaks under the bad code; splits between 'E' and '-'
207 '1.23456789E-5',
208 ],
209 ];
210 }
211
212 /**
213 * @dataProvider provideExponentLineBreaking
214 * @covers JavaScriptMinifier::minify
215 */
216 public function testExponentLineBreaking( $num ) {
217 // Long line breaking was being incorrectly done between the base and
218 // exponent part of a number, causing a syntax error. The line should
219 // instead break at the start of the number. (T34548)
220 $prefix = 'var longVarName' . str_repeat( '_', 973 ) . '=';
221 $suffix = ',shortVarName=0;';
222
223 $input = $prefix . $num . $suffix;
224 $expected = $prefix . "\n" . $num . $suffix;
225
226 $minified = JavaScriptMinifier::minify( $input );
227
228 $this->assertEquals( $expected, $minified, "Line breaks must not occur in middle of exponent" );
229 }
230 }