* @author Tijs Verkoyen * @version 1.2.0 * * @copyright Copyright (c) 2012, Matthias Mullie. All rights reserved. * @license MIT License */ class JS extends Minify { const STRIP_COMMENTS = 1; const STRIP_WHITESPACE = 2; /** * Minify the data. * Perform JS optimizations. * * @param string[optional] $path The path the data should be written to. * @param int[optional] $options The minify options to be applied. * @return string The minified data. */ public function minify($path = false, $options = self::ALL) { $content = ''; // loop files foreach ($this->data as $source => $js) { // combine js $content .= $js; } /* * Strings are a pattern we need to match, in order to ignore potential * code-like content inside them, but we just want all of the string * content to remain untouched. */ $this->registerPattern('/^([\'"]).*?\\1/s', '\\0'); if($options & static::STRIP_COMMENTS) $content = $this->stripComments($content); if($options & static::STRIP_WHITESPACE) $content = $this->stripWhitespace($content); $content = $this->replace($content); // save to path if($path !== false) $this->save($content, $path); return $content; } /** * Strip comments from source code. * * @param string $content The content to strip the comments for. * @return string */ protected function stripComments($content) { // single-line comments $this->registerPattern('/^\/\/.*$[\r\n]*/m', ''); // multi-line comments $this->registerPattern('/^\/\*.*?\*\//s', ''); return $content; } /** * Strip whitespace. * * @param string $content The content to strip the whitespace for. * @return string */ protected function stripWhitespace($content) { // newlines > linefeed $this->registerPattern('/^(\r\n|\r)/m', "\n"); // empty lines > collapse $this->registerPattern('/^\n\s+/', "\n"); // redundant whitespace > remove $callback = function($match) { return $match[1]; }; $this->registerPattern('/^([{}\[\]\(\)=><&\|;:,\?!\+-])[ \t]+/', $callback); $this->registerPattern('/^[ \t]+(?=[{}\[\]\(\)=><&\|;:,\?!\+-])/', ''); // redundant semicolons (followed by another semicolon or closing curly bracket) > remove $this->registerPattern('/^;\s*(?=[;}])/s', ''); /* * @todo: we could remove all line feeds, but then we have to be certain that all statements are properly * terminated with a semi-colon. So we'd first have to parse the statements to see which require a semi-colon, * add it if it's not present, and then remove the line feeds. The semi-colon just before a closing curly * bracket can then also be omitted. */ return $content; } }