xajaxCompress.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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 chamilo.include.xajax
  32. * @version $Id: xajaxCompress.php,v 1.1 2006/07/21 15:29:46 elixir_inter Exp $
  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. * Compresses the Javascript code for more efficient delivery.
  38. * (used internally)
  39. *
  40. * @param string contains the Javascript code to compress
  41. */
  42. function xajaxCompressJavascript($sJS)
  43. {
  44. //remove windows cariage returns
  45. $sJS = str_replace("\r","",$sJS);
  46. //array to store replaced literal strings
  47. $literal_strings = array();
  48. //explode the string into lines
  49. $lines = explode("\n",$sJS);
  50. //loop through all the lines, building a new string at the same time as removing literal strings
  51. $clean = "";
  52. $inComment = false;
  53. $literal = "";
  54. $inQuote = false;
  55. $escaped = false;
  56. $quoteChar = "";
  57. for($i=0;$i<count($lines);$i++)
  58. {
  59. $line = $lines[$i];
  60. $inNormalComment = false;
  61. //loop through line's characters and take out any literal strings, replace them with ___i___ where i is the index of this string
  62. for($j=0;$j<strlen($line);$j++)
  63. {
  64. $c = substr($line,$j,1);
  65. $d = substr($line,$j,2);
  66. //look for start of quote
  67. if(!$inQuote && !$inComment)
  68. {
  69. //is this character a quote or a comment
  70. if(($c=="\"" || $c=="'") && !$inComment && !$inNormalComment)
  71. {
  72. $inQuote = true;
  73. $inComment = false;
  74. $escaped = false;
  75. $quoteChar = $c;
  76. $literal = $c;
  77. }
  78. else if($d=="/*" && !$inNormalComment)
  79. {
  80. $inQuote = false;
  81. $inComment = true;
  82. $escaped = false;
  83. $quoteChar = $d;
  84. $literal = $d;
  85. $j++;
  86. }
  87. else if($d=="//") //ignore string markers that are found inside comments
  88. {
  89. $inNormalComment = true;
  90. $clean .= $c;
  91. }
  92. else
  93. {
  94. $clean .= $c;
  95. }
  96. }
  97. else //allready in a string so find end quote
  98. {
  99. if($c == $quoteChar && !$escaped && !$inComment)
  100. {
  101. $inQuote = false;
  102. $literal .= $c;
  103. //subsitute in a marker for the string
  104. $clean .= "___" . count($literal_strings) . "___";
  105. //push the string onto our array
  106. array_push($literal_strings,$literal);
  107. }
  108. else if($inComment && $d=="*/")
  109. {
  110. $inComment = false;
  111. $literal .= $d;
  112. //subsitute in a marker for the string
  113. $clean .= "___" . count($literal_strings) . "___";
  114. //push the string onto our array
  115. array_push($literal_strings,$literal);
  116. $j++;
  117. }
  118. else if($c == "\\" && !$escaped)
  119. $escaped = true;
  120. else
  121. $escaped = false;
  122. $literal .= $c;
  123. }
  124. }
  125. if($inComment) $literal .= "\n";
  126. $clean .= "\n";
  127. }
  128. //explode the clean string into lines again
  129. $lines = explode("\n",$clean);
  130. //now process each line at a time
  131. for($i=0;$i<count($lines);$i++)
  132. {
  133. $line = $lines[$i];
  134. //remove comments
  135. $line = preg_replace("/\/\/(.*)/","",$line);
  136. //strip leading and trailing whitespace
  137. $line = trim($line);
  138. //remove all whitespace with a single space
  139. $line = preg_replace("/\s+/"," ",$line);
  140. //remove any whitespace that occurs after/before an operator
  141. $line = preg_replace("/\s*([!\}\{;,&=\|\-\+\*\/\)\(:])\s*/","\\1",$line);
  142. $lines[$i] = $line;
  143. }
  144. //implode the lines
  145. $sJS = implode("\n",$lines);
  146. //make sure there is a max of 1 \n after each line
  147. $sJS = preg_replace("/[\n]+/","\n",$sJS);
  148. //strip out line breaks that immediately follow a semi-colon
  149. $sJS = preg_replace("/;\n/",";",$sJS);
  150. //curly brackets aren't on their own
  151. $sJS = preg_replace("/[\n]*\{[\n]*/","{",$sJS);
  152. //finally loop through and replace all the literal strings:
  153. for($i=0;$i<count($literal_strings);$i++)
  154. $sJS = str_replace("___".$i."___",$literal_strings[$i],$sJS);
  155. return $sJS;
  156. }
  157. ?>