Author: Stephen Clay (steve@mrclay.org)
Exemple #1
0
 /**
  * Minify a CSS string
  * 
  * @param string $css
  * 
  * @param array $options available options:
  * 
  * 'preserveComments': (default true) multi-line comments that begin
  * with "/*!" will be preserved with newlines before and after to
  * enhance readability.
  * 
  * 'prependRelativePath': (default null) if given, this string will be
  * prepended to all relative URIs in import/url declarations
  * 
  * 'currentDir': (default null) if given, this is assumed to be the
  * directory of the current CSS file. Using this, minify will rewrite
  * all relative URIs in import/url declarations to correctly point to
  * the desired files. For this to work, the files *must* exist and be
  * visible by the PHP process.
  *
  * 'symlinks': (default = array()) If the CSS file is stored in 
  * a symlink-ed directory, provide an array of link paths to
  * target paths, where the link paths are within the document root. Because 
  * paths need to be normalized for this to work, use "//" to substitute 
  * the doc root in the link paths (the array keys). E.g.:
  * <code>
  * array('//symlink' => '/real/target/path') // unix
  * array('//static' => 'D:\\staticStorage')  // Windows
  * </code>
  * 
  * @return string
  */
 static public function minify($css, $options = array()) 
 {
     require_once 'Minify/CSS/Compressor.php';
     if (isset($options['preserveComments']) 
         && !$options['preserveComments']) {
         $css = Minify_CSS_Compressor::process($css, $options);
     } else {
         require_once 'Minify/CommentPreserver.php';
         $css = Minify_CommentPreserver::process(
             $css
             ,array('Minify_CSS_Compressor', 'process')
             ,array($options)
         );
     }
     if (! isset($options['currentDir']) && ! isset($options['prependRelativePath'])) {
         return $css;
     }
     require_once 'Minify/CSS/UriRewriter.php';
     if (isset($options['currentDir'])) {
         return Minify_CSS_UriRewriter::rewrite(
             $css
             ,$options['currentDir']
             ,isset($options['docRoot']) ? $options['docRoot'] : $_SERVER['DOCUMENT_ROOT']
             ,isset($options['symlinks']) ? $options['symlinks'] : array()
         );  
     } else {
         return Minify_CSS_UriRewriter::prepend(
             $css
             ,$options['prependRelativePath']
         );
     }
 }
