MediaWiki REL1_31
JavaScriptMinifierTest.php
Go to the documentation of this file.
1<?php
2
3class 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
19 [
20 "/**\n * Foo\n * {\n * 'bar' : {\n * "
21 . "//Multiple rules with configurable operators\n * 'baz' : false\n * }\n */",
22 "" ],
23
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 char class in regexp (T75556)
90 // FIXME: This is invalid, but currently tolerated
91 [ "/a[b/.test", "/a[b/.test", false ],
92
93 // Cover failure case of incomplete string at end of file (T75556)
94 // FIXME: This is invalid, but currently tolerated
95 [ "'a", "'a", false ],
96
97 // Token separation
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)" ],
104
105 // State machine
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" ],
126
127 // Multiline quoted string
128 [ "var foo=\"\\\nblah\\\n\";", "var foo=\"\\\nblah\\\n\";" ],
129
130 // Multiline quoted string followed by string with spaces
131 [
132 "var foo=\"\\\nblah\\\n\";\nvar baz = \" foo \";\n",
133 "var foo=\"\\\nblah\\\n\";var baz=\" foo \";"
134 ],
135
136 // URL in quoted string ( // is not a comment)
137 [
138 "aNode.setAttribute('href','http://foo.bar.org/baz');",
139 "aNode.setAttribute('href','http://foo.bar.org/baz');"
140 ],
141
142 // URL in quoted string after multiline quoted string
143 [
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');"
146 ],
147
148 // Division vs. regex nastiness
149 [
150 "alert( (10+10) / '/'.charCodeAt( 0 ) + '//' );",
151 "alert((10+10)/'/'.charCodeAt(0)+'//');"
152 ],
153 [ "if(1)/a /g.exec('Pa ss');", "if(1)/a /g.exec('Pa ss');" ],
154
155 // newline insertion after 1000 chars: break after the "++", not before
156 [ str_repeat( ';', 996 ) . "if(x++);", str_repeat( ';', 996 ) . "if(x++\n);" ],
157
158 // Unicode letter characters should pass through ok in identifiers (T33187)
159 [ "var KaŝSkatolVal = {}", 'var KaŝSkatolVal={}' ],
160
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={}' ],
164
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();' ],
172
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;" ],
179 ];
180 }
181
187 public function testMinifyOutput( $code, $expectedOutput, $expectedValid = true ) {
188 $minified = JavaScriptMinifier::minify( $code );
189
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 );
197 }
198
199 $this->assertEquals(
200 $expectedOutput,
201 $minified,
202 "Minified output should be in the form expected."
203 );
204 }
205
206 public static function provideExponentLineBreaking() {
207 return [
208 [
209 // This one gets interpreted all together by the prior code;
210 // no break at the 'E' happens.
211 '1.23456789E55',
212 ],
213 [
214 // This one breaks under the bad code; splits between 'E' and '+'
215 '1.23456789E+5',
216 ],
217 [
218 // This one breaks under the bad code; splits between 'E' and '-'
219 '1.23456789E-5',
220 ],
221 ];
222 }
223
228 public function testExponentLineBreaking( $num ) {
229 // Long line breaking was being incorrectly done between the base and
230 // exponent part of a number, causing a syntax error. The line should
231 // instead break at the start of the number. (T34548)
232 $prefix = 'var longVarName' . str_repeat( '_', 973 ) . '=';
233 $suffix = ',shortVarName=0;';
234
235 $input = $prefix . $num . $suffix;
236 $expected = $prefix . "\n" . $num . $suffix;
237
238 $minified = JavaScriptMinifier::minify( $input );
239
240 $this->assertEquals( $expected, $minified, "Line breaks must not occur in middle of exponent" );
241 }
242}
Apache License January AND DISTRIBUTION Definitions License shall mean the terms and conditions for use
testMinifyOutput( $code, $expectedOutput, $expectedValid=true)
provideCases JavaScriptMinifier::minify JavaScriptMinifier::parseError
testExponentLineBreaking( $num)
provideExponentLineBreaking JavaScriptMinifier::minify
static minify( $s)
Returns minified JavaScript code.
do that in ParserLimitReportFormat instead $parser
Definition hooks.txt:2603
this hook is for auditing only or null if authentication failed before getting that far or null if we can t even determine that probably a stub it is not rendered in wiki pages or galleries in category pages allow injecting custom HTML after the section Any uses of the hook need to handle escaping see BaseTemplate::getToolbox and BaseTemplate::makeListItem for details on the format of individual items inside of this array or by returning and letting standard HTTP rendering take place modifiable or by returning false and taking over the output modifiable & $code
Definition hooks.txt:865
processing should stop and the error should be shown to the user * false
Definition hooks.txt:187
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency which acts as the top level factory for services in MediaWiki which can be used to gain access to default instances of various services MediaWikiServices however also allows new services to be defined and default services to be redefined Services are defined or redefined by providing a callback the instantiator that will return a new instance of the service When it will create an instance of MediaWikiServices and populate it with the services defined in the files listed by thereby bootstrapping the DI framework Per $wgServiceWiringFiles lists includes ServiceWiring php
Definition injection.txt:37
if(is_array($mode)) switch( $mode) $input