3520279baabf61c7b682de3703b1efcb0cc74f19
4 * CSSTidy - CSS Parser and Optimiser
8 * Copyright 2005, 2006, 2007 Florian Schmitz
10 * This file is part of CSSTidy.
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.
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.
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/>.
25 * @license http://opensource.org/licenses/lgpl-license.php GNU Lesser General Public License
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
36 * Defines ctype functions if required.
38 * @TODO: Make these methods of CSSTidy.
41 if (!function_exists('ctype_space')){
42 /* ctype_space Check for whitespace character(s) */
43 function ctype_space($text){
44 return (1===preg_match("/^[ \r\n\t\f]+$/", $text));
47 if (!function_exists('ctype_alpha')){
48 /* ctype_alpha Check for alphabetic character(s) */
49 function ctype_alpha($text){
50 return (1===preg_match('/^[a-zA-Z]+$/', $text));
53 if (!function_exists('ctype_xdigit')){
54 /* ctype_xdigit Check for HEX character(s) */
55 function ctype_xdigit($text){
56 return (1===preg_match('/^[a-fA-F0-9]+$/', $text));
62 * @todo //TODO: make them class constants of csstidy
64 define('AT_START', 1);
66 define('SEL_START', 3);
68 define('PROPERTY', 5);
71 define('IMPORTANT_COMMENT',8);
72 define('DEFAULT_AT', 41);
75 * Contains a class for printing CSS code
79 require('class.csstidy_print.php');
82 * Contains a class for optimising CSS code
86 require('class.csstidy_optimise.php');
91 * This class represents a CSS parser which reads CSS code and saves it in an array.
92 * In opposite to most other CSS parsers, it does not use regular expressions and
93 * thus has full CSS2 support and a higher reliability.
94 * Additional to that it applies some optimisations and fixes to the CSS code.
95 * An online version should be available here: http://cdburnerxp.se/cssparse/css_optimiser.php
97 * @author Florian Schmitz (floele at gmail dot com) 2005-2006
103 * Saves the parsed CSS. This array is empty if preserve_css is on.
107 public $css = array();
109 * Saves the parsed CSS (raw)
113 public $tokens = array();
123 * @see csstidy_optimise
129 * Saves the CSS charset (@charset)
133 public $charset = '';
135 * Saves all @import URLs
139 public $import = array();
141 * Saves the namespace
145 public $namespace = '';
147 * Contains the version of csstidy
151 public $version = '1.6.4';
153 * Stores the settings
157 public $settings = array();
159 * Saves the parser-status.
165 * - instr = in string (started at " or ' or ( )
166 * - ic = in comment (ignore everything)
172 public $status = 'is';
174 * Saves the current at rule (@media)
180 * Saves the at rule for next selector (during @font-face or other @)
184 public $next_selector_at = '';
187 * Saves the current selector
191 public $selector = '';
193 * Saves the current property
197 public $property = '';
199 * Saves the position of , in selectors
203 public $sel_separate = array();
205 * Saves the current value
211 * Saves the current sub-value
213 * Example for a subvalue:
214 * background:url(foo.png) red no-repeat;
215 * "url(foo.png)", "red", and "no-repeat" are subvalues,
216 * seperated by whitespace
220 public $sub_value = '';
222 * Array which saves all subvalues for a property.
227 public $sub_value_arr = array();
229 * Saves the stack of characters that opened the current strings
233 public $str_char = array();
234 public $cur_string = array();
236 * Status from which the parser switched to ic or instr
240 public $from = array();
243 * =true if in invalid at-rule
247 public $invalid_at = false;
249 * =true if something has been added to the current selector
253 public $added = false;
255 * Array which saves the message log
259 public $log = array();
261 * Saves the line number
267 * Marks if we need to leave quotes for a string
271 public $quoted_string = array();
277 public $tokens_list = "";
280 * Various CSS Data for CSSTidy
283 public $data = array();
286 * Loads standard template and sets default settings
290 public function __construct() {
292 include('data.inc.php');
295 $this->settings
['remove_bslash'] = true;
296 $this->settings
['compress_colors'] = true;
297 $this->settings
['compress_font-weight'] = true;
298 $this->settings
['lowercase_s'] = false;
300 1 common shorthands optimization
301 2 + font property optimization
302 3 + background property optimization
304 $this->settings
['optimise_shorthands'] = 1;
305 $this->settings
['remove_last_;'] = true;
306 $this->settings
['space_before_important'] = false;
307 /* rewrite all properties with low case, better for later gzip OK, safe*/
308 $this->settings
['case_properties'] = 1;
309 /* sort properties in alpabetic order, better for later gzip
310 * but can cause trouble in case of overiding same propertie or using hack
312 $this->settings
['sort_properties'] = false;
314 1, 3, 5, etc -- enable sorting selectors inside @media: a{}b{}c{}
315 2, 5, 8, etc -- enable sorting selectors inside one CSS declaration: a,b,c{}
316 preserve order by default cause it can break functionnality
318 $this->settings
['sort_selectors'] = 0;
319 /* is dangeroues to be used: CSS is broken sometimes */
320 $this->settings
['merge_selectors'] = 0;
321 /* preserve or not browser hacks */
323 /* Useful to produce a rtl css from a ltr one (or the opposite) */
324 $this->settings
['reverse_left_and_right'] = 0;
326 $this->settings
['discard_invalid_selectors'] = false;
327 $this->settings
['discard_invalid_properties'] = false;
328 $this->settings
['css_level'] = 'CSS3.0';
329 $this->settings
['preserve_css'] = false;
330 $this->settings
['timestamp'] = false;
331 $this->settings
['template'] = ''; // say that propertie exist
332 $this->set_cfg('template','default'); // call load_template
333 $this->optimise
= new csstidy_optimise($this);
335 $this->tokens_list
= & $this->data
['csstidy']['tokens'];
339 * Get the value of a setting.
340 * @param string $setting
345 public function get_cfg($setting) {
346 if (isset($this->settings
[$setting])) {
347 return $this->settings
[$setting];
354 * @param string $template used by set_cfg to load a template via a configuration setting
358 public function _load_template($template) {
361 $this->load_template('default');
365 $this->load_template('highest_compression');
369 $this->load_template('high_compression');
373 $this->load_template('low_compression');
377 $this->load_template($template);
383 * Set the value of a setting.
384 * @param string $setting
385 * @param mixed $value
390 public function set_cfg($setting, $value=null) {
391 if (is_array($setting) && $value === null) {
392 foreach ($setting as $setprop => $setval) {
393 $this->settings
[$setprop] = $setval;
395 if (array_key_exists('template', $setting)) {
396 $this->_load_template($this->settings
['template']);
399 } elseif (isset($this->settings
[$setting]) && $value !== '') {
400 $this->settings
[$setting] = $value;
401 if ($setting === 'template') {
402 $this->_load_template($this->settings
['template']);
410 * Adds a token to $this->tokens
412 * @param string $data
413 * @param bool $do add a token even if preserve_css is off
417 public function _add_token($type, $data, $do = false) {
418 if ($this->get_cfg('preserve_css') ||
$do) {
419 $this->tokens
[] = array($type, ($type == COMMENT
or $type == IMPORTANT_COMMENT
) ?
$data : trim($data));
424 * Add a message to the message log
425 * @param string $message
426 * @param string $type
427 * @param integer $line
431 public function log($message, $type, $line = -1) {
435 $line = intval($line);
436 $add = array('m' => $message, 't' => $type);
437 if (!isset($this->log
[$line]) ||
!in_array($add, $this->log
[$line])) {
438 $this->log
[$line][] = $add;
443 * Parse unicode notations and find a replacement character
444 * @param string $string
450 public function _unicode(&$string, &$i) {
455 while ($i < strlen($string) && (ctype_xdigit($string{$i}) ||
ctype_space($string{$i})) && strlen($add) < 6) {
458 if (ctype_space($string{$i})) {
464 if (hexdec($add) > 47 && hexdec($add) < 58 ||
hexdec($add) > 64 && hexdec($add) < 91 ||
hexdec($add) > 96 && hexdec($add) < 123) {
465 $this->log('Replaced unicode notation: Changed \\' . $add . ' to ' . chr(hexdec($add)), 'Information');
466 $add = chr(hexdec($add));
469 $add = trim('\\' . $add);
472 if (@ctype_xdigit
($string{$i +
1}) && ctype_space($string{$i})
473 && !$replaced ||
!ctype_space($string{$i})) {
477 if ($add !== '\\' ||
!$this->get_cfg('remove_bslash') ||
strpos($this->tokens_list
, $string{$i +
1}) !== false) {
482 $this->log('Removed unnecessary backslash', 'Information');
488 * Write formatted output to a file
489 * @param string $filename
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
497 public function write_page($filename, $doctype='xhtml1.1', $externalcss=true, $title='', $lang='en') {
498 $this->write($filename, true);
502 * Write plain output to a file
503 * @param string $filename
504 * @param bool $formatted whether to print formatted or not
505 * @param string $doctype when printing formatted, is a shorthand for the document type
506 * @param bool $externalcss when printing formatted, indicates whether styles to be attached internally or as an external stylesheet
507 * @param string $title when printing formatted, is the title to be added in the head of the document
508 * @param string $lang when printing formatted, gives a two-letter language code to be added to the output
509 * @param bool $pre_code whether to add pre and code tags around the code (for light HTML formatted templates)
513 public function write($filename, $formatted=false, $doctype='xhtml1.1', $externalcss=true, $title='', $lang='en', $pre_code=true) {
514 $filename .= ( $formatted) ?
'.xhtml' : '.css';
516 if (!is_dir('temp')) {
517 $madedir = mkdir('temp');
519 print 'Could not make directory "temp" in ' . dirname(__FILE__
);
523 $handle = fopen('temp/' . $filename, 'w');
526 fwrite($handle, $this->print->plain());
528 fwrite($handle, $this->print->formatted_page($doctype, $externalcss, $title, $lang, $pre_code));
535 * Loads a new template
536 * @param string $content either filename (if $from_file == true), content of a template file, "high_compression", "highest_compression", "low_compression", or "default"
537 * @param bool $from_file uses $content as filename if true
540 * @see http://csstidy.sourceforge.net/templates.php
542 public function load_template($content, $from_file=true) {
543 $predefined_templates = & $this->data
['csstidy']['predefined_templates'];
544 if ($content === 'high_compression' ||
$content === 'default' ||
$content === 'highest_compression' ||
$content === 'low_compression') {
545 $this->template
= $predefined_templates[$content];
551 $content = strip_tags(file_get_contents($content), '<span>');
553 $content = str_replace("\r\n", "\n", $content); // Unify newlines (because the output also only uses \n)
554 $template = explode('|', $content);
556 for ($i = 0; $i < count($template); $i++
) {
557 $this->template
[$i] = $template[$i];
562 * Starts parsing from URL
567 public function parse_from_url($url) {
568 return $this->parse(@file_get_contents
($url));
572 * Checks if there is a token at the current position
573 * @param string $string
578 public function is_token(&$string, $i) {
579 return (strpos($this->tokens_list
, $string{$i}) !== false && !$this->escaped($string, $i));
583 * Parses CSS in $string. The code is saved as array in $this->css
584 * @param string $string the CSS code
589 public function parse($string) {
590 // Temporarily set locale to en_US in order to handle floats properly
591 $old = @setlocale
(LC_ALL
, 0);
592 @setlocale
(LC_ALL
, 'C');
594 // PHP bug? Settings need to be refreshed in PHP4
595 $this->print = new csstidy_print($this);
596 $this->optimise
= new csstidy_optimise($this);
598 $all_properties = & $this->data
['csstidy']['all_properties'];
599 $at_rules = & $this->data
['csstidy']['at_rules'];
600 $quoted_string_properties = & $this->data
['csstidy']['quoted_string_properties'];
602 $this->css
= array();
603 $this->print->input_css
= $string;
604 $string = str_replace("\r\n", "\n", $string) . ' ';
607 for ($i = 0, $size = strlen($string); $i < $size; $i++
) {
608 if ($string{$i} === "\n" ||
$string{$i} === "\r") {
612 switch ($this->status
) {
613 /* Case in at-block */
615 if ($this->is_token($string, $i)) {
616 if ($string{$i} === '/' && @$string{$i +
1} === '*') {
617 $this->status
= 'ic';
619 $this->from
[] = 'at';
620 } elseif ($string{$i} === '{') {
621 $this->status
= 'is';
622 $this->at
= $this->css_new_media_section($this->at
);
623 $this->_add_token(AT_START
, $this->at
);
624 } elseif ($string{$i} === ',') {
625 $this->at
= trim($this->at
) . ',';
626 } elseif ($string{$i} === '\\') {
627 $this->at
.= $this->_unicode($string, $i);
629 // fix for complicated media, i.e @media screen and (-webkit-min-device-pixel-ratio:1.5)
630 elseif (in_array($string{$i}, array('(', ')', ':', '.', '/'))) {
631 $this->at
.= $string{$i};
634 $lastpos = strlen($this->at
) - 1;
635 if (!( (ctype_space($this->at
{$lastpos}) ||
$this->is_token($this->at
, $lastpos) && $this->at
{$lastpos} === ',') && ctype_space($string{$i}))) {
636 $this->at
.= $string{$i};
641 /* Case in-selector */
643 if ($this->is_token($string, $i)) {
644 if ($string{$i} === '/' && @$string{$i +
1} === '*' && trim($this->selector
) == '') {
645 $this->status
= 'ic';
647 $this->from
[] = 'is';
648 } elseif ($string{$i} === '@' && trim($this->selector
) == '') {
650 $this->invalid_at
= true;
651 foreach ($at_rules as $name => $type) {
652 if (!strcasecmp(substr($string, $i +
1, strlen($name)), $name)) {
653 ($type === 'at') ?
$this->at
= '@' . $name : $this->selector
= '@' . $name;
654 if ($type === 'atis') {
655 $this->next_selector_at
= ($this->next_selector_at?
$this->next_selector_at
:($this->at?
$this->at
:DEFAULT_AT
));
656 $this->at
= $this->css_new_media_section(' ');
659 $this->status
= $type;
661 $this->invalid_at
= false;
665 if ($this->invalid_at
) {
666 $this->selector
= '@';
667 $invalid_at_name = '';
668 for ($j = $i +
1; $j < $size; ++
$j) {
669 if (!ctype_alpha($string{$j})) {
672 $invalid_at_name .= $string{$j};
674 $this->log('Invalid @-rule: ' . $invalid_at_name . ' (removed)', 'Warning');
676 } elseif (($string{$i} === '"' ||
$string{$i} === "'")) {
677 $this->cur_string
[] = $string{$i};
678 $this->status
= 'instr';
679 $this->str_char
[] = $string{$i};
680 $this->from
[] = 'is';
681 /* fixing CSS3 attribute selectors, i.e. a[href$=".mp3" */
682 $this->quoted_string
[] = ($string{$i - 1} === '=' );
683 } elseif ($this->invalid_at
&& $string{$i} === ';') {
684 $this->invalid_at
= false;
685 $this->status
= 'is';
686 if ($this->next_selector_at
) {
687 $this->at
= $this->css_new_media_section($this->next_selector_at
);
688 $this->next_selector_at
= '';
690 } elseif ($string{$i} === '{') {
691 $this->status
= 'ip';
692 if ($this->at
== '') {
693 $this->at
= $this->css_new_media_section(DEFAULT_AT
);
695 $this->selector
= $this->css_new_selector($this->at
,$this->selector
);
696 $this->_add_token(SEL_START
, $this->selector
);
697 $this->added
= false;
698 } elseif ($string{$i} === '}') {
699 $this->_add_token(AT_END
, $this->at
);
701 $this->selector
= '';
702 $this->sel_separate
= array();
703 } elseif ($string{$i} === ',') {
704 $this->selector
= trim($this->selector
) . ',';
705 $this->sel_separate
[] = strlen($this->selector
);
706 } elseif ($string{$i} === '\\') {
707 $this->selector
.= $this->_unicode($string, $i);
708 } elseif ($string{$i} === '*' && @in_array
($string{$i +
1}, array('.', '#', '[', ':')) && ($i==0 OR $string{$i - 1}!=='/')) {
709 // remove unnecessary universal selector, FS#147, but not comment in selector
711 $this->selector
.= $string{$i};
714 $lastpos = strlen($this->selector
) - 1;
715 if ($lastpos == -1 ||
!( (ctype_space($this->selector
{$lastpos}) ||
$this->is_token($this->selector
, $lastpos) && $this->selector
{$lastpos} === ',') && ctype_space($string{$i}))) {
716 $this->selector
.= $string{$i};
721 /* Case in-property */
723 if ($this->is_token($string, $i)) {
724 if (($string{$i} === ':' ||
$string{$i} === '=') && $this->property
!= '') {
725 $this->status
= 'iv';
726 if (!$this->get_cfg('discard_invalid_properties') ||
$this->property_is_valid($this->property
)) {
727 $this->property
= $this->css_new_property($this->at
,$this->selector
,$this->property
);
728 $this->_add_token(PROPERTY
, $this->property
);
730 } elseif ($string{$i} === '/' && @$string{$i +
1} === '*' && $this->property
== '') {
731 $this->status
= 'ic';
733 $this->from
[] = 'ip';
734 } elseif ($string{$i} === '}') {
735 $this->explode_selectors();
736 $this->status
= 'is';
737 $this->invalid_at
= false;
738 $this->_add_token(SEL_END
, $this->selector
);
739 $this->selector
= '';
740 $this->property
= '';
741 if ($this->next_selector_at
) {
742 $this->at
= $this->css_new_media_section($this->next_selector_at
);
743 $this->next_selector_at
= '';
745 } elseif ($string{$i} === ';') {
746 $this->property
= '';
747 } elseif ($string{$i} === '\\') {
748 $this->property
.= $this->_unicode($string, $i);
750 // else this is dumb IE a hack, keep it
752 elseif (($this->property
=== '' && !ctype_space($string{$i}))
753 ||
($this->property
=== '/' ||
$string{$i} === '/')) {
754 $this->property
.= $string{$i};
756 } elseif (!ctype_space($string{$i})) {
757 $this->property
.= $string{$i};
763 $pn = (($string{$i} === "\n" ||
$string{$i} === "\r") && $this->property_is_next($string, $i +
1) ||
$i == strlen($string) - 1);
764 if ($this->is_token($string, $i) ||
$pn) {
765 if ($string{$i} === '/' && @$string{$i +
1} === '*') {
766 $this->status
= 'ic';
768 $this->from
[] = 'iv';
769 } elseif (($string{$i} === '"' ||
$string{$i} === "'" ||
$string{$i} === '(')) {
770 $this->cur_string
[] = $string{$i};
771 $this->str_char
[] = ($string{$i} === '(') ?
')' : $string{$i};
772 $this->status
= 'instr';
773 $this->from
[] = 'iv';
774 $this->quoted_string
[] = in_array(strtolower($this->property
), $quoted_string_properties);
775 } elseif ($string{$i} === ',') {
776 $this->sub_value
= trim($this->sub_value
) . ',';
777 } elseif ($string{$i} === '\\') {
778 $this->sub_value
.= $this->_unicode($string, $i);
779 } elseif ($string{$i} === ';' ||
$pn) {
780 if ($this->selector
{0} === '@' && isset($at_rules[substr($this->selector
, 1)]) && $at_rules[substr($this->selector
, 1)] === 'iv') {
781 /* Add quotes to charset, import, namespace */
782 $this->sub_value_arr
[] = trim($this->sub_value
);
784 $this->status
= 'is';
786 switch ($this->selector
) {
787 case '@charset': $this->charset
= '"'.$this->sub_value_arr
[0].'"';
789 case '@namespace': $this->namespace = implode(' ', $this->sub_value_arr
);
791 case '@import': $this->import
[] = implode(' ', $this->sub_value_arr
);
795 $this->sub_value_arr
= array();
796 $this->sub_value
= '';
797 $this->selector
= '';
798 $this->sel_separate
= array();
800 $this->status
= 'ip';
802 } elseif ($string{$i} !== '}') {
803 $this->sub_value
.= $string{$i};
805 if (($string{$i} === '}' ||
$string{$i} === ';' ||
$pn) && !empty($this->selector
)) {
806 if ($this->at
== '') {
807 $this->at
= $this->css_new_media_section(DEFAULT_AT
);
811 if ($this->get_cfg('lowercase_s')) {
812 $this->selector
= strtolower($this->selector
);
814 $this->property
= strtolower($this->property
);
816 $this->optimise
->subvalue();
817 if ($this->sub_value
!= '') {
818 $this->sub_value_arr
[] = $this->sub_value
;
819 $this->sub_value
= '';
823 while (count($this->sub_value_arr
)) {
824 $sub = array_shift($this->sub_value_arr
);
825 if (strstr($this->selector
, 'font-face')) {
826 $sub = $this->quote_font_format($sub);
830 $this->value
.= ((!strlen($this->value
) ||
substr($this->value
,-1,1) === ',')?
'':' ').$sub;
833 $this->optimise
->value();
835 $valid = $this->property_is_valid($this->property
);
836 if ((!$this->invalid_at ||
$this->get_cfg('preserve_css')) && (!$this->get_cfg('discard_invalid_properties') ||
$valid)) {
837 $this->css_add_property($this->at
, $this->selector
, $this->property
, $this->value
);
838 $this->_add_token(VALUE
, $this->value
);
839 $this->optimise
->shorthands();
842 if ($this->get_cfg('discard_invalid_properties')) {
843 $this->log('Removed invalid property: ' . $this->property
, 'Warning');
845 $this->log('Invalid property in ' . strtoupper($this->get_cfg('css_level')) . ': ' . $this->property
, 'Warning');
849 $this->property
= '';
850 $this->sub_value_arr
= array();
853 if ($string{$i} === '}') {
854 $this->explode_selectors();
855 $this->_add_token(SEL_END
, $this->selector
);
856 $this->status
= 'is';
857 $this->invalid_at
= false;
858 $this->selector
= '';
859 if ($this->next_selector_at
) {
860 $this->at
= $this->css_new_media_section($this->next_selector_at
);
861 $this->next_selector_at
= '';
865 $this->sub_value
.= $string{$i};
867 if (ctype_space($string{$i})) {
868 $this->optimise
->subvalue();
869 if ($this->sub_value
!= '') {
870 $this->sub_value_arr
[] = $this->sub_value
;
871 $this->sub_value
= '';
879 $_str_char = $this->str_char
[count($this->str_char
)-1];
880 $_cur_string = $this->cur_string
[count($this->cur_string
)-1];
881 $_quoted_string = $this->quoted_string
[count($this->quoted_string
)-1];
882 $temp_add = $string{$i};
884 // Add another string to the stack. Strings can't be nested inside of quotes, only parentheses, but
885 // parentheticals can be nested more than once.
886 if ($_str_char === ")" && ($string{$i} === "(" ||
$string{$i} === '"' ||
$string{$i} === '\'') && !$this->escaped($string, $i)) {
887 $this->cur_string
[] = $string{$i};
888 $this->str_char
[] = $string{$i} === '(' ?
')' : $string{$i};
889 $this->from
[] = 'instr';
890 $this->quoted_string
[] = ($_str_char === ')' && $string{$i} !== '(' && trim($_cur_string)==='(')?
$_quoted_string:!($string{$i} === '(');
894 if ($_str_char !== ")" && ($string{$i} === "\n" ||
$string{$i} === "\r") && !($string{$i - 1} === '\\' && !$this->escaped($string, $i - 1))) {
896 $this->log('Fixed incorrect newline in string', 'Warning');
899 $_cur_string .= $temp_add;
901 if ($string{$i} === $_str_char && !$this->escaped($string, $i)) {
902 $this->status
= array_pop($this->from
);
904 if (!preg_match('|[' . implode('', $this->data
['csstidy']['whitespace']) . ']|uis', $_cur_string) && $this->property
!== 'content') {
905 if (!$_quoted_string) {
906 if ($_str_char !== ')') {
907 // Convert properties like
908 // font-family: 'Arial';
910 // font-family: Arial;
915 $_cur_string = substr($_cur_string, 1, -1);
918 $_quoted_string = false;
922 array_pop($this->cur_string
);
923 array_pop($this->quoted_string
);
924 array_pop($this->str_char
);
926 if ($_str_char === ')') {
927 $_cur_string = '(' . trim(substr($_cur_string, 1, -1)) . ')';
930 if ($this->status
=== 'iv') {
931 if (!$_quoted_string) {
932 if (strpos($_cur_string,',') !== false)
933 // we can on only remove space next to ','
934 $_cur_string = implode(',', array_map('trim', explode(',',$_cur_string)));
935 // and multiple spaces (too expensive)
936 if (strpos($_cur_string, ' ') !== false)
937 $_cur_string = preg_replace(",\s+,", ' ', $_cur_string);
939 $this->sub_value
.= $_cur_string;
940 } elseif ($this->status
=== 'is') {
941 $this->selector
.= $_cur_string;
942 } elseif ($this->status
=== 'instr') {
943 $this->cur_string
[count($this->cur_string
)-1] .= $_cur_string;
946 $this->cur_string
[count($this->cur_string
)-1] = $_cur_string;
950 /* Case in-comment */
952 if ($string{$i} === '*' && $string{$i +
1} === '/') {
953 $this->status
= array_pop($this->from
);
955 if (strlen($cur_comment) > 1 and strncmp($cur_comment, '!', 1) === 0) {
956 $this->_add_token(IMPORTANT_COMMENT
, $cur_comment);
957 $this->css_add_important_comment($cur_comment);
960 $this->_add_token(COMMENT
, $cur_comment);
964 $cur_comment .= $string{$i};
970 $this->optimise
->postparse();
972 $this->print->_reset();
974 @setlocale
(LC_ALL
, $old); // Set locale back to original setting
976 return!(empty($this->css
) && empty($this->import
) && empty($this->charset
) && empty($this->tokens
) && empty($this->namespace));
981 * format() in font-face needs quoted values for somes browser (FF at least)
986 public function quote_font_format($value) {
987 if (strncmp($value,'format',6) == 0) {
988 $p = strpos($value,')',7);
989 $end = substr($value,$p);
990 $format_strings = $this->parse_string_list(substr($value, 7, $p-7));
991 if (!$format_strings) {
996 foreach ($format_strings as $format_string) {
997 $value .= '"' . str_replace('"', '\\"', $format_string) . '",';
1000 $value = substr($value, 0, -1) . $end;
1007 * Explodes selectors
1011 public function explode_selectors() {
1012 // Explode multiple selectors
1013 if ($this->get_cfg('merge_selectors') === 1) {
1014 $new_sels = array();
1016 $this->sel_separate
[] = strlen($this->selector
);
1017 foreach ($this->sel_separate
as $num => $pos) {
1018 if ($num == count($this->sel_separate
) - 1) {
1022 $new_sels[] = substr($this->selector
, $lastpos, $pos - $lastpos - 1);
1026 if (count($new_sels) > 1) {
1027 foreach ($new_sels as $selector) {
1028 if (isset($this->css
[$this->at
][$this->selector
])) {
1029 $this->merge_css_blocks($this->at
, $selector, $this->css
[$this->at
][$this->selector
]);
1032 unset($this->css
[$this->at
][$this->selector
]);
1035 $this->sel_separate
= array();
1039 * Checks if a character is escaped (and returns true if it is)
1040 * @param string $string
1041 * @param integer $pos
1046 static function escaped(&$string, $pos) {
1047 return!(@($string{$pos - 1} !== '\\') || csstidy
::escaped($string, $pos - 1));
1052 * Add an important comment to the css code
1053 * (one we want to keep)
1056 public function css_add_important_comment($comment) {
1057 if ($this->get_cfg('preserve_css') ||
trim($comment) == '') {
1060 if (!isset($this->css
['!'])) {
1061 $this->css
['!'] = '';
1064 $this->css
['!'] .= "\n";
1066 $this->css
['!'] .= $comment;
1070 * Adds a property with value to the existing CSS code
1071 * @param string $media
1072 * @param string $selector
1073 * @param string $property
1074 * @param string $new_val
1078 public function css_add_property($media, $selector, $property, $new_val) {
1079 if ($this->get_cfg('preserve_css') ||
trim($new_val) == '') {
1083 $this->added
= true;
1084 if (isset($this->css
[$media][$selector][$property])) {
1085 if (($this->is_important($this->css
[$media][$selector][$property]) && $this->is_important($new_val)) ||
!$this->is_important($this->css
[$media][$selector][$property])) {
1086 $this->css
[$media][$selector][$property] = trim($new_val);
1089 $this->css
[$media][$selector][$property] = trim($new_val);
1094 * Start a new media section.
1095 * Check if the media is not already known,
1096 * else rename it with extra spaces
1099 * @param string $media
1102 public function css_new_media_section($media) {
1103 if ($this->get_cfg('preserve_css')) {
1107 // if the last @media is the same as this
1109 if (!$this->css ||
!is_array($this->css
) ||
empty($this->css
)) {
1113 $at = key($this->css
);
1114 if ($at == $media) {
1117 while (isset($this->css
[$media]))
1118 if (is_numeric($media))
1126 * Start a new selector.
1127 * If already referenced in this media section,
1128 * rename it with extra space to avoid merging
1129 * except if merging is required,
1130 * or last selector is the same (merge siblings)
1132 * never merge @font-face
1134 * @param string $media
1135 * @param string $selector
1138 public function css_new_selector($media,$selector) {
1139 if ($this->get_cfg('preserve_css')) {
1142 $selector = trim($selector);
1143 if (strncmp($selector,'@font-face',10)!=0) {
1144 if ($this->settings
['merge_selectors'] != false)
1147 if (!$this->css ||
!isset($this->css
[$media]) ||
!$this->css
[$media])
1150 // if last is the same, keep it
1151 end($this->css
[$media]);
1152 $sel = key($this->css
[$media]);
1153 if ($sel == $selector) {
1158 while (isset($this->css
[$media][$selector]))
1164 * Start a new propertie.
1165 * If already references in this selector,
1166 * rename it with extra space to avoid override
1168 * @param string $media
1169 * @param string $selector
1170 * @param string $property
1173 public function css_new_property($media, $selector, $property) {
1174 if ($this->get_cfg('preserve_css')) {
1177 if (!$this->css ||
!isset($this->css
[$media][$selector]) ||
!$this->css
[$media][$selector])
1180 while (isset($this->css
[$media][$selector][$property]))
1187 * Adds CSS to an existing media/selector
1188 * @param string $media
1189 * @param string $selector
1190 * @param array $css_add
1194 public function merge_css_blocks($media, $selector, $css_add) {
1195 foreach ($css_add as $property => $value) {
1196 $this->css_add_property($media, $selector, $property, $value, false);
1201 * Checks if $value is !important.
1202 * @param string $value
1207 public function is_important(&$value) {
1209 strpos($value, '!') !== false // quick test
1210 AND !strcasecmp(substr(str_replace($this->data
['csstidy']['whitespace'], '', $value), -10, 10), '!important'));
1214 * Returns a value without !important
1215 * @param string $value
1220 public function gvw_important($value) {
1221 if ($this->is_important($value)) {
1222 $value = trim($value);
1223 $value = substr($value, 0, -9);
1224 $value = trim($value);
1225 $value = substr($value, 0, -1);
1226 $value = trim($value);
1233 * Checks if the next word in a string from pos is a CSS property
1234 * @param string $istring
1235 * @param integer $pos
1240 public function property_is_next($istring, $pos) {
1241 $all_properties = & $this->data
['csstidy']['all_properties'];
1242 $istring = substr($istring, $pos, strlen($istring) - $pos);
1243 $pos = strpos($istring, ':');
1244 if ($pos === false) {
1247 $istring = strtolower(trim(substr($istring, 0, $pos)));
1248 if (isset($all_properties[$istring])) {
1249 $this->log('Added semicolon to the end of declaration', 'Warning');
1256 * Checks if a property is valid
1257 * @param string $property
1262 public function property_is_valid($property) {
1263 if (in_array(trim($property), $this->data
['csstidy']['multiple_properties'])) $property = trim($property);
1264 $all_properties = & $this->data
['csstidy']['all_properties'];
1265 return (isset($all_properties[$property]) && strpos($all_properties[$property], strtoupper($this->get_cfg('css_level'))) !== false );
1269 * Accepts a list of strings (e.g., the argument to format() in a @font-face src property)
1270 * and returns a list of the strings. Converts things like:
1272 * format(abc) => format("abc")
1273 * format(abc def) => format("abc","def")
1274 * format(abc "def") => format("abc","def")
1275 * format(abc, def, ghi) => format("abc","def","ghi")
1276 * format("abc",'def') => format("abc","def")
1277 * format("abc, def, ghi") => format("abc, def, ghi")
1283 public function parse_string_list($value) {
1284 $value = trim($value);
1287 if (!$value) return array();
1292 $current_string = '';
1294 for ($i = 0, $_len = strlen($value); $i < $_len; $i++
) {
1295 if (($value{$i} === ',' ||
$value{$i} === ' ') && $in_str === true) {
1297 $strings[] = $current_string;
1298 $current_string = '';
1299 } elseif ($value{$i} === '"' ||
$value{$i} === "'") {
1300 if ($in_str === $value{$i}) {
1301 $strings[] = $current_string;
1303 $current_string = '';
1305 } elseif (!$in_str) {
1306 $in_str = $value{$i};
1310 $current_string .= $value{$i};
1312 if (!preg_match("/[\s,]/", $value{$i})) {
1314 $current_string = $value{$i};
1320 if ($current_string) {
1321 $strings[] = $current_string;