[ Index ]

PHP Cross Reference of JPSpan 0.4 (beta)

title

Body

[close]

/JPSpan/ -> Script.php (source)

   1  <?php
   2  /**
   3  * @package JPSpan
   4  * @subpackage Script
   5  * @version $Id: Script.php,v 1.1 2004/11/10 16:04:46 harryf Exp $
   6  */
   7  
   8  /**
   9  * Utilities for managing Javascript
  10  * @package JPSpan
  11  * @subpackage Script
  12  * @access public
  13  */
  14  class JPSpan_Script {
  15  
  16      /**
  17      * "Compress" Javascript
  18      * Scheduled to be replaced eventually with something using the Lexer
  19      * @see http://pupius.co.uk/download/php_scripts/preview/script-compress.phps/
  20      * @param string Javascript
  21      * @return string Javascript (compressed)
  22      * @access public
  23      * @static
  24      */
  25      function compress($script) {
  26          //remove windows cariage returns
  27          $script = str_replace("\r",'',$script);
  28          
  29          //array to store replaced literal strings
  30          $literal_strings = array();
  31          
  32          //explode the string into lines
  33          $lines = explode("\n",$script);
  34          
  35          //loop through all the lines, building a new string at the same time as removing literal strings
  36          $clean = '';
  37          $inComment = false;
  38          $literal = '';
  39          $inQuote = false;
  40          $escaped = false;
  41          $quoteChar = '';
  42          
  43          for($i=0;$i<count($lines);$i++) {
  44              $line = $lines[$i];
  45          
  46              $inNormalComment = false;
  47          
  48              //loop through line's characters and take out any literal strings, replace them with ___i___ where i is the index of this string
  49              for($j=0;$j<strlen($line);$j++) {
  50                  $c = substr($line,$j,1);
  51                  $d = substr($line,$j,2);
  52          
  53                  //look for start of quote
  54                  if(!$inQuote && !$inComment) {
  55                      //is this character a quote or a comment
  56                      if(($c=='"' || $c=="'") && !$inComment && !$inNormalComment) {
  57                          $inQuote = true;
  58                          $inComment = false;
  59                          $escaped = false;
  60                          $quoteChar = $c;
  61                          $literal = $c;
  62          
  63                      } else if($d=='/*' && !$inNormalComment) {
  64                          $inQuote = false;
  65                          $inComment = true;
  66                          $escaped = false;
  67                          $quoteChar = $d;
  68                          $literal = $d;
  69          
  70                          $j++;
  71          
  72                      //ignore string markers that are found inside comments
  73                      } else if($d=='//') {
  74                          $inNormalComment = true;
  75                          $clean .= $c;
  76                      } else {
  77                          $clean .= $c;
  78                      }
  79          
  80          
  81                  //already in a string so find end quote
  82                  } else {
  83                      if($c == $quoteChar && !$escaped && !$inComment) {
  84                          $inQuote = false;
  85                          $literal .= $c;
  86          
  87                          //subsitute in a marker for the string
  88                          $clean .= '___' . count($literal_strings) . '___';
  89          
  90                          //push the string onto our array
  91                          array_push($literal_strings,$literal);
  92          
  93                      } else if($inComment && $d=="*/") {
  94                          $inComment = false;
  95                          $literal .= $d;
  96          
  97                          //subsitute in a marker for the string
  98                          $clean .= '___' . count($literal_strings) . '___';
  99          
 100                          //push the string onto our array
 101                          array_push($literal_strings,$literal);
 102          
 103                          $j++;
 104          
 105                      } else if($c == "\\" && !$escaped) $escaped = true;
 106                      else $escaped = false;
 107          
 108                      $literal .= $c;
 109                  }
 110              }
 111          
 112              if($inComment) $literal .= "\n";
 113              $clean .= "\n";
 114          }
 115          
 116          //explode the clean string into lines again
 117          $lines = explode("\n",$clean);
 118          
 119          //now process each line at a time
 120          for($i=0;$i<count($lines);$i++) {
 121              $line = $lines[$i];
 122          
 123              //remove comments
 124              $line = preg_replace("/\/\/(.*)/",'',$line);
 125          
 126              //strip leading and trailing whitespace
 127              $line = trim($line);
 128          
 129              //remove all whitespace with a single space
 130              $line = preg_replace("/\s+/",' ',$line);
 131          
 132              //remove any whitespace that occurs after/before an operator
 133              $line = preg_replace("/\s*([!\}\{;,&=\|\-\+\*\/\)\(:])\s*/","\\1",$line);
 134          
 135              $lines[$i] = $line;
 136          }
 137          
 138          //implode the lines
 139          $script = implode("\n",$lines);
 140          
 141          //make sure there is a max of 1 \n after each line
 142          $script = preg_replace("/[\n]+/","\n",$script);
 143          
 144          //strip out line breaks that immediately follow a semi-colon
 145          $script = preg_replace("/;\n/",';',$script);
 146          
 147          //curly brackets aren't on their own
 148          $script = preg_replace("/[\n]*\{[\n]*/","{",$script);
 149          
 150          //finally loop through and replace all the literal strings:
 151          for($i=0;$i<count($literal_strings);$i++) {
 152              $script = str_replace('___'.$i.'___',$literal_strings[$i],$script);
 153          }
 154          
 155          return $script;
 156      }
 157  }


Generated: Fri Nov 26 11:42:46 2004 Cross-referenced by PHPXref 0.6