Merge "Update docs/hooks.txt for ShowSearchHitTitle"
[mediawiki.git] / tests / phpunit / includes / libs / JavaScriptMinifierTest.php
blob3787962affd7ddcb30ce537b6fc76d5567c65444
1 <?php
3 class JavaScriptMinifierTest extends PHPUnit_Framework_TestCase {
5 public static function provideCases() {
6 return [
8 // Basic whitespace and comments that should be stripped entirely
9 [ "\r\t\f \v\n\r", "" ],
10 [ "/* Foo *\n*bar\n*/", "" ],
12 /**
13 * Slashes used inside block comments (bug 26931).
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 */",
20 "" ],
22 /**
23 * ' Foo \' bar \
24 * baz \' quox ' .
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"
40 // HTML comments
41 [ "<!-- Foo bar", "" ],
42 [ "<!-- Foo --> bar", "" ],
43 [ "--> Foo", "" ],
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;" ],
52 [ "x\ny;", "x\ny;" ],
53 [ "x\n++y;", "x\n++y;" ],
54 [ "x\n!y;", "x\n!y;" ],
55 [ "x\n{y}", "x\n{y}" ],
56 [ "x\n+y;", "x+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 // Semicolon insertion between an expression having an inline
63 // comment after it, and a statement on the next line (bug 27046).
65 "var a = this //foo bar \n for ( b = 0; c < d; b++ ) {}",
66 "var a=this\nfor(b=0;c<d;b++){}"
69 // Token separation
70 [ "x in y", "x in y" ],
71 [ "/x/g in y", "/x/g in y" ],
72 [ "x in 30", "x in 30" ],
73 [ "x + ++ y", "x+ ++y" ],
74 [ "x ++ + y", "x++ +y" ],
75 [ "x / /y/.exec(z)", "x/ /y/.exec(z)" ],
77 // State machine
78 [ "/ x/g", "/ x/g" ],
79 [ "(function(){return/ x/g})", "(function(){return/ x/g})" ],
80 [ "+/ x/g", "+/ x/g" ],
81 [ "++/ x/g", "++/ x/g" ],
82 [ "x/ x/g", "x/x/g" ],
83 [ "(/ x/g)", "(/ x/g)" ],
84 [ "if(/ x/g);", "if(/ x/g);" ],
85 [ "(x/ x/g)", "(x/x/g)" ],
86 [ "([/ x/g])", "([/ x/g])" ],
87 [ "+x/ x/g", "+x/x/g" ],
88 [ "{}/ x/g", "{}/ x/g" ],
89 [ "+{}/ x/g", "+{}/x/g" ],
90 [ "(x)/ x/g", "(x)/x/g" ],
91 [ "if(x)/ x/g", "if(x)/ x/g" ],
92 [ "for(x;x;{}/ x/g);", "for(x;x;{}/x/g);" ],
93 [ "x;x;{}/ x/g", "x;x;{}/ x/g" ],
94 [ "x:{}/ x/g", "x:{}/ x/g" ],
95 [ "switch(x){case y?z:{}/ x/g:{}/ x/g;}", "switch(x){case y?z:{}/x/g:{}/ x/g;}" ],
96 [ "function x(){}/ x/g", "function x(){}/ x/g" ],
97 [ "+function x(){}/ x/g", "+function x(){}/x/g" ],
99 // Multiline quoted string
100 [ "var foo=\"\\\nblah\\\n\";", "var foo=\"\\\nblah\\\n\";" ],
102 // Multiline quoted string followed by string with spaces
104 "var foo=\"\\\nblah\\\n\";\nvar baz = \" foo \";\n",
105 "var foo=\"\\\nblah\\\n\";var baz=\" foo \";"
108 // URL in quoted string ( // is not a comment)
110 "aNode.setAttribute('href','http://foo.bar.org/baz');",
111 "aNode.setAttribute('href','http://foo.bar.org/baz');"
114 // URL in quoted string after multiline quoted string
116 "var foo=\"\\\nblah\\\n\";\naNode.setAttribute('href','http://foo.bar.org/baz');",
117 "var foo=\"\\\nblah\\\n\";aNode.setAttribute('href','http://foo.bar.org/baz');"
120 // Division vs. regex nastiness
122 "alert( (10+10) / '/'.charCodeAt( 0 ) + '//' );",
123 "alert((10+10)/'/'.charCodeAt(0)+'//');"
125 [ "if(1)/a /g.exec('Pa ss');", "if(1)/a /g.exec('Pa ss');" ],
127 // newline insertion after 1000 chars: break after the "++", not before
128 [ str_repeat( ';', 996 ) . "if(x++);", str_repeat( ';', 996 ) . "if(x++\n);" ],
130 // Unicode letter characters should pass through ok in identifiers (bug 31187)
131 [ "var KaŝSkatolVal = {}", 'var KaŝSkatolVal={}' ],
133 // Per spec unicode char escape values should work in identifiers,
134 // as long as it's a valid char. In future it might get normalized.
135 [ "var Ka\\u015dSkatolVal = {}", 'var Ka\\u015dSkatolVal={}' ],
137 // Some structures that might look invalid at first sight
138 [ "var a = 5.;", "var a=5.;" ],
139 [ "5.0.toString();", "5.0.toString();" ],
140 [ "5..toString();", "5..toString();" ],
141 [ "5...toString();", false ],
142 [ "5.\n.toString();", '5..toString();' ],
144 // Boolean minification (!0 / !1)
145 [ "var a = { b: true };", "var a={b:!0};" ],
146 [ "var a = { true: 12 };", "var a={true:12};", false ],
147 [ "a.true = 12;", "a.true=12;", false ],
148 [ "a.foo = true;", "a.foo=!0;" ],
149 [ "a.foo = false;", "a.foo=!1;" ],
154 * @dataProvider provideCases
155 * @covers JavaScriptMinifier::minify
157 public function testJavaScriptMinifierOutput( $code, $expectedOutput, $expectedValid = true ) {
158 $minified = JavaScriptMinifier::minify( $code );
160 // JSMin+'s parser will throw an exception if output is not valid JS.
161 // suppression of warnings needed for stupid crap
162 if ( $expectedValid ) {
163 MediaWiki\suppressWarnings();
164 $parser = new JSParser();
165 MediaWiki\restoreWarnings();
166 $parser->parse( $minified, 'minify-test.js', 1 );
169 $this->assertEquals(
170 $expectedOutput,
171 $minified,
172 "Minified output should be in the form expected."
176 public static function provideExponentLineBreaking() {
177 return [
179 // This one gets interpreted all together by the prior code;
180 // no break at the 'E' happens.
181 '1.23456789E55',
184 // This one breaks under the bad code; splits between 'E' and '+'
185 '1.23456789E+5',
188 // This one breaks under the bad code; splits between 'E' and '-'
189 '1.23456789E-5',
195 * @dataProvider provideExponentLineBreaking
196 * @covers JavaScriptMinifier::minify
198 public function testExponentLineBreaking( $num ) {
199 // Long line breaking was being incorrectly done between the base and
200 // exponent part of a number, causing a syntax error. The line should
201 // instead break at the start of the number. (T34548)
202 $prefix = 'var longVarName' . str_repeat( '_', 973 ) . '=';
203 $suffix = ',shortVarName=0;';
205 $input = $prefix . $num . $suffix;
206 $expected = $prefix . "\n" . $num . $suffix;
208 $minified = JavaScriptMinifier::minify( $input );
210 $this->assertEquals( $expected, $minified, "Line breaks must not occur in middle of exponent" );