includes/js-compressor.php
changeset 420 301f546688d1
parent 411 d1a95497b68f
child 507 586fd7d3202d
equal deleted inserted replaced
419:b8b4e38825db 420:301f546688d1
   105 
   105 
   106 	/**
   106 	/**
   107 	 * public constructor
   107 	 * public constructor
   108          * 	creates a new BaseConvert class variable (base 36)
   108          * 	creates a new BaseConvert class variable (base 36)
   109 	 */
   109 	 */
   110 	function JavaScriptCompressor() {
   110 	function __construct() {
   111 		$this->__SourceMap = new SourceMap();
   111 		$this->__SourceMap = new SourceMap();
   112 		$this->__BC = new BaseConvert('0123456789abcdefghijklmnopqrstuvwxyz');
   112 		$this->__BC = new BaseConvert('0123456789abcdefghijklmnopqrstuvwxyz');
   113 		$this->__delimeter = array(
   113 		$this->__delimeter = array(
   114 			array('name'=>'doublequote', 'start'=>'"', 'end'=>'"', 'noslash'=>true),
   114 			array('name'=>'doublequote', 'start'=>'"', 'end'=>'"', 'noslash'=>true),
   115 			array('name'=>'singlequote', 'start'=>"'", 'end'=>"'", 'noslash'=>true),
   115 			array('name'=>'singlequote', 'start'=>"'", 'end'=>"'", 'noslash'=>true),
   491         $next = 1; $len = $position - $next;
   491         $next = 1; $len = $position - $next;
   492         while($len > 0 && $source{$len} === '\\') $len = $position - (++$next);
   492         while($len > 0 && $source{$len} === '\\') $len = $position - (++$next);
   493         return (($next - 1) % 2 === 0);
   493         return (($next - 1) % 2 === 0);
   494     }
   494     }
   495 }
   495 }
       
   496 
       
   497 /**
       
   498  * Wrapper for the JavaScriptCompressor class.
       
   499  * @param string Javascript code to compact. If this doesn't contain any newline characters, will be treated as a filename.
       
   500  * @param bool If true, aggressively compresses the code. Otherwise, just strips comments and some whitespace.
       
   501  * @return string Compressed JS
       
   502  */
       
   503 
       
   504 function perform_js_compress($text_or_file, $aggressive = false)
       
   505 {
       
   506   static $compressor = false;
       
   507   
       
   508   if ( !is_object($compressor) )
       
   509     $compressor = new JavaScriptCompressor();
       
   510   
       
   511   if ( strpos($text_or_file, "\n") )
       
   512   {
       
   513     $text =& $text_or_file;
       
   514   }
       
   515   else if ( file_exists($text_or_file) )
       
   516   {
       
   517     $text = file_get_contents($text_or_file);
       
   518   }
       
   519   else
       
   520   {
       
   521     $text =& $text_or_file;
       
   522   }
       
   523   
       
   524   return ( $aggressive ) ? $compressor->getPacked($text) : $compressor->getClean($text);
       
   525 }
       
   526 
   496 ?>
   527 ?>