* @category Horde
* @copyright 2014-2017 Horde LLC
* @license http://www.horde.org/licenses/lgpl21 LGPL 2.1
* @package CssMinify
*/
class Horde_CssMinify_CssParser extends Horde_CssMinify
{
/**
* @param array $opts Driver specific options:
*
* - dataurl: (callback) A callback function to convert a URI to a
* data URL. Takes one argument (URI) and returns the data
* URI to be used in the file.
* - import: (callback) A callback function to convert a URI to a
* pathname. Takes one argument (URI) and expects an array
* back with two elements (URI, filename).
*
*/
public function setOptions(array $opts = array())
{
parent::setOptions($opts);
}
/**
*/
public function minify()
{
if (is_string($this->_data)) {
$parser = new Horde_Css_Parser($this->_data);
return $parser->compress();
}
return $this->_minify($this->_data);
}
/**
*/
protected function _minify($data)
{
$out = '';
foreach ($data as $uri => $file) {
if (!is_readable($file)) {
$this->_opts['logger']->log(
sprintf('Could not open CSS file %s.', $file),
Horde_Log::ERR
);
continue;
}
$css = file_get_contents($file);
try {
$parser = new Horde_Css_Parser($css);
} catch (Exception $e) {
/* If the CSS is broken, log error and output as-is. */
$this->_opts['logger']->log($e, Horde_Log::ERR);
$out .= $css;
continue;
}
if (!empty($this->_opts['import'])) {
foreach ($parser->doc->getContents() as $val) {
if ($val instanceof Sabberworm\CSS\Property\Import) {
$res = call_user_func($this->_opts['import'], dirname($uri) . '/' . $val->getLocation()->getURL()->getString());
$out .= $this->_minify(array($res[0] => $res[1]));
$parser->doc->remove($val);
}
}
}
$url = array();
foreach ($parser->doc->getAllRuleSets() as $val) {
foreach (array_merge($val->getRules('background-'), $val->getRules('src')) as $val2) {
$item = $val2->getValue();
if ($item instanceof Sabberworm\CSS\Value\URL) {
$url[] = $item;
} elseif ($item instanceof Sabberworm\CSS\Value\RuleValueList) {
foreach ($item->getListComponents() as $val3) {
if ($val3 instanceof Sabberworm\CSS\Value\URL) {
$url[] = $val3;
} elseif ($val3 instanceof Sabberworm\CSS\Value\RuleValueList) {
foreach ($val3->getListComponents() as $val4) {
if ($val4 instanceof Sabberworm\CSS\Value\URL) {
$url[] = $val4;
}
}
}
}
}
}
}
foreach ($url as $val) {
$url_ob = $val->getURL();
$url_str = ltrim($url_ob->getString());
if ((stripos($url_str, 'http') !== 0) &&
!Horde_Url_Data::isData($url_str)) {
$url_str = dirname($uri) . '/' . $url_str;
if (!empty($this->_opts['dataurl'])) {
$url_str = call_user_func($this->_opts['dataurl'], $url_str);
}
}
$url_ob->setString($url_str);
}
$out .= $parser->compress();
}
return $out;
}
}
Horde_CssMinify-1.0.4/lib/Horde/CssMinify/Exception.php 0000664 0001750 0001750 00000001265 13160152777 021044 0 ustar jan jan
* @category Horde
* @license http://www.horde.org/licenses/lgpl21 LGPL 2.1
* @package CssMinify
*/
/**
* Exception handler for the Horde_CssMinify package.
*
* @author Michael Slusarz
* @category Horde
* @copyright 2014-2017 Horde LLC
* @license http://www.horde.org/licenses/lgpl21 LGPL 2.1
* @package CssMinify
*/
class Horde_CssMinify_Exception extends Horde_Exception_Wrapped
{
}
Horde_CssMinify-1.0.4/lib/Horde/CssMinify.php 0000664 0001750 0001750 00000004534 13160152777 017110 0 ustar jan jan
* @category Horde
* @license http://www.horde.org/licenses/lgpl21 LGPL 2.1
* @package CssMinify
*/
/**
* Abstract base class for implementing a CSS minification driver.
*
* @author Michael Slusarz
* @category Horde
* @copyright 2014-2017 Horde LLC
* @license http://www.horde.org/licenses/lgpl21 LGPL 2.1
* @package CssMinify
*/
abstract class Horde_CssMinify
{
/**
* Original CSS data.
*
* @var mixed
*/
protected $_data;
/**
* Minification options.
*
* @var array
*/
protected $_opts = array();
/**
* Constructor.
*
* @param mixed $css Either a string (the CSS text to compress) or an
* array of URLs (keys) to filenames (values)
* containing the CSS data to compress.
* @param array $opts Additional options. See setOptions().
*/
public function __construct($css, array $opts = array())
{
if (!is_array($css) && !is_string($css)) {
throw new InvalidArgumentException('First argument must either be an array or a string.');
}
$this->_data = $css;
$this->setOptions($opts);
}
/**
* @see Horde_CssMinify::minify()
*/
public function __toString()
{
return $this->minify();
}
/**
* Set minification options.
*
* @param array $opts Options:
*
* - logger: (Horde_Log_Logger) Log object to use for log messages.
*
*/
public function setOptions(array $opts = array())
{
$this->_opts = array_merge($this->_opts, $opts);
// Ensure we have a logger object.
if (!isset($this->_opts['logger']) ||
!($this->_opts['logger'] instanceof Horde_Log_Logger)) {
$this->_opts['logger'] = new Horde_Log_Logger(
new Horde_Log_Handler_Null()
);
}
}
/**
* Return the minified CSS.
*
* @return string Minified CSS.
* @throws Horde_CssMinify_Exception
*/
abstract public function minify();
}