9897fbd1687b414ad662b09c45c202041864c445
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
45 * Saves the input CSS string
51 * Saves the formatted CSS string
57 * Saves the formatted CSS string (plain text)
61 var $output_css_plain = '';
65 * @param array $css contains the class csstidy
69 function csstidy_print(&$css) {
70 $this->parser
= & $css;
71 $this->css
= & $css->css
;
72 $this->template
= & $css->template
;
73 $this->tokens
= & $css->tokens
;
74 $this->charset
= & $css->charset
;
75 $this->import
= & $css->import
;
76 $this->namespace = & $css->namespace;
80 * Resets output_css and output_css_plain (new css code)
85 $this->output_css
= '';
86 $this->output_css_plain
= '';
90 * Returns the CSS code as plain text
91 * @param string $default_media default @media to add to selectors without any @media
96 function plain($default_media='') {
97 $this->_print(true, $default_media);
98 return $this->output_css_plain
;
102 * Returns the formatted CSS code
103 * @param string $default_media default @media to add to selectors without any @media
108 function formatted($default_media='') {
109 $this->_print(false, $default_media);
110 return $this->output_css
;
114 * Returns the formatted CSS code to make a complete webpage
115 * @param string $doctype shorthand for the document type
116 * @param bool $externalcss indicates whether styles to be attached internally or as an external stylesheet
117 * @param string $title title to be added in the head of the document
118 * @param string $lang two-letter language code to be added to the output
123 function formatted_page($doctype='xhtml1.1', $externalcss=true, $title='', $lang='en') {
125 case 'xhtml1.0strict':
126 $doctype_output = '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
127 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">';
131 $doctype_output = '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
132 "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">';
136 $output = $cssparsed = '';
137 $this->output_css_plain
= & $output;
139 $output .= $doctype_output . "\n" . '<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="' . $lang . '"';
140 $output .= ( $doctype === 'xhtml1.1') ?
'>' : ' lang="' . $lang . '">';
141 $output .= "\n<head>\n <title>$title</title>";
144 $output .= "\n <style type=\"text/css\">\n";
145 $cssparsed = file_get_contents('cssparsed.css');
146 $output .= $cssparsed; // Adds an invisible BOM or something, but not in css_optimised.php
147 $output .= "\n</style>";
149 $output .= "\n" . ' <link rel="stylesheet" type="text/css" href="cssparsed.css" />';
152 $output .= "\n</head>\n<body><code id=\"copytext\">";
153 $output .= $this->formatted();
154 $output .= '</code>' . "\n" . '</body></html>';
155 return $this->output_css_plain
;
159 * Returns the formatted CSS Code and saves it into $this->output_css and $this->output_css_plain
160 * @param bool $plain plain text or not
161 * @param string $default_media default @media to add to selectors without any @media
165 function _print($plain = false, $default_media='') {
166 if ($this->output_css
&& $this->output_css_plain
) {
171 if (!$this->parser
->get_cfg('preserve_css')) {
172 $this->_convert_raw_css($default_media);
175 $template = & $this->template
;
178 $template = array_map('strip_tags', $template);
181 if ($this->parser
->get_cfg('timestamp')) {
182 array_unshift($this->tokens
, array(COMMENT
, ' CSSTidy ' . $this->parser
->version
. ': ' . date('r') . ' '));
185 if (!empty($this->charset
)) {
186 $output .= $template[0] . '@charset ' . $template[5] . $this->charset
. $template[6] . $template[13];
189 if (!empty($this->import
)) {
190 for ($i = 0, $size = count($this->import
); $i < $size; $i++
) {
191 if (substr($this->import
[$i], 0, 4) === 'url(' && substr($this->import
[$i], -1, 1) === ')') {
192 $this->import
[$i] = '"' . substr($this->import
[$i], 4, -1) . '"';
193 $this->parser
->log('Optimised @import : Removed "url("', 'Information');
195 $output .= $template[0] . '@import ' . $template[5] . $this->import
[$i] . $template[6] . $template[13];
199 if (!empty($this->namespace)) {
200 if (($p=strpos($this->namespace,"url("))!==false && substr($this->namespace, -1, 1) === ')') {
201 $this->namespace = substr_replace($this->namespace,'"',$p,4);
202 $this->namespace = substr($this->namespace, 0, -1) . '"';
203 $this->parser
->log('Optimised @namespace : Removed "url("', 'Information');
205 $output .= $template[0] . '@namespace ' . $template[5] . $this->namespace . $template[6] . $template[13];
211 foreach ($this->tokens
as $key => $token) {
214 $out .= $template[0] . $this->_htmlsp($token[1], $plain) . $template[1];
219 if ($this->parser
->get_cfg('lowercase_s'))
220 $token[1] = strtolower($token[1]);
221 $out .= ( $token[1]{0} !== '@') ?
$template[2] . $this->_htmlsp($token[1], $plain) : $template[0] . $this->_htmlsp($token[1], $plain);
222 $out .= $template[3];
226 if ($this->parser
->get_cfg('case_properties') === 2) {
227 $token[1] = strtoupper($token[1]);
228 } elseif ($this->parser
->get_cfg('case_properties') === 1) {
229 $token[1] = strtolower($token[1]);
231 $out .= $template[4] . $this->_htmlsp($token[1], $plain) . ':' . $template[5];
235 $out .= $this->_htmlsp($token[1], $plain);
236 if ($this->_seeknocomment($key, 1) == SEL_END
&& $this->parser
->get_cfg('remove_last_;')) {
237 $out .= str_replace(';', '', $template[6]);
239 $out .= $template[6];
244 $out .= $template[7];
245 if ($this->_seeknocomment($key, 1) != AT_END
)
246 $out .= $template[8];
251 $out .= $template[10] . str_replace("\n", "\n" . $template[10], $in_at_out);
253 $out .= $template[9];
257 $out .= $template[11] . '/*' . $this->_htmlsp($token[1], $plain) . '*/' . $template[12];
262 $output = trim($output);
265 $this->output_css
= $output;
268 // If using spaces in the template, don't want these to appear in the plain output
269 $this->output_css_plain
= str_replace(' ', '', $output);
274 * Gets the next token type which is $move away from $key, excluding comments
275 * @param integer $key current position
276 * @param integer $move move this far
277 * @return mixed a token type
281 function _seeknocomment($key, $move) {
282 $go = ($move > 0) ?
1 : -1;
283 for ($i = $key +
1; abs($key - $i) - 1 < abs($move); $i +
= $go) {
284 if (!isset($this->tokens
[$i])) {
287 if ($this->tokens
[$i][0] == COMMENT
) {
291 return $this->tokens
[$i][0];
296 * Converts $this->css array to a raw array ($this->tokens)
297 * @param string $default_media default @media to add to selectors without any @media
301 function _convert_raw_css($default_media='') {
302 $this->tokens
= array();
303 $sort_selectors = $this->parser
->get_cfg('sort_selectors');
304 $sort_properties = $this->parser
->get_cfg('sort_properties');
306 foreach ($this->css
as $medium => $val) {
309 if (intval($medium) < DEFAULT_AT
) {
310 // un medium vide (contenant @font-face ou autre @) ne produit aucun conteneur
311 if (strlen(trim($medium))){
312 $this->parser
->_add_token(AT_START
, $medium, true);
315 elseif ($default_media) {
316 $this->parser
->_add_token(AT_START
, $default_media, true);
319 foreach ($val as $selector => $vali) {
320 if ($sort_properties)
322 $this->parser
->_add_token(SEL_START
, $selector, true);
325 '*' => array(), // IE7 hacks first
326 '_' => array(), // IE6 hacks
327 '/' => array(), // IE6 hacks
328 '-' => array() // IE6 hacks
330 foreach ($vali as $property => $valj) {
331 if (strncmp($property,"//",2)!==0){
333 if ($sort_properties && preg_match('/^(\*|_|\/|-)(?!(ms|moz|o\b|xv|atsc|wap|khtml|webkit|ah|hp|ro|rim|tc)-)/', $property, $matches)) {
334 $invalid[$matches[1]][$property] = $valj;
336 $this->parser
->_add_token(PROPERTY
, $property, true);
337 $this->parser
->_add_token(VALUE
, $valj, true);
341 foreach ($invalid as $prefix => $props) {
342 foreach ($props as $property => $valj) {
343 $this->parser
->_add_token(PROPERTY
, $property, true);
344 $this->parser
->_add_token(VALUE
, $valj, true);
347 $this->parser
->_add_token(SEL_END
, $selector, true);
350 if (intval($medium) < DEFAULT_AT
) {
351 // un medium vide (contenant @font-face ou autre @) ne produit aucun conteneur
352 if (strlen(trim($medium))){
353 $this->parser
->_add_token(AT_END
, $medium, true);
356 elseif ($default_media) {
357 $this->parser
->_add_token(AT_END
, $default_media, true);
363 * Same as htmlspecialchars, only that chars are not replaced if $plain !== true. This makes print_code() cleaner.
364 * @param string $string
367 * @see csstidy_print::_print()
371 function _htmlsp($string, $plain) {
373 return htmlspecialchars($string, ENT_QUOTES
, 'utf-8');
379 * Get compression ratio
384 function get_ratio() {
385 if (!$this->output_css_plain
) {
388 return round((strlen($this->input_css
) - strlen($this->output_css_plain
)) / strlen($this->input_css
), 3) * 100;
392 * Get difference between the old and new code in bytes and prints the code if necessary.
397 function get_diff() {
398 if (!$this->output_css_plain
) {
402 $diff = strlen($this->output_css_plain
) - strlen($this->input_css
);
406 } elseif ($diff == 0) {
414 * Get the size of either input or output CSS in KB
415 * @param string $loc default is "output"
420 function size($loc = 'output') {
421 if ($loc === 'output' && !$this->output_css
) {
425 if ($loc === 'input') {
426 return (strlen($this->input_css
) / 1000);
428 return (strlen($this->output_css_plain
) / 1000);