Source: https://github.com/sabberworm/PHP-CSS-Parser
Author: Michael Slusarz (slusarz@horde.org)
Example #1
0
 /**
  */
 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 ($val->getRules('background-') 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;
                         }
                     }
                 }
             }
         }
         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;
 }
Example #2
0
 public function testDoubleAsteriskAtBeginningOfComment()
 {
     $a = '/** Foo */#bar{width:1px;}';
     $css = new Horde_Css_Parser($a);
     $this->assertEquals('#bar{width:1px}', $css->compress());
 }
Example #3
0
 /**
  * @small
  */
 public function testEmptyDocument()
 {
     $css = new Horde_Css_Parser('');
     $this->assertEquals('', $css->compress());
 }