[3052] | 1 | <?php |
---|
| 2 | /** |
---|
| 3 | * CSSTidy - CSS Parser and Optimiser |
---|
| 4 | * |
---|
| 5 | * CSS Parser class |
---|
| 6 | * |
---|
| 7 | * This file is part of CSSTidy. |
---|
| 8 | * |
---|
| 9 | * CSSTidy is free software; you can redistribute it and/or modify |
---|
| 10 | * it under the terms of the GNU General Public License as published by |
---|
| 11 | * the Free Software Foundation; either version 2 of the License, or |
---|
| 12 | * (at your option) any later version. |
---|
| 13 | * |
---|
| 14 | * CSSTidy is distributed in the hope that it will be useful, |
---|
| 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
| 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
| 17 | * GNU General Public License for more details. |
---|
| 18 | * |
---|
| 19 | * You should have received a copy of the GNU General Public License |
---|
| 20 | * along with CSSTidy; if not, write to the Free Software |
---|
| 21 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
---|
| 22 | * |
---|
| 23 | * @license http://opensource.org/licenses/gpl-license.php GNU Public License |
---|
| 24 | * @package csstidy |
---|
| 25 | * @author Florian Schmitz (floele at gmail dot com) 2005-2006 |
---|
| 26 | */ |
---|
| 27 | |
---|
| 28 | /** |
---|
| 29 | * Various CSS data needed for correct optimisations etc. |
---|
| 30 | * |
---|
| 31 | * @version 1.3 |
---|
| 32 | */ |
---|
| 33 | require('data.inc.php'); |
---|
| 34 | |
---|
| 35 | /** |
---|
| 36 | * Contains a class for printing CSS code |
---|
| 37 | * |
---|
| 38 | * @version 1.0 |
---|
| 39 | */ |
---|
| 40 | require('class.csstidy_print.php'); |
---|
| 41 | |
---|
| 42 | /** |
---|
| 43 | * Contains a class for optimising CSS code |
---|
| 44 | * |
---|
| 45 | * @version 1.0 |
---|
| 46 | */ |
---|
| 47 | require('class.csstidy_optimise.php'); |
---|
| 48 | |
---|
| 49 | /** |
---|
| 50 | * CSS Parser class |
---|
| 51 | * |
---|
| 52 | * This class represents a CSS parser which reads CSS code and saves it in an array. |
---|
| 53 | * In opposite to most other CSS parsers, it does not use regular expressions and |
---|
| 54 | * thus has full CSS2 support and a higher reliability. |
---|
| 55 | * Additional to that it applies some optimisations and fixes to the CSS code. |
---|
| 56 | * An online version should be available here: http://cdburnerxp.se/cssparse/css_optimiser.php |
---|
| 57 | * @package csstidy |
---|
| 58 | * @author Florian Schmitz (floele at gmail dot com) 2005-2006 |
---|
| 59 | * @version 1.3 |
---|
| 60 | */ |
---|
| 61 | class csstidy { |
---|
| 62 | |
---|
| 63 | /** |
---|
| 64 | * Saves the parsed CSS |
---|
| 65 | * @var array |
---|
| 66 | * @access public |
---|
| 67 | */ |
---|
| 68 | var $css = array(); |
---|
| 69 | |
---|
| 70 | /** |
---|
| 71 | * Saves the parsed CSS (raw) |
---|
| 72 | * @var array |
---|
| 73 | * @access private |
---|
| 74 | */ |
---|
| 75 | var $tokens = array(); |
---|
| 76 | |
---|
| 77 | /** |
---|
| 78 | * Printer class |
---|
| 79 | * @see csstidy_print |
---|
| 80 | * @var object |
---|
| 81 | * @access public |
---|
| 82 | */ |
---|
| 83 | var $print; |
---|
| 84 | |
---|
| 85 | /** |
---|
| 86 | * Optimiser class |
---|
| 87 | * @see csstidy_optimise |
---|
| 88 | * @var object |
---|
| 89 | * @access private |
---|
| 90 | */ |
---|
| 91 | var $optimise; |
---|
| 92 | |
---|
| 93 | /** |
---|
| 94 | * Saves the CSS charset (@charset) |
---|
| 95 | * @var string |
---|
| 96 | * @access private |
---|
| 97 | */ |
---|
| 98 | var $charset = ''; |
---|
| 99 | |
---|
| 100 | /** |
---|
| 101 | * Saves all @import URLs |
---|
| 102 | * @var array |
---|
| 103 | * @access private |
---|
| 104 | */ |
---|
| 105 | var $import = array(); |
---|
| 106 | |
---|
| 107 | /** |
---|
| 108 | * Saves the namespace |
---|
| 109 | * @var string |
---|
| 110 | * @access private |
---|
| 111 | */ |
---|
| 112 | var $namespace = ''; |
---|
| 113 | |
---|
| 114 | /** |
---|
| 115 | * Contains the version of csstidy |
---|
| 116 | * @var string |
---|
| 117 | * @access private |
---|
| 118 | */ |
---|
| 119 | var $version = '1.3'; |
---|
| 120 | |
---|
| 121 | /** |
---|
| 122 | * Stores the settings |
---|
| 123 | * @var array |
---|
| 124 | * @access private |
---|
| 125 | */ |
---|
| 126 | var $settings = array(); |
---|
| 127 | |
---|
| 128 | /** |
---|
| 129 | * Saves the parser-status. |
---|
| 130 | * |
---|
| 131 | * Possible values: |
---|
| 132 | * - is = in selector |
---|
| 133 | * - ip = in property |
---|
| 134 | * - iv = in value |
---|
| 135 | * - instr = in string (started at " or ' or ( ) |
---|
| 136 | * - ic = in comment (ignore everything) |
---|
| 137 | * - at = in @-block |
---|
| 138 | * |
---|
| 139 | * @var string |
---|
| 140 | * @access private |
---|
| 141 | */ |
---|
| 142 | var $status = 'is'; |
---|
| 143 | |
---|
| 144 | |
---|
| 145 | /** |
---|
| 146 | * Saves the current at rule (@media) |
---|
| 147 | * @var string |
---|
| 148 | * @access private |
---|
| 149 | */ |
---|
| 150 | var $at = ''; |
---|
| 151 | |
---|
| 152 | /** |
---|
| 153 | * Saves the current selector |
---|
| 154 | * @var string |
---|
| 155 | * @access private |
---|
| 156 | */ |
---|
| 157 | var $selector = ''; |
---|
| 158 | |
---|
| 159 | /** |
---|
| 160 | * Saves the current property |
---|
| 161 | * @var string |
---|
| 162 | * @access private |
---|
| 163 | */ |
---|
| 164 | var $property = ''; |
---|
| 165 | |
---|
| 166 | /** |
---|
| 167 | * Saves the position of , in selectors |
---|
| 168 | * @var array |
---|
| 169 | * @access private |
---|
| 170 | */ |
---|
| 171 | var $sel_separate = array(); |
---|
| 172 | |
---|
| 173 | /** |
---|
| 174 | * Saves the current value |
---|
| 175 | * @var string |
---|
| 176 | * @access private |
---|
| 177 | */ |
---|
| 178 | var $value = ''; |
---|
| 179 | |
---|
| 180 | /** |
---|
| 181 | * Saves the current sub-value |
---|
| 182 | * |
---|
| 183 | * Example for a subvalue: |
---|
| 184 | * background:url(foo.png) red no-repeat; |
---|
| 185 | * "url(foo.png)", "red", and "no-repeat" are subvalues, |
---|
| 186 | * seperated by whitespace |
---|
| 187 | * @var string |
---|
| 188 | * @access private |
---|
| 189 | */ |
---|
| 190 | var $sub_value = ''; |
---|
| 191 | |
---|
| 192 | /** |
---|
| 193 | * Array which saves all subvalues for a property. |
---|
| 194 | * @var array |
---|
| 195 | * @see sub_value |
---|
| 196 | * @access private |
---|
| 197 | */ |
---|
| 198 | var $sub_value_arr = array(); |
---|
| 199 | |
---|
| 200 | /** |
---|
| 201 | * Saves the char which opened the last string |
---|
| 202 | * @var string |
---|
| 203 | * @access private |
---|
| 204 | */ |
---|
| 205 | var $str_char = ''; |
---|
| 206 | var $cur_string = ''; |
---|
| 207 | |
---|
| 208 | /** |
---|
| 209 | * Status from which the parser switched to ic or instr |
---|
| 210 | * @var string |
---|
| 211 | * @access private |
---|
| 212 | */ |
---|
| 213 | var $from = ''; |
---|
| 214 | |
---|
| 215 | /** |
---|
| 216 | * Variable needed to manage string-in-strings, for example url("foo.png") |
---|
| 217 | * @var string |
---|
| 218 | * @access private |
---|
| 219 | */ |
---|
| 220 | var $str_in_str = false; |
---|
| 221 | |
---|
| 222 | /** |
---|
| 223 | * =true if in invalid at-rule |
---|
| 224 | * @var bool |
---|
| 225 | * @access private |
---|
| 226 | */ |
---|
| 227 | var $invalid_at = false; |
---|
| 228 | |
---|
| 229 | /** |
---|
| 230 | * =true if something has been added to the current selector |
---|
| 231 | * @var bool |
---|
| 232 | * @access private |
---|
| 233 | */ |
---|
| 234 | var $added = false; |
---|
| 235 | |
---|
| 236 | /** |
---|
| 237 | * Array which saves the message log |
---|
| 238 | * @var array |
---|
| 239 | * @access private |
---|
| 240 | */ |
---|
| 241 | var $log = array(); |
---|
| 242 | |
---|
| 243 | /** |
---|
| 244 | * Saves the line number |
---|
| 245 | * @var integer |
---|
| 246 | * @access private |
---|
| 247 | */ |
---|
| 248 | var $line = 1; |
---|
| 249 | |
---|
| 250 | /** |
---|
| 251 | * Loads standard template and sets default settings |
---|
| 252 | * @access private |
---|
| 253 | * @version 1.3 |
---|
| 254 | */ |
---|
| 255 | function csstidy() |
---|
| 256 | { |
---|
| 257 | $this->settings['remove_bslash'] = true; |
---|
| 258 | $this->settings['compress_colors'] = true; |
---|
| 259 | $this->settings['compress_font-weight'] = true; |
---|
| 260 | $this->settings['lowercase_s'] = false; |
---|
| 261 | $this->settings['optimise_shorthands'] = 1; |
---|
| 262 | $this->settings['remove_last_;'] = false; |
---|
| 263 | $this->settings['case_properties'] = 1; |
---|
| 264 | $this->settings['sort_properties'] = false; |
---|
| 265 | $this->settings['sort_selectors'] = false; |
---|
| 266 | $this->settings['merge_selectors'] = 2; |
---|
| 267 | $this->settings['discard_invalid_properties'] = false; |
---|
| 268 | $this->settings['css_level'] = 'CSS2.1'; |
---|
| 269 | $this->settings['preserve_css'] = false; |
---|
| 270 | $this->settings['timestamp'] = false; |
---|
| 271 | |
---|
| 272 | $this->load_template('default'); |
---|
| 273 | $this->print = new csstidy_print($this); |
---|
| 274 | $this->optimise = new csstidy_optimise($this); |
---|
| 275 | } |
---|
| 276 | |
---|
| 277 | /** |
---|
| 278 | * Get the value of a setting. |
---|
| 279 | * @param string $setting |
---|
| 280 | * @access public |
---|
| 281 | * @return mixed |
---|
| 282 | * @version 1.0 |
---|
| 283 | */ |
---|
| 284 | function get_cfg($setting) |
---|
| 285 | { |
---|
| 286 | if(isset($this->settings[$setting])) |
---|
| 287 | { |
---|
| 288 | return $this->settings[$setting]; |
---|
| 289 | } |
---|
| 290 | return false; |
---|
| 291 | } |
---|
| 292 | |
---|
| 293 | /** |
---|
| 294 | * Set the value of a setting. |
---|
| 295 | * @param string $setting |
---|
| 296 | * @param mixed $value |
---|
| 297 | * @access public |
---|
| 298 | * @return bool |
---|
| 299 | * @version 1.0 |
---|
| 300 | */ |
---|
| 301 | function set_cfg($setting,$value) |
---|
| 302 | { |
---|
| 303 | if(isset($this->settings[$setting]) && $value !== '') |
---|
| 304 | { |
---|
| 305 | $this->settings[$setting] = $value; |
---|
| 306 | return true; |
---|
| 307 | } |
---|
| 308 | return false; |
---|
| 309 | } |
---|
| 310 | |
---|
| 311 | /** |
---|
| 312 | * Adds a token to $this->tokens |
---|
| 313 | * @param mixed $type |
---|
| 314 | * @param string $data |
---|
| 315 | * @param bool $do add a token even if preserve_css is off |
---|
| 316 | * @access private |
---|
| 317 | * @version 1.0 |
---|
| 318 | */ |
---|
| 319 | function _add_token($type, $data, $do = false) { |
---|
| 320 | if($this->get_cfg('preserve_css') || $do) { |
---|
| 321 | $this->tokens[] = array($type, ($type == COMMENT) ? $data : trim($data)); |
---|
| 322 | } |
---|
| 323 | } |
---|
| 324 | |
---|
| 325 | /** |
---|
| 326 | * Add a message to the message log |
---|
| 327 | * @param string $message |
---|
| 328 | * @param string $type |
---|
| 329 | * @param integer $line |
---|
| 330 | * @access private |
---|
| 331 | * @version 1.0 |
---|
| 332 | */ |
---|
| 333 | function log($message,$type,$line = -1) |
---|
| 334 | { |
---|
| 335 | if($line === -1) |
---|
| 336 | { |
---|
| 337 | $line = $this->line; |
---|
| 338 | } |
---|
| 339 | $line = intval($line); |
---|
| 340 | $add = array('m' => $message, 't' => $type); |
---|
| 341 | if(!isset($this->log[$line]) || !in_array($add,$this->log[$line])) |
---|
| 342 | { |
---|
| 343 | $this->log[$line][] = $add; |
---|
| 344 | } |
---|
| 345 | } |
---|
| 346 | |
---|
| 347 | /** |
---|
| 348 | * Parse unicode notations and find a replacement character |
---|
| 349 | * @param string $string |
---|
| 350 | * @param integer $i |
---|
| 351 | * @access private |
---|
| 352 | * @return string |
---|
| 353 | * @version 1.2 |
---|
| 354 | */ |
---|
| 355 | function _unicode(&$string, &$i) |
---|
| 356 | { |
---|
| 357 | ++$i; |
---|
| 358 | $add = ''; |
---|
| 359 | $tokens =& $GLOBALS['csstidy']['tokens']; |
---|
| 360 | $replaced = false; |
---|
| 361 | |
---|
| 362 | while($i < strlen($string) && (ctype_xdigit($string{$i}) || ctype_space($string{$i})) && strlen($add) < 6) |
---|
| 363 | { |
---|
| 364 | $add .= $string{$i}; |
---|
| 365 | |
---|
| 366 | if(ctype_space($string{$i})) { |
---|
| 367 | break; |
---|
| 368 | } |
---|
[7655] | 369 | ++$i; |
---|
[3052] | 370 | } |
---|
| 371 | |
---|
| 372 | if(hexdec($add) > 47 && hexdec($add) < 58 || hexdec($add) > 64 && hexdec($add) < 91 || hexdec($add) > 96 && hexdec($add) < 123) |
---|
| 373 | { |
---|
| 374 | $this->log('Replaced unicode notation: Changed \\'. $add .' to ' . chr(hexdec($add)),'Information'); |
---|
| 375 | $add = chr(hexdec($add)); |
---|
| 376 | $replaced = true; |
---|
| 377 | } |
---|
| 378 | else { |
---|
| 379 | $add = trim('\\'.$add); |
---|
| 380 | } |
---|
| 381 | |
---|
| 382 | if(@ctype_xdigit($string{$i+1}) && ctype_space($string{$i}) |
---|
| 383 | && !$replaced || !ctype_space($string{$i})) { |
---|
| 384 | $i--; |
---|
| 385 | } |
---|
| 386 | |
---|
| 387 | if($add != '\\' || !$this->get_cfg('remove_bslash') || strpos($tokens, $string{$i+1}) !== false) { |
---|
| 388 | return $add; |
---|
| 389 | } |
---|
| 390 | |
---|
| 391 | if($add == '\\') { |
---|
| 392 | $this->log('Removed unnecessary backslash','Information'); |
---|
| 393 | } |
---|
| 394 | return ''; |
---|
| 395 | } |
---|
| 396 | |
---|
| 397 | /** |
---|
| 398 | * Loads a new template |
---|
| 399 | * @param string $content either filename (if $from_file == true), content of a template file, "high_compression", "highest_compression", "low_compression", or "default" |
---|
| 400 | * @param bool $from_file uses $content as filename if true |
---|
| 401 | * @access public |
---|
| 402 | * @version 1.1 |
---|
| 403 | * @see http://csstidy.sourceforge.net/templates.php |
---|
| 404 | */ |
---|
| 405 | function load_template($content, $from_file=true) |
---|
| 406 | { |
---|
| 407 | $predefined_templates =& $GLOBALS['csstidy']['predefined_templates']; |
---|
| 408 | if($content == 'high_compression' || $content == 'default' || $content == 'highest_compression' || $content == 'low_compression') |
---|
| 409 | { |
---|
| 410 | $this->template = $predefined_templates[$content]; |
---|
| 411 | return; |
---|
| 412 | } |
---|
| 413 | |
---|
| 414 | if($from_file) |
---|
| 415 | { |
---|
| 416 | $content = strip_tags(file_get_contents($content),'<span>'); |
---|
| 417 | } |
---|
| 418 | $content = str_replace("\r\n","\n",$content); // Unify newlines (because the output also only uses \n) |
---|
| 419 | $template = explode('|',$content); |
---|
| 420 | |
---|
[7655] | 421 | for ($i = 0; $i < count($template); ++$i ) |
---|
[3052] | 422 | { |
---|
| 423 | $this->template[$i] = $template[$i]; |
---|
| 424 | } |
---|
| 425 | } |
---|
| 426 | |
---|
| 427 | /** |
---|
| 428 | * Starts parsing from URL |
---|
| 429 | * @param string $url |
---|
| 430 | * @access public |
---|
| 431 | * @version 1.0 |
---|
| 432 | */ |
---|
| 433 | function parse_from_url($url) |
---|
| 434 | { |
---|
| 435 | return $this->parse(@file_get_contents($url)); |
---|
| 436 | } |
---|
| 437 | |
---|
| 438 | /** |
---|
| 439 | * Checks if there is a token at the current position |
---|
| 440 | * @param string $string |
---|
| 441 | * @param integer $i |
---|
| 442 | * @access public |
---|
| 443 | * @version 1.11 |
---|
| 444 | */ |
---|
| 445 | function is_token(&$string, $i) |
---|
| 446 | { |
---|
| 447 | $tokens =& $GLOBALS['csstidy']['tokens']; |
---|
| 448 | return (strpos($tokens, $string{$i}) !== false && !csstidy::escaped($string,$i)); |
---|
| 449 | } |
---|
| 450 | |
---|
| 451 | |
---|
| 452 | /** |
---|
| 453 | * Parses CSS in $string. The code is saved as array in $this->css |
---|
| 454 | * @param string $string the CSS code |
---|
| 455 | * @access public |
---|
| 456 | * @return bool |
---|
| 457 | * @version 1.1 |
---|
| 458 | */ |
---|
| 459 | function parse($string) { |
---|
| 460 | // PHP bug? Settings need to be refreshed in PHP4 |
---|
| 461 | $this->print = new csstidy_print($this); |
---|
| 462 | $this->optimise = new csstidy_optimise($this); |
---|
| 463 | |
---|
| 464 | $all_properties =& $GLOBALS['csstidy']['all_properties']; |
---|
| 465 | $at_rules =& $GLOBALS['csstidy']['at_rules']; |
---|
| 466 | |
---|
| 467 | $this->css = array(); |
---|
| 468 | $this->print->input_css = $string; |
---|
| 469 | $string = str_replace("\r\n","\n",$string) . ' '; |
---|
| 470 | $cur_comment = ''; |
---|
| 471 | |
---|
[7655] | 472 | for ($i = 0, $size = strlen($string); $i < $size; ++$i ) |
---|
[3052] | 473 | { |
---|
| 474 | if($string{$i} == "\n" || $string{$i} == "\r") |
---|
| 475 | { |
---|
| 476 | ++$this->line; |
---|
| 477 | } |
---|
| 478 | |
---|
| 479 | switch($this->status) |
---|
| 480 | { |
---|
| 481 | /* Case in at-block */ |
---|
| 482 | case 'at': |
---|
| 483 | if(csstidy::is_token($string,$i)) |
---|
| 484 | { |
---|
| 485 | if($string{$i} == '/' && @$string{$i+1} == '*') |
---|
| 486 | { |
---|
| 487 | $this->status = 'ic'; ++$i; |
---|
| 488 | $this->from = 'at'; |
---|
| 489 | } |
---|
| 490 | elseif($string{$i} == '{') |
---|
| 491 | { |
---|
| 492 | $this->status = 'is'; |
---|
| 493 | $this->_add_token(AT_START, $this->at); |
---|
| 494 | } |
---|
| 495 | elseif($string{$i} == ',') |
---|
| 496 | { |
---|
| 497 | $this->at = trim($this->at).','; |
---|
| 498 | } |
---|
| 499 | elseif($string{$i} == '\\') |
---|
| 500 | { |
---|
| 501 | $this->at .= $this->_unicode($string,$i); |
---|
| 502 | } |
---|
| 503 | } |
---|
| 504 | else |
---|
| 505 | { |
---|
| 506 | $lastpos = strlen($this->at)-1; |
---|
| 507 | if(!( (ctype_space($this->at{$lastpos}) || csstidy::is_token($this->at,$lastpos) && $this->at{$lastpos} == ',') && ctype_space($string{$i}))) |
---|
| 508 | { |
---|
| 509 | $this->at .= $string{$i}; |
---|
| 510 | } |
---|
| 511 | } |
---|
| 512 | break; |
---|
| 513 | |
---|
| 514 | /* Case in-selector */ |
---|
| 515 | case 'is': |
---|
| 516 | if(csstidy::is_token($string,$i)) |
---|
| 517 | { |
---|
| 518 | if($string{$i} == '/' && @$string{$i+1} == '*' && trim($this->selector) == '') |
---|
| 519 | { |
---|
| 520 | $this->status = 'ic'; ++$i; |
---|
| 521 | $this->from = 'is'; |
---|
| 522 | } |
---|
| 523 | elseif($string{$i} == '@' && trim($this->selector) == '') |
---|
| 524 | { |
---|
| 525 | // Check for at-rule |
---|
| 526 | $this->invalid_at = true; |
---|
| 527 | foreach($at_rules as $name => $type) |
---|
| 528 | { |
---|
| 529 | if(!strcasecmp(substr($string,$i+1,strlen($name)),$name)) |
---|
| 530 | { |
---|
| 531 | ($type == 'at') ? $this->at = '@'.$name : $this->selector = '@'.$name; |
---|
| 532 | $this->status = $type; |
---|
| 533 | $i += strlen($name); |
---|
| 534 | $this->invalid_at = false; |
---|
| 535 | } |
---|
| 536 | } |
---|
| 537 | |
---|
| 538 | if($this->invalid_at) |
---|
| 539 | { |
---|
| 540 | $this->selector = '@'; |
---|
| 541 | $invalid_at_name = ''; |
---|
| 542 | for($j = $i+1; $j < $size; ++$j) |
---|
| 543 | { |
---|
| 544 | if(!ctype_alpha($string{$j})) |
---|
| 545 | { |
---|
| 546 | break; |
---|
| 547 | } |
---|
| 548 | $invalid_at_name .= $string{$j}; |
---|
| 549 | } |
---|
| 550 | $this->log('Invalid @-rule: '.$invalid_at_name.' (removed)','Warning'); |
---|
| 551 | } |
---|
| 552 | } |
---|
| 553 | elseif(($string{$i} == '"' || $string{$i} == "'")) |
---|
| 554 | { |
---|
| 555 | $this->cur_string = $string{$i}; |
---|
| 556 | $this->status = 'instr'; |
---|
| 557 | $this->str_char = $string{$i}; |
---|
| 558 | $this->from = 'is'; |
---|
| 559 | } |
---|
| 560 | elseif($this->invalid_at && $string{$i} == ';') |
---|
| 561 | { |
---|
| 562 | $this->invalid_at = false; |
---|
| 563 | $this->status = 'is'; |
---|
| 564 | } |
---|
| 565 | elseif($string{$i} == '{') |
---|
| 566 | { |
---|
| 567 | $this->status = 'ip'; |
---|
| 568 | $this->_add_token(SEL_START, $this->selector); |
---|
| 569 | $this->added = false; |
---|
| 570 | } |
---|
| 571 | elseif($string{$i} == '}') |
---|
| 572 | { |
---|
| 573 | $this->_add_token(AT_END, $this->at); |
---|
| 574 | $this->at = ''; |
---|
| 575 | $this->selector = ''; |
---|
| 576 | $this->sel_separate = array(); |
---|
| 577 | } |
---|
| 578 | elseif($string{$i} == ',') |
---|
| 579 | { |
---|
| 580 | $this->selector = trim($this->selector).','; |
---|
| 581 | $this->sel_separate[] = strlen($this->selector); |
---|
| 582 | } |
---|
| 583 | elseif($string{$i} == '\\') |
---|
| 584 | { |
---|
| 585 | $this->selector .= $this->_unicode($string,$i); |
---|
| 586 | } |
---|
| 587 | // remove unnecessary universal selector, FS#147 |
---|
| 588 | else if(!($string{$i} == '*' && @in_array($string{$i+1}, array('.', '#', '[', ':')))) { |
---|
| 589 | $this->selector .= $string{$i}; |
---|
| 590 | } |
---|
| 591 | } |
---|
| 592 | else |
---|
| 593 | { |
---|
| 594 | $lastpos = strlen($this->selector)-1; |
---|
| 595 | if($lastpos == -1 || !( (ctype_space($this->selector{$lastpos}) || csstidy::is_token($this->selector,$lastpos) && $this->selector{$lastpos} == ',') && ctype_space($string{$i}))) |
---|
| 596 | { |
---|
| 597 | $this->selector .= $string{$i}; |
---|
| 598 | } |
---|
| 599 | } |
---|
| 600 | break; |
---|
| 601 | |
---|
| 602 | /* Case in-property */ |
---|
| 603 | case 'ip': |
---|
| 604 | if(csstidy::is_token($string,$i)) |
---|
| 605 | { |
---|
| 606 | if(($string{$i} == ':' || $string{$i} == '=') && $this->property != '') |
---|
| 607 | { |
---|
| 608 | $this->status = 'iv'; |
---|
| 609 | if(!$this->get_cfg('discard_invalid_properties') || csstidy::property_is_valid($this->property)) { |
---|
| 610 | $this->_add_token(PROPERTY, $this->property); |
---|
| 611 | } |
---|
| 612 | } |
---|
| 613 | elseif($string{$i} == '/' && @$string{$i+1} == '*' && $this->property == '') |
---|
| 614 | { |
---|
| 615 | $this->status = 'ic'; ++$i; |
---|
| 616 | $this->from = 'ip'; |
---|
| 617 | } |
---|
| 618 | elseif($string{$i} == '}') |
---|
| 619 | { |
---|
| 620 | $this->explode_selectors(); |
---|
| 621 | $this->status = 'is'; |
---|
| 622 | $this->invalid_at = false; |
---|
| 623 | $this->_add_token(SEL_END, $this->selector); |
---|
| 624 | $this->selector = ''; |
---|
| 625 | $this->property = ''; |
---|
| 626 | } |
---|
| 627 | elseif($string{$i} == ';') |
---|
| 628 | { |
---|
| 629 | $this->property = ''; |
---|
| 630 | } |
---|
| 631 | elseif($string{$i} == '\\') |
---|
| 632 | { |
---|
| 633 | $this->property .= $this->_unicode($string,$i); |
---|
| 634 | } |
---|
| 635 | } |
---|
| 636 | elseif(!ctype_space($string{$i})) |
---|
| 637 | { |
---|
| 638 | $this->property .= $string{$i}; |
---|
| 639 | } |
---|
| 640 | break; |
---|
| 641 | |
---|
| 642 | /* Case in-value */ |
---|
| 643 | case 'iv': |
---|
| 644 | $pn = (($string{$i} == "\n" || $string{$i} == "\r") && $this->property_is_next($string,$i+1) || $i == strlen($string)-1); |
---|
| 645 | if(csstidy::is_token($string,$i) || $pn) |
---|
| 646 | { |
---|
| 647 | if($string{$i} == '/' && @$string{$i+1} == '*') |
---|
| 648 | { |
---|
| 649 | $this->status = 'ic'; ++$i; |
---|
| 650 | $this->from = 'iv'; |
---|
| 651 | } |
---|
| 652 | elseif(($string{$i} == '"' || $string{$i} == "'" || $string{$i} == '(')) |
---|
| 653 | { |
---|
| 654 | $this->cur_string = $string{$i}; |
---|
| 655 | $this->str_char = ($string{$i} == '(') ? ')' : $string{$i}; |
---|
| 656 | $this->status = 'instr'; |
---|
| 657 | $this->from = 'iv'; |
---|
| 658 | } |
---|
| 659 | elseif($string{$i} == ',') |
---|
| 660 | { |
---|
| 661 | $this->sub_value = trim($this->sub_value).','; |
---|
| 662 | } |
---|
| 663 | elseif($string{$i} == '\\') |
---|
| 664 | { |
---|
| 665 | $this->sub_value .= $this->_unicode($string,$i); |
---|
| 666 | } |
---|
| 667 | elseif($string{$i} == ';' || $pn) |
---|
| 668 | { |
---|
| 669 | if($this->selector{0} == '@' && isset($at_rules[substr($this->selector,1)]) && $at_rules[substr($this->selector,1)] == 'iv') |
---|
| 670 | { |
---|
| 671 | $this->sub_value_arr[] = trim($this->sub_value); |
---|
| 672 | |
---|
| 673 | $this->status = 'is'; |
---|
| 674 | |
---|
| 675 | switch($this->selector) |
---|
| 676 | { |
---|
| 677 | case '@charset': $this->charset = $this->sub_value_arr[0]; break; |
---|
| 678 | case '@namespace': $this->namespace = implode(' ',$this->sub_value_arr); break; |
---|
| 679 | case '@import': $this->import[] = implode(' ',$this->sub_value_arr); break; |
---|
| 680 | } |
---|
| 681 | |
---|
| 682 | $this->sub_value_arr = array(); |
---|
| 683 | $this->sub_value = ''; |
---|
| 684 | $this->selector = ''; |
---|
| 685 | $this->sel_separate = array(); |
---|
| 686 | } |
---|
| 687 | else |
---|
| 688 | { |
---|
| 689 | $this->status = 'ip'; |
---|
| 690 | } |
---|
| 691 | } |
---|
| 692 | elseif($string{$i} != '}') |
---|
| 693 | { |
---|
| 694 | $this->sub_value .= $string{$i}; |
---|
| 695 | } |
---|
| 696 | if(($string{$i} == '}' || $string{$i} == ';' || $pn) && !empty($this->selector)) |
---|
| 697 | { |
---|
| 698 | if($this->at == '') |
---|
| 699 | { |
---|
| 700 | $this->at = DEFAULT_AT; |
---|
| 701 | } |
---|
| 702 | |
---|
| 703 | // case settings |
---|
| 704 | if($this->get_cfg('lowercase_s')) |
---|
| 705 | { |
---|
| 706 | $this->selector = strtolower($this->selector); |
---|
| 707 | } |
---|
| 708 | $this->property = strtolower($this->property); |
---|
| 709 | |
---|
| 710 | $this->optimise->subvalue(); |
---|
| 711 | if($this->sub_value != '') { |
---|
| 712 | $this->sub_value_arr[] = $this->sub_value; |
---|
| 713 | $this->sub_value = ''; |
---|
| 714 | } |
---|
| 715 | |
---|
| 716 | $this->value = implode(' ',$this->sub_value_arr); |
---|
| 717 | |
---|
| 718 | $this->selector = trim($this->selector); |
---|
| 719 | |
---|
| 720 | $this->optimise->value(); |
---|
| 721 | |
---|
| 722 | $valid = csstidy::property_is_valid($this->property); |
---|
| 723 | if((!$this->invalid_at || $this->get_cfg('preserve_css')) && (!$this->get_cfg('discard_invalid_properties') || $valid)) |
---|
| 724 | { |
---|
| 725 | $this->css_add_property($this->at,$this->selector,$this->property,$this->value); |
---|
| 726 | $this->_add_token(VALUE, $this->value); |
---|
| 727 | $this->optimise->shorthands(); |
---|
| 728 | } |
---|
| 729 | if(!$valid) |
---|
| 730 | { |
---|
| 731 | if($this->get_cfg('discard_invalid_properties')) |
---|
| 732 | { |
---|
| 733 | $this->log('Removed invalid property: '.$this->property,'Warning'); |
---|
| 734 | } |
---|
| 735 | else |
---|
| 736 | { |
---|
| 737 | $this->log('Invalid property in '.strtoupper($this->get_cfg('css_level')).': '.$this->property,'Warning'); |
---|
| 738 | } |
---|
| 739 | } |
---|
| 740 | |
---|
| 741 | $this->property = ''; |
---|
| 742 | $this->sub_value_arr = array(); |
---|
| 743 | $this->value = ''; |
---|
| 744 | } |
---|
| 745 | if($string{$i} == '}') |
---|
| 746 | { |
---|
| 747 | $this->explode_selectors(); |
---|
| 748 | $this->_add_token(SEL_END, $this->selector); |
---|
| 749 | $this->status = 'is'; |
---|
| 750 | $this->invalid_at = false; |
---|
| 751 | $this->selector = ''; |
---|
| 752 | } |
---|
| 753 | } |
---|
| 754 | elseif(!$pn) |
---|
| 755 | { |
---|
| 756 | $this->sub_value .= $string{$i}; |
---|
| 757 | |
---|
| 758 | if(ctype_space($string{$i})) |
---|
| 759 | { |
---|
| 760 | $this->optimise->subvalue(); |
---|
| 761 | if($this->sub_value != '') { |
---|
| 762 | $this->sub_value_arr[] = $this->sub_value; |
---|
| 763 | $this->sub_value = ''; |
---|
| 764 | } |
---|
| 765 | } |
---|
| 766 | } |
---|
| 767 | break; |
---|
| 768 | |
---|
| 769 | /* Case in string */ |
---|
| 770 | case 'instr': |
---|
| 771 | if($this->str_char == ')' && ($string{$i} == '"' || $string{$i} == '\'') && !$this->str_in_str && !csstidy::escaped($string,$i)) |
---|
| 772 | { |
---|
| 773 | $this->str_in_str = true; |
---|
| 774 | } |
---|
| 775 | elseif($this->str_char == ')' && ($string{$i} == '"' || $string{$i} == '\'') && $this->str_in_str && !csstidy::escaped($string,$i)) |
---|
| 776 | { |
---|
| 777 | $this->str_in_str = false; |
---|
| 778 | } |
---|
| 779 | $temp_add = $string{$i}; // ...and no not-escaped backslash at the previous position |
---|
| 780 | if( ($string{$i} == "\n" || $string{$i} == "\r") && !($string{$i-1} == '\\' && !csstidy::escaped($string,$i-1)) ) |
---|
| 781 | { |
---|
| 782 | $temp_add = "\\A "; |
---|
| 783 | $this->log('Fixed incorrect newline in string','Warning'); |
---|
| 784 | } |
---|
| 785 | if (!($this->str_char == ')' && in_array($string{$i}, $GLOBALS['csstidy']['whitespace']) && !$this->str_in_str)) { |
---|
| 786 | $this->cur_string .= $temp_add; |
---|
| 787 | } |
---|
| 788 | if($string{$i} == $this->str_char && !csstidy::escaped($string,$i) && !$this->str_in_str) |
---|
| 789 | { |
---|
| 790 | $this->status = $this->from; |
---|
| 791 | if (!preg_match('|[' . implode('', $GLOBALS['csstidy']['whitespace']) . ']|uis', $this->cur_string) && $this->property != 'content') { |
---|
| 792 | if ($this->str_char == '"' || $this->str_char == '\'') { |
---|
| 793 | $this->cur_string = substr($this->cur_string, 1, -1); |
---|
| 794 | } else if (strlen($this->cur_string) > 3 && ($this->cur_string[1] == '"' || $this->cur_string[1] == '\'')) /* () */ { |
---|
| 795 | $this->cur_string = $this->cur_string[0] . substr($this->cur_string, 2, -2) . substr($this->cur_string, -1); |
---|
| 796 | } |
---|
| 797 | } |
---|
| 798 | if($this->from == 'iv') |
---|
| 799 | { |
---|
| 800 | $this->sub_value .= $this->cur_string; |
---|
| 801 | } |
---|
| 802 | elseif($this->from == 'is') |
---|
| 803 | { |
---|
| 804 | $this->selector .= $this->cur_string; |
---|
| 805 | } |
---|
| 806 | } |
---|
| 807 | break; |
---|
| 808 | |
---|
| 809 | /* Case in-comment */ |
---|
| 810 | case 'ic': |
---|
| 811 | if($string{$i} == '*' && $string{$i+1} == '/') |
---|
| 812 | { |
---|
| 813 | $this->status = $this->from; |
---|
[7655] | 814 | ++$i; |
---|
[3052] | 815 | $this->_add_token(COMMENT, $cur_comment); |
---|
| 816 | $cur_comment = ''; |
---|
| 817 | } |
---|
| 818 | else |
---|
| 819 | { |
---|
| 820 | $cur_comment .= $string{$i}; |
---|
| 821 | } |
---|
| 822 | break; |
---|
| 823 | } |
---|
| 824 | } |
---|
| 825 | |
---|
| 826 | $this->optimise->postparse(); |
---|
| 827 | |
---|
| 828 | $this->print->_reset(); |
---|
| 829 | |
---|
| 830 | return !(empty($this->css) && empty($this->import) && empty($this->charset) && empty($this->tokens) && empty($this->namespace)); |
---|
| 831 | } |
---|
| 832 | |
---|
| 833 | /** |
---|
| 834 | * Explodes selectors |
---|
| 835 | * @access private |
---|
| 836 | * @version 1.0 |
---|
| 837 | */ |
---|
| 838 | function explode_selectors() |
---|
| 839 | { |
---|
| 840 | // Explode multiple selectors |
---|
| 841 | if($this->get_cfg('merge_selectors') == 1) |
---|
| 842 | { |
---|
| 843 | $new_sels = array(); |
---|
| 844 | $lastpos = 0; |
---|
| 845 | $this->sel_separate[] = strlen($this->selector); |
---|
| 846 | foreach($this->sel_separate as $num => $pos) |
---|
| 847 | { |
---|
| 848 | if($num == count($this->sel_separate)-1) { |
---|
| 849 | $pos += 1; |
---|
| 850 | } |
---|
| 851 | |
---|
| 852 | $new_sels[] = substr($this->selector,$lastpos,$pos-$lastpos-1); |
---|
| 853 | $lastpos = $pos; |
---|
| 854 | } |
---|
| 855 | |
---|
| 856 | if(count($new_sels) > 1) |
---|
| 857 | { |
---|
| 858 | foreach($new_sels as $selector) |
---|
| 859 | { |
---|
| 860 | $this->merge_css_blocks($this->at,$selector,$this->css[$this->at][$this->selector]); |
---|
| 861 | } |
---|
| 862 | unset($this->css[$this->at][$this->selector]); |
---|
| 863 | } |
---|
| 864 | } |
---|
| 865 | $this->sel_separate = array(); |
---|
| 866 | } |
---|
| 867 | |
---|
| 868 | /** |
---|
| 869 | * Checks if a character is escaped (and returns true if it is) |
---|
| 870 | * @param string $string |
---|
| 871 | * @param integer $pos |
---|
| 872 | * @access public |
---|
| 873 | * @return bool |
---|
| 874 | * @version 1.02 |
---|
| 875 | */ |
---|
| 876 | function escaped(&$string,$pos) |
---|
| 877 | { |
---|
| 878 | return !(@($string{$pos-1} != '\\') || csstidy::escaped($string,$pos-1)); |
---|
| 879 | } |
---|
| 880 | |
---|
| 881 | /** |
---|
| 882 | * Adds a property with value to the existing CSS code |
---|
| 883 | * @param string $media |
---|
| 884 | * @param string $selector |
---|
| 885 | * @param string $property |
---|
| 886 | * @param string $new_val |
---|
| 887 | * @access private |
---|
| 888 | * @version 1.2 |
---|
| 889 | */ |
---|
| 890 | function css_add_property($media,$selector,$property,$new_val) |
---|
| 891 | { |
---|
| 892 | if($this->get_cfg('preserve_css') || trim($new_val) == '') { |
---|
| 893 | return; |
---|
| 894 | } |
---|
| 895 | |
---|
| 896 | $this->added = true; |
---|
| 897 | if(isset($this->css[$media][$selector][$property])) |
---|
| 898 | { |
---|
| 899 | if((csstidy::is_important($this->css[$media][$selector][$property]) && csstidy::is_important($new_val)) || !csstidy::is_important($this->css[$media][$selector][$property])) |
---|
| 900 | { |
---|
| 901 | unset($this->css[$media][$selector][$property]); |
---|
| 902 | $this->css[$media][$selector][$property] = trim($new_val); |
---|
| 903 | } |
---|
| 904 | } |
---|
| 905 | else |
---|
| 906 | { |
---|
| 907 | $this->css[$media][$selector][$property] = trim($new_val); |
---|
| 908 | } |
---|
| 909 | } |
---|
| 910 | |
---|
| 911 | /** |
---|
| 912 | * Adds CSS to an existing media/selector |
---|
| 913 | * @param string $media |
---|
| 914 | * @param string $selector |
---|
| 915 | * @param array $css_add |
---|
| 916 | * @access private |
---|
| 917 | * @version 1.1 |
---|
| 918 | */ |
---|
| 919 | function merge_css_blocks($media,$selector,$css_add) |
---|
| 920 | { |
---|
| 921 | foreach($css_add as $property => $value) |
---|
| 922 | { |
---|
| 923 | $this->css_add_property($media,$selector,$property,$value,false); |
---|
| 924 | } |
---|
| 925 | } |
---|
| 926 | |
---|
| 927 | /** |
---|
| 928 | * Checks if $value is !important. |
---|
| 929 | * @param string $value |
---|
| 930 | * @return bool |
---|
| 931 | * @access public |
---|
| 932 | * @version 1.0 |
---|
| 933 | */ |
---|
[5141] | 934 | static function is_important(&$value) |
---|
[3052] | 935 | { |
---|
| 936 | return (!strcasecmp(substr(str_replace($GLOBALS['csstidy']['whitespace'],'',$value),-10,10),'!important')); |
---|
| 937 | } |
---|
| 938 | |
---|
| 939 | /** |
---|
| 940 | * Returns a value without !important |
---|
| 941 | * @param string $value |
---|
| 942 | * @return string |
---|
| 943 | * @access public |
---|
| 944 | * @version 1.0 |
---|
| 945 | */ |
---|
| 946 | function gvw_important($value) |
---|
| 947 | { |
---|
| 948 | if(csstidy::is_important($value)) |
---|
| 949 | { |
---|
| 950 | $value = trim($value); |
---|
| 951 | $value = substr($value,0,-9); |
---|
| 952 | $value = trim($value); |
---|
| 953 | $value = substr($value,0,-1); |
---|
| 954 | $value = trim($value); |
---|
| 955 | return $value; |
---|
| 956 | } |
---|
| 957 | return $value; |
---|
| 958 | } |
---|
| 959 | |
---|
| 960 | /** |
---|
| 961 | * Checks if the next word in a string from pos is a CSS property |
---|
| 962 | * @param string $istring |
---|
| 963 | * @param integer $pos |
---|
| 964 | * @return bool |
---|
| 965 | * @access private |
---|
| 966 | * @version 1.2 |
---|
| 967 | */ |
---|
| 968 | function property_is_next($istring, $pos) |
---|
| 969 | { |
---|
| 970 | $all_properties =& $GLOBALS['csstidy']['all_properties']; |
---|
| 971 | $istring = substr($istring,$pos,strlen($istring)-$pos); |
---|
| 972 | $pos = strpos($istring,':'); |
---|
| 973 | if($pos === false) |
---|
| 974 | { |
---|
| 975 | return false; |
---|
| 976 | } |
---|
| 977 | $istring = strtolower(trim(substr($istring,0,$pos))); |
---|
| 978 | if(isset($all_properties[$istring])) |
---|
| 979 | { |
---|
| 980 | $this->log('Added semicolon to the end of declaration','Warning'); |
---|
| 981 | return true; |
---|
| 982 | } |
---|
| 983 | return false; |
---|
| 984 | } |
---|
| 985 | |
---|
| 986 | /** |
---|
| 987 | * Checks if a property is valid |
---|
| 988 | * @param string $property |
---|
| 989 | * @return bool; |
---|
| 990 | * @access public |
---|
| 991 | * @version 1.0 |
---|
| 992 | */ |
---|
| 993 | function property_is_valid($property) { |
---|
| 994 | $all_properties =& $GLOBALS['csstidy']['all_properties']; |
---|
| 995 | return (isset($all_properties[$property]) && strpos($all_properties[$property],strtoupper($this->get_cfg('css_level'))) !== false ); |
---|
| 996 | } |
---|
| 997 | |
---|
| 998 | } |
---|
| 999 | ?> |
---|