Index: trunk/phase3/includes/libs/JavaScriptDistiller.php |
— | — | @@ -31,13 +31,13 @@ |
32 | 32 | public static function stripHorizontalSpace( $script ) { |
33 | 33 | $parser = self::createParser(); |
34 | 34 | // Collapse horizontal whitespaces between variable names into a single space |
35 | | - $parser->add( '/(\b|\$)[ \t]+(\b|\$)/', '$2 $3' ); |
| 35 | + $parser->add( '(\b|\$) [ \t]+ (\b|\$)', '$2 $3' ); |
36 | 36 | // Collapse horizontal whitespaces between unary operators into a single space |
37 | | - $parser->add( '/([+\-])[ \t]+([+\-])/', '$2 $3' ); |
| 37 | + $parser->add( '([+\-]) [ \t]+ ([+\-])', '$2 $3' ); |
38 | 38 | // Remove all remaining un-protected horizontal whitespace |
39 | | - $parser->add( '/[ \t]+/'); |
| 39 | + $parser->add( '[ \t]+'); |
40 | 40 | // Collapse multiple vertical whitespaces with some horizontal spaces between them |
41 | | - $parser->add( '/[\r\n]+[ \t]*[\r\n]+/', "\n" ); |
| 41 | + $parser->add( '[\r\n]+ [ \t]* [\r\n]+', "\n" ); |
42 | 42 | // Execute and return |
43 | 43 | return $parser->exec($script); |
44 | 44 | } |
— | — | @@ -45,16 +45,16 @@ |
46 | 46 | public static function stripVerticalSpace( $script ) { |
47 | 47 | $parser = self::createParser(); |
48 | 48 | // Collapse whitespaces between and after a ){ pair (function definitions) |
49 | | - $parser->add( '/\)\s+\{\s+/', '){' ); |
| 49 | + $parser->add( '\) \s+ \{ \s+', '){' ); |
50 | 50 | // Collapse whitespaces between and after a ({ pair (JSON argument) |
51 | | - $parser->add( '/\(\s+\{\s+/', '({' ); |
| 51 | + $parser->add( '\( \s+ \{ \s+', '({' ); |
52 | 52 | // Collapse whitespaces between a parenthesis and a period (call chaining) |
53 | | - $parser->add( '/\)\s+\./', ').'); |
| 53 | + $parser->add( '\) \s+ \.', ').'); |
54 | 54 | // Collapse vertical whitespaces which come directly after a semicolon or a comma |
55 | | - $parser->add( '/([;,])\s+/', '$2' ); |
| 55 | + $parser->add( '( [;,] ) \s+', '$2' ); |
56 | 56 | // Collapse whitespaces between multiple parenthesis/brackets of similar direction |
57 | | - $parser->add( '/([\)\}])\s+([\)\}])/', '$2$3' ); |
58 | | - $parser->add( '/([\(\{])\\s+([\(\{])/', '$2$3' ); |
| 57 | + $parser->add( '( [\)\}] ) \s+ ( [\)\}] )', '$2$3' ); |
| 58 | + $parser->add( '( [\(\{] ) \s+ ( [\(\{] )', '$2$3' ); |
59 | 59 | return $parser->exec( $script ); |
60 | 60 | } |
61 | 61 | |
— | — | @@ -77,15 +77,52 @@ |
78 | 78 | // Protect strings. The original code had [^\'\\v] here, but that didn't armor multiline |
79 | 79 | // strings correctly. This also armors multiline strings that don't have backslashes at the |
80 | 80 | // 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' ); |
| 81 | + |
| 82 | + // Single quotes |
| 83 | + $parser->add( |
| 84 | + '\' (' . // start quote |
| 85 | + '[^\'\\\\]*' . // a run of non-special characters |
| 86 | + '(' . |
| 87 | + '\\\\ ( . | [\r\n] )' . // a backslash followed by a character or line ending |
| 88 | + '[^\'\\\\]*' . // a run of non-special characters |
| 89 | + ')*' . // any number of the above |
| 90 | + ') \'', // end quote |
| 91 | + '$1' ); |
| 92 | + |
| 93 | + // Double quotes: same as above |
| 94 | + $parser->add( '" ( [^"\\\\]* ( \\\\ ( . | [\r\n] ) [^"\\\\]* )* ) "', '$1' ); |
| 95 | + |
83 | 96 | // 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])*?\*\//' ); |
| 97 | + // Regular expression with whitespace before it |
| 98 | + $parser->add( |
| 99 | + '[ \t]+ ( ( \/' . // whitespace then start slash |
| 100 | + '[^\r\n\*]' . // not a comment-start or line ending |
| 101 | + '[^\/\r\n\\\\]*' . // a sequence of non-special characters |
| 102 | + '(' . |
| 103 | + '\\\\.' . // an escaped dot |
| 104 | + '[^\/\r\n\\\\]*' . // a sequence of non-special characters |
| 105 | + ')*' . // any number of the above |
| 106 | + '\/(i|g)*' . // pattern end, optional modifier |
| 107 | + ') )', |
| 108 | + '$1' ); |
| 109 | + // Regular expression with an operator before it |
| 110 | + $parser->add( |
| 111 | + '( [^\w\$\/\'"*)\?:] (\/' . // certain kinds of punctuation and then start slash |
| 112 | + '[^\r\n\*]' . // not a comment-start or line ending |
| 113 | + '[^\/\r\n\\\\]*' . // a sequence of non-special characters |
| 114 | + '(' . |
| 115 | + '\\\\.' . // an escaped dot |
| 116 | + '[^\/\r\n\\\\]*' . // a sequence of non-special characters |
| 117 | + ')*' . // any number of the above |
| 118 | + '\/(i|g)*)' . // pattern end, optional modifier |
| 119 | + ')', |
| 120 | + '$1' ); |
| 121 | + |
| 122 | + // C-style comment: use non-greedy repetition to find the end |
| 123 | + $parser->add( '\/ \* ( . | [\r\n] )*? \* \/' ); |
| 124 | + |
88 | 125 | // Preserve the newline after a C++-style comment -- bug 27046 |
89 | | - $parser->add( '/\/\/[^\r\n]*([\r\n])/', '$2' ); |
| 126 | + $parser->add( '\/ \/ [^\r\n]* ( [\r\n] )', '$2' ); |
90 | 127 | return $parser; |
91 | 128 | } |
92 | 129 | } |
— | — | @@ -160,9 +197,9 @@ |
161 | 198 | // simulate the _patterns.toSTring of Dean |
162 | 199 | $regexp = '/'; |
163 | 200 | foreach ($this->_patterns as $reg) { |
164 | | - $regexp .= '(' . substr($reg[self::EXPRESSION], 1, -1) . ')|'; |
| 201 | + $regexp .= '(' . $reg[self::EXPRESSION] . ')|'; |
165 | 202 | } |
166 | | - $regexp = substr($regexp, 0, -1) . '/S'; |
| 203 | + $regexp = substr($regexp, 0, -1) . '/Sx'; |
167 | 204 | $regexp .= ($this->ignoreCase) ? 'i' : ''; |
168 | 205 | |
169 | 206 | $string = $this->_escape($string, $this->escapeChar); |