1<?php 2 3/** 4 * CSSTidy - CSS Parser and Optimiser 5 * 6 * CSS Parser class 7 * 8 * Copyright 2005, 2006, 2007 Florian Schmitz 9 * 10 * This file is part of CSSTidy. 11 * 12 * CSSTidy is free software; you can redistribute it and/or modify 13 * it under the terms of the GNU Lesser General Public License as published by 14 * the Free Software Foundation; either version 2.1 of the License, or 15 * (at your option) any later version. 16 * 17 * CSSTidy is distributed in the hope that it will be useful, 18 * but WITHOUT ANY WARRANTY; without even the implied warranty of 19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 20 * GNU Lesser General Public License for more details. 21 * 22 * You should have received a copy of the GNU Lesser General Public License 23 * along with this program. If not, see <http://www.gnu.org/licenses/>. 24 * 25 * @license http://opensource.org/licenses/lgpl-license.php GNU Lesser General Public License 26 * @package csstidy 27 * @author Florian Schmitz (floele at gmail dot com) 2005-2007 28 * @author Brett Zamir (brettz9 at yahoo dot com) 2007 29 * @author Nikolay Matsievsky (speed at webo dot name) 2009-2010 30 * @author Cedric Morin (cedric at yterium dot com) 2010-2012 31 * @author Christopher Finke (cfinke at gmail.com) 2012 32 * @author Mark Scherer (remove $GLOBALS once and for all + PHP5.4 comp) 2012 33 */ 34 35/** 36 * Defines constants 37 * @todo //TODO: make them class constants of csstidy 38 */ 39define('AT_START', 1); 40define('AT_END', 2); 41define('SEL_START', 3); 42define('SEL_END', 4); 43define('PROPERTY', 5); 44define('VALUE', 6); 45define('COMMENT', 7); 46define('IMPORTANT_COMMENT',8); 47define('DEFAULT_AT', 41); 48 49/** 50 * Contains a class for printing CSS code 51 * 52 * @version 1.1.0 53 */ 54require('class.csstidy_print.php'); 55 56/** 57 * Contains a class for optimising CSS code 58 * 59 * @version 1.0 60 */ 61require('class.csstidy_optimise.php'); 62 63/** 64 * CSS Parser class 65 * 66 * This class represents a CSS parser which reads CSS code and saves it in an array. 67 * In opposite to most other CSS parsers, it does not use regular expressions and 68 * thus has full CSS2 support and a higher reliability. 69 * Additional to that it applies some optimisations and fixes to the CSS code. 70 * An online version should be available here: http://cdburnerxp.se/cssparse/css_optimiser.php 71 * @package csstidy 72 * @author Florian Schmitz (floele at gmail dot com) 2005-2006 73 * @version 1.7.3 74 */ 75class csstidy { 76 77 /** 78 * Saves the parsed CSS. This array is empty if preserve_css is on. 79 * @var array 80 * @access public 81 */ 82 public $css = array(); 83 /** 84 * Saves the parsed CSS (raw) 85 * @var array 86 * @access private 87 */ 88 public $tokens = array(); 89 /** 90 * Printer class 91 * @see csstidy_print 92 * @var object 93 * @access public 94 */ 95 public $print; 96 /** 97 * Optimiser class 98 * @see csstidy_optimise 99 * @var object 100 * @access private 101 */ 102 public $optimise; 103 /** 104 * Saves the CSS charset (@charset) 105 * @var string 106 * @access private 107 */ 108 public $charset = ''; 109 /** 110 * Saves all @import URLs 111 * @var array 112 * @access private 113 */ 114 public $import = array(); 115 /** 116 * Saves the namespace 117 * @var string 118 * @access private 119 */ 120 public $namespace = ''; 121 /** 122 * Contains the version of csstidy 123 * @var string 124 * @access private 125 */ 126 public $version = '1.7.3'; 127 /** 128 * Stores the settings 129 * @var array 130 * @access private 131 */ 132 public $settings = array(); 133 /** 134 * Saves the parser-status. 135 * 136 * Possible values: 137 * - is = in selector 138 * - ip = in property 139 * - iv = in value 140 * - instr = in string (started at " or ' or ( ) 141 * - ic = in comment (ignore everything) 142 * - at = in @-block 143 * 144 * @var string 145 * @access private 146 */ 147 public $status = 'is'; 148 /** 149 * Saves the current at rule (@media) 150 * @var string 151 * @access private 152 */ 153 public $at = ''; 154 /** 155 * Saves the at rule for next selector (during @font-face or other @) 156 * @var string 157 * @access private 158 */ 159 public $next_selector_at = ''; 160 161 /** 162 * Saves the current selector 163 * @var string 164 * @access private 165 */ 166 public $selector = ''; 167 /** 168 * Saves the current property 169 * @var string 170 * @access private 171 */ 172 public $property = ''; 173 /** 174 * Saves the position of , in selectors 175 * @var array 176 * @access private 177 */ 178 public $sel_separate = array(); 179 /** 180 * Saves the current value 181 * @var string 182 * @access private 183 */ 184 public $value = ''; 185 /** 186 * Saves the current sub-value 187 * 188 * Example for a subvalue: 189 * background:url(foo.png) red no-repeat; 190 * "url(foo.png)", "red", and "no-repeat" are subvalues, 191 * seperated by whitespace 192 * @var string 193 * @access private 194 */ 195 public $sub_value = ''; 196 /** 197 * Array which saves all subvalues for a property. 198 * @var array 199 * @see sub_value 200 * @access private 201 */ 202 public $sub_value_arr = array(); 203 /** 204 * Saves the stack of characters that opened the current strings 205 * @var array 206 * @access private 207 */ 208 public $str_char = array(); 209 public $cur_string = array(); 210 /** 211 * Status from which the parser switched to ic or instr 212 * @var array 213 * @access private 214 */ 215 public $from = array(); 216 /** 217 /** 218 * =true if in invalid at-rule 219 * @var bool 220 * @access private 221 */ 222 public $invalid_at = false; 223 /** 224 * =true if something has been added to the current selector 225 * @var bool 226 * @access private 227 */ 228 public $added = false; 229 /** 230 * Array which saves the message log 231 * @var array 232 * @access private 233 */ 234 public $log = array(); 235 /** 236 * Saves the line number 237 * @var integer 238 * @access private 239 */ 240 public $line = 1; 241 /** 242 * Marks if we need to leave quotes for a string 243 * @var array 244 * @access private 245 */ 246 public $quoted_string = array(); 247 248 /** 249 * List of tokens 250 * @var string 251 */ 252 public $tokens_list = ""; 253 254 /** 255 * Various CSS Data for CSSTidy 256 * @var array 257 */ 258 public $data = array(); 259 260 /** 261 * Loads standard template and sets default settings 262 * @access private 263 * @version 1.3 264 */ 265 public function __construct() { 266 $data = array(); 267 include('data.inc.php'); 268 $this->data = $data; 269 270 $this->settings['remove_bslash'] = true; 271 $this->settings['compress_colors'] = true; 272 $this->settings['compress_font-weight'] = true; 273 $this->settings['lowercase_s'] = false; 274 /* 275 1 common shorthands optimization 276 2 + font property optimization 277 3 + background property optimization 278 */ 279 $this->settings['optimise_shorthands'] = 1; 280 $this->settings['remove_last_;'] = true; 281 $this->settings['space_before_important'] = false; 282 /* rewrite all properties with low case, better for later gzip OK, safe*/ 283 $this->settings['case_properties'] = 1; 284 /* sort properties in alpabetic order, better for later gzip 285 * but can cause trouble in case of overiding same propertie or using hack 286 */ 287 $this->settings['sort_properties'] = false; 288 /* 289 1, 3, 5, etc -- enable sorting selectors inside @media: a{}b{}c{} 290 2, 5, 8, etc -- enable sorting selectors inside one CSS declaration: a,b,c{} 291 preserve order by default cause it can break functionnality 292 */ 293 $this->settings['sort_selectors'] = 0; 294 /* is dangeroues to be used: CSS is broken sometimes */ 295 $this->settings['merge_selectors'] = 0; 296 /* preserve or not browser hacks */ 297 298 /* Useful to produce a rtl css from a ltr one (or the opposite) */ 299 $this->settings['reverse_left_and_right'] = 0; 300 301 $this->settings['discard_invalid_selectors'] = false; 302 $this->settings['discard_invalid_properties'] = false; 303 $this->settings['css_level'] = 'CSS3.0'; 304 $this->settings['preserve_css'] = false; 305 $this->settings['timestamp'] = false; 306 $this->settings['template'] = ''; // say that propertie exist 307 $this->set_cfg('template','default'); // call load_template 308 $this->optimise = new csstidy_optimise($this); 309 310 $this->tokens_list = & $this->data['csstidy']['tokens']; 311 } 312 313 /** 314 * Get the value of a setting. 315 * @param string $setting 316 * @access public 317 * @return mixed 318 * @version 1.0 319 */ 320 public function get_cfg($setting) { 321 if (isset($this->settings[$setting])) { 322 return $this->settings[$setting]; 323 } 324 return false; 325 } 326 327 /** 328 * Load a template 329 * @param string $template used by set_cfg to load a template via a configuration setting 330 * @access private 331 * @version 1.4 332 */ 333 public function _load_template($template) { 334 switch ($template) { 335 case 'default': 336 $this->load_template('default'); 337 break; 338 339 case 'highest': 340 $this->load_template('highest_compression'); 341 break; 342 343 case 'high': 344 $this->load_template('high_compression'); 345 break; 346 347 case 'low': 348 $this->load_template('low_compression'); 349 break; 350 351 default: 352 $this->load_template($template); 353 break; 354 } 355 } 356 357 /** 358 * Set the value of a setting. 359 * @param string $setting 360 * @param mixed $value 361 * @access public 362 * @return bool 363 * @version 1.0 364 */ 365 public function set_cfg($setting, $value=null) { 366 if (is_array($setting) && $value === null) { 367 foreach ($setting as $setprop => $setval) { 368 $this->settings[$setprop] = $setval; 369 } 370 if (array_key_exists('template', $setting)) { 371 $this->_load_template($this->settings['template']); 372 } 373 return true; 374 } elseif (isset($this->settings[$setting]) && $value !== '') { 375 $this->settings[$setting] = $value; 376 if ($setting === 'template') { 377 $this->_load_template($this->settings['template']); 378 } 379 return true; 380 } 381 return false; 382 } 383 384 /** 385 * Adds a token to $this->tokens 386 * @param mixed $type 387 * @param string $data 388 * @param bool $do add a token even if preserve_css is off 389 * @access private 390 * @version 1.0 391 */ 392 public function _add_token($type, $data, $do = false) { 393 if ($this->get_cfg('preserve_css') || $do) { 394 // nested @... : if opening a new part we just closed, remove the previous closing instead of adding opening 395 if ($type === AT_START 396 and count($this->tokens) 397 and $last = end($this->tokens) 398 and $last[0] === AT_END 399 and $last[1] === trim($data)) { 400 array_pop($this->tokens); 401 } 402 else { 403 $this->tokens[] = array($type, ($type == COMMENT or $type == IMPORTANT_COMMENT) ? $data : trim($data)); 404 } 405 } 406 } 407 408 /** 409 * Add a message to the message log 410 * @param string $message 411 * @param string $type 412 * @param integer $line 413 * @access private 414 * @version 1.0 415 */ 416 public function log($message, $type, $line = -1) { 417 if ($line === -1) { 418 $line = $this->line; 419 } 420 $line = intval($line); 421 $add = array('m' => $message, 't' => $type); 422 if (!isset($this->log[$line]) || !in_array($add, $this->log[$line])) { 423 $this->log[$line][] = $add; 424 } 425 } 426 427 /** 428 * Parse unicode notations and find a replacement character 429 * @param string $string 430 * @param integer $i 431 * @access private 432 * @return string 433 * @version 1.2 434 */ 435 public function _unicode(&$string, &$i) { 436 ++$i; 437 $add = ''; 438 $replaced = false; 439 440 while ($i < strlen($string) && (ctype_xdigit($string[$i]) || ctype_space($string[$i])) && strlen($add) < 6) { 441 $add .= $string[$i]; 442 443 if (ctype_space($string[$i])) { 444 break; 445 } 446 $i++; 447 } 448 449 if (hexdec($add) > 47 && hexdec($add) < 58 || hexdec($add) > 64 && hexdec($add) < 91 || hexdec($add) > 96 && hexdec($add) < 123) { 450 $this->log('Replaced unicode notation: Changed \\' . $add . ' to ' . chr(hexdec($add)), 'Information'); 451 $add = chr(hexdec($add)); 452 $replaced = true; 453 } else { 454 $add = trim('\\' . $add); 455 } 456 457 if (@ctype_xdigit($string[$i + 1]) && ctype_space($string[$i]) 458 && !$replaced || !ctype_space($string[$i])) { 459 $i--; 460 } 461 462 if ($add !== '\\' || !$this->get_cfg('remove_bslash') || strpos($this->tokens_list, $string[$i + 1]) !== false) { 463 return $add; 464 } 465 466 if ($add === '\\') { 467 $this->log('Removed unnecessary backslash', 'Information'); 468 } 469 return ''; 470 } 471 472 /** 473 * Write formatted output to a file 474 * @param string $filename 475 * @param string $doctype when printing formatted, is a shorthand for the document type 476 * @param bool $externalcss when printing formatted, indicates whether styles to be attached internally or as an external stylesheet 477 * @param string $title when printing formatted, is the title to be added in the head of the document 478 * @param string $lang when printing formatted, gives a two-letter language code to be added to the output 479 * @access public 480 * @version 1.4 481 */ 482 public function write_page($filename, $doctype='xhtml1.1', $externalcss=true, $title='', $lang='en') { 483 $this->write($filename, true); 484 } 485 486 /** 487 * Write plain output to a file 488 * @param string $filename 489 * @param bool $formatted whether to print formatted or not 490 * @param string $doctype when printing formatted, is a shorthand for the document type 491 * @param bool $externalcss when printing formatted, indicates whether styles to be attached internally or as an external stylesheet 492 * @param string $title when printing formatted, is the title to be added in the head of the document 493 * @param string $lang when printing formatted, gives a two-letter language code to be added to the output 494 * @param bool $pre_code whether to add pre and code tags around the code (for light HTML formatted templates) 495 * @access public 496 * @version 1.4 497 */ 498 public function write($filename, $formatted=false, $doctype='xhtml1.1', $externalcss=true, $title='', $lang='en', $pre_code=true) { 499 $filename .= ( $formatted) ? '.xhtml' : '.css'; 500 501 if (!is_dir('temp')) { 502 $madedir = mkdir('temp'); 503 if (!$madedir) { 504 print 'Could not make directory "temp" in ' . dirname(__FILE__); 505 exit; 506 } 507 } 508 $handle = fopen('temp/' . $filename, 'w'); 509 if ($handle) { 510 if (!$formatted) { 511 fwrite($handle, $this->print->plain()); 512 } else { 513 fwrite($handle, $this->print->formatted_page($doctype, $externalcss, $title, $lang, $pre_code)); 514 } 515 } 516 fclose($handle); 517 } 518 519 /** 520 * Loads a new template 521 * @param string $content either filename (if $from_file == true), content of a template file, "high_compression", "highest_compression", "low_compression", or "default" 522 * @param bool $from_file uses $content as filename if true 523 * @access public 524 * @version 1.1 525 * @see http://csstidy.sourceforge.net/templates.php 526 */ 527 public function load_template($content, $from_file=true) { 528 $predefined_templates = & $this->data['csstidy']['predefined_templates']; 529 if ($content === 'high_compression' || $content === 'default' || $content === 'highest_compression' || $content === 'low_compression') { 530 $this->template = $predefined_templates[$content]; 531 return; 532 } 533 534 535 if ($from_file) { 536 $content = strip_tags(file_get_contents($content), '<span>'); 537 } 538 $content = str_replace("\r\n", "\n", $content); // Unify newlines (because the output also only uses \n) 539 $template = explode('|', $content); 540 541 for ($i = 0; $i < count($template); $i++) { 542 $this->template[$i] = $template[$i]; 543 } 544 } 545 546 /** 547 * Starts parsing from URL 548 * @param string $url 549 * @access public 550 * @version 1.0 551 */ 552 public function parse_from_url($url) { 553 return $this->parse(@file_get_contents($url)); 554 } 555 556 /** 557 * Checks if there is a token at the current position 558 * @param string $string 559 * @param integer $i 560 * @access public 561 * @version 1.11 562 */ 563 public function is_token(&$string, $i) { 564 return (strpos($this->tokens_list, $string[$i]) !== false && !$this->escaped($string, $i)); 565 } 566 567 /** 568 * Parses CSS in $string. The code is saved as array in $this->css 569 * @param string $string the CSS code 570 * @access public 571 * @return bool 572 * @version 1.1 573 */ 574 public function parse($string) { 575 // Temporarily set locale to en_US in order to handle floats properly 576 $old = @setlocale(LC_ALL, 0); 577 @setlocale(LC_ALL, 'C'); 578 579 // PHP bug? Settings need to be refreshed in PHP4 580 $this->print = new csstidy_print($this); 581 $this->optimise = new csstidy_optimise($this); 582 583 $all_properties = & $this->data['csstidy']['all_properties']; 584 $at_rules = & $this->data['csstidy']['at_rules']; 585 $quoted_string_properties = & $this->data['csstidy']['quoted_string_properties']; 586 587 $this->css = array(); 588 $this->print->input_css = $string; 589 $string = str_replace("\r\n", "\n", $string) . ' '; 590 $cur_comment = ''; 591 $cur_at = ''; 592 593 for ($i = 0, $size = strlen($string); $i < $size; $i++) { 594 if ($string[$i] === "\n" || $string[$i] === "\r") { 595 ++$this->line; 596 } 597 598 switch ($this->status) { 599 /* Case in at-block */ 600 case 'at': 601 if ($this->is_token($string, $i)) { 602 if ($string[$i] === '/' && @$string[$i + 1] === '*') { 603 $this->status = 'ic'; 604 ++$i; 605 $this->from[] = 'at'; 606 } elseif ($string[$i] === '{') { 607 $this->status = 'is'; 608 $this->at = $this->css_new_media_section($this->at, $cur_at); 609 $this->_add_token(AT_START, $this->at); 610 } elseif ($string[$i] === ',') { 611 $cur_at = trim($cur_at) . ','; 612 } elseif ($string[$i] === '\\') { 613 $cur_at .= $this->_unicode($string, $i); 614 } 615 // fix for complicated media, i.e @media screen and (-webkit-min-device-pixel-ratio:1.5) 616 elseif (in_array($string[$i], array('(', ')', ':', '.', '/'))) { 617 $cur_at .= $string[$i]; 618 } 619 } else { 620 $lastpos = strlen($cur_at) - 1; 621 if (!( (ctype_space($cur_at[$lastpos]) || $this->is_token($cur_at, $lastpos) && $cur_at[$lastpos] === ',') && ctype_space($string[$i]))) { 622 $cur_at .= $string[$i]; 623 } 624 } 625 break; 626 627 /* Case in-selector */ 628 case 'is': 629 if ($this->is_token($string, $i)) { 630 if ($string[$i] === '/' && @$string[$i + 1] === '*' && trim($this->selector) == '') { 631 $this->status = 'ic'; 632 ++$i; 633 $this->from[] = 'is'; 634 } elseif ($string[$i] === '@' && trim($this->selector) == '') { 635 // Check for at-rule 636 $this->invalid_at = true; 637 foreach ($at_rules as $name => $type) { 638 if (!strcasecmp(substr($string, $i + 1, strlen($name)), $name)) { 639 ($type === 'at') ? $cur_at = '@' . $name : $this->selector = '@' . $name; 640 if ($type === 'atis') { 641 $this->next_selector_at = ($this->next_selector_at?$this->next_selector_at:($this->at?$this->at:DEFAULT_AT)); 642 $this->at = $this->css_new_media_section($this->at, ' ', true); 643 $type = 'is'; 644 } 645 $this->status = $type; 646 $i += strlen($name); 647 $this->invalid_at = false; 648 break; 649 } 650 } 651 652 if ($this->invalid_at) { 653 $this->selector = '@'; 654 $invalid_at_name = ''; 655 for ($j = $i + 1; $j < $size; ++$j) { 656 if (!ctype_alpha($string[$j])) { 657 break; 658 } 659 $invalid_at_name .= $string[$j]; 660 } 661 $this->log('Invalid @-rule: ' . $invalid_at_name . ' (removed)', 'Warning'); 662 } 663 } elseif (($string[$i] === '"' || $string[$i] === "'")) { 664 $this->cur_string[] = $string[$i]; 665 $this->status = 'instr'; 666 $this->str_char[] = $string[$i]; 667 $this->from[] = 'is'; 668 /* fixing CSS3 attribute selectors, i.e. a[href$=".mp3" */ 669 $this->quoted_string[] = ($string[$i - 1] === '=' ); 670 } elseif ($this->invalid_at && $string[$i] === ';') { 671 $this->invalid_at = false; 672 $this->status = 'is'; 673 if ($this->next_selector_at) { 674 $this->at = $this->css_close_media_section($this->at); 675 $this->at = $this->css_new_media_section($this->at, $this->next_selector_at); 676 $this->next_selector_at = ''; 677 } 678 } elseif ($string[$i] === '{') { 679 $this->status = 'ip'; 680 if ($this->at == '') { 681 $this->at = $this->css_new_media_section($this->at, DEFAULT_AT); 682 } 683 $this->selector = $this->css_new_selector($this->at,$this->selector); 684 $this->_add_token(SEL_START, $this->selector); 685 $this->added = false; 686 } elseif ($string[$i] === '}') { 687 $this->_add_token(AT_END, $this->at); 688 $this->at = $this->css_close_media_section($this->at); 689 $this->selector = ''; 690 $this->sel_separate = array(); 691 } elseif ($string[$i] === ',') { 692 $this->selector = trim($this->selector) . ','; 693 $this->sel_separate[] = strlen($this->selector); 694 } elseif ($string[$i] === '\\') { 695 $this->selector .= $this->_unicode($string, $i); 696 } elseif ($string[$i] === '*' && @in_array($string[$i + 1], array('.', '#', '[', ':')) && ($i==0 OR $string[$i - 1]!=='/')) { 697 // remove unnecessary universal selector, FS#147, but not comment in selector 698 } else { 699 $this->selector .= $string[$i]; 700 } 701 } else { 702 $lastpos = strlen($this->selector) - 1; 703 if ($lastpos == -1 || !( (ctype_space($this->selector[$lastpos]) || $this->is_token($this->selector, $lastpos) && $this->selector[$lastpos] === ',') && ctype_space($string[$i]))) { 704 $this->selector .= $string[$i]; 705 } 706 } 707 break; 708 709 /* Case in-property */ 710 case 'ip': 711 if ($this->is_token($string, $i)) { 712 if (($string[$i] === ':' || $string[$i] === '=') && $this->property != '') { 713 $this->status = 'iv'; 714 if (!$this->get_cfg('discard_invalid_properties') || $this->property_is_valid($this->property)) { 715 $this->property = $this->css_new_property($this->at,$this->selector,$this->property); 716 $this->_add_token(PROPERTY, $this->property); 717 } 718 } elseif ($string[$i] === '/' && @$string[$i + 1] === '*' && $this->property == '') { 719 $this->status = 'ic'; 720 ++$i; 721 $this->from[] = 'ip'; 722 } elseif ($string[$i] === '}') { 723 $this->explode_selectors(); 724 $this->status = 'is'; 725 $this->invalid_at = false; 726 $this->_add_token(SEL_END, $this->selector); 727 $this->selector = ''; 728 $this->property = ''; 729 if ($this->next_selector_at) { 730 $this->at = $this->css_close_media_section($this->at); 731 $this->at = $this->css_new_media_section($this->at, $this->next_selector_at); 732 $this->next_selector_at = ''; 733 } 734 } elseif ($string[$i] === ';') { 735 $this->property = ''; 736 } elseif ($string[$i] === '\\') { 737 $this->property .= $this->_unicode($string, $i); 738 } 739 // else this is dumb IE a hack, keep it 740 // including // 741 elseif (($this->property === '' && !ctype_space($string[$i])) 742 || ($this->property === '/' || $string[$i] === '/')) { 743 $this->property .= $string[$i]; 744 } 745 } elseif (!ctype_space($string[$i])) { 746 $this->property .= $string[$i]; 747 } 748 break; 749 750 /* Case in-value */ 751 case 'iv': 752 $pn = (($string[$i] === "\n" || $string[$i] === "\r") && $this->property_is_next($string, $i + 1) || $i == strlen($string) - 1); 753 if ($this->is_token($string, $i) || $pn) { 754 if ($string[$i] === '/' && @$string[$i + 1] === '*') { 755 $this->status = 'ic'; 756 ++$i; 757 $this->from[] = 'iv'; 758 } elseif (($string[$i] === '"' || $string[$i] === "'" || $string[$i] === '(')) { 759 $this->cur_string[] = $string[$i]; 760 $this->str_char[] = ($string[$i] === '(') ? ')' : $string[$i]; 761 $this->status = 'instr'; 762 $this->from[] = 'iv'; 763 $this->quoted_string[] = in_array(strtolower($this->property), $quoted_string_properties); 764 } elseif ($string[$i] === ',') { 765 $this->sub_value = trim($this->sub_value) . ','; 766 } elseif ($string[$i] === '\\') { 767 $this->sub_value .= $this->_unicode($string, $i); 768 } elseif ($string[$i] === ';' || $pn) { 769 if ($this->selector[0] === '@' && isset($at_rules[substr($this->selector, 1)]) && $at_rules[substr($this->selector, 1)] === 'iv') { 770 /* Add quotes to charset, import, namespace */ 771 $this->sub_value_arr[] = trim($this->sub_value); 772 773 $this->status = 'is'; 774 775 switch ($this->selector) { 776 case '@charset': $this->charset = '"'.$this->sub_value_arr[0].'"'; 777 break; 778 case '@namespace': $this->namespace = implode(' ', $this->sub_value_arr); 779 break; 780 case '@import': $this->import[] = implode(' ', $this->sub_value_arr); 781 break; 782 } 783 784 $this->sub_value_arr = array(); 785 $this->sub_value = ''; 786 $this->selector = ''; 787 $this->sel_separate = array(); 788 } else { 789 $this->status = 'ip'; 790 } 791 } elseif ($string[$i] !== '}') { 792 $this->sub_value .= $string[$i]; 793 } 794 if (($string[$i] === '}' || $string[$i] === ';' || $pn) && !empty($this->selector)) { 795 if ($this->at == '') { 796 $this->at = $this->css_new_media_section($this->at,DEFAULT_AT); 797 } 798 799 // case settings 800 if ($this->get_cfg('lowercase_s')) { 801 $this->selector = strtolower($this->selector); 802 } 803 $this->property = strtolower($this->property); 804 805 $this->optimise->subvalue(); 806 if ($this->sub_value != '') { 807 $this->sub_value_arr[] = $this->sub_value; 808 $this->sub_value = ''; 809 } 810 811 $this->value = ''; 812 while (count($this->sub_value_arr)) { 813 $sub = array_shift($this->sub_value_arr); 814 if (strstr($this->selector, 'font-face')) { 815 $sub = $this->quote_font_format($sub); 816 } 817 818 if ($sub != '') 819 $this->value .= ((!strlen($this->value) || substr($this->value,-1,1) === ',')?'':' ').$sub; 820 } 821 822 $this->optimise->value(); 823 824 $valid = $this->property_is_valid($this->property); 825 if ((!$this->invalid_at || $this->get_cfg('preserve_css')) && (!$this->get_cfg('discard_invalid_properties') || $valid)) { 826 $this->css_add_property($this->at, $this->selector, $this->property, $this->value); 827 $this->_add_token(VALUE, $this->value); 828 $this->optimise->shorthands(); 829 } 830 if (!$valid) { 831 if ($this->get_cfg('discard_invalid_properties')) { 832 $this->log('Removed invalid property: ' . $this->property, 'Warning'); 833 } else { 834 $this->log('Invalid property in ' . strtoupper($this->get_cfg('css_level')) . ': ' . $this->property, 'Warning'); 835 } 836 } 837 838 $this->property = ''; 839 $this->sub_value_arr = array(); 840 $this->value = ''; 841 } 842 if ($string[$i] === '}') { 843 $this->explode_selectors(); 844 $this->_add_token(SEL_END, $this->selector); 845 $this->status = 'is'; 846 $this->invalid_at = false; 847 $this->selector = ''; 848 if ($this->next_selector_at) { 849 $this->at = $this->css_close_media_section($this->at); 850 $this->at = $this->css_new_media_section($this->at, $this->next_selector_at); 851 $this->next_selector_at = ''; 852 } 853 } 854 } elseif (!$pn) { 855 $this->sub_value .= $string[$i]; 856 857 if (ctype_space($string[$i])) { 858 $this->optimise->subvalue(); 859 if ($this->sub_value != '') { 860 $this->sub_value_arr[] = $this->sub_value; 861 $this->sub_value = ''; 862 } 863 } 864 } 865 break; 866 867 /* Case in string */ 868 case 'instr': 869 $_str_char = $this->str_char[count($this->str_char)-1]; 870 $_cur_string = $this->cur_string[count($this->cur_string)-1]; 871 $_quoted_string = $this->quoted_string[count($this->quoted_string)-1]; 872 $temp_add = $string[$i]; 873 874 // Add another string to the stack. Strings can't be nested inside of quotes, only parentheses, but 875 // parentheticals can be nested more than once. 876 if ($_str_char === ")" && ($string[$i] === "(" || $string[$i] === '"' || $string[$i] === '\'') && !$this->escaped($string, $i)) { 877 $this->cur_string[] = $string[$i]; 878 $this->str_char[] = $string[$i] === '(' ? ')' : $string[$i]; 879 $this->from[] = 'instr'; 880 $this->quoted_string[] = ($_str_char === ')' && $string[$i] !== '(' && trim($_cur_string)==='(')?$_quoted_string:!($string[$i] === '('); 881 continue 2; 882 } 883 884 if ($_str_char !== ")" && ($string[$i] === "\n" || $string[$i] === "\r") && !($string[$i - 1] === '\\' && !$this->escaped($string, $i - 1))) { 885 $temp_add = "\\A"; 886 $this->log('Fixed incorrect newline in string', 'Warning'); 887 } 888 889 $_cur_string .= $temp_add; 890 891 if ($string[$i] === $_str_char && !$this->escaped($string, $i)) { 892 $this->status = array_pop($this->from); 893 894 if (!preg_match('|[' . implode('', $this->data['csstidy']['whitespace']) . ']|uis', $_cur_string) && $this->property !== 'content') { 895 if (!$_quoted_string) { 896 if ($_str_char !== ')') { 897 // Convert properties like 898 // font-family: 'Arial'; 899 // to 900 // font-family: Arial; 901 // or 902 // url("abc") 903 // to 904 // url(abc) 905 $_cur_string = substr($_cur_string, 1, -1); 906 } 907 } else { 908 $_quoted_string = false; 909 } 910 } 911 912 array_pop($this->cur_string); 913 array_pop($this->quoted_string); 914 array_pop($this->str_char); 915 916 if ($_str_char === ')') { 917 $_cur_string = '(' . trim(substr($_cur_string, 1, -1)) . ')'; 918 } 919 920 if ($this->status === 'iv') { 921 if (!$_quoted_string) { 922 if (strpos($_cur_string,',') !== false) 923 // we can on only remove space next to ',' 924 $_cur_string = implode(',', array_map('trim', explode(',',$_cur_string))); 925 // and multiple spaces (too expensive) 926 if (strpos($_cur_string, ' ') !== false) 927 $_cur_string = preg_replace(",\s+,", ' ', $_cur_string); 928 } 929 $this->sub_value .= $_cur_string; 930 } elseif ($this->status === 'is') { 931 $this->selector .= $_cur_string; 932 } elseif ($this->status === 'instr') { 933 $this->cur_string[count($this->cur_string)-1] .= $_cur_string; 934 } 935 } else { 936 $this->cur_string[count($this->cur_string)-1] = $_cur_string; 937 } 938 break; 939 940 /* Case in-comment */ 941 case 'ic': 942 if ($string[$i] === '*' && $string[$i + 1] === '/') { 943 $this->status = array_pop($this->from); 944 $i++; 945 if (strlen($cur_comment) > 1 and strncmp($cur_comment, '!', 1) === 0) { 946 $this->_add_token(IMPORTANT_COMMENT, $cur_comment); 947 $this->css_add_important_comment($cur_comment); 948 } 949 else { 950 $this->_add_token(COMMENT, $cur_comment); 951 } 952 $cur_comment = ''; 953 } else { 954 $cur_comment .= $string[$i]; 955 } 956 break; 957 } 958 } 959 960 $this->optimise->postparse(); 961 962 $this->print->_reset(); 963 964 @setlocale(LC_ALL, $old); // Set locale back to original setting 965 966 return!(empty($this->css) && empty($this->import) && empty($this->charset) && empty($this->tokens) && empty($this->namespace)); 967 } 968 969 970 /** 971 * format() in font-face needs quoted values for somes browser (FF at least) 972 * 973 * @param $value 974 * @return string 975 */ 976 public function quote_font_format($value) { 977 if (strncmp($value,'format',6) == 0) { 978 $p = strpos($value,')',7); 979 $end = substr($value,$p); 980 $format_strings = $this->parse_string_list(substr($value, 7, $p-7)); 981 if (!$format_strings) { 982 $value = ''; 983 } else { 984 $value = 'format('; 985 986 foreach ($format_strings as $format_string) { 987 $value .= '"' . str_replace('"', '\\"', $format_string) . '",'; 988 } 989 990 $value = substr($value, 0, -1) . $end; 991 } 992 } 993 return $value; 994 } 995 996 /** 997 * Explodes selectors 998 * @access private 999 * @version 1.0 1000 */ 1001 public function explode_selectors() { 1002 // Explode multiple selectors 1003 if ($this->get_cfg('merge_selectors') === 1) { 1004 $new_sels = array(); 1005 $lastpos = 0; 1006 $this->sel_separate[] = strlen($this->selector); 1007 foreach ($this->sel_separate as $num => $pos) { 1008 if ($num == count($this->sel_separate) - 1) { 1009 $pos += 1; 1010 } 1011 1012 $new_sels[] = substr($this->selector, $lastpos, $pos - $lastpos - 1); 1013 $lastpos = $pos; 1014 } 1015 1016 if (count($new_sels) > 1) { 1017 foreach ($new_sels as $selector) { 1018 if (isset($this->css[$this->at][$this->selector])) { 1019 $this->merge_css_blocks($this->at, $selector, $this->css[$this->at][$this->selector]); 1020 } 1021 } 1022 unset($this->css[$this->at][$this->selector]); 1023 } 1024 } 1025 $this->sel_separate = array(); 1026 } 1027 1028 /** 1029 * Checks if a character is escaped (and returns true if it is) 1030 * @param string $string 1031 * @param integer $pos 1032 * @access public 1033 * @return bool 1034 * @version 1.02 1035 */ 1036 static function escaped(&$string, $pos) { 1037 return!(@($string[$pos - 1] !== '\\') || csstidy::escaped($string, $pos - 1)); 1038 } 1039 1040 1041 /** 1042 * Add an important comment to the css code 1043 * (one we want to keep) 1044 * @param $comment 1045 */ 1046 public function css_add_important_comment($comment) { 1047 if ($this->get_cfg('preserve_css') || trim($comment) == '') { 1048 return; 1049 } 1050 if (!isset($this->css['!'])) { 1051 $this->css['!'] = ''; 1052 } 1053 else { 1054 $this->css['!'] .= "\n"; 1055 } 1056 $this->css['!'] .= $comment; 1057 } 1058 1059 /** 1060 * Adds a property with value to the existing CSS code 1061 * @param string $media 1062 * @param string $selector 1063 * @param string $property 1064 * @param string $new_val 1065 * @access private 1066 * @version 1.2 1067 */ 1068 public function css_add_property($media, $selector, $property, $new_val) { 1069 if ($this->get_cfg('preserve_css') || trim($new_val) == '') { 1070 return; 1071 } 1072 1073 $this->added = true; 1074 if (isset($this->css[$media][$selector][$property])) { 1075 if (($this->is_important($this->css[$media][$selector][$property]) && $this->is_important($new_val)) || !$this->is_important($this->css[$media][$selector][$property])) { 1076 $this->css[$media][$selector][$property] = trim($new_val); 1077 } 1078 } else { 1079 $this->css[$media][$selector][$property] = trim($new_val); 1080 } 1081 } 1082 1083 /** 1084 * Check if a current media section is the continuation of the last one 1085 * if not inc the name of the media section to avoid a merging 1086 * 1087 * @param int|string $media 1088 * @return int|string 1089 */ 1090 public function css_check_last_media_section_or_inc($media) { 1091 // are we starting? 1092 if (!$this->css || !is_array($this->css) || empty($this->css)) { 1093 return $media; 1094 } 1095 1096 // if the last @media is the same as this 1097 // keep it 1098 end($this->css); 1099 $at = key($this->css); 1100 if ($at == $media) { 1101 return $media; 1102 } 1103 1104 // else inc the section in the array 1105 while (isset($this->css[$media])) 1106 if (is_numeric($media)) 1107 $media++; 1108 else 1109 $media .= ' '; 1110 return $media; 1111 } 1112 1113 /** 1114 * Start a new media section. 1115 * Check if the media is not already known, 1116 * else rename it with extra spaces 1117 * to avoid merging 1118 * 1119 * @param string $current_media 1120 * @param string $media 1121 * @param bool $at_root 1122 * @return string 1123 */ 1124 public function css_new_media_section($current_media, $new_media, $at_root = false) { 1125 if ($this->get_cfg('preserve_css')) { 1126 return $new_media; 1127 } 1128 1129 // if we already are in a media and CSS level is 3, manage nested medias 1130 if ($current_media 1131 && !$at_root 1132 // numeric $current_media means DEFAULT_AT or inc 1133 && !is_numeric($current_media) 1134 && strncmp($this->get_cfg('css_level'), 'CSS3', 4) == 0) { 1135 1136 $new_media = rtrim($current_media) . "{" . rtrim($new_media); 1137 } 1138 1139 return $this->css_check_last_media_section_or_inc($new_media); 1140 } 1141 1142 /** 1143 * Close a media section 1144 * Find the parent media we were in before or the root 1145 * @param $current_media 1146 * @return string 1147 */ 1148 public function css_close_media_section($current_media) { 1149 if ($this->get_cfg('preserve_css')) { 1150 return ''; 1151 } 1152 1153 if (strpos($current_media, '{') !== false) { 1154 $current_media = explode('{', $current_media); 1155 array_pop($current_media); 1156 $current_media = implode('{', $current_media); 1157 return $current_media; 1158 } 1159 1160 return ''; 1161 } 1162 1163 /** 1164 * Start a new selector. 1165 * If already referenced in this media section, 1166 * rename it with extra space to avoid merging 1167 * except if merging is required, 1168 * or last selector is the same (merge siblings) 1169 * 1170 * never merge @font-face 1171 * 1172 * @param string $media 1173 * @param string $selector 1174 * @return string 1175 */ 1176 public function css_new_selector($media,$selector) { 1177 if ($this->get_cfg('preserve_css')) { 1178 return $selector; 1179 } 1180 $selector = trim($selector); 1181 if (strncmp($selector,'@font-face',10)!=0) { 1182 if ($this->settings['merge_selectors'] != false) 1183 return $selector; 1184 1185 if (!$this->css || !isset($this->css[$media]) || !$this->css[$media]) 1186 return $selector; 1187 1188 // if last is the same, keep it 1189 end($this->css[$media]); 1190 $sel = key($this->css[$media]); 1191 if ($sel == $selector) { 1192 return $selector; 1193 } 1194 } 1195 1196 while (isset($this->css[$media][$selector])) 1197 $selector .= ' '; 1198 return $selector; 1199 } 1200 1201 /** 1202 * Start a new propertie. 1203 * If already references in this selector, 1204 * rename it with extra space to avoid override 1205 * 1206 * @param string $media 1207 * @param string $selector 1208 * @param string $property 1209 * @return string 1210 */ 1211 public function css_new_property($media, $selector, $property) { 1212 if ($this->get_cfg('preserve_css')) { 1213 return $property; 1214 } 1215 if (!$this->css || !isset($this->css[$media][$selector]) || !$this->css[$media][$selector]) 1216 return $property; 1217 1218 while (isset($this->css[$media][$selector][$property])) 1219 $property .= ' '; 1220 1221 return $property; 1222 } 1223 1224 /** 1225 * Adds CSS to an existing media/selector 1226 * @param string $media 1227 * @param string $selector 1228 * @param array $css_add 1229 * @access private 1230 * @version 1.1 1231 */ 1232 public function merge_css_blocks($media, $selector, $css_add) { 1233 foreach ($css_add as $property => $value) { 1234 $this->css_add_property($media, $selector, $property, $value, false); 1235 } 1236 } 1237 1238 /** 1239 * Checks if $value is !important. 1240 * @param string $value 1241 * @return bool 1242 * @access public 1243 * @version 1.0 1244 */ 1245 public function is_important(&$value) { 1246 return ( 1247 strpos($value, '!') !== false // quick test 1248 AND !strcasecmp(substr(str_replace($this->data['csstidy']['whitespace'], '', $value), -10, 10), '!important')); 1249 } 1250 1251 /** 1252 * Returns a value without !important 1253 * @param string $value 1254 * @return string 1255 * @access public 1256 * @version 1.0 1257 */ 1258 public function gvw_important($value) { 1259 if ($this->is_important($value)) { 1260 $value = trim($value); 1261 $value = substr($value, 0, -9); 1262 $value = trim($value); 1263 $value = substr($value, 0, -1); 1264 $value = trim($value); 1265 return $value; 1266 } 1267 return $value; 1268 } 1269 1270 /** 1271 * Checks if the next word in a string from pos is a CSS property 1272 * @param string $istring 1273 * @param integer $pos 1274 * @return bool 1275 * @access private 1276 * @version 1.2 1277 */ 1278 public function property_is_next($istring, $pos) { 1279 $all_properties = & $this->data['csstidy']['all_properties']; 1280 $istring = substr($istring, $pos, strlen($istring) - $pos); 1281 $pos = strpos($istring, ':'); 1282 if ($pos === false) { 1283 return false; 1284 } 1285 $istring = strtolower(trim(substr($istring, 0, $pos))); 1286 if (isset($all_properties[$istring])) { 1287 $this->log('Added semicolon to the end of declaration', 'Warning'); 1288 return true; 1289 } 1290 return false; 1291 } 1292 1293 /** 1294 * Checks if a property is valid 1295 * @param string $property 1296 * @return bool 1297 * @access public 1298 * @version 1.0 1299 */ 1300 public function property_is_valid($property) { 1301 if (in_array(trim($property), $this->data['csstidy']['multiple_properties'])) $property = trim($property); 1302 $all_properties = & $this->data['csstidy']['all_properties']; 1303 return (isset($all_properties[$property]) && strpos($all_properties[$property], strtoupper($this->get_cfg('css_level'))) !== false ); 1304 } 1305 1306 /** 1307 * Accepts a list of strings (e.g., the argument to format() in a @font-face src property) 1308 * and returns a list of the strings. Converts things like: 1309 * 1310 * format(abc) => format("abc") 1311 * format(abc def) => format("abc","def") 1312 * format(abc "def") => format("abc","def") 1313 * format(abc, def, ghi) => format("abc","def","ghi") 1314 * format("abc",'def') => format("abc","def") 1315 * format("abc, def, ghi") => format("abc, def, ghi") 1316 * 1317 * @param string 1318 * @return array 1319 */ 1320 public function parse_string_list($value) { 1321 $value = trim($value); 1322 1323 // Case: empty 1324 if (!$value) return array(); 1325 1326 $strings = array(); 1327 1328 $in_str = false; 1329 $current_string = ''; 1330 1331 for ($i = 0, $_len = strlen($value); $i < $_len; $i++) { 1332 if (($value[$i] === ',' || $value[$i] === ' ') && $in_str === true) { 1333 $in_str = false; 1334 $strings[] = $current_string; 1335 $current_string = ''; 1336 } elseif ($value[$i] === '"' || $value[$i] === "'") { 1337 if ($in_str === $value[$i]) { 1338 $strings[] = $current_string; 1339 $in_str = false; 1340 $current_string = ''; 1341 continue; 1342 } elseif (!$in_str) { 1343 $in_str = $value[$i]; 1344 } 1345 } else { 1346 if ($in_str) { 1347 $current_string .= $value[$i]; 1348 } else { 1349 if (!preg_match("/[\s,]/", $value[$i])) { 1350 $in_str = true; 1351 $current_string = $value[$i]; 1352 } 1353 } 1354 } 1355 } 1356 1357 if ($current_string) { 1358 $strings[] = $current_string; 1359 } 1360 1361 return $strings; 1362 } 1363} 1364