Exemple #2
0
 /**
  * Minify a CSS string
  * 
  * @param string $css
  * 
  * @param array $options available options:
  * 
  * 'preserveComments': (default true) multi-line comments that begin
  * with "/*!" will be preserved with newlines before and after to
  * enhance readability.
  *
  * 'removeCharsets': (default true) remove all @charset at-rules
  * 
  * 'prependRelativePath': (default null) if given, this string will be
  * prepended to all relative URIs in import/url declarations
  * 
  * 'currentDir': (default null) if given, this is assumed to be the
  * directory of the current CSS file. Using this, minify will rewrite
  * all relative URIs in import/url declarations to correctly point to
  * the desired files. For this to work, the files *must* exist and be
  * visible by the PHP process.
  *
  * 'symlinks': (default = array()) If the CSS file is stored in 
  * a symlink-ed directory, provide an array of link paths to
  * target paths, where the link paths are within the document root. Because 
  * paths need to be normalized for this to work, use "//" to substitute 
  * the doc root in the link paths (the array keys). E.g.:
  * <code>
  * array('//symlink' => '/real/target/path') // unix
  * array('//static' => 'D:\\staticStorage')  // Windows
  * </code>
  *
  * 'docRoot': (default = $_SERVER['DOCUMENT_ROOT'])
  * see Minify_CSS_UriRewriter::rewrite
  * 
  * @return string
  */
 public static function minify($css, $options = array())
 {
     $options = array_merge(array('compress' => true, 'removeCharsets' => true, 'preserveComments' => true, 'currentDir' => null, 'docRoot' => $_SERVER['DOCUMENT_ROOT'], 'prependRelativePath' => null, 'symlinks' => array()), $options);
     if ($options['removeCharsets']) {
         $css = preg_replace('/@charset[^;]+;\\s*/', '', $css);
     }
     if ($options['compress']) {
         if (!$options['preserveComments']) {
             require_once 'Compressor.php';
             $css = Minify_CSS_Compressor::process($css, $options);
         } else {
             require_once 'CommentPreserver.php';
             require_once 'Compressor.php';
             $css = Minify_CommentPreserver::process($css, array('Minify_CSS_Compressor', 'process'), array($options));
         }
     }
     if (!$options['currentDir'] && !$options['prependRelativePath']) {
         return $css;
     }
     require_once 'UriRewriter.php';
     if ($options['currentDir']) {
         return Minify_CSS_UriRewriter::rewrite($css, $options['currentDir'], $options['docRoot'], $options['symlinks']);
     } else {
         return Minify_CSS_UriRewriter::prepend($css, $options['prependRelativePath']);
     }
 }
 /**
  * Minify a CSS string
  * 
  * @param string $css
  * 
  * @param array $options available options:
  * 
  * 'preserveComments': (default true) multi-line comments that begin
  * with "/*!" will be preserved with newlines before and after to
  * enhance readability.
  * 
  * 'prependRelativePath': (default null) if given, this string will be
  * prepended to all relative URIs in import/url declarations
  * 
  * 'currentDir': (default null) if given, this is assumed to be the
  * directory of the current CSS file. Using this, minify will rewrite
  * all relative URIs in import/url declarations to correctly point to
  * the desired files. For this to work, the files *must* exist and be
  * visible by the PHP process.
  * 
  * @return string
  */
 public static function minify($css, $options = array())
 {
     if (isset($options['preserveComments']) && !$options['preserveComments']) {
         return self::_minify($css, $options);
     }
     // recursive calls don't preserve comments
     $options['preserveComments'] = false;
     return Minify_CommentPreserver::process($css, array(self::$className, 'minify'), array($options));
 }
Exemple #4
0
 /**
  * Minify a CSS string
  * 
  * @param string $css
  * 
  * @param array $options available options:
  * 
  * 'preserveComments': (default true) multi-line comments that begin
  * with "/*!" will be preserved with newlines before and after to
  * enhance readability.
  * 
  * 'prependRelativePath': (default null) if given, this string will be
  * prepended to all relative URIs in import/url declarations
  * 
  * 'currentDir': (default null) if given, this is assumed to be the
  * directory of the current CSS file. Using this, minify will rewrite
  * all relative URIs in import/url declarations to correctly point to
  * the desired files. For this to work, the files *must* exist and be
  * visible by the PHP process.
  * 
  * @return string
  */
 public static function minify($css, $options = array())
 {
     if (isset($options['preserveComments']) && !$options['preserveComments']) {
         return self::_minify($css, $options);
     }
     require_once 'Minify/CommentPreserver.php';
     // recursive calls don't preserve comments
     $options['preserveComments'] = false;
     return Minify_CommentPreserver::process($css, array('Minify_CSS', 'minify'), array($options));
 }
