[SPIP] ~v3.0.20-->v3.0.25
[lhc/web/clavette_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 $output .= $template[0] . '@import ' . $template[5] . $this->import[$i] . $template[6] . $template[13];
204 }
205 }
206
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');
212 }
213 $output .= $template[0] . '@namespace ' . $template[5] . $this->namespace . $template[6] . $template[13];
214 }
215
216 $in_at_out = '';
217 $out = & $output;
218
219 foreach ($this->tokens as $key => $token) {
220 switch ($token[0]) {
221 case AT_START:
222 $out .= $template[0] . $this->_htmlsp($token[1], $plain) . $template[1];
223 $out = & $in_at_out;
224 break;
225
226 case SEL_START:
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];
231 break;
232
233 case PROPERTY:
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]);
238 }
239 $out .= $template[4] . $this->_htmlsp($token[1], $plain) . ':' . $template[5];
240 break;
241
242 case VALUE:
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]);
246 } else {
247 $out .= $template[6];
248 }
249 break;
250
251 case SEL_END:
252 $out .= $template[7];
253 if ($this->_seeknocomment($key, 1) != AT_END)
254 $out .= $template[8];
255 break;
256
257 case AT_END:
258 $out = & $output;
259 $in_at_out = str_replace("\n\n", "\r\n", $in_at_out); // don't fill empty lines
260 $in_at_out = str_replace("\n", "\n" . $template[10], $in_at_out);
261 $in_at_out = str_replace("\r\n", "\n\n", $in_at_out);
262 $out .= $template[10] . $in_at_out . $template[9];
263 $in_at_out = '';
264 break;
265
266 case COMMENT:
267 $out .= $template[11] . '/*' . $this->_htmlsp($token[1], $plain) . '*/' . $template[12];
268 break;
269 }
270 }
271
272 $output = trim($output);
273
274 if (!$plain) {
275 $this->output_css = $output;
276 $this->_print(true);
277 } else {
278 // If using spaces in the template, don't want these to appear in the plain output
279 $this->output_css_plain = str_replace('&#160;', '', $output);
280 }
281 }
282
283 /**
284 * Gets the next token type which is $move away from $key, excluding comments
285 * @param integer $key current position
286 * @param integer $move move this far
287 * @return mixed a token type
288 * @access private
289 * @version 1.0
290 */
291 public function _seeknocomment($key, $move) {
292 $go = ($move > 0) ? 1 : -1;
293 for ($i = $key + 1; abs($key - $i) - 1 < abs($move); $i += $go) {
294 if (!isset($this->tokens[$i])) {
295 return;
296 }
297 if ($this->tokens[$i][0] == COMMENT) {
298 $move += 1;
299 continue;
300 }
301 return $this->tokens[$i][0];
302 }
303 }
304
305 /**
306 * Converts $this->css array to a raw array ($this->tokens)
307 * @param string $default_media default @media to add to selectors without any @media
308 * @access private
309 * @version 1.0
310 */
311 public function _convert_raw_css($default_media='') {
312 $this->tokens = array();
313 $sort_selectors = $this->parser->get_cfg('sort_selectors');
314 $sort_properties = $this->parser->get_cfg('sort_properties');
315
316 foreach ($this->css as $medium => $val) {
317 if ($sort_selectors)
318 ksort($val);
319 if (intval($medium) < DEFAULT_AT) {
320 // un medium vide (contenant @font-face ou autre @) ne produit aucun conteneur
321 if (strlen(trim($medium))) {
322 $this->parser->_add_token(AT_START, $medium, true);
323 }
324 } elseif ($default_media) {
325 $this->parser->_add_token(AT_START, $default_media, true);
326 }
327
328 foreach ($val as $selector => $vali) {
329 if ($sort_properties)
330 ksort($vali);
331 $this->parser->_add_token(SEL_START, $selector, true);
332
333 $invalid = array(
334 '*' => array(), // IE7 hacks first
335 '_' => array(), // IE6 hacks
336 '/' => array(), // IE6 hacks
337 '-' => array() // IE6 hacks
338 );
339 foreach ($vali as $property => $valj) {
340 if (strncmp($property,"//",2)!==0) {
341 $matches = array();
342 if ($sort_properties && preg_match('/^(\*|_|\/|-)(?!(ms|moz|o\b|xv|atsc|wap|khtml|webkit|ah|hp|ro|rim|tc)-)/', $property, $matches)) {
343 $invalid[$matches[1]][$property] = $valj;
344 } else {
345 $this->parser->_add_token(PROPERTY, $property, true);
346 $this->parser->_add_token(VALUE, $valj, true);
347 }
348 }
349 }
350 foreach ($invalid as $prefix => $props) {
351 foreach ($props as $property => $valj) {
352 $this->parser->_add_token(PROPERTY, $property, true);
353 $this->parser->_add_token(VALUE, $valj, true);
354 }
355 }
356 $this->parser->_add_token(SEL_END, $selector, true);
357 }
358
359 if (intval($medium) < DEFAULT_AT) {
360 // un medium vide (contenant @font-face ou autre @) ne produit aucun conteneur
361 if (strlen(trim($medium))) {
362 $this->parser->_add_token(AT_END, $medium, true);
363 }
364 } elseif ($default_media) {
365 $this->parser->_add_token(AT_END, $default_media, true);
366 }
367 }
368 }
369
370 /**
371 * Same as htmlspecialchars, only that chars are not replaced if $plain !== true. This makes print_code() cleaner.
372 * @param string $string
373 * @param bool $plain
374 * @return string
375 * @see csstidy_print::_print()
376 * @access private
377 * @version 1.0
378 */
379 public function _htmlsp($string, $plain) {
380 if (!$plain) {
381 return htmlspecialchars($string, ENT_QUOTES, 'utf-8');
382 }
383 return $string;
384 }
385
386 /**
387 * Get compression ratio
388 * @access public
389 * @return float
390 * @version 1.2
391 */
392 public function get_ratio() {
393 if (!$this->output_css_plain) {
394 $this->formatted();
395 }
396 return round((strlen($this->input_css) - strlen($this->output_css_plain)) / strlen($this->input_css), 3) * 100;
397 }
398
399 /**
400 * Get difference between the old and new code in bytes and prints the code if necessary.
401 * @access public
402 * @return string
403 * @version 1.1
404 */
405 public function get_diff() {
406 if (!$this->output_css_plain) {
407 $this->formatted();
408 }
409
410 $diff = strlen($this->output_css_plain) - strlen($this->input_css);
411
412 if ($diff > 0) {
413 return '+' . $diff;
414 } elseif ($diff == 0) {
415 return '+-' . $diff;
416 }
417
418 return $diff;
419 }
420
421 /**
422 * Get the size of either input or output CSS in KB
423 * @param string $loc default is "output"
424 * @access public
425 * @return integer
426 * @version 1.0
427 */
428 public function size($loc = 'output') {
429 if ($loc === 'output' && !$this->output_css) {
430 $this->formatted();
431 }
432
433 if ($loc === 'input') {
434 return (strlen($this->input_css) / 1000);
435 } else {
436 return (strlen($this->output_css_plain) / 1000);
437 }
438 }
439
440 }