762b26eedd6a7a966782e81791eff8089877992d
[lhc/web/www.git] / www / plugins-dist / compresseur / lib / csstidy / class.csstidy_print.php
1 <?php
2
3 /**
4 * CSSTidy - CSS Parser and Optimiser
5 *
6 * CSS Printing class
7 * This class prints CSS data generated by csstidy.
8 *
9 * Copyright 2005, 2006, 2007 Florian Schmitz
10 *
11 * This file is part of CSSTidy.
12 *
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.
17 *
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.
22 *
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/>.
25 *
26 * @license http://opensource.org/licenses/lgpl-license.php GNU Lesser General Public License
27 * @package csstidy
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
31 */
32
33 /**
34 * CSS Printing class
35 *
36 * This class prints CSS data generated by csstidy.
37 *
38 * @package csstidy
39 * @author Florian Schmitz (floele at gmail dot com) 2005-2006
40 * @version 1.1.0
41 */
42 class csstidy_print {
43
44 /**
45 * csstidy object
46 * @var object
47 */
48 public $parser;
49
50 /**
51 * Saves the input CSS string
52 * @var string
53 * @access private
54 */
55 public $input_css = '';
56 /**
57 * Saves the formatted CSS string
58 * @var string
59 * @access public
60 */
61 public $output_css = '';
62 /**
63 * Saves the formatted CSS string (plain text)
64 * @var string
65 * @access public
66 */
67 public $output_css_plain = '';
68
69 /**
70 * Constructor
71 * @param array $css contains the class csstidy
72 * @access private
73 * @version 1.0
74 */
75 public function __construct($css) {
76 $this->parser = $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;
83 }
84
85 /**
86 * Resets output_css and output_css_plain (new css code)
87 * @access private
88 * @version 1.0
89 */
90 public function _reset() {
91 $this->output_css = '';
92 $this->output_css_plain = '';
93 }
94
95 /**
96 * Returns the CSS code as plain text
97 * @param string $default_media default @media to add to selectors without any @media
98 * @return string
99 * @access public
100 * @version 1.0
101 */
102 public function plain($default_media='') {
103 $this->_print(true, $default_media);
104 return $this->output_css_plain;
105 }
106
107 /**
108 * Returns the formatted CSS code
109 * @param string $default_media default @media to add to selectors without any @media
110 * @return string
111 * @access public
112 * @version 1.0
113 */
114 public function formatted($default_media='') {
115 $this->_print(false, $default_media);
116 return $this->output_css;
117 }
118
119 /**
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
125 * @return string
126 * @access public
127 * @version 1.4
128 */
129 public function formatted_page($doctype='html5', $externalcss=true, $title='', $lang='en') {
130 switch ($doctype) {
131 case 'html5':
132 $doctype_output = '<!DOCTYPE html>';
133 break;
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">';
137 break;
138 case 'xhtml1.1':
139 default:
140 $doctype_output = '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
141 "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">';
142 break;
143 }
144
145 $output = $cssparsed = '';
146 $this->output_css_plain = & $output;
147
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>";
151
152 if ($externalcss) {
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>";
157 } else {
158 $output .= "\n" . ' <link rel="stylesheet" type="text/css" href="cssparsed.css" />';
159 }
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;
164 }
165
166 /**
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
170 * @access private
171 * @version 2.0
172 */
173 public function _print($plain = false, $default_media='') {
174 if ($this->output_css && $this->output_css_plain) {
175 return;
176 }
177
178 $output = '';
179 if (!$this->parser->get_cfg('preserve_css')) {
180 $this->_convert_raw_css($default_media);
181 }
182
183 $template = & $this->template;
184
185 if ($plain) {
186 $template = array_map('strip_tags', $template);
187 }
188
189 if ($this->parser->get_cfg('timestamp')) {
190 array_unshift($this->tokens, array(COMMENT, ' CSSTidy ' . $this->parser->version . ': ' . date('r') . ' '));
191 }
192
193 if (!empty($this->charset)) {
194 $output .= $template[0] . '@charset ' . $template[5] . $this->charset . $template[6] . $template[13];
195 }
196
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');
202 }
203 else if (!preg_match('/^".+"$/',$this->import[$i])) {
204 // fixes a bug for @import ".." instead of the expected @import url("..")
205 // If it comes in due to @import ".." the "" will be missing and the output will become @import .. (which is an error)
206 $this->import[$i] = '"' . $this->import[$i] . '"';
207 }
208
209 $output .= $template[0] . '@import ' . $template[5] . $this->import[$i] . $template[6] . $template[13];
210 }
211 }
212
213 if (!empty($this->namespace)) {
214 if (($p=strpos($this->namespace,"url("))!==false && substr($this->namespace, -1, 1) === ')') {
215 $this->namespace = substr_replace($this->namespace,'"',$p,4);
216 $this->namespace = substr($this->namespace, 0, -1) . '"';
217 $this->parser->log('Optimised @namespace : Removed "url("', 'Information');
218 }
219 $output .= $template[0] . '@namespace ' . $template[5] . $this->namespace . $template[6] . $template[13];
220 }
221
222 $in_at_out = '';
223 $out = & $output;
224
225 foreach ($this->tokens as $key => $token) {
226 switch ($token[0]) {
227 case AT_START:
228 $out .= $template[0] . $this->_htmlsp($token[1], $plain) . $template[1];
229 $out = & $in_at_out;
230 break;
231
232 case SEL_START:
233 if ($this->parser->get_cfg('lowercase_s'))
234 $token[1] = strtolower($token[1]);
235 $out .= ( $token[1]{0} !== '@') ? $template[2] . $this->_htmlsp($token[1], $plain) : $template[0] . $this->_htmlsp($token[1], $plain);
236 $out .= $template[3];
237 break;
238
239 case PROPERTY:
240 if ($this->parser->get_cfg('case_properties') === 2) {
241 $token[1] = strtoupper($token[1]);
242 } elseif ($this->parser->get_cfg('case_properties') === 1) {
243 $token[1] = strtolower($token[1]);
244 }
245 $out .= $template[4] . $this->_htmlsp($token[1], $plain) . ':' . $template[5];
246 break;
247
248 case VALUE:
249 $out .= $this->_htmlsp($token[1], $plain);
250 if ($this->_seeknocomment($key, 1) == SEL_END && $this->parser->get_cfg('remove_last_;')) {
251 $out .= str_replace(';', '', $template[6]);
252 } else {
253 $out .= $template[6];
254 }
255 break;
256
257 case SEL_END:
258 $out .= $template[7];
259 if ($this->_seeknocomment($key, 1) != AT_END)
260 $out .= $template[8];
261 break;
262
263 case AT_END:
264 $out = & $output;
265 $in_at_out = str_replace("\n\n", "\r\n", $in_at_out); // don't fill empty lines
266 $in_at_out = str_replace("\n", "\n" . $template[10], $in_at_out);
267 $in_at_out = str_replace("\r\n", "\n\n", $in_at_out);
268 $out .= $template[10] . $in_at_out . $template[9];
269 $in_at_out = '';
270 break;
271
272 case COMMENT:
273 $out .= $template[11] . '/*' . $this->_htmlsp($token[1], $plain) . '*/' . $template[12];
274 break;
275 }
276 }
277
278 $output = trim($output);
279
280 if (!$plain) {
281 $this->output_css = $output;
282 $this->_print(true);
283 } else {
284 // If using spaces in the template, don't want these to appear in the plain output
285 $this->output_css_plain = str_replace('&#160;', '', $output);
286 }
287 }
288
289 /**
290 * Gets the next token type which is $move away from $key, excluding comments
291 * @param integer $key current position
292 * @param integer $move move this far
293 * @return mixed a token type
294 * @access private
295 * @version 1.0
296 */
297 public function _seeknocomment($key, $move) {
298 $go = ($move > 0) ? 1 : -1;
299 for ($i = $key + 1; abs($key - $i) - 1 < abs($move); $i += $go) {
300 if (!isset($this->tokens[$i])) {
301 return;
302 }
303 if ($this->tokens[$i][0] == COMMENT) {
304 $move += 1;
305 continue;
306 }
307 return $this->tokens[$i][0];
308 }
309 }
310
311 /**
312 * Converts $this->css array to a raw array ($this->tokens)
313 * @param string $default_media default @media to add to selectors without any @media
314 * @access private
315 * @version 1.0
316 */
317 public function _convert_raw_css($default_media='') {
318 $this->tokens = array();
319 $sort_selectors = $this->parser->get_cfg('sort_selectors');
320 $sort_properties = $this->parser->get_cfg('sort_properties');
321
322 foreach ($this->css as $medium => $val) {
323 if ($sort_selectors)
324 ksort($val);
325 if (intval($medium) < DEFAULT_AT) {
326 // un medium vide (contenant @font-face ou autre @) ne produit aucun conteneur
327 if (strlen(trim($medium))) {
328 $this->parser->_add_token(AT_START, $medium, true);
329 }
330 } elseif ($default_media) {
331 $this->parser->_add_token(AT_START, $default_media, true);
332 }
333
334 foreach ($val as $selector => $vali) {
335 if ($sort_properties)
336 ksort($vali);
337 $this->parser->_add_token(SEL_START, $selector, true);
338
339 $invalid = array(
340 '*' => array(), // IE7 hacks first
341 '_' => array(), // IE6 hacks
342 '/' => array(), // IE6 hacks
343 '-' => array() // IE6 hacks
344 );
345 foreach ($vali as $property => $valj) {
346 if (strncmp($property,"//",2)!==0) {
347 $matches = array();
348 if ($sort_properties && preg_match('/^(\*|_|\/|-)(?!(ms|moz|o\b|xv|atsc|wap|khtml|webkit|ah|hp|ro|rim|tc)-)/', $property, $matches)) {
349 $invalid[$matches[1]][$property] = $valj;
350 } else {
351 $this->parser->_add_token(PROPERTY, $property, true);
352 $this->parser->_add_token(VALUE, $valj, true);
353 }
354 }
355 }
356 foreach ($invalid as $prefix => $props) {
357 foreach ($props as $property => $valj) {
358 $this->parser->_add_token(PROPERTY, $property, true);
359 $this->parser->_add_token(VALUE, $valj, true);
360 }
361 }
362 $this->parser->_add_token(SEL_END, $selector, true);
363 }
364
365 if (intval($medium) < DEFAULT_AT) {
366 // un medium vide (contenant @font-face ou autre @) ne produit aucun conteneur
367 if (strlen(trim($medium))) {
368 $this->parser->_add_token(AT_END, $medium, true);
369 }
370 } elseif ($default_media) {
371 $this->parser->_add_token(AT_END, $default_media, true);
372 }
373 }
374 }
375
376 /**
377 * Same as htmlspecialchars, only that chars are not replaced if $plain !== true. This makes print_code() cleaner.
378 * @param string $string
379 * @param bool $plain
380 * @return string
381 * @see csstidy_print::_print()
382 * @access private
383 * @version 1.0
384 */
385 public function _htmlsp($string, $plain) {
386 if (!$plain) {
387 return htmlspecialchars($string, ENT_QUOTES, 'utf-8');
388 }
389 return $string;
390 }
391
392 /**
393 * Get compression ratio
394 * @access public
395 * @return float
396 * @version 1.2
397 */
398 public function get_ratio() {
399 if (!$this->output_css_plain) {
400 $this->formatted();
401 }
402 return round((strlen($this->input_css) - strlen($this->output_css_plain)) / strlen($this->input_css), 3) * 100;
403 }
404
405 /**
406 * Get difference between the old and new code in bytes and prints the code if necessary.
407 * @access public
408 * @return string
409 * @version 1.1
410 */
411 public function get_diff() {
412 if (!$this->output_css_plain) {
413 $this->formatted();
414 }
415
416 $diff = strlen($this->output_css_plain) - strlen($this->input_css);
417
418 if ($diff > 0) {
419 return '+' . $diff;
420 } elseif ($diff == 0) {
421 return '+-' . $diff;
422 }
423
424 return $diff;
425 }
426
427 /**
428 * Get the size of either input or output CSS in KB
429 * @param string $loc default is "output"
430 * @access public
431 * @return integer
432 * @version 1.0
433 */
434 public function size($loc = 'output') {
435 if ($loc === 'output' && !$this->output_css) {
436 $this->formatted();
437 }
438
439 if ($loc === 'input') {
440 return (strlen($this->input_css) / 1000);
441 } else {
442 return (strlen($this->output_css_plain) / 1000);
443 }
444 }
445
446 }