3 * xajaxCompress.php :: function to compress Javascript
6 * copyright (c) 2005 by Jared White & J. Max Wilson
7 * http://www.xajaxproject.org
9 * xajax is an open source PHP class library for easily creating powerful
10 * PHP-driven, web-based Ajax Applications. Using xajax, you can asynchronously
11 * call PHP functions and update the content of your your webpage without
14 * xajax is released under the terms of the LGPL license
15 * http://www.gnu.org/copyleft/lesser.html#SEC3
17 * This library is free software; you can redistribute it and/or
18 * modify it under the terms of the GNU Lesser General Public
19 * License as published by the Free Software Foundation; either
20 * version 2.1 of the License, or (at your option) any later version.
22 * This library is distributed in the hope that it will be useful,
23 * but WITHOUT ANY WARRANTY; without even the implied warranty of
24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
25 * Lesser General Public License for more details.
27 * You should have received a copy of the GNU Lesser General Public
28 * License along with this library; if not, write to the Free Software
29 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
33 * @copyright Copyright (c) 2005-2006 by Jared White & J. Max Wilson
34 * @license http://www.gnu.org/copyleft/lesser.html#SEC3 LGPL License
38 * Compresses the Javascript code for more efficient delivery.
41 * @param string contains the Javascript code to compress
43 function xajaxCompressJavascript($sJS)
45 //remove windows cariage returns
46 $sJS = str_replace("\r","",$sJS);
48 //array to store replaced literal strings
49 $literal_strings = array();
51 //explode the string into lines
52 $lines = explode("\n",$sJS);
53 //loop through all the lines, building a new string at the same time as removing literal strings
61 for($i=0;$i<count($lines);$i++
)
64 $inNormalComment = false
;
66 //loop through line's characters and take out any literal strings, replace them with ___i___ where i is the index of this string
67 for($j=0;$j<strlen($line);$j++
)
69 $c = substr($line,$j,1);
70 $d = substr($line,$j,2);
72 //look for start of quote
73 if(!$inQuote && !$inComment)
75 //is this character a quote or a comment
76 if(($c=="\"" ||
$c=="'") && !$inComment && !$inNormalComment)
84 else if($d=="/*" && !$inNormalComment)
93 else if($d=="//") //ignore string markers that are found inside comments
95 $inNormalComment = true
;
103 else //allready in a string so find end quote
105 if($c == $quoteChar && !$escaped && !$inComment)
110 //subsitute in a marker for the string
111 $clean .= "___" . count($literal_strings) . "___";
113 //push the string onto our array
114 array_push($literal_strings,$literal);
117 else if($inComment && $d=="*/")
122 //subsitute in a marker for the string
123 $clean .= "___" . count($literal_strings) . "___";
125 //push the string onto our array
126 array_push($literal_strings,$literal);
130 else if($c == "\\" && !$escaped)
138 if($inComment) $literal .= "\n";
141 //explode the clean string into lines again
142 $lines = explode("\n",$clean);
144 //now process each line at a time
145 for($i=0;$i<count($lines);$i++
)
150 $line = preg_replace("/\/\/(.*)/","",$line);
152 //strip leading and trailing whitespace
155 //remove all whitespace with a single space
156 $line = preg_replace("/\s+/"," ",$line);
158 //remove any whitespace that occurs after/before an operator
159 $line = preg_replace("/\s*([!\}\{;,&=\|\-\+\*\/\)\(:])\s*/","\\1",$line);
165 $sJS = implode("\n",$lines);
167 //make sure there is a max of 1 \n after each line
168 $sJS = preg_replace("/[\n]+/","\n",$sJS);
170 //strip out line breaks that immediately follow a semi-colon
171 $sJS = preg_replace("/;\n/",";",$sJS);
173 //curly brackets aren't on their own
174 $sJS = preg_replace("/[\n]*\{[\n]*/","{",$sJS);
176 //finally loop through and replace all the literal strings:
177 for($i=0;$i<count($literal_strings);$i++
)
178 $sJS = str_replace("___".$i."___",$literal_strings[$i],$sJS);