Merge "WebInstallerOutput: Apply wfBCP47() to lang attribute"
[mediawiki.git] / tests / phpunit / includes / libs / JavaScriptMinifierTest.php
blobc8795b2e46d5b57308954aa8b63d1a4f1cc42034
1 <?php
3 class JavaScriptMinifierTest extends MediaWikiTestCase {
5 public static function provideCases() {
6 return array(
8 // Basic whitespace and comments that should be stripped entirely
9 array( "\r\t\f \v\n\r", "" ),
10 array( "/* 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.
17 array(
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 ' .
26 array(
27 "' Foo \\' bar \\\n baz \\' quox ' .length",
28 "' Foo \\' bar \\\n baz \\' quox '.length"
30 array(
31 "\" Foo \\\" bar \\\n baz \\\" quox \" .length",
32 "\" Foo \\\" bar \\\n baz \\\" quox \".length"
34 array( "// Foo b/ar baz", "" ),
35 array(
36 "/ Foo \\/ bar [ / \\] / ] baz / .length",
37 "/ Foo \\/ bar [ / \\] / ] baz /.length"
40 // HTML comments
41 array( "<!-- Foo bar", "" ),
42 array( "<!-- Foo --> bar", "" ),
43 array( "--> Foo", "" ),
44 array( "x --> y", "x-->y" ),
46 // Semicolon insertion
47 array( "(function(){return\nx;})", "(function(){return\nx;})" ),
48 array( "throw\nx;", "throw\nx;" ),
49 array( "while(p){continue\nx;}", "while(p){continue\nx;}" ),
50 array( "while(p){break\nx;}", "while(p){break\nx;}" ),
51 array( "var\nx;", "var x;" ),
52 array( "x\ny;", "x\ny;" ),
53 array( "x\n++y;", "x\n++y;" ),
54 array( "x\n!y;", "x\n!y;" ),
55 array( "x\n{y}", "x\n{y}" ),
56 array( "x\n+y;", "x+y;" ),
57 array( "x\n(y);", "x(y);" ),
58 array( "5.\nx;", "5.\nx;" ),
59 array( "0xFF.\nx;", "0xFF.x;" ),
60 array( "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).
64 array(
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 array( "x in y", "x in y" ),
71 array( "/x/g in y", "/x/g in y" ),
72 array( "x in 30", "x in 30" ),
73 array( "x + ++ y", "x+ ++y" ),
74 array( "x ++ + y", "x++ +y" ),
75 array( "x / /y/.exec(z)", "x/ /y/.exec(z)" ),
77 // State machine
78 array( "/ x/g", "/ x/g" ),
79 array( "(function(){return/ x/g})", "(function(){return/ x/g})" ),
80 array( "+/ x/g", "+/ x/g" ),
81 array( "++/ x/g", "++/ x/g" ),
82 array( "x/ x/g", "x/x/g" ),
83 array( "(/ x/g)", "(/ x/g)" ),
84 array( "if(/ x/g);", "if(/ x/g);" ),
85 array( "(x/ x/g)", "(x/x/g)" ),
86 array( "([/ x/g])", "([/ x/g])" ),
87 array( "+x/ x/g", "+x/x/g" ),
88 array( "{}/ x/g", "{}/ x/g" ),
89 array( "+{}/ x/g", "+{}/x/g" ),
90 array( "(x)/ x/g", "(x)/x/g" ),
91 array( "if(x)/ x/g", "if(x)/ x/g" ),
92 array( "for(x;x;{}/ x/g);", "for(x;x;{}/x/g);" ),
93 array( "x;x;{}/ x/g", "x;x;{}/ x/g" ),
94 array( "x:{}/ x/g", "x:{}/ x/g" ),
95 array( "switch(x){case y?z:{}/ x/g:{}/ x/g;}", "switch(x){case y?z:{}/x/g:{}/ x/g;}" ),
96 array( "function x(){}/ x/g", "function x(){}/ x/g" ),
97 array( "+function x(){}/ x/g", "+function x(){}/x/g" ),
99 // Multiline quoted string
100 array( "var foo=\"\\\nblah\\\n\";", "var foo=\"\\\nblah\\\n\";" ),
102 // Multiline quoted string followed by string with spaces
103 array(
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)
109 array(
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
115 array(
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
121 array(
122 "alert( (10+10) / '/'.charCodeAt( 0 ) + '//' );",
123 "alert((10+10)/'/'.charCodeAt(0)+'//');"
125 array( "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 array( str_repeat( ';', 996 ) . "if(x++);", str_repeat( ';', 996 ) . "if(x++\n);" ),
130 // Unicode letter characters should pass through ok in identifiers (bug 31187)
131 array( "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 array( "var Ka\\u015dSkatolVal = {}", 'var Ka\\u015dSkatolVal={}' ),
137 // Some structures that might look invalid at first sight
138 array( "var a = 5.;", "var a=5.;" ),
139 array( "5.0.toString();", "5.0.toString();" ),
140 array( "5..toString();", "5..toString();" ),
141 array( "5...toString();", false ),
142 array( "5.\n.toString();", '5..toString();' ),
147 * @dataProvider provideCases
148 * @covers JavaScriptMinifier::minify
150 public function testJavaScriptMinifierOutput( $code, $expectedOutput ) {
151 $minified = JavaScriptMinifier::minify( $code );
153 // JSMin+'s parser will throw an exception if output is not valid JS.
154 // suppression of warnings needed for stupid crap
155 wfSuppressWarnings();
156 $parser = new JSParser();
157 wfRestoreWarnings();
158 $parser->parse( $minified, 'minify-test.js', 1 );
160 $this->assertEquals(
161 $expectedOutput,
162 $minified,
163 "Minified output should be in the form expected."
167 public static function provideBug32548() {
168 return array(
169 array(
170 // This one gets interpreted all together by the prior code;
171 // no break at the 'E' happens.
172 '1.23456789E55',
174 array(
175 // This one breaks under the bad code; splits between 'E' and '+'
176 '1.23456789E+5',
178 array(
179 // This one breaks under the bad code; splits between 'E' and '-'
180 '1.23456789E-5',
186 * @dataProvider provideBug32548
187 * @covers JavaScriptMinifier::minify
188 * @todo give this test a real name explaining what is being tested here
190 public function testBug32548Exponent( $num ) {
191 // Long line breaking was being incorrectly done between the base and
192 // exponent part of a number, causing a syntax error. The line should
193 // instead break at the start of the number.
194 $prefix = 'var longVarName' . str_repeat( '_', 973 ) . '=';
195 $suffix = ',shortVarName=0;';
197 $input = $prefix . $num . $suffix;
198 $expected = $prefix . "\n" . $num . $suffix;
200 $minified = JavaScriptMinifier::minify( $input );
202 $this->assertEquals( $expected, $minified, "Line breaks must not occur in middle of exponent" );