Exemple #5
0
 /**
  * Minify a CSS string
  *
  * @param string $css
  *
  * @param array $options available options:
  *
  * 'preserveComments': (default true) multi-line comments that begin
  * with "/*!" will be preserved with newlines before and after to
  * enhance readability.
  *
  * 'prependRelativePath': (default null) if given, this string will be
  * prepended to all relative URIs in import/url declarations
  *
  * 'currentDir': (default null) if given, this is assumed to be the
  * directory of the current CSS file. Using this, minify will rewrite
  * all relative URIs in import/url declarations to correctly point to
  * the desired files. For this to work, the files *must* exist and be
  * visible by the PHP process.
  *
  * 'symlinks': (default = array()) If the CSS file is stored in
  * a symlink-ed directory, provide an array of link paths to
  * target paths, where the link paths are within the document root. Because
  * paths need to be normalized for this to work, use "//" to substitute
  * the doc root in the link paths (the array keys). E.g.:
  * <code>
  * array('//symlink' => '/real/target/path') // unix
  * array('//static' => 'D:\\staticStorage')  // Windows
  * </code>
  *
  * @return string
  */
 public static function minify($css, $options = array())
 {
     require_once W3TC_LIB_MINIFY_DIR . '/Minify/CSS/Compressor.php';
     if (isset($options['preserveComments']) && $options['preserveComments']) {
         require_once W3TC_LIB_MINIFY_DIR . '/Minify/CommentPreserver.php';
         $css = Minify_CommentPreserver::process($css, array('Minify_CSS_Compressor', 'process'), array($options));
     } else {
         $css = Minify_CSS_Compressor::process($css, $options);
     }
     require_once W3TC_LIB_MINIFY_DIR . '/Minify/CSS/UriRewriter.php';
     $css = Minify_CSS_UriRewriter::rewrite($css, $options);
     return $css;
 }
Exemple #6
0
 /**
  * Minify a CSS string
  * 
  * @param string $css
  * 
  * @param array $options available options:
  * 
  * 'preserveComments': (default true) multi-line comments that begin
  * with "/*!" will be preserved with newlines before and after to
  * enhance readability.
  *
  * 'removeCharsets': (default true) remove all @charset at-rules
  * 
  * 'prependRelativePath': (default null) if given, this string will be
  * prepended to all relative URIs in import/url declarations
  * 
  * 'currentDir': (default null) if given, this is assumed to be the
  * directory of the current CSS file. Using this, minify will rewrite
  * all relative URIs in import/url declarations to correctly point to
  * the desired files. For this to work, the files *must* exist and be
  * visible by the PHP process.
  *
  * 'symlinks': (default = array()) If the CSS file is stored in 
  * a symlink-ed directory, provide an array of link paths to
  * target paths, where the link paths are within the document root. Because 
  * paths need to be normalized for this to work, use "//" to substitute 
  * the doc root in the link paths (the array keys). E.g.:
  * <code>
  * array('//symlink' => '/real/target/path') // unix
  * array('//static' => 'D:\\staticStorage')  // Windows
  * </code>
  *
  * 'docRoot': (default = $_SERVER['DOCUMENT_ROOT'])
  * see Minify_CSS_UriRewriter::rewrite
  * 
  * @return string
  */
 public static function minify($css, $options = array())
 {
     $options = array_merge(array('removeCharsets' => true, 'preserveComments' => true), $options);
     if ($options['removeCharsets']) {
         $css = preg_replace('/@charset[^;]+;\\s*/', '', $css);
     }
     if (!$options['preserveComments']) {
         $css = Minify_CSS_Compressor::process($css, $options);
     } else {
         $css = Minify_CommentPreserver::process($css, array('Minify_CSS_Compressor', 'process'), array($options));
     }
     return $css;
 }
function test_Minify_CommentPreserver()
{
    global $thisDir;
    $inOut = array('/*!*/' => "\n/**/\n", '/*!*/a' => "\n/**/\n1A", 'a/*!*//*!*/b' => "2A\n/**/\n\n/**/\n3B", 'a/*!*/b/*!*/' => "4A\n/**/\n5B\n/**/\n");
    foreach ($inOut as $in => $expected) {
        $actual = Minify_CommentPreserver::process($in, '_test_MCP_processor');
        $passed = assertTrue($expected === $actual, 'Minify_CommentPreserver');
        if (__FILE__ === realpath($_SERVER['SCRIPT_FILENAME'])) {
            echo "\n---Output: " . strlen($actual) . " bytes\n\n{$actual}\n\n";
            if (!$passed) {
                echo "---Expected: " . strlen($expected) . " bytes\n\n{$expected}\n\n\n";
            }
        }
    }
}