2 /** Remove spaces and comments from JavaScript code
3 * @param string code with commands terminated by semicolon
4 * @return string shrinked code
5 * @link http://vrana.github.com/JsShrink/
6 * @author Jakub Vrana, http://www.vrana.cz/
7 * @copyright 2007 Jakub Vrana
8 * @license http://www.apache.org/licenses/LICENSE-2.0 Apache License, Version 2.0
9 * @license http://www.gnu.org/licenses/gpl-2.0.html GNU General Public License, version 2 (one or other)
11 function jsShrink($input) {
12 return preg_replace_callback('(
14 (^|[-+\([{}=,:;!%^&*|?~]|/(?![/*])|return|throw) # context before regexp
15 (?:\s|//[^\n]*+\n|/\*(?:[^*]|\*(?!/))*+\*/)* # optional space
19 |\[(?:\\\\[^\n]|[^]])++
22 |\'(?:\\\\.|[^\n\'\\\\])*\'
23 |"(?:\\\\.|[^\n"\\\\])*"
28 )(?:\s|//[^\n]*+\n|/\*(?:[^*]|\*(?!/))*+\*/)* # optional space
29 )sx', 'jsShrinkCallback', "$input\n");
32 function jsShrinkCallback($match) {
34 $match +
= array_fill(1, 5, null); // avoid E_NOTICE
35 list(, $context, $regexp, $result, $word, $operator) = $match;
37 $result = ($last == 'word' ?
"\n" : ($last == 'return' ?
" " : "")) . $result;
38 $last = ($word == 'return' ||
$word == 'throw' ||
$word == 'break' ?
'return' : 'word');
39 } elseif ($operator) {
40 $result = ($last == $operator[0] ?
"\n" : "") . $result;
44 $result = $context . ($context == '/' ?
"\n" : "") . $regexp;