| Index: branches/wmf/1.17wmf1/includes/libs/JavaScriptDistiller.php |
| — | — | @@ -19,42 +19,50 @@ |
| 20 | 20 | * @param $stripVerticalSpace Boolean: Try to remove as much vertical whitespace as possible |
| 21 | 21 | */ |
| 22 | 22 | public static function stripWhiteSpace( $script, $stripVerticalSpace = false ) { |
| | 23 | + // Try to avoid segfaulting |
| | 24 | + // I saw segfaults with a limit of 10000, 1000 seems to work |
| | 25 | + $oldLimit = ini_get( 'pcre.recursion_limit' ); |
| | 26 | + if ( intval( $oldLimit ) > 1000 ) { |
| | 27 | + ini_set( 'pcre.recursion_limit', '1000' ); |
| | 28 | + } |
| | 29 | + |
| 23 | 30 | $script = self::stripHorizontalSpace( $script ); |
| 24 | 31 | // If requested, make some vertical whitespace collapsing as well |
| 25 | 32 | if ( $stripVerticalSpace ) { |
| 26 | 33 | $script = self::stripVerticalSpace( $script ); |
| 27 | 34 | } |
| 28 | 35 | // Done |
| | 36 | + ini_set( 'pcre.recursion_limit', $oldLimit ); |
| 29 | 37 | return $script; |
| 30 | 38 | } |
| 31 | 39 | |
| 32 | | - private static function stripHorizontalSpace( $script ) { |
| | 40 | + public static function stripHorizontalSpace( $script ) { |
| 33 | 41 | $parser = self::createParser(); |
| 34 | 42 | // Collapse horizontal whitespaces between variable names into a single space |
| 35 | | - $parser->add( '/(\\b|\\$)[ \\t]+(\\b|\\$)/', '$2 $3' ); |
| | 43 | + $parser->add( '(\b|\$) [ \t]+ (\b|\$)', '$2 $3' ); |
| 36 | 44 | // Collapse horizontal whitespaces between unary operators into a single space |
| 37 | | - $parser->add( '/([+\\-])[ \\t]+([+\\-])/', '$2 $3' ); |
| | 45 | + $parser->add( '([+\-]) [ \t]+ ([+\-])', '$2 $3' ); |
| 38 | 46 | // Remove all remaining un-protected horizontal whitespace |
| 39 | | - $parser->add( '/[ \\t]+/'); |
| | 47 | + $parser->add( '[ \t]+'); |
| 40 | 48 | // Collapse multiple vertical whitespaces with some horizontal spaces between them |
| 41 | | - $parser->add( '/[\\r\\n]+[ \\t]*[\\r\\n]+/', "\n" ); |
| | 49 | + $parser->add( '[\r\n]+ [ \t]* [\r\n]+', "\n" ); |
| 42 | 50 | // Execute and return |
| 43 | 51 | return $parser->exec($script); |
| 44 | 52 | } |
| 45 | 53 | |
| 46 | | - private static function stripVerticalSpace( $script ) { |
| | 54 | + public static function stripVerticalSpace( $script ) { |
| 47 | 55 | $parser = self::createParser(); |
| 48 | 56 | // Collapse whitespaces between and after a ){ pair (function definitions) |
| 49 | | - $parser->add( '/\\)\\s+\\{\\s+/', '){' ); |
| | 57 | + $parser->add( '\) \s+ \{ \s+', '){' ); |
| 50 | 58 | // Collapse whitespaces between and after a ({ pair (JSON argument) |
| 51 | | - $parser->add( '/\\(\\s+\\{\\s+/', '({' ); |
| | 59 | + $parser->add( '\( \s+ \{ \s+', '({' ); |
| 52 | 60 | // Collapse whitespaces between a parenthesis and a period (call chaining) |
| 53 | | - $parser->add( '/\\)\\s+\\./', ').'); |
| | 61 | + $parser->add( '\) \s+ \.', ').'); |
| 54 | 62 | // Collapse vertical whitespaces which come directly after a semicolon or a comma |
| 55 | | - $parser->add( '/([;,])\\s+/', '$2' ); |
| | 63 | + $parser->add( '( [;,] ) \s+', '$2' ); |
| 56 | 64 | // Collapse whitespaces between multiple parenthesis/brackets of similar direction |
| 57 | | - $parser->add( '/([\\)\\}])\\s+([\\)\\}])/', '$2$3' ); |
| 58 | | - $parser->add( '/([\\(\\{])\\s+([\\(\\{])/', '$2$3' ); |
| | 65 | + $parser->add( '( [\)\}] ) \s+ ( [\)\}] )', '$2$3' ); |
| | 66 | + $parser->add( '( [\(\{] ) \s+ ( [\(\{] )', '$2$3' ); |
| 59 | 67 | return $parser->exec( $script ); |
| 60 | 68 | } |
| 61 | 69 | |
| — | — | @@ -74,18 +82,44 @@ |
| 75 | 83 | // to \s if we use a backslash as the escape character. We work around this by using an |
| 76 | 84 | // obscure escape character that we hope will never appear at the end of a line. |
| 77 | 85 | $parser->escapeChar = chr( 1 ); |
| | 86 | + |
| | 87 | + // C-style comment: use non-greedy repetition to find the end |
| | 88 | + $parser->add( '\/ \* .*? \* \/' ); |
| | 89 | + |
| | 90 | + // Preserve the newline after a C++-style comment -- bug 27046 |
| | 91 | + $parser->add( '\/ \/ [^\r\n]* ( [\r\n] )', '$2' ); |
| | 92 | + |
| 78 | 93 | // Protect strings. The original code had [^\'\\v] here, but that didn't armor multiline |
| 79 | 94 | // strings correctly. This also armors multiline strings that don't have backslashes at the |
| 80 | 95 | // end of the line (these are invalid), but that's fine because we're just armoring here. |
| 81 | | - $parser->add( '/\'([^\'\\\\]*(\\\\(.|[\r\n])[^\'\\\\]*)*)\'/', '$1' ); |
| 82 | | - $parser->add( '/"([^"\\\\]*(\\\\(.|[\r\n])[^"\\\\]*)*)"/', '$1' ); |
| | 96 | + |
| | 97 | + // Single quotes |
| | 98 | + $parser->add( |
| | 99 | + '\'' . // start quote |
| | 100 | + '[^\'\\\\]*' . // a run of non-special characters |
| | 101 | + '(?:' . |
| | 102 | + '\\\\ .' . // a backslash followed by a character or line ending |
| | 103 | + '[^\'\\\\]*' . // a run of non-special characters |
| | 104 | + ')*' . // any number of the above |
| | 105 | + '\'', // end quote |
| | 106 | + '$1' ); |
| | 107 | + |
| | 108 | + // Double quotes: same as above |
| | 109 | + $parser->add( '" [^"\\\\]* (?: \\\\ . [^"\\\\]* )* "', '$1' ); |
| | 110 | + |
| 83 | 111 | // Protect regular expressions |
| 84 | | - $parser->add( '/[ \\t]+((\\/[^\\r\\n\\*][^\\/\\r\\n\\\\]*(\\\\.[^\\/\\r\\n\\\\]*)*\\/(i|g)*))/', '$1' ); |
| 85 | | - $parser->add( '/([^\\w\\$\\/\'"*)\\?:](\\/[^\\r\\n\\*][^\\/\\r\\n\\\\]*(\\\\.[^\\/\\r\\n\\\\]*)*\\/(i|g)*))/', '$1' ); |
| 86 | | - // Remove comments |
| 87 | | - $parser->add( '/\\/\\*(.|[\\r\\n])*?\\*\\//' ); |
| 88 | | - // Preserve the newline after a C++-style comment -- bug 27046 |
| 89 | | - $parser->add( '/\\/\\/[^\\r\\n]*([\\r\\n])/', '$2' ); |
| | 112 | + // Regular expression with whitespace before it |
| | 113 | + $parser->add( |
| | 114 | + '(?<= [ \t] | [^\w\$\/\'"*)\?:] )' . // assert that whitespace or punctuation precedes |
| | 115 | + '\/' . // start slash |
| | 116 | + '[^\r\n\*]' . // not a comment-start or line ending |
| | 117 | + '[^\/\r\n\\\\]*' . // a sequence of non-special characters |
| | 118 | + '(?:' . |
| | 119 | + '\\\\.' . // an escaped dot |
| | 120 | + '[^\/\r\n\\\\]*' . // a sequence of non-special characters |
| | 121 | + ')*' . // any number of the above |
| | 122 | + '\/[ig]*' , // pattern end, optional modifier |
| | 123 | + '$1' ); |
| 90 | 124 | return $parser; |
| 91 | 125 | } |
| 92 | 126 | } |
| — | — | @@ -112,13 +146,12 @@ |
| 113 | 147 | const LENGTH = 2; |
| 114 | 148 | |
| 115 | 149 | // used to determine nesting levels |
| 116 | | - private $GROUPS = '/\\(/';//g |
| 117 | | - private $SUB_REPLACE = '/\\$\\d/'; |
| 118 | | - private $INDEXED = '/^\\$\\d+$/'; |
| 119 | | - private $TRIM = '/([\'"])\\1\\.(.*)\\.\\1\\1$/'; |
| | 150 | + private $GROUPS = '/\( (?! \? ) /x';//g |
| | 151 | + private $SUB_REPLACE = '/\$\d/'; |
| | 152 | + private $INDEXED = '/^\$\d+$/'; |
| 120 | 153 | private $ESCAPE = '/\\\./';//g |
| 121 | 154 | private $QUOTE = '/\'/'; |
| 122 | | - private $DELETED = '/\\x01[^\\x01]*\\x01/';//g |
| | 155 | + private $DELETED = '/\x01[^\x01]*\x01/';//g |
| 123 | 156 | |
| 124 | 157 | public function add($expression, $replacement = '') { |
| 125 | 158 | // count the number of sub-expressions |
| — | — | @@ -160,9 +193,9 @@ |
| 161 | 194 | // simulate the _patterns.toSTring of Dean |
| 162 | 195 | $regexp = '/'; |
| 163 | 196 | foreach ($this->_patterns as $reg) { |
| 164 | | - $regexp .= '(' . substr($reg[self::EXPRESSION], 1, -1) . ')|'; |
| | 197 | + $regexp .= '(' . $reg[self::EXPRESSION] . ")|\n"; |
| 165 | 198 | } |
| 166 | | - $regexp = substr($regexp, 0, -1) . '/S'; |
| | 199 | + $regexp = substr($regexp, 0, -2) . '/Sxs'; |
| 167 | 200 | $regexp .= ($this->ignoreCase) ? 'i' : ''; |
| 168 | 201 | |
| 169 | 202 | $string = $this->_escape($string, $this->escapeChar); |
| Property changes on: branches/wmf/1.17wmf1/includes/libs/JavaScriptDistiller.php |
| ___________________________________________________________________ |
| Modified: svn:mergeinfo |
| 170 | 203 | Merged /trunk/phase3/includes/libs/JavaScriptDistiller.php:r82380,82384,82399 |