MediaWiki  1.23.2
JavaScriptMinifierTest.php
Go to the documentation of this file.
1 <?php
2 
4 
5  public static function provideCases() {
6  return array(
7 
8  // Basic whitespace and comments that should be stripped entirely
9  array( "\r\t\f \v\n\r", "" ),
10  array( "/* Foo *\n*bar\n*/", "" ),
11 
17  array( "/**\n * Foo\n * {\n * 'bar' : {\n * //Multiple rules with configurable operators\n * 'baz' : false\n * }\n */", "" ),
18 
23  array( "' Foo \\' bar \\\n baz \\' quox ' .length", "' Foo \\' bar \\\n baz \\' quox '.length" ),
24  array( "\" Foo \\\" bar \\\n baz \\\" quox \" .length", "\" Foo \\\" bar \\\n baz \\\" quox \".length" ),
25  array( "// Foo b/ar baz", "" ),
26  array( "/ Foo \\/ bar [ / \\] / ] baz / .length", "/ Foo \\/ bar [ / \\] / ] baz /.length" ),
27 
28  // HTML comments
29  array( "<!-- Foo bar", "" ),
30  array( "<!-- Foo --> bar", "" ),
31  array( "--> Foo", "" ),
32  array( "x --> y", "x-->y" ),
33 
34  // Semicolon insertion
35  array( "(function(){return\nx;})", "(function(){return\nx;})" ),
36  array( "throw\nx;", "throw\nx;" ),
37  array( "while(p){continue\nx;}", "while(p){continue\nx;}" ),
38  array( "while(p){break\nx;}", "while(p){break\nx;}" ),
39  array( "var\nx;", "var x;" ),
40  array( "x\ny;", "x\ny;" ),
41  array( "x\n++y;", "x\n++y;" ),
42  array( "x\n!y;", "x\n!y;" ),
43  array( "x\n{y}", "x\n{y}" ),
44  array( "x\n+y;", "x+y;" ),
45  array( "x\n(y);", "x(y);" ),
46  array( "5.\nx;", "5.\nx;" ),
47  array( "0xFF.\nx;", "0xFF.x;" ),
48  array( "5.3.\nx;", "5.3.x;" ),
49 
50  // Semicolon insertion between an expression having an inline
51  // comment after it, and a statement on the next line (bug 27046).
52  array( "var a = this //foo bar \n for ( b = 0; c < d; b++ ) {}", "var a=this\nfor(b=0;c<d;b++){}" ),
53 
54  // Token separation
55  array( "x in y", "x in y" ),
56  array( "/x/g in y", "/x/g in y" ),
57  array( "x in 30", "x in 30" ),
58  array( "x + ++ y", "x+ ++y" ),
59  array( "x ++ + y", "x++ +y" ),
60  array( "x / /y/.exec(z)", "x/ /y/.exec(z)" ),
61 
62  // State machine
63  array( "/ x/g", "/ x/g" ),
64  array( "(function(){return/ x/g})", "(function(){return/ x/g})" ),
65  array( "+/ x/g", "+/ x/g" ),
66  array( "++/ x/g", "++/ x/g" ),
67  array( "x/ x/g", "x/x/g" ),
68  array( "(/ x/g)", "(/ x/g)" ),
69  array( "if(/ x/g);", "if(/ x/g);" ),
70  array( "(x/ x/g)", "(x/x/g)" ),
71  array( "([/ x/g])", "([/ x/g])" ),
72  array( "+x/ x/g", "+x/x/g" ),
73  array( "{}/ x/g", "{}/ x/g" ),
74  array( "+{}/ x/g", "+{}/x/g" ),
75  array( "(x)/ x/g", "(x)/x/g" ),
76  array( "if(x)/ x/g", "if(x)/ x/g" ),
77  array( "for(x;x;{}/ x/g);", "for(x;x;{}/x/g);" ),
78  array( "x;x;{}/ x/g", "x;x;{}/ x/g" ),
79  array( "x:{}/ x/g", "x:{}/ x/g" ),
80  array( "switch(x){case y?z:{}/ x/g:{}/ x/g;}", "switch(x){case y?z:{}/x/g:{}/ x/g;}" ),
81  array( "function x(){}/ x/g", "function x(){}/ x/g" ),
82  array( "+function x(){}/ x/g", "+function x(){}/x/g" ),
83 
84  // Multiline quoted string
85  array( "var foo=\"\\\nblah\\\n\";", "var foo=\"\\\nblah\\\n\";" ),
86 
87  // Multiline quoted string followed by string with spaces
88  array( "var foo=\"\\\nblah\\\n\";\nvar baz = \" foo \";\n", "var foo=\"\\\nblah\\\n\";var baz=\" foo \";" ),
89 
90  // URL in quoted string ( // is not a comment)
91  array( "aNode.setAttribute('href','http://foo.bar.org/baz');", "aNode.setAttribute('href','http://foo.bar.org/baz');" ),
92 
93  // URL in quoted string after multiline quoted string
94  array( "var foo=\"\\\nblah\\\n\";\naNode.setAttribute('href','http://foo.bar.org/baz');", "var foo=\"\\\nblah\\\n\";aNode.setAttribute('href','http://foo.bar.org/baz');" ),
95 
96  // Division vs. regex nastiness
97  array( "alert( (10+10) / '/'.charCodeAt( 0 ) + '//' );", "alert((10+10)/'/'.charCodeAt(0)+'//');" ),
98  array( "if(1)/a /g.exec('Pa ss');", "if(1)/a /g.exec('Pa ss');" ),
99 
100  // newline insertion after 1000 chars: break after the "++", not before
101  array( str_repeat( ';', 996 ) . "if(x++);", str_repeat( ';', 996 ) . "if(x++\n);" ),
102 
103  // Unicode letter characters should pass through ok in identifiers (bug 31187)
104  array( "var KaŝSkatolVal = {}", 'var KaŝSkatolVal={}' ),
105 
106  // Per spec unicode char escape values should work in identifiers,
107  // as long as it's a valid char. In future it might get normalized.
108  array( "var Ka\\u015dSkatolVal = {}", 'var Ka\\u015dSkatolVal={}' ),
109 
110  // Some structures that might look invalid at first sight
111  array( "var a = 5.;", "var a=5.;" ),
112  array( "5.0.toString();", "5.0.toString();" ),
113  array( "5..toString();", "5..toString();" ),
114  array( "5...toString();", false ),
115  array( "5.\n.toString();", '5..toString();' ),
116  );
117  }
118 
123  public function testJavaScriptMinifierOutput( $code, $expectedOutput ) {
124  $minified = JavaScriptMinifier::minify( $code );
125 
126  // JSMin+'s parser will throw an exception if output is not valid JS.
127  // suppression of warnings needed for stupid crap
129  $parser = new JSParser();
131  $parser->parse( $minified, 'minify-test.js', 1 );
132 
133  $this->assertEquals( $expectedOutput, $minified, "Minified output should be in the form expected." );
134  }
135 
136  public static function provideBug32548() {
137  return array(
138  array(
139  // This one gets interpreted all together by the prior code;
140  // no break at the 'E' happens.
141  '1.23456789E55',
142  ),
143  array(
144  // This one breaks under the bad code; splits between 'E' and '+'
145  '1.23456789E+5',
146  ),
147  array(
148  // This one breaks under the bad code; splits between 'E' and '-'
149  '1.23456789E-5',
150  ),
151  );
152  }
153 
159  public function testBug32548Exponent( $num ) {
160  // Long line breaking was being incorrectly done between the base and
161  // exponent part of a number, causing a syntax error. The line should
162  // instead break at the start of the number.
163  $prefix = 'var longVarName' . str_repeat( '_', 973 ) . '=';
164  $suffix = ',shortVarName=0;';
165 
166  $input = $prefix . $num . $suffix;
167  $expected = $prefix . "\n" . $num . $suffix;
168 
169  $minified = JavaScriptMinifier::minify( $input );
170 
171  $this->assertEquals( $expected, $minified, "Line breaks must not occur in middle of exponent" );
172  }
173 }
JavaScriptMinifierTest
Definition: JavaScriptMinifierTest.php:3
JavaScriptMinifierTest\testJavaScriptMinifierOutput
testJavaScriptMinifierOutput( $code, $expectedOutput)
@dataProvider provideCases @covers JavaScriptMinifier::minify
Definition: JavaScriptMinifierTest.php:123
php
skin txt MediaWiki includes four core it has been set as the default in MediaWiki since the replacing Monobook it had been been the default skin since before being replaced by Vector largely rewritten in while keeping its appearance Several legacy skins were removed in the as the burden of supporting them became too heavy to bear Those in etc for skin dependent CSS etc for skin dependent JavaScript These can also be customised on a per user by etc This feature has led to a wide variety of user styles becoming that gallery is a good place to ending in php
Definition: skin.txt:62
JavaScriptMinifierTest\provideBug32548
static provideBug32548()
Definition: JavaScriptMinifierTest.php:136
wfSuppressWarnings
wfSuppressWarnings( $end=false)
Reference-counted warning suppression.
Definition: GlobalFunctions.php:2387
JavaScriptMinifier\minify
static minify( $s, $statementsOnOwnLine=false, $maxLineLength=1000)
Returns minified JavaScript code.
Definition: JavaScriptMinifier.php:80
wfRestoreWarnings
wfRestoreWarnings()
Restore error level to previous value.
Definition: GlobalFunctions.php:2417
$parser
do that in ParserLimitReportFormat instead $parser
Definition: hooks.txt:1956
MediaWikiTestCase
Definition: MediaWikiTestCase.php:6
array
the array() calling protocol came about after MediaWiki 1.4rc1.
List of Api Query prop modules.
JavaScriptMinifierTest\provideCases
static provideCases()
Definition: JavaScriptMinifierTest.php:5
JavaScriptMinifierTest\testBug32548Exponent
testBug32548Exponent( $num)
@dataProvider provideBug32548 @covers JavaScriptMinifier::minify
Definition: JavaScriptMinifierTest.php:159
JSParser
Definition: jsminplus.php:673