/** * Optimize an input CSS string or parsed css_group * * @param string|css_group $css * * @return string|css_group */ public function process($css) { //Parse CSS if ($css instanceof css_group) { $css_doc = $css; } else { $parser = new css_parser(); $css_doc = $parser->parse($css); } //Remove comments if ($this->remove_comments) { foreach ($css_doc->find_all('css_element') as $element) { if ($element->type == 'comment') { $element->remove(); } } } //Lowercase all property names foreach ($css_doc->find_all('css_property') as $property) { $property->name = strtolower($property->name); } //Remove IE hacks if ($this->remove_ie_hacks) { $this->_remove_ie_hacks($css_doc); } //Optimize if ($this->optimize) { $this->_optimize($css_doc); } //Extra optimize if ($this->extra_optimize) { $this->_extra_optimize($css_doc); } //Add vendor prefixes if ($this->prefixes) { $prefixer = new css_prefixer(); $options = explode(',', $this->prefixes); $prefixer->webkit = $this->prefixes == 'all' || in_array('webkit', $options); $prefixer->mozilla = $this->prefixes == 'all' || in_array('mozilla', $options); $prefixer->opera = $this->prefixes == 'all' || in_array('opera', $options); $prefixer->msie = $this->prefixes == 'all' || in_array('msie', $options); $prefixer->add_prefixes($css_doc); } return $css instanceof css_group ? $css_doc : $css_doc->render($this->compress); }
} else { //Read from input file $in = array_shift($argv); if (!$in) { echo $HELP; exit(1); } $data = file_get_contents($in); if ($data === false) { err("Could not read to file {$in}"); exit(1); } } //Parse input $parser = new css_parser(); $css_doc = $parser->parse($data); if (has('u')) { //css_cleaner if (!has('p')) { err('Please, indicate the project source path. Multiple paths can be concatenated using ' . PATH_SEPARATOR); exit(1); } $cleaner = new css_cleaner(); $cleaner->verbose = true; $cleaner->project_files = explode(PATH_SEPARATOR, $opts['p']); $cleaner->method = get('m', 'safe') == 'safe' ? css_cleaner::METHOD_SAFE : css_cleaner::METHOD_BEST_CLEAN; if (has('e')) { $cleaner->extensions = get('e'); } if (has('r')) { $cleaner->report_path = get('r');
function do_optimization() { $settings = array(); $cleaner = new css_cleaner(); foreach (array('method', 'compress') as $prop) { $settings[$prop] = isset($_POST[$prop]) ? $_POST[$prop] : (isset($cleaner->{$prop}) ? $cleaner->{$prop} : false); $cleaner->{$prop} = $settings[$prop]; } $result = array(); $result['settings'] = $settings; if (!empty($_POST['source'])) { $cleaner->verbose = true; $cleaner->project_files[] = dirname(__FILE__); $result['source'] = $_POST['source']; $parser = new css_parser(); //Clean CSS ob_start(); $start = microtime(true); $result['css'] = $cleaner->clean($parser->parse($result['source']))->render($settings['compress']); $result['execution_time'] = microtime(true) - $start; $result['output'] = ob_get_clean(); } return $result; }