[ Index ] |
|
Code source de CMS made simple 1.0.5 |
1 <?php 2 /** 3 * xajaxCompress.php :: function to compress Javascript 4 * 5 * xajax version 0.2.4 6 * copyright (c) 2005 by Jared White & J. Max Wilson 7 * http://www.xajaxproject.org 8 * 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 12 * reloading the page. 13 * 14 * xajax is released under the terms of the LGPL license 15 * http://www.gnu.org/copyleft/lesser.html#SEC3 16 * 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. 21 * 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. 26 * 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 30 * 31 * @package xajax 32 * @version $Id$ 33 * @copyright Copyright (c) 2005-2006 by Jared White & J. Max Wilson 34 * @license http://www.gnu.org/copyleft/lesser.html#SEC3 LGPL License 35 */ 36 37 /** 38 * Compresses the Javascript code for more efficient delivery. 39 * (used internally) 40 * 41 * @param string contains the Javascript code to compress 42 */ 43 function xajaxCompressJavascript($sJS) 44 { 45 //remove windows cariage returns 46 $sJS = str_replace("\r","",$sJS); 47 48 //array to store replaced literal strings 49 $literal_strings = array(); 50 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 54 $clean = ""; 55 $inComment = false; 56 $literal = ""; 57 $inQuote = false; 58 $escaped = false; 59 $quoteChar = ""; 60 61 for($i=0;$i<count($lines);$i++) 62 { 63 $line = $lines[$i]; 64 $inNormalComment = false; 65 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++) 68 { 69 $c = substr($line,$j,1); 70 $d = substr($line,$j,2); 71 72 //look for start of quote 73 if(!$inQuote && !$inComment) 74 { 75 //is this character a quote or a comment 76 if(($c=="\"" || $c=="'") && !$inComment && !$inNormalComment) 77 { 78 $inQuote = true; 79 $inComment = false; 80 $escaped = false; 81 $quoteChar = $c; 82 $literal = $c; 83 } 84 else if($d=="/*" && !$inNormalComment) 85 { 86 $inQuote = false; 87 $inComment = true; 88 $escaped = false; 89 $quoteChar = $d; 90 $literal = $d; 91 $j++; 92 } 93 else if($d=="//") //ignore string markers that are found inside comments 94 { 95 $inNormalComment = true; 96 $clean .= $c; 97 } 98 else 99 { 100 $clean .= $c; 101 } 102 } 103 else //allready in a string so find end quote 104 { 105 if($c == $quoteChar && !$escaped && !$inComment) 106 { 107 $inQuote = false; 108 $literal .= $c; 109 110 //subsitute in a marker for the string 111 $clean .= "___" . count($literal_strings) . "___"; 112 113 //push the string onto our array 114 array_push($literal_strings,$literal); 115 116 } 117 else if($inComment && $d=="*/") 118 { 119 $inComment = false; 120 $literal .= $d; 121 122 //subsitute in a marker for the string 123 $clean .= "___" . count($literal_strings) . "___"; 124 125 //push the string onto our array 126 array_push($literal_strings,$literal); 127 128 $j++; 129 } 130 else if($c == "\\" && !$escaped) 131 $escaped = true; 132 else 133 $escaped = false; 134 135 $literal .= $c; 136 } 137 } 138 if($inComment) $literal .= "\n"; 139 $clean .= "\n"; 140 } 141 //explode the clean string into lines again 142 $lines = explode("\n",$clean); 143 144 //now process each line at a time 145 for($i=0;$i<count($lines);$i++) 146 { 147 $line = $lines[$i]; 148 149 //remove comments 150 $line = preg_replace("/\/\/(.*)/","",$line); 151 152 //strip leading and trailing whitespace 153 $line = trim($line); 154 155 //remove all whitespace with a single space 156 $line = preg_replace("/\s+/"," ",$line); 157 158 //remove any whitespace that occurs after/before an operator 159 $line = preg_replace("/\s*([!\}\{;,&=\|\-\+\*\/\)\(:])\s*/","\\1",$line); 160 161 $lines[$i] = $line; 162 } 163 164 //implode the lines 165 $sJS = implode("\n",$lines); 166 167 //make sure there is a max of 1 \n after each line 168 $sJS = preg_replace("/[\n]+/","\n",$sJS); 169 170 //strip out line breaks that immediately follow a semi-colon 171 $sJS = preg_replace("/;\n/",";",$sJS); 172 173 //curly brackets aren't on their own 174 $sJS = preg_replace("/[\n]*\{[\n]*/","{",$sJS); 175 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); 179 180 return $sJS; 181 } 182 ?>
titre
Description
Corps
titre
Description
Corps
titre
Description
Corps
titre
Corps
Généré le : Tue Apr 3 18:50:37 2007 | par Balluche grâce à PHPXref 0.7 |