setMaxLineLength( 1000 ); } private function setMaxLineLength( $val ) { $classReflect = new ReflectionClass( JavaScriptMinifier::class ); $propertyReflect = $classReflect->getProperty( 'maxLineLength' ); $propertyReflect->setAccessible( true ); $propertyReflect->setValue( JavaScriptMinifier::class, $val ); } public static function provideCases() { return [ // Basic whitespace and comments that should be stripped entirely [ "\r\t\f \v\n\r", "" ], [ "/* Foo *\n*bar\n*/", "" ], /** * Slashes used inside block comments (T28931). * At some point there was a bug that caused this comment to be ended at '* /', * causing /M... to be left as the beginning of a regex. */ [ "/**\n * Foo\n * {\n * 'bar' : {\n * " . "//Multiple rules with configurable operators\n * 'baz' : false\n * }\n */", "" ], /** * ' Foo \' bar \ * baz \' quox ' . */ [ "' Foo \\' bar \\\n baz \\' quox ' .length", "' Foo \\' bar \\\n baz \\' quox '.length" ], [ "\" Foo \\\" bar \\\n baz \\\" quox \" .length", "\" Foo \\\" bar \\\n baz \\\" quox \".length" ], [ "// Foo b/ar baz", "" ], [ "/ Foo \\/ bar [ / \\] / ] baz / .length", "/ Foo \\/ bar [ / \\] / ] baz /.length" ], // HTML comments [ " bar", "" ], [ "--> Foo", "" ], [ "x --> y", "x-->y" ], // Semicolon insertion [ "(function(){return\nx;})", "(function(){return\nx;})" ], [ "throw\nx;", "throw\nx;" ], [ "while(p){continue\nx;}", "while(p){continue\nx;}" ], [ "while(p){break\nx;}", "while(p){break\nx;}" ], [ "var\nx;", "var x;" ], [ "x\ny;", "x\ny;" ], [ "x\n++y;", "x\n++y;" ], [ "x\n!y;", "x\n!y;" ], [ "x\n{y}", "x\n{y}" ], [ "x\n+y;", "x+y;" ], [ "x\n(y);", "x(y);" ], [ "5.\nx;", "5.\nx;" ], [ "0xFF.\nx;", "0xFF.x;" ], [ "5.3.\nx;", "5.3.x;" ], // Cover failure case for incomplete hex literal [ "0x;", false, false ], // Cover failure case for number with no digits after E [ "1.4E", false, false ], // Cover failure case for number with several E [ "1.4EE2", false, false ], [ "1.4EE", false, false ], // Cover failure case for number with several E (nonconsecutive) // FIXME: This is invalid, but currently tolerated [ "1.4E2E3", "1.4E2 E3", false ], // Semicolon insertion between an expression having an inline // comment after it, and a statement on the next line (T29046). [ "var a = this //foo bar \n for ( b = 0; c < d; b++ ) {}", "var a=this\nfor(b=0;cparse( $minified, 'minify-test.js', 1 ); } $this->assertEquals( $expectedOutput, $minified, "Minified output should be in the form expected." ); } public static function provideLineBreaker() { return [ [ // Regression tests for T34548. // Must not break between 'E' and '+'. 'var name = 1.23456789E55;', [ 'var', 'name', '=', '1.23456789E55', ';', ], ], [ 'var name = 1.23456789E+5;', [ 'var', 'name', '=', '1.23456789E+5', ';', ], ], [ 'var name = 1.23456789E-5;', [ 'var', 'name', '=', '1.23456789E-5', ';', ], ], [ // Must not break before '++' 'if(x++);', [ 'if', '(', 'x++', ')', ';', ], ], [ // Regression test for T201606. // Must not break between 'return' and Expression. // Was caused by bad state after '{}' in property value. <<setMaxLineLength( 1 ); $actual = JavaScriptMinifier::minify( $code ); $this->assertEquals( array_merge( [ '' ], $expectedLines ), explode( "\n", $actual ) ); } }