Add javascript minifier filter.
authorMichael M Slusarz <slusarz@curecanti.org>
Thu, 30 Jul 2009 18:28:50 +0000 (12:28 -0600)
committerMichael M Slusarz <slusarz@curecanti.org>
Thu, 30 Jul 2009 20:08:19 +0000 (14:08 -0600)
framework/Text_Filter/lib/Horde/Text/Filter/JavascriptMinify.php [new file with mode: 0644]
framework/Text_Filter/lib/Horde/Text/Filter/JavascriptMinify/JsMin.php [new file with mode: 0644]
framework/Text_Filter/package.xml

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 (file)
index 0000000..f4b0258
--- /dev/null
@@ -0,0 +1,67 @@
+<?php
+/**
+ * This filter cleans up javascript output by running it through a PHP-based
+ * optimizer/compressor.
+ *
+ * License/copyright from original jsmin.php library:
+ *
+ * --
+ * Copyright (c) 2002 Douglas Crockford  (www.crockford.com)
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the "Software"),
+ * to deal in the Software without restriction, including without limitation
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ * and/or sell copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all
+ * copies or substantial portions of the Software.
+ *
+ * The Software shall be used for Good, not Evil.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
+ * DEALINGS IN THE
+ * SOFTWARE.
+ *
+ * Author: Ryan Grove <ryan@wonko.com>
+ * (c) 2002 Douglas Crockford <douglas@crockford.com> (jsmin.c)
+ * (c) 2008 Ryan Grove <ryan@wonko.com> (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 <slusarz@horde.org>
+ * @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 (file)
index 0000000..f039dc0
--- /dev/null
@@ -0,0 +1,220 @@
+<?php
+/**
+ * PHP implementation of Douglas Crockford's JSMin.
+ *
+ * See Horde_Text_Filter_JavscriptMinify for license information.
+ */
+
+class Horde_Text_Filter_JavascriptMinify_JsMin
+{
+    /* Constants. */
+    const ORD_LF = 10;
+    const ORD_SPACE = 32;
+
+    /* Member variables. */
+    protected $_a = '';
+    protected $_b = '';
+    protected $_input;
+    protected $_inputIndex = 0;
+    protected $_inputLength;
+    protected $_lookAhead = null;
+    protected $_output = '';
+
+    public function __construct($input)
+    {
+        $this->_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;
+    }
+
+}
index 830d7a8..647f2f0 100644 (file)
@@ -37,7 +37,8 @@ http://pear.php.net/dtd/package-2.0.xsd">
   <api>beta</api>
  </stability>
  <license uri="http://www.gnu.org/copyleft/lesser.html">LGPL</license>
- <notes>* Add support for using the tidy extension when filtering HTML data.
+ <notes>* Added javscript minify filter.
+ * Add support for using the tidy extension when filtering HTML data.
  * Initial Horde 4 package.
  </notes>
  <contents>
@@ -55,6 +56,10 @@ http://pear.php.net/dtd/package-2.0.xsd">
        <file name="Environment.php" role="php" />
        <file name="Highlightquotes.php" role="php" />
        <file name="Html2text.php" role="php" />
+       <file name="JavascriptMinify.php" role="php" />
+       <dir name="JavascriptMinify">
+        <file name="JsMin.php" role="php" />
+       </dir> <!-- /lib/Horde/Text/Filter/JavascriptMinify -->
        <file name="Linkurls.php" role="php" />
        <file name="Simplemarkup.php" role="php" />
        <file name="Space2html.php" role="php" />
@@ -212,6 +217,8 @@ http://pear.php.net/dtd/package-2.0.xsd">
    <install name="lib/Horde/Text/Filter/Environment.php" as="Horde/Text/Filter/Environment.php" />
    <install name="lib/Horde/Text/Filter/Highlightquotes.php" as="Horde/Text/Filter/Highlightquotes.php" />
    <install name="lib/Horde/Text/Filter/Html2text.php" as="Horde/Text/Filter/Html2text.php" />
+   <install name="lib/Horde/Text/Filter/JavascriptMinify.php" as="Horde/Text/Filter/JavascriptMinify.php" />
+   <install name="lib/Horde/Text/Filter/JavascriptMinify/JsMin.php" as="Horde/Text/Filter/JavascriptMinify/JsMin.php" />
    <install name="lib/Horde/Text/Filter/Linkurls.php" as="Horde/Text/Filter/Linkurls.php" />
    <install name="lib/Horde/Text/Filter/Simplemarkup.php" as="Horde/Text/Filter/Simplemarkup.php" />
    <install name="lib/Horde/Text/Filter/Space2html.php" as="Horde/Text/Filter/Space2html.php" />