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 */ 42class 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 public $css; 70 public $template; 71 public $tokens; 72 public $charset; 73 public $import; 74 public $namespace; 75 76 /** 77 * Constructor 78 * @param array $css contains the class csstidy 79 * @access private 80 * @version 1.0 81 */ 82 public function __construct($css) { 83 $this->parser = $css; 84 $this->css = & $css->css; 85 $this->template = & $css->template; 86 $this->tokens = & $css->tokens; 87 $this->charset = & $css->charset; 88 $this->import = & $css->import; 89 $this->namespace = & $css->namespace; 90 } 91 92 /** 93 * Resets output_css and output_css_plain (new css code) 94 * @access private 95 * @version 1.0 96 */ 97 public function _reset() { 98 $this->output_css = ''; 99 $this->output_css_plain = ''; 100 } 101 102 /** 103 * Returns the CSS code as plain text 104 * @param string $default_media default @media to add to selectors without any @media 105 * @return string 106 * @access public 107 * @version 1.0 108 */ 109 public function plain($default_media='') { 110 $this->_print(true, $default_media); 111 return $this->output_css_plain; 112 } 113 114 /** 115 * Returns the formatted CSS code 116 * @param string $default_media default @media to add to selectors without any @media 117 * @return string 118 * @access public 119 * @version 1.0 120 */ 121 public function formatted($default_media='') { 122 $this->_print(false, $default_media); 123 return $this->output_css; 124 } 125 126 /** 127 * Returns the formatted CSS code to make a complete webpage 128 * @param string $doctype shorthand for the document type 129 * @param bool $externalcss indicates whether styles to be attached internally or as an external stylesheet 130 * @param string $title title to be added in the head of the document 131 * @param string $lang two-letter language code to be added to the output 132 * @return string 133 * @access public 134 * @version 1.4 135 */ 136 public function formatted_page($doctype='html5', $externalcss=true, $title='', $lang='en') { 137 switch ($doctype) { 138 case 'html5': 139 $doctype_output = '<!DOCTYPE html>'; 140 break; 141 case 'xhtml1.0strict': 142 $doctype_output = '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" 143 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">'; 144 break; 145 case 'xhtml1.1': 146 default: 147 $doctype_output = '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" 148 "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">'; 149 break; 150 } 151 152 $output = $cssparsed = ''; 153 $this->output_css_plain = & $output; 154 155 $output .= $doctype_output . "\n" . '<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="' . $lang . '"'; 156 $output .= ( $doctype === 'xhtml1.1') ? '>' : ' lang="' . $lang . '">'; 157 $output .= "\n<head>\n <title>$title</title>"; 158 159 if ($externalcss) { 160 $output .= "\n <style type=\"text/css\">\n"; 161 $cssparsed = file_get_contents('cssparsed.css'); 162 $output .= $cssparsed; // Adds an invisible BOM or something, but not in css_optimised.php 163 $output .= "\n</style>"; 164 } else { 165 $output .= "\n" . ' <link rel="stylesheet" type="text/css" href="cssparsed.css" />'; 166 } 167 $output .= "\n</head>\n<body><code id=\"copytext\">"; 168 $output .= $this->formatted(); 169 $output .= '</code>' . "\n" . '</body></html>'; 170 return $this->output_css_plain; 171 } 172 173 /** 174 * Returns the formatted CSS Code and saves it into $this->output_css and $this->output_css_plain 175 * @param bool $plain plain text or not 176 * @param string $default_media default @media to add to selectors without any @media 177 * @access private 178 * @version 2.0 179 */ 180 public function _print($plain = false, $default_media='') { 181 if ($this->output_css && $this->output_css_plain) { 182 return; 183 } 184 185 $output = ''; 186 if (!$this->parser->get_cfg('preserve_css')) { 187 $this->_convert_raw_css($default_media); 188 } 189 190 $template = & $this->template; 191 192 if ($plain) { 193 $template = array_map('strip_tags', $template); 194 } 195 196 if ($this->parser->get_cfg('timestamp')) { 197 array_unshift($this->tokens, array(COMMENT, ' CSSTidy ' . $this->parser->version . ': ' . date('r') . ' ')); 198 } 199 200 if (!empty($this->charset)) { 201 $output .= $template[0] . '@charset ' . $template[5] . $this->charset . $template[6] . $template[13]; 202 } 203 204 if (!empty($this->import)) { 205 for ($i = 0, $size = count($this->import); $i < $size; $i++) { 206 if (substr($this->import[$i], 0, 4) === 'url(' && substr($this->import[$i], -1, 1) === ')') { 207 $this->import[$i] = '"' . substr($this->import[$i], 4, -1) . '"'; 208 $this->parser->log('Optimised @import : Removed "url("', 'Information'); 209 } 210 else if (!preg_match('/^".+"$/',$this->import[$i])) { 211 // fixes a bug for @import ".." instead of the expected @import url("..") 212 // If it comes in due to @import ".." the "" will be missing and the output will become @import .. (which is an error) 213 $this->import[$i] = '"' . $this->import[$i] . '"'; 214 } 215 216 $output .= $template[0] . '@import ' . $template[5] . $this->import[$i] . $template[6] . $template[13]; 217 } 218 } 219 220 if (!empty($this->namespace)) { 221 if (($p=strpos($this->namespace,"url("))!==false && substr($this->namespace, -1, 1) === ')') { 222 $this->namespace = substr_replace($this->namespace,'"',$p,4); 223 $this->namespace = substr($this->namespace, 0, -1) . '"'; 224 $this->parser->log('Optimised @namespace : Removed "url("', 'Information'); 225 } 226 $output .= $template[0] . '@namespace ' . $template[5] . $this->namespace . $template[6] . $template[13]; 227 } 228 229 $in_at_out = []; 230 $out = & $output; 231 $indent_level = 0; 232 233 foreach ($this->tokens as $key => $token) { 234 switch ($token[0]) { 235 case AT_START: 236 $out .= $template[0] . $this->_htmlsp($token[1], $plain) . $template[1]; 237 $indent_level++; 238 if (!isset($in_at_out[$indent_level])) { 239 $in_at_out[$indent_level] = ''; 240 } 241 $out = & $in_at_out[$indent_level]; 242 break; 243 244 case SEL_START: 245 if ($this->parser->get_cfg('lowercase_s')) 246 $token[1] = strtolower($token[1]); 247 $out .= ( $token[1][0] !== '@') ? $template[2] . $this->_htmlsp($token[1], $plain) : $template[0] . $this->_htmlsp($token[1], $plain); 248 $out .= $template[3]; 249 break; 250 251 case PROPERTY: 252 if ($this->parser->get_cfg('case_properties') === 2) { 253 $token[1] = strtoupper($token[1]); 254 } elseif ($this->parser->get_cfg('case_properties') === 1) { 255 $token[1] = strtolower($token[1]); 256 } 257 $out .= $template[4] . $this->_htmlsp($token[1], $plain) . ':' . $template[5]; 258 break; 259 260 case VALUE: 261 $out .= $this->_htmlsp($token[1], $plain); 262 if ($this->_seeknocomment($key, 1) == SEL_END && $this->parser->get_cfg('remove_last_;')) { 263 $out .= str_replace(';', '', $template[6]); 264 } else { 265 $out .= $template[6]; 266 } 267 break; 268 269 case SEL_END: 270 $out .= $template[7]; 271 if ($this->_seeknocomment($key, 1) != AT_END) 272 $out .= $template[8]; 273 break; 274 275 case AT_END: 276 if (strlen($template[10])) { 277 // indent the bloc we are closing 278 $out = str_replace("\n\n", "\r\n", $out); // don't fill empty lines 279 $out = str_replace("\n", "\n" . $template[10], $out); 280 $out = str_replace("\r\n", "\n\n", $out); 281 } 282 if ($indent_level > 1) { 283 $out = & $in_at_out[$indent_level-1]; 284 } 285 else { 286 $out = & $output; 287 } 288 $out .= $template[10] . $in_at_out[$indent_level]; 289 if ($this->_seeknocomment($key, 1) != AT_END) { 290 $out .= $template[9]; 291 } 292 else { 293 $out .= rtrim($template[9]); 294 } 295 296 unset($in_at_out[$indent_level]); 297 $indent_level--; 298 break; 299 300 case IMPORTANT_COMMENT: 301 case COMMENT: 302 $out .= $template[11] . '/*' . $this->_htmlsp($token[1], $plain) . '*/' . $template[12]; 303 break; 304 } 305 } 306 307 $output = trim($output); 308 309 if (!$plain) { 310 $this->output_css = $output; 311 $this->_print(true); 312 } else { 313 // If using spaces in the template, don't want these to appear in the plain output 314 $this->output_css_plain = str_replace(' ', '', $output); 315 } 316 } 317 318 /** 319 * Gets the next token type which is $move away from $key, excluding comments 320 * @param integer $key current position 321 * @param integer $move move this far 322 * @return mixed a token type 323 * @access private 324 * @version 1.0 325 */ 326 public function _seeknocomment($key, $move) { 327 $go = ($move > 0) ? 1 : -1; 328 for ($i = $key + 1; abs($key - $i) - 1 < abs($move); $i += $go) { 329 if (!isset($this->tokens[$i])) { 330 return; 331 } 332 if ($this->tokens[$i][0] == COMMENT) { 333 $move += 1; 334 continue; 335 } 336 return $this->tokens[$i][0]; 337 } 338 } 339 340 /** 341 * Converts $this->css array to a raw array ($this->tokens) 342 * @param string $default_media default @media to add to selectors without any @media 343 * @access private 344 * @version 1.0 345 */ 346 public function _convert_raw_css($default_media='') { 347 $this->tokens = array(); 348 $sort_selectors = $this->parser->get_cfg('sort_selectors'); 349 $sort_properties = $this->parser->get_cfg('sort_properties'); 350 351 // important comment section ? 352 if (isset($this->css['!'])) { 353 $this->parser->_add_token(IMPORTANT_COMMENT, rtrim($this->css['!']), true); 354 unset($this->css['!']); 355 } 356 357 foreach ($this->css as $medium => $val) { 358 if ($sort_selectors) 359 ksort($val); 360 if (intval($medium) < DEFAULT_AT) { 361 // un medium vide (contenant @font-face ou autre @) ne produit aucun conteneur 362 if (strlen(trim($medium))) { 363 $parts_to_open = explode('{', $medium); 364 foreach ($parts_to_open as $part) { 365 $this->parser->_add_token(AT_START, $part, true); 366 } 367 } 368 } elseif ($default_media) { 369 $this->parser->_add_token(AT_START, $default_media, true); 370 } 371 372 foreach ($val as $selector => $vali) { 373 if ($sort_properties) 374 ksort($vali); 375 $this->parser->_add_token(SEL_START, $selector, true); 376 377 $invalid = array( 378 '*' => array(), // IE7 hacks first 379 '_' => array(), // IE6 hacks 380 '/' => array(), // IE6 hacks 381 '-' => array() // IE6 hacks 382 ); 383 foreach ($vali as $property => $valj) { 384 if (strncmp($property,"//",2)!==0) { 385 $matches = array(); 386 if ($sort_properties && preg_match('/^(\*|_|\/|-)(?!(ms|moz|o\b|xv|atsc|wap|khtml|webkit|ah|hp|ro|rim|tc)-)/', $property, $matches)) { 387 $invalid[$matches[1]][$property] = $valj; 388 } else { 389 $this->parser->_add_token(PROPERTY, $property, true); 390 $this->parser->_add_token(VALUE, $valj, true); 391 } 392 } 393 } 394 foreach ($invalid as $prefix => $props) { 395 foreach ($props as $property => $valj) { 396 $this->parser->_add_token(PROPERTY, $property, true); 397 $this->parser->_add_token(VALUE, $valj, true); 398 } 399 } 400 $this->parser->_add_token(SEL_END, $selector, true); 401 } 402 403 if (intval($medium) < DEFAULT_AT) { 404 // un medium vide (contenant @font-face ou autre @) ne produit aucun conteneur 405 if (strlen(trim($medium))) { 406 $parts_to_close = explode('{', $medium); 407 foreach (array_reverse($parts_to_close) as $part) { 408 $this->parser->_add_token(AT_END, $part, true); 409 } 410 } 411 } elseif ($default_media) { 412 $this->parser->_add_token(AT_END, $default_media, true); 413 } 414 } 415 } 416 417 /** 418 * Same as htmlspecialchars, only that chars are not replaced if $plain !== true. This makes print_code() cleaner. 419 * @param string $string 420 * @param bool $plain 421 * @return string 422 * @see csstidy_print::_print() 423 * @access private 424 * @version 1.0 425 */ 426 public function _htmlsp($string, $plain) { 427 if (!$plain) { 428 return htmlspecialchars($string, ENT_QUOTES, 'utf-8'); 429 } 430 return $string; 431 } 432 433 /** 434 * Get compression ratio 435 * @access public 436 * @return float 437 * @version 1.2 438 */ 439 public function get_ratio() { 440 if (!$this->output_css_plain) { 441 $this->formatted(); 442 } 443 return round((strlen($this->input_css) - strlen($this->output_css_plain)) / strlen($this->input_css), 3) * 100; 444 } 445 446 /** 447 * Get difference between the old and new code in bytes and prints the code if necessary. 448 * @access public 449 * @return string 450 * @version 1.1 451 */ 452 public function get_diff() { 453 if (!$this->output_css_plain) { 454 $this->formatted(); 455 } 456 457 $diff = strlen($this->output_css_plain) - strlen($this->input_css); 458 459 if ($diff > 0) { 460 return '+' . $diff; 461 } elseif ($diff == 0) { 462 return '+-' . $diff; 463 } 464 465 return $diff; 466 } 467 468 /** 469 * Get the size of either input or output CSS in KB 470 * @param string $loc default is "output" 471 * @access public 472 * @return integer 473 * @version 1.0 474 */ 475 public function size($loc = 'output') { 476 if ($loc === 'output' && !$this->output_css) { 477 $this->formatted(); 478 } 479 480 if ($loc === 'input') { 481 return (strlen($this->input_css) / 1000); 482 } else { 483 return (strlen($this->output_css_plain) / 1000); 484 } 485 } 486 487} 488