X-Git-Url: https://git.heureux-cyclage.org/?p=lhc%2Fweb%2Fwiklou.git;a=blobdiff_plain;f=includes%2Flibs%2FJavaScriptMinifier.php;h=bbba33a7eaca87571ec628576d7ec27c4e2120e1;hp=141a5153d4a3a787bb2d04086c02592681256d2d;hb=1a40e0cc86b6ee0706606ded3ea243dfde4a414c;hpb=4f985950ba5df1efa0f6d91ccaf52a55e33ece00 diff --git a/includes/libs/JavaScriptMinifier.php b/includes/libs/JavaScriptMinifier.php index 141a5153d4..bbba33a7ea 100644 --- a/includes/libs/JavaScriptMinifier.php +++ b/includes/libs/JavaScriptMinifier.php @@ -74,11 +74,10 @@ class JavaScriptMinifier { * or when required to guard against semicolon insertion. * * @param string $s JavaScript code to minify - * @param bool $statementsOnOwnLine Whether to put each statement on its own line * @param int $maxLineLength Maximum length of a single line, or -1 for no maximum. * @return String Minified code */ - public static function minify( $s, $statementsOnOwnLine = false, $maxLineLength = 1000 ) { + public static function minify( $s, $maxLineLength = 1000 ) { // First we declare a few tables that contain our parsing rules // $opChars : characters, which can be combined without whitespace in between them @@ -387,23 +386,6 @@ class JavaScriptMinifier { ) ); - // Rules for when newlines should be inserted if - // $statementsOnOwnLine is enabled. - // $newlineBefore is checked before switching state, - // $newlineAfter is checked after - $newlineBefore = array( - self::STATEMENT => array( - self::TYPE_BRACE_CLOSE => true, - ), - ); - $newlineAfter = array( - self::STATEMENT => array( - self::TYPE_BRACE_OPEN => true, - self::TYPE_PAREN_CLOSE => true, - self::TYPE_SEMICOLON => true, - ), - ); - // $divStates : Contains all states that can be followed by a division operator $divStates = array( self::EXPRESSION_OP => true, @@ -467,18 +449,24 @@ class JavaScriptMinifier { // We have to distinguish between regexp literals and division operators // A division operator is only possible in certain states } elseif( $ch === '/' && !isset( $divStates[$state] ) ) { - // Regexp literal, search to the end, skipping over backslash escapes and - // character classes + // Regexp literal for( ; ; ) { do{ + // Skip until we find "/" (end of regexp), "\" (backslash escapes), + // or "[" (start of character classes). $end += strcspn( $s, '/[\\', $end ) + 2; + // If backslash escape, keep searching... } while( $end - 2 < $length && $s[$end - 2] === '\\' ); $end--; + // If the end, stop here. if( $end - 1 >= $length || $s[$end - 1] === '/' ) { break; } + // (Implicit else), we must've found the start of a char class, + // skip until we find "]" (end of char class), or "\" (backslash escape) do{ $end += strcspn( $s, ']\\', $end ) + 2; + // If backslash escape, keep searching... } while( $end - 2 < $length && $s[$end - 2] === '\\' ); $end--; }; @@ -580,15 +568,6 @@ class JavaScriptMinifier { $pos = $end; $newlineFound = false; - // Output a newline after the token if required - // This is checked before AND after switching state - $newlineAdded = false; - if ( $statementsOnOwnLine && !$newlineAdded && isset( $newlineBefore[$state][$type] ) ) { - $out .= "\n"; - $lineLength = 0; - $newlineAdded = true; - } - // Now that we have output our token, transition into the new state. if( isset( $push[$state][$type] ) && count( $stack ) < self::STACK_LIMIT ) { $stack[] = $push[$state][$type]; @@ -598,12 +577,6 @@ class JavaScriptMinifier { } elseif( isset( $goto[$state][$type] ) ) { $state = $goto[$state][$type]; } - - // Check for newline insertion again - if ( $statementsOnOwnLine && !$newlineAdded && isset( $newlineAfter[$state][$type] ) ) { - $out .= "\n"; - $lineLength = 0; - } } return $out; }