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