BUG 6213 Improve bandwith usage (minimize js and css files) SOLVED

- The minimization wasn't applied to some js files because the library used gives some errors
- Upgrade the jsmin class and minify the remaining js files
This commit is contained in:
Julio Cesar Laura
2012-09-17 10:54:39 -04:00
parent 406d5b5be7
commit c6117149e8
2 changed files with 127 additions and 33 deletions

View File

@@ -1442,8 +1442,8 @@ $output = $outputHeader.$output;
case 'maborak.js' : case 'maborak.js' :
$oHeadPublisher =& headPublisher::getSingleton(); $oHeadPublisher =& headPublisher::getSingleton();
foreach ( $oHeadPublisher->maborakFiles as $fileJS ) { foreach ( $oHeadPublisher->maborakFiles as $fileJS ) {
//$output .= JSMin::minify ( file_get_contents ( $fileJS ) ); $output .= JSMin::minify ( file_get_contents ( $fileJS ) );
$output .= G::trimSourceCodeFile ($fileJS ); //$output .= G::trimSourceCodeFile ($fileJS );
} }
break; break;
case 'maborak.loader.js': case 'maborak.loader.js':
@@ -1454,8 +1454,8 @@ $output = $outputHeader.$output;
} }
break; break;
default : default :
$output = file_get_contents ( $filename ) ; //$output = file_get_contents ( $filename ) ;
//$output = JSMin::minify ( file_get_contents ( $filename ) ); $output = JSMin::minify ( file_get_contents ( $filename ) );
//$output = G::trimSourceCodeFile ($filename ); //$output = G::trimSourceCodeFile ($filename );
} }
print $output; print $output;

View File

