From: Michael M Slusarz Date: Thu, 30 Jul 2009 18:28:50 +0000 (-0600) Subject: Add javascript minifier filter. X-Git-Url: https://git.internetallee.de/?a=commitdiff_plain;h=0a1dd0cd19b9adc33aea4d4f137b4d6b0aef4e8f;p=horde.git Add javascript minifier filter. --- diff --git a/framework/Text_Filter/lib/Horde/Text/Filter/JavascriptMinify.php b/framework/Text_Filter/lib/Horde/Text/Filter/JavascriptMinify.php new file mode 100644 index 000000000..f4b025829 --- /dev/null +++ b/framework/Text_Filter/lib/Horde/Text/Filter/JavascriptMinify.php @@ -0,0 +1,67 @@ + + * (c) 2002 Douglas Crockford (jsmin.c) + * (c) 2008 Ryan Grove (PHP port) + * Version: 1.1.1 (2008-03-02) + * URL: http://code.google.com/p/jsmin-php/ + * -- + * + * Copyright 2009 The Horde Project (http://www.horde.org/) + * + * See the enclosed file COPYING for license information (GPL). If you + * did not receive this file, see http://www.fsf.org/copyleft/gpl.html. + * + * @author Michael Slusarz + * @package Horde_Text + */ +class Horde_Text_Filter_JavascriptMinify extends Horde_Text_Filter +{ + /** + * Executes any code necessary after applying the filter patterns. + * + * @param string $text The text after the filtering. + * + * @return string The modified text. + */ + public function postProcess($text) + { + $jsmin = new Horde_Text_Filter_JavascriptMinify_JsMin($text); + try { + return $jsmin->minify(); + } catch (Exception $e) { + return $text; + } + } + +} diff --git a/framework/Text_Filter/lib/Horde/Text/Filter/JavascriptMinify/JsMin.php b/framework/Text_Filter/lib/Horde/Text/Filter/JavascriptMinify/JsMin.php new file mode 100644 index 000000000..f039dc07d --- /dev/null +++ b/framework/Text_Filter/lib/Horde/Text/Filter/JavascriptMinify/JsMin.php @@ -0,0 +1,220 @@ +_input = str_replace("\r\n", "\n", $input); + $this->_inputLength = strlen($this->_input); + } + + public function minify() + { + $this->_a = "\n"; + $this->_action(3); + + while (!is_null($this->_a)) { + switch ($this->_a) { + case ' ': + $this->_action($this->_isAlphaNum($this->_b) ? 1 : 2); + break; + + case "\n": + switch ($this->_b) { + case '{': + case '[': + case '(': + case '+': + case '-': + $this->_action(1); + break; + + case ' ': + $this->_action(3); + break; + + default: + $this->_action($this->_isAlphaNum($this->_b) ? 1 : 2); + break; + } + break; + + default: + switch ($this->_b) { + case ' ': + $this->_action($this->_isAlphaNum($this->_a) ? 1 : 3); + break; + + case "\n": + switch ($this->_a) { + case '}': + case ']': + case ')': + case '+': + case '-': + case '"': + case "'": + $this->_action(1); + break; + + default: + $this->_action($this->_isAlphaNum($this->_a) ? 1 : 3); + break; + } + break; + + default: + $this->_action(1); + break; + } + } + } + + return $this->_output; + } + + protected function _action($d) + { + switch($d) { + case 1: + $this->_output .= $this->_a; + + case 2: + $this->_a = $this->_b; + + if ($this->_a === '\'' || $this->_a === '"') { + for (;;) { + $this->_output .= $this->_a; + $this->_a = $this->_get(); + + if ($this->_a === $this->_b) { + break; + } + + if (ord($this->_a) <= self::ORD_LF) { + throw new Exception('Unterminated string literal.'); + } + + if ($this->_a === '\\') { + $this->_output .= $this->_a; + $this->_a = $this->_get(); + } + } + } + + case 3: + $this->_b = $this->_next(); + + if ($this->_b === '/' && strspn($this->_a, '(,=:[!&|?')) { + $this->_output .= $this->_a . $this->_b; + + for (;;) { + $this->_a = $this->_get(); + + if ($this->_a === '/') { + break; + } elseif ($this->_a === '\\') { + $this->_output .= $this->_a; + $this->_a = $this->_get(); + } elseif (ord($this->_a) <= self::ORD_LF) { + throw new Exception('Unterminated regular expression literal.'); + } + + $this->_output .= $this->_a; + } + + $this->_b = $this->_next(); + } + } + } + + protected function _get() + { + $c = $this->_lookAhead; + $this->_lookAhead = null; + + if (is_null($c) && + ($this->_inputIndex < $this->_inputLength)) { + $c = $this->_input[$this->_inputIndex]; + $this->_inputIndex += 1; + } + + if ($c === "\r") { + return "\n"; + } + + if (is_null($c) || ($c === "\n") || (ord($c) >= self::ORD_SPACE)) { + return $c; + } + + return ' '; + } + + protected function _isAlphaNum($c) + { + return (ord($c) > 126 || ($c === '\\') || (preg_match('/^[\w\$]$/', $c) === 1)); + } + + protected function _next() + { + $c = $this->_get(); + + if ($c !== '/') { + return $c; + } + + switch ($this->_peek()) { + case '/': + for (;;) { + $c = $this->_get(); + if (ord($c) <= self::ORD_LF) { + return $c; + } + } + + case '*': + $this->_get(); + + for (;;) { + switch($this->_get()) { + case '*': + if ($this->_peek() === '/') { + $this->_get(); + return ' '; + } + break; + + case null: + throw new Exception('Unterminated comment.'); + } + } + } + + return $c; + } + + protected function _peek() + { + $this->_lookAhead = $this->_get(); + return $this->_lookAhead; + } + +} diff --git a/framework/Text_Filter/package.xml b/framework/Text_Filter/package.xml index 830d7a840..647f2f079 100644 --- a/framework/Text_Filter/package.xml +++ b/framework/Text_Filter/package.xml @@ -37,7 +37,8 @@ http://pear.php.net/dtd/package-2.0.xsd"> beta LGPL - * Add support for using the tidy extension when filtering HTML data. + * Added javscript minify filter. + * Add support for using the tidy extension when filtering HTML data. * Initial Horde 4 package. @@ -55,6 +56,10 @@ http://pear.php.net/dtd/package-2.0.xsd"> + + + + @@ -212,6 +217,8 @@ http://pear.php.net/dtd/package-2.0.xsd"> + +