4 * CSSTidy - CSS Parser and Optimiser
7 * This class prints CSS data generated by csstidy.
9 * Copyright 2005, 2006, 2007 Florian Schmitz
11 * This file is part of CSSTidy.
13 * CSSTidy is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU Lesser General Public License as published by
15 * the Free Software Foundation; either version 2.1 of the License, or
16 * (at your option) any later version.
18 * CSSTidy is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License for more details.
23 * You should have received a copy of the GNU Lesser General Public License
24 * along with this program. If not, see <http://www.gnu.org/licenses/>.
26 * @license http://opensource.org/licenses/lgpl-license.php GNU Lesser General Public License
28 * @author Florian Schmitz (floele at gmail dot com) 2005-2007
29 * @author Brett Zamir (brettz9 at yahoo dot com) 2007
30 * @author Cedric Morin (cedric at yterium dot com) 2010-2012
36 * This class prints CSS data generated by csstidy.
39 * @author Florian Schmitz (floele at gmail dot com) 2005-2006
51 * Saves the input CSS string
55 public $input_css = '';
57 * Saves the formatted CSS string
61 public $output_css = '';
63 * Saves the formatted CSS string (plain text)
67 public $output_css_plain = '';
71 * @param array $css contains the class csstidy
75 public function __construct($css) {
77 $this->css
= & $css->css
;
78 $this->template
= & $css->template
;
79 $this->tokens
= & $css->tokens
;
80 $this->charset
= & $css->charset
;
81 $this->import
= & $css->import
;
82 $this->namespace = & $css->namespace;
86 * Resets output_css and output_css_plain (new css code)
90 public function _reset() {
91 $this->output_css
= '';
92 $this->output_css_plain
= '';
96 * Returns the CSS code as plain text
97 * @param string $default_media default @media to add to selectors without any @media
102 public function plain($default_media='') {
103 $this->_print(true, $default_media);
104 return $this->output_css_plain
;
108 * Returns the formatted CSS code
109 * @param string $default_media default @media to add to selectors without any @media
114 public function formatted($default_media='') {
115 $this->_print(false, $default_media);
116 return $this->output_css
;
120 * Returns the formatted CSS code to make a complete webpage
121 * @param string $doctype shorthand for the document type
122 * @param bool $externalcss indicates whether styles to be attached internally or as an external stylesheet
123 * @param string $title title to be added in the head of the document
124 * @param string $lang two-letter language code to be added to the output
129 public function formatted_page($doctype='html5', $externalcss=true, $title='', $lang='en') {
132 $doctype_output = '<!DOCTYPE html>';
134 case 'xhtml1.0strict':
135 $doctype_output = '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
136 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">';
140 $doctype_output = '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
141 "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">';
145 $output = $cssparsed = '';
146 $this->output_css_plain
= & $output;
148 $output .= $doctype_output . "\n" . '<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="' . $lang . '"';
149 $output .= ( $doctype === 'xhtml1.1') ?
'>' : ' lang="' . $lang . '">';
150 $output .= "\n<head>\n <title>$title</title>";
153 $output .= "\n <style type=\"text/css\">\n";
154 $cssparsed = file_get_contents('cssparsed.css');
155 $output .= $cssparsed; // Adds an invisible BOM or something, but not in css_optimised.php
156 $output .= "\n</style>";
158 $output .= "\n" . ' <link rel="stylesheet" type="text/css" href="cssparsed.css" />';
160 $output .= "\n</head>\n<body><code id=\"copytext\">";
161 $output .= $this->formatted();
162 $output .= '</code>' . "\n" . '</body></html>';
163 return $this->output_css_plain
;
167 * Returns the formatted CSS Code and saves it into $this->output_css and $this->output_css_plain
168 * @param bool $plain plain text or not
169 * @param string $default_media default @media to add to selectors without any @media
173 public function _print($plain = false, $default_media='') {
174 if ($this->output_css
&& $this->output_css_plain
) {
179 if (!$this->parser
->get_cfg('preserve_css')) {
180 $this->_convert_raw_css($default_media);
183 $template = & $this->template
;
186 $template = array_map('strip_tags', $template);
189 if ($this->parser
->get_cfg('timestamp')) {
190 array_unshift($this->tokens
, array(COMMENT
, ' CSSTidy ' . $this->parser
->version
. ': ' . date('r') . ' '));
193 if (!empty($this->charset
)) {
194 $output .= $template[0] . '@charset ' . $template[5] . $this->charset
. $template[6] . $template[13];
197 if (!empty($this->import
)) {
198 for ($i = 0, $size = count($this->import
); $i < $size; $i++
) {
199 if (substr($this->import
[$i], 0, 4) === 'url(' && substr($this->import
[$i], -1, 1) === ')') {
200 $this->import
[$i] = '"' . substr($this->import
[$i], 4, -1) . '"';
201 $this->parser
->log('Optimised @import : Removed "url("', 'Information');
203 $output .= $template[0] . '@import ' . $template[5] . $this->import
[$i] . $template[6] . $template[13];
207 if (!empty($this->namespace)) {
208 if (($p=strpos($this->namespace,"url("))!==false && substr($this->namespace, -1, 1) === ')') {
209 $this->namespace = substr_replace($this->namespace,'"',$p,4);
210 $this->namespace = substr($this->namespace, 0, -1) . '"';
211 $this->parser
->log('Optimised @namespace : Removed "url("', 'Information');
213 $output .= $template[0] . '@namespace ' . $template[5] . $this->namespace . $template[6] . $template[13];
219 foreach ($this->tokens
as $key => $token) {
222 $out .= $template[0] . $this->_htmlsp($token[1], $plain) . $template[1];
227 if ($this->parser
->get_cfg('lowercase_s'))
228 $token[1] = strtolower($token[1]);
229 $out .= ( $token[1]{0} !== '@') ?
$template[2] . $this->_htmlsp($token[1], $plain) : $template[0] . $this->_htmlsp($token[1], $plain);
230 $out .= $template[3];
234 if ($this->parser
->get_cfg('case_properties') === 2) {
235 $token[1] = strtoupper($token[1]);
236 } elseif ($this->parser
->get_cfg('case_properties') === 1) {
237 $token[1] = strtolower($token[1]);
239 $out .= $template[4] . $this->_htmlsp($token[1], $plain) . ':' . $template[5];
243 $out .= $this->_htmlsp($token[1], $plain);
244 if ($this->_seeknocomment($key, 1) == SEL_END
&& $this->parser
->get_cfg('remove_last_;')) {
245 $out .= str_replace(';', '', $template[6]);
247 $out .= $template[6];
252 $out .= $template[7];
253 if ($this->_seeknocomment($key, 1) != AT_END
)
254 $out .= $template[8];
259 $out .= $template[10] . str_replace("\n", "\n" . $template[10], $in_at_out);
261 $out .= $template[9];
265 $out .= $template[11] . '/*' . $this->_htmlsp($token[1], $plain) . '*/' . $template[12];
270 $output = trim($output);
273 $this->output_css
= $output;
276 // If using spaces in the template, don't want these to appear in the plain output
277 $this->output_css_plain
= str_replace(' ', '', $output);
282 * Gets the next token type which is $move away from $key, excluding comments
283 * @param integer $key current position
284 * @param integer $move move this far
285 * @return mixed a token type
289 public function _seeknocomment($key, $move) {
290 $go = ($move > 0) ?
1 : -1;
291 for ($i = $key +
1; abs($key - $i) - 1 < abs($move); $i +
= $go) {
292 if (!isset($this->tokens
[$i])) {
295 if ($this->tokens
[$i][0] == COMMENT
) {
299 return $this->tokens
[$i][0];
304 * Converts $this->css array to a raw array ($this->tokens)
305 * @param string $default_media default @media to add to selectors without any @media
309 public function _convert_raw_css($default_media='') {
310 $this->tokens
= array();
311 $sort_selectors = $this->parser
->get_cfg('sort_selectors');
312 $sort_properties = $this->parser
->get_cfg('sort_properties');
314 foreach ($this->css
as $medium => $val) {
317 if (intval($medium) < DEFAULT_AT
) {
318 // un medium vide (contenant @font-face ou autre @) ne produit aucun conteneur
319 if (strlen(trim($medium))) {
320 $this->parser
->_add_token(AT_START
, $medium, true);
322 } elseif ($default_media) {
323 $this->parser
->_add_token(AT_START
, $default_media, true);
326 foreach ($val as $selector => $vali) {
327 if ($sort_properties)
329 $this->parser
->_add_token(SEL_START
, $selector, true);
332 '*' => array(), // IE7 hacks first
333 '_' => array(), // IE6 hacks
334 '/' => array(), // IE6 hacks
335 '-' => array() // IE6 hacks
337 foreach ($vali as $property => $valj) {
338 if (strncmp($property,"//",2)!==0) {
340 if ($sort_properties && preg_match('/^(\*|_|\/|-)(?!(ms|moz|o\b|xv|atsc|wap|khtml|webkit|ah|hp|ro|rim|tc)-)/', $property, $matches)) {
341 $invalid[$matches[1]][$property] = $valj;
343 $this->parser
->_add_token(PROPERTY
, $property, true);
344 $this->parser
->_add_token(VALUE
, $valj, true);
348 foreach ($invalid as $prefix => $props) {
349 foreach ($props as $property => $valj) {
350 $this->parser
->_add_token(PROPERTY
, $property, true);
351 $this->parser
->_add_token(VALUE
, $valj, true);
354 $this->parser
->_add_token(SEL_END
, $selector, true);
357 if (intval($medium) < DEFAULT_AT
) {
358 // un medium vide (contenant @font-face ou autre @) ne produit aucun conteneur
359 if (strlen(trim($medium))) {
360 $this->parser
->_add_token(AT_END
, $medium, true);
362 } elseif ($default_media) {
363 $this->parser
->_add_token(AT_END
, $default_media, true);
369 * Same as htmlspecialchars, only that chars are not replaced if $plain !== true. This makes print_code() cleaner.
370 * @param string $string
373 * @see csstidy_print::_print()
377 public function _htmlsp($string, $plain) {
379 return htmlspecialchars($string, ENT_QUOTES
, 'utf-8');
385 * Get compression ratio
390 public function get_ratio() {
391 if (!$this->output_css_plain
) {
394 return round((strlen($this->input_css
) - strlen($this->output_css_plain
)) / strlen($this->input_css
), 3) * 100;
398 * Get difference between the old and new code in bytes and prints the code if necessary.
403 public function get_diff() {
404 if (!$this->output_css_plain
) {
408 $diff = strlen($this->output_css_plain
) - strlen($this->input_css
);
412 } elseif ($diff == 0) {
420 * Get the size of either input or output CSS in KB
421 * @param string $loc default is "output"
426 public function size($loc = 'output') {
427 if ($loc === 'output' && !$this->output_css
) {
431 if ($loc === 'input') {
432 return (strlen($this->input_css
) / 1000);
434 return (strlen($this->output_css_plain
) / 1000);