@@ -1,6 +1,6 @@
<?php <?php
/** /**
* jsmin.php - PHP implementation of Douglas Crockford's JSMin. * jsmin.php - PHP implementation of Douglas Crockford's JSMin á.
* *
* This is pretty much a direct port of jsmin.c to PHP with just a few * This is pretty much a direct port of jsmin.c to PHP with just a few
* PHP-specific performance tweaks. Also, whereas jsmin.c reads from stdin and * PHP-specific performance tweaks. Also, whereas jsmin.c reads from stdin and
@@ -40,14 +40,18 @@
* @author Ryan Grove <ryan@wonko.com> * @author Ryan Grove <ryan@wonko.com>
* @copyright 2002 Douglas Crockford <douglas@crockford.com> (jsmin.c) * @copyright 2002 Douglas Crockford <douglas@crockford.com> (jsmin.c)
* @copyright 2008 Ryan Grove <ryan@wonko.com> (PHP port) * @copyright 2008 Ryan Grove <ryan@wonko.com> (PHP port)
* @copyright 2012 Adam Goforth <aag@adamgoforth.com> (Updates)
* @license http://opensource.org/licenses/mit-license.php MIT License * @license http://opensource.org/licenses/mit-license.php MIT License
* @version 1.1.1 (2008-03-02) * @version 1.1.2 (2012-05-01)
* @link http://code.google.com/p/jsmin-php/ * @link https://github.com/rgrove/jsmin-php
*/ */
class JSMin { class JSMin {
const ORD_LF = 10; const ORD_LF = 10;
const ORD_SPACE = 32; const ORD_SPACE = 32;
const ACTION_KEEP_A = 1;
const ACTION_DELETE_A = 2;
const ACTION_DELETE_A_B = 3;
protected $a = ''; protected $a = '';
protected $b = ''; protected $b = '';
@@ -59,6 +63,14 @@ class JSMin {
// -- Public Static Methods -------------------------------------------------- // -- Public Static Methods --------------------------------------------------
/**
* Minify Javascript
*
* @uses __construct()
* @uses min()
* @param string $js Javascript to be minified
* @return string
*/
public static function minify($js) { public static function minify($js) {
$jsmin = new JSMin($js); $jsmin = new JSMin($js);
return $jsmin->min(); return $jsmin->min();
@@ -66,20 +78,41 @@ class JSMin {
// -- Public Instance Methods ------------------------------------------------ // -- Public Instance Methods ------------------------------------------------
/**
* Constructor
*
* @param string $input Javascript to be minified
*/
public function __construct($input) { public function __construct($input) {
$this->input = str_replace("\r\n", "\n", $input); $this->input = str_replace("\r\n", "\n", $input);
// $this->input = str_replace("\n", "", $this->input);
$this->inputLength = strlen($this->input); $this->inputLength = strlen($this->input);
} }
// -- Protected Instance Methods --------------------------------------------- // -- Protected Instance Methods ---------------------------------------------
protected function action($d) { /**
switch($d) { * Action -- do something! What to do is determined by the $command argument.
case 1: *
* action treats a string as a single character. Wow!
* action recognizes a regular expression if it is preceded by ( or , or =.
*
* @uses next()
* @uses get()
* @throws JSMinException If parser errors are found:
* - Unterminated string literal
* - Unterminated regular expression set in regex literal
* - Unterminated regular expression literal
* @param int $command One of class constants:
* ACTION_KEEP_A Output A. Copy B to A. Get the next B.
* ACTION_DELETE_A Copy B to A. Get the next B. (Delete A).
* ACTION_DELETE_A_B Get the next B. (Delete B).
*/
protected function action($command) {
switch($command) {
case self::ACTION_KEEP_A:
$this->output .= $this->a; $this->output .= $this->a;
case 2: case self::ACTION_DELETE_A:
$this->a = $this->b; $this->a = $this->b;
if ($this->a === "'" || $this->a === '"') { if ($this->a === "'" || $this->a === '"') {
@@ -102,27 +135,46 @@ class JSMin {
} }
} }
case 3: case self::ACTION_DELETE_A_B:
$this->b = $this->next(); $this->b = $this->next();
if ($this->b === '/' && ( if ($this->b === '/' && (
$this->a === '(' || $this->a === ',' || $this->a === '=' || $this->a === '(' || $this->a === ',' || $this->a === '=' ||
$this->a === ':' || $this->a === '[' || $this->a === '!' || $this->a === ':' || $this->a === '[' || $this->a === '!' ||
$this->a === '&' || $this->a === '|' || $this->a === '?')) { $this->a === '&' || $this->a === '|' || $this->a === '?' ||
$this->a === '{' || $this->a === '}' || $this->a === ';' ||
$this->a === "\n" )) {
$this->output .= $this->a . $this->b; $this->output .= $this->a . $this->b;
for (;;) { for (;;) {
$this->a = $this->get(); $this->a = $this->get();
if ($this->a === '/') { if ($this->a === '[') {
/*
inside a regex [...] set, which MAY contain a '/' itself. Example: mootools Form.Validator near line 460:
return Form.Validator.getValidator('IsEmpty').test(element) || (/^(?:[a-z0-9!#$%&'*+/=?^_`{|}~-]\.?){0,63}[a-z0-9!#$%&'*+/=?^_`{|}~-]@(?:(?:[a-z0-9](?:[a-z0-9-]{0,61}[a-z0-9])?\.)*[a-z0-9](?:[a-z0-9-]{0,61}[a-z0-9])?|\[(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\])$/i).test(element.get('value'));
*/
for (;;) {
$this->output .= $this->a;
$this->a = $this->get();
if ($this->a === ']') {
break; break;
} elseif ($this->a === '\\') { } elseif ($this->a === '\\') {
$this->output .= $this->a; $this->output .= $this->a;
$this->a = $this->get(); $this->a = $this->get();
} elseif (ord($this->a) <= self::ORD_LF) { } elseif (ord($this->a) <= self::ORD_LF) {
throw new JSMinException('Unterminated regular expression '. throw new JSMinException('Unterminated regular expression set in regex literal.');
'literal.'); }
}
} elseif ($this->a === '/') {
break;
} elseif ($this->a === '\\') {
$this->output .= $this->a;
$this->a = $this->get();
} elseif (ord($this->a) <= self::ORD_LF) {
throw new JSMinException('Unterminated regular expression literal.');
} }
$this->output .= $this->a; $this->output .= $this->a;
@@ -133,13 +185,18 @@ class JSMin {
} }
} }
/**
* Get next char. Convert ctrl char to space.
*
* @return string|null
*/
protected function get() { protected function get() {
$c = $this->lookAhead; $c = $this->lookAhead;
$this->lookAhead = null; $this->lookAhead = null;
if ($c === null) { if ($c === null) {
if ($this->inputIndex < $this->inputLength) { if ($this->inputIndex < $this->inputLength) {
$c = $this->input[$this->inputIndex]; $c = substr($this->input, $this->inputIndex, 1);
$this->inputIndex += 1; $this->inputIndex += 1;
} else { } else {
$c = null; $c = null;
@@ -157,21 +214,41 @@ class JSMin {
return ' '; return ' ';
} }
/**
* Is $c a letter, digit, underscore, dollar sign, or non-ASCII character.
*
* @return bool
*/
protected function isAlphaNum($c) { protected function isAlphaNum($c) {
return ord($c) > 126 || $c === '\\' || preg_match('/^[\w\$]$/', $c) === 1; return ord($c) > 126 || $c === '\\' || preg_match('/^[\w\$]$/', $c) === 1;
} }
/**
* Perform minification, return result
*
* @uses action()
* @uses isAlphaNum()
* @uses get()
* @uses peek()
* @return string
*/
protected function min() { protected function min() {
if (0 == strncmp($this->peek(), "\xef", 1)) {
$this->get();
$this->get();
$this->get();
}
$this->a = "\n"; $this->a = "\n";
$this->action(3); $this->action(self::ACTION_DELETE_A_B);
while ($this->a !== null) { while ($this->a !== null) {
switch ($this->a) { switch ($this->a) {
case ' ': case ' ':
if ($this->isAlphaNum($this->b)) { if ($this->isAlphaNum($this->b)) {
$this->action(1); $this->action(self::ACTION_KEEP_A);
} else { } else {
$this->action(2); $this->action(self::ACTION_DELETE_A);
} }
break; break;
@@ -182,19 +259,21 @@ class JSMin {
case '(': case '(':
case '+': case '+':
case '-': case '-':
$this->action(1); case '!':
case '~':
$this->action(self::ACTION_KEEP_A);
break; break;
case ' ': case ' ':
$this->action(3); $this->action(self::ACTION_DELETE_A_B);
break; break;
default: default:
if ($this->isAlphaNum($this->b)) { if ($this->isAlphaNum($this->b)) {
$this->action(1); $this->action(self::ACTION_KEEP_A);
} }
else { else {
$this->action(2); $this->action(self::ACTION_DELETE_A);
} }
} }
break; break;
@@ -203,11 +282,11 @@ class JSMin {
switch ($this->b) { switch ($this->b) {
case ' ': case ' ':
if ($this->isAlphaNum($this->a)) { if ($this->isAlphaNum($this->a)) {
$this->action(1); $this->action(self::ACTION_KEEP_A);
break; break;
} }
$this->action(3); $this->action(self::ACTION_DELETE_A_B);
break; break;
case "\n": case "\n":
@@ -219,21 +298,21 @@ class JSMin {
case '-': case '-':
case '"': case '"':
case "'": case "'":
$this->action(1); $this->action(self::ACTION_KEEP_A);
break; break;
default: default:
if ($this->isAlphaNum($this->a)) { if ($this->isAlphaNum($this->a)) {
$this->action(1); $this->action(self::ACTION_KEEP_A);
} }
else { else {
$this->action(3); $this->action(self::ACTION_DELETE_A_B);
} }
} }
break; break;
default: default:
$this->action(1); $this->action(self::ACTION_KEEP_A);
break; break;
} }
} }
@@ -242,6 +321,15 @@ class JSMin {
return $this->output; return $this->output;
} }
/**
* Get the next character, skipping over comments. peek() is used to see
* if a '/' is followed by a '/' or '*'.
*
* @uses get()
* @uses peek()
* @throws JSMinException On unterminated comment.
* @return string
*/
protected function next() { protected function next() {
$c = $this->get(); $c = $this->get();
@@ -281,6 +369,12 @@ class JSMin {
return $c; return $c;
} }
/**
* Get next char. If is ctrl character, translate to a space or newline.
*
* @uses get()
* @return string|null
*/
protected function peek() { protected function peek() {
$this->lookAhead = $this->get(); $this->lookAhead = $this->get();
return $this->lookAhead; return $this->lookAhead;