function Compress($script, $constantsProcessor) { // Concatenates all string with escaping new lines strings (ending with \). $script = preg_replace('/\\\\[\\n\\r]+/s', '\\n', $script); $stringsProc = new FCKStringsProcessor(); // Protect the script strings. $script = $stringsProc->ProtectStrings($script); // Remove "/* */" comments $script = preg_replace('/(?<!\\/)\\/\\*.*?\\*\\//s', '', $script); // Remove "//" comments $script = preg_replace('/\\/\\/.*$/m', '', $script); // Remove spaces before the ";" at the end of the lines $script = preg_replace('/\\s*(?=;\\s*$)/m', '', $script); // Remove spaces next to "=" $script = preg_replace('/^([^"\'\\r\\n]*?)\\s*=\\s*/m', '$1=', $script); // Remove spaces on "()": "( content )" = "(content)" $script = preg_replace('/^([^\\r\\n""\']*?\\()\\s+(.*?)\\s+(?=\\)[^\\)]*$)/m', '$1$2', $script); // Concatenate lines that doesn't end with [;{}] using a space $script = preg_replace('/(?<![;{}\\n\\r\\s])\\s*[\\n\\r]+\\s*(?![\\s\\n\\r{}])/s', ' ', $script); // Concatenate lines that end with "}" using a ";", except for "else", // "while", "catch" and "finally" cases, or when followed by, "'", ";", // "}" or ")". $script = preg_replace('/\\s*}\\s*[\\n\\r]+\\s*(?!\\s*(else|catch|finally|while|[}\\),;]))/s', '};', $script); // Remove blank lines, spaces at the begining or the at the end and \n\r $script = preg_replace('/(^\\s*$)|(^\\s+)|(\\s+$\\n)/m', '', $script); // Remove the spaces between statements. $script = FCKJavaScriptCompressor::_RemoveInnerSpaces($script); // Process constants. // CHECK if ($constantsProcessor->HasConstants) { $script = $constantsProcessor->Process($script); } // Replace "new Object()". $script = preg_replace('/new Object\\(\\)/', '{}', $script); // Replace "new Array()". $script = preg_replace('/new Array\\(\\)/', '[]', $script); // Process function contents, renaming parameters and variables. $script = FCKJavaScriptCompressor::_ProcessFunctions($script); // Join consecutive string concatened with a "+". $script = $stringsProc->ConcatProtectedStrings($script); // Restore the protected script strings. $script = $stringsProc->RestoreStrings($script); return $script; }
function Compress($script, $constantsProcessor = null, $params = array('variable' => 1, 'space' => 1)) { // detect cr and replace it with "\n" preg_match('/(\\r\\n|\\r|\\n)/s', $script, $cr); if (!empty($cr[0]) and $cr[0] != "\n") { $script = strtr($script, $cr[0], "\n"); //$script = str_replace($cr[0],"\n",$script); } // Concatenates all string with escaping new lines strings (ending with \). $script = preg_replace('/\\\\\\n+/s', '\\n', $script); $stringsProc = new FCKStringsProcessor(); // Protect the script strings. // buggy XXX $script = $stringsProc->ProtectStrings($script); // Remove "/* */" "//" comments $script = preg_replace(array('/(?<!\\/)\\/\\*.*?\\*\\//s', '/\\/\\/.*$/m'), array('', ''), $script); // Remove spaces before the ";" at the end of the lines //$script = preg_replace( // '/\s*(?=;\s*$)/m', // '', $script ) ; // Remove spaces next to "=" //$script = preg_replace( // '/^([^"\'\r\n]*?)\s*=\s*/m', // '$1=', $script ) ; // Remove spaces on "()": "( content )" = "(content)" //$script = preg_replace( // '/^([^\r\n""\']*?\()\s+(.*?)\s+(?=\)[^\)]*$)/m', // '$1$2', $script ) ; // Concatenate lines that doesn't end with [;{}] using a space if (!empty($params['space'])) { $script = preg_replace('/(?<![;{}\\n\\s])\\s*\\n+\\s*(?![\\s\\n{}])/s', ' ', $script); } // Concatenate lines that end with "}" using a "\n", except for "else", // "while", "catch" and "finally" cases, or when followed by, "'", ";", // "}", ")" or "]". ///$script = preg_replace( /// '/\s*}\s*\n+\s*(?!\s*(else|catch|finally|while|[\]}\),;]))/s', /// "}\n", $script ) ; ## not needed ## XXX // Concat lines that end with ";" or "{" // $script = preg_replace('/\s*([;{])\s*[\n\r]+\s*/s','\\1', $script ) ; $script = preg_replace('/\\s*([;{])\\s*/s', '\\1', $script); ## $script = preg_replace('/\s*([;{}])\s*/s','\\1', $script ) ; // shorter // Remove blank lines, spaces at the begining or the at the end and \n\r $script = preg_replace('/(^\\s*$)|(^\\s+)/m', '', $script); // Remove the spaces between statements. //$script = FCKJavaScriptCompressor::_RemoveInnerSpaces( $script ) ; # error with mootools // Process constants. // CHECK if (is_object($constantsProcessor) and $constantsProcessor->HasConstants) { $script = $constantsProcessor->Process($script); } // Replace "new Object()" "new Array()". $script = preg_replace(array('/new\\s+Object\\(\\)/', '/new\\s+Array\\(\\)/'), array('{}', '[]'), $script); if (!empty($params['space'])) { $delim = '([+\\-])|([:=?*\\/&,;><|!{}\\[\\]\\(\\)])'; // space + delim + space => delim, except a++ +b $script = preg_replace('/[ ]*(' . $delim . ')[ ]*(?(2)(?![+\\-]))/', '\\1', $script); } else { // only some spaces are removed: ' : '=> ':', ' = '=> '=' $script = preg_replace('/[ ]*([:=])[ ]*/', '\\1', $script); } // workaround for a = (b) ? c:d; case. c is not a object menber. $script = preg_replace('/(?<=\\?)([^:]+):/', '\\1 :', $script); // Process function contents, renaming parameters and variables. if (!empty($params['variable'])) { $script = FCKJavaScriptCompressor::_ProcessFunctions($script); } // Join consecutive string concatened with a "+". $script = $stringsProc->ConcatProtectedStrings($script); // Restore the protected script strings. $script = $stringsProc->RestoreStrings($script); return $script; }