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
37 * @todo //TODO: make them class constants of csstidy
39 define('AT_START', 1);
41 define('SEL_START', 3);
43 define('PROPERTY', 5);
46 define('IMPORTANT_COMMENT',8);
47 define('DEFAULT_AT', 41);
50 * Contains a class for printing CSS code
54 require('class.csstidy_print.php');
57 * Contains a class for optimising CSS code
61 require('class.csstidy_optimise.php');
66 * This class represents a CSS parser which reads CSS code and saves it in an array.
67 * In opposite to most other CSS parsers, it does not use regular expressions and
68 * thus has full CSS2 support and a higher reliability.
69 * Additional to that it applies some optimisations and fixes to the CSS code.
70 * An online version should be available here: http://cdburnerxp.se/cssparse/css_optimiser.php
72 * @author Florian Schmitz (floele at gmail dot com) 2005-2006
78 * Saves the parsed CSS. This array is empty if preserve_css is on.
82 public $css = array();
84 * Saves the parsed CSS (raw)
88 public $tokens = array();
98 * @see csstidy_optimise
104 * Saves the CSS charset (@charset)
108 public $charset = '';
110 * Saves all @import URLs
114 public $import = array();
116 * Saves the namespace
120 public $namespace = '';
122 * Contains the version of csstidy
126 public $version = '1.7.3';
128 * Stores the settings
132 public $settings = array();
134 * Saves the parser-status.
140 * - instr = in string (started at " or ' or ( )
141 * - ic = in comment (ignore everything)
147 public $status = 'is';
149 * Saves the current at rule (@media)
155 * Saves the at rule for next selector (during @font-face or other @)
159 public $next_selector_at = '';
162 * Saves the current selector
166 public $selector = '';
168 * Saves the current property
172 public $property = '';
174 * Saves the position of , in selectors
178 public $sel_separate = array();
180 * Saves the current value
186 * Saves the current sub-value
188 * Example for a subvalue:
189 * background:url(foo.png) red no-repeat;
190 * "url(foo.png)", "red", and "no-repeat" are subvalues,
191 * seperated by whitespace
195 public $sub_value = '';
197 * Array which saves all subvalues for a property.
202 public $sub_value_arr = array();
204 * Saves the stack of characters that opened the current strings
208 public $str_char = array();
209 public $cur_string = array();
211 * Status from which the parser switched to ic or instr
215 public $from = array();
218 * =true if in invalid at-rule
222 public $invalid_at = false;
224 * =true if something has been added to the current selector
228 public $added = false;
230 * Array which saves the message log
234 public $log = array();
236 * Saves the line number
242 * Marks if we need to leave quotes for a string
246 public $quoted_string = array();
252 public $tokens_list = "";
255 * Various CSS Data for CSSTidy
258 public $data = array();
261 * Loads standard template and sets default settings
265 public function __construct() {
267 include('data.inc.php');
270 $this->settings
['remove_bslash'] = true;
271 $this->settings
['compress_colors'] = true;
272 $this->settings
['compress_font-weight'] = true;
273 $this->settings
['lowercase_s'] = false;
275 1 common shorthands optimization
276 2 + font property optimization
277 3 + background property optimization
279 $this->settings
['optimise_shorthands'] = 1;
280 $this->settings
['remove_last_;'] = true;
281 $this->settings
['space_before_important'] = false;
282 /* rewrite all properties with low case, better for later gzip OK, safe*/
283 $this->settings
['case_properties'] = 1;
284 /* sort properties in alpabetic order, better for later gzip
285 * but can cause trouble in case of overiding same propertie or using hack
287 $this->settings
['sort_properties'] = false;
289 1, 3, 5, etc -- enable sorting selectors inside @media: a{}b{}c{}
290 2, 5, 8, etc -- enable sorting selectors inside one CSS declaration: a,b,c{}
291 preserve order by default cause it can break functionnality
293 $this->settings
['sort_selectors'] = 0;
294 /* is dangeroues to be used: CSS is broken sometimes */
295 $this->settings
['merge_selectors'] = 0;
296 /* preserve or not browser hacks */
298 /* Useful to produce a rtl css from a ltr one (or the opposite) */
299 $this->settings
['reverse_left_and_right'] = 0;
301 $this->settings
['discard_invalid_selectors'] = false;
302 $this->settings
['discard_invalid_properties'] = false;
303 $this->settings
['css_level'] = 'CSS3.0';
304 $this->settings
['preserve_css'] = false;
305 $this->settings
['timestamp'] = false;
306 $this->settings
['template'] = ''; // say that propertie exist
307 $this->set_cfg('template','default'); // call load_template
308 $this->optimise
= new csstidy_optimise($this);
310 $this->tokens_list
= & $this->data
['csstidy']['tokens'];
314 * Get the value of a setting.
315 * @param string $setting
320 public function get_cfg($setting) {
321 if (isset($this->settings
[$setting])) {
322 return $this->settings
[$setting];
329 * @param string $template used by set_cfg to load a template via a configuration setting
333 public function _load_template($template) {
336 $this->load_template('default');
340 $this->load_template('highest_compression');
344 $this->load_template('high_compression');
348 $this->load_template('low_compression');
352 $this->load_template($template);
358 * Set the value of a setting.
359 * @param string $setting
360 * @param mixed $value
365 public function set_cfg($setting, $value=null) {
366 if (is_array($setting) && $value === null) {
367 foreach ($setting as $setprop => $setval) {
368 $this->settings
[$setprop] = $setval;
370 if (array_key_exists('template', $setting)) {
371 $this->_load_template($this->settings
['template']);
374 } elseif (isset($this->settings
[$setting]) && $value !== '') {
375 $this->settings
[$setting] = $value;
376 if ($setting === 'template') {
377 $this->_load_template($this->settings
['template']);
385 * Adds a token to $this->tokens
387 * @param string $data
388 * @param bool $do add a token even if preserve_css is off
392 public function _add_token($type, $data, $do = false) {
393 if ($this->get_cfg('preserve_css') ||
$do) {
394 // nested @... : if opening a new part we just closed, remove the previous closing instead of adding opening
395 if ($type === AT_START
396 and count($this->tokens
)
397 and $last = end($this->tokens
)
398 and $last[0] === AT_END
399 and $last[1] === trim($data)) {
400 array_pop($this->tokens
);
403 $this->tokens
[] = array($type, ($type == COMMENT
or $type == IMPORTANT_COMMENT
) ?
$data : trim($data));
409 * Add a message to the message log
410 * @param string $message
411 * @param string $type
412 * @param integer $line
416 public function log($message, $type, $line = -1) {
420 $line = intval($line);
421 $add = array('m' => $message, 't' => $type);
422 if (!isset($this->log
[$line]) ||
!in_array($add, $this->log
[$line])) {
423 $this->log
[$line][] = $add;
428 * Parse unicode notations and find a replacement character
429 * @param string $string
435 public function _unicode(&$string, &$i) {
440 while ($i < strlen($string) && (ctype_xdigit($string[$i]) ||
ctype_space($string[$i])) && strlen($add) < 6) {
443 if (ctype_space($string[$i])) {
449 if (hexdec($add) > 47 && hexdec($add) < 58 ||
hexdec($add) > 64 && hexdec($add) < 91 ||
hexdec($add) > 96 && hexdec($add) < 123) {
450 $this->log('Replaced unicode notation: Changed \\' . $add . ' to ' . chr(hexdec($add)), 'Information');
451 $add = chr(hexdec($add));
454 $add = trim('\\' . $add);
457 if (@ctype_xdigit
($string[$i +
1]) && ctype_space($string[$i])
458 && !$replaced ||
!ctype_space($string[$i])) {
462 if ($add !== '\\' ||
!$this->get_cfg('remove_bslash') ||
strpos($this->tokens_list
, $string[$i +
1]) !== false) {
467 $this->log('Removed unnecessary backslash', 'Information');
473 * Write formatted output to a file
474 * @param string $filename
475 * @param string $doctype when printing formatted, is a shorthand for the document type
476 * @param bool $externalcss when printing formatted, indicates whether styles to be attached internally or as an external stylesheet
477 * @param string $title when printing formatted, is the title to be added in the head of the document
478 * @param string $lang when printing formatted, gives a two-letter language code to be added to the output
482 public function write_page($filename, $doctype='xhtml1.1', $externalcss=true, $title='', $lang='en') {
483 $this->write($filename, true);
487 * Write plain output to a file
488 * @param string $filename
489 * @param bool $formatted whether to print formatted or not
490 * @param string $doctype when printing formatted, is a shorthand for the document type
491 * @param bool $externalcss when printing formatted, indicates whether styles to be attached internally or as an external stylesheet
492 * @param string $title when printing formatted, is the title to be added in the head of the document
493 * @param string $lang when printing formatted, gives a two-letter language code to be added to the output
494 * @param bool $pre_code whether to add pre and code tags around the code (for light HTML formatted templates)
498 public function write($filename, $formatted=false, $doctype='xhtml1.1', $externalcss=true, $title='', $lang='en', $pre_code=true) {
499 $filename .= ( $formatted) ?
'.xhtml' : '.css';
501 if (!is_dir('temp')) {
502 $madedir = mkdir('temp');
504 print 'Could not make directory "temp" in ' . dirname(__FILE__
);
508 $handle = fopen('temp/' . $filename, 'w');
511 fwrite($handle, $this->print->plain());
513 fwrite($handle, $this->print->formatted_page($doctype, $externalcss, $title, $lang, $pre_code));
520 * Loads a new template
521 * @param string $content either filename (if $from_file == true), content of a template file, "high_compression", "highest_compression", "low_compression", or "default"
522 * @param bool $from_file uses $content as filename if true
525 * @see http://csstidy.sourceforge.net/templates.php
527 public function load_template($content, $from_file=true) {
528 $predefined_templates = & $this->data
['csstidy']['predefined_templates'];
529 if ($content === 'high_compression' ||
$content === 'default' ||
$content === 'highest_compression' ||
$content === 'low_compression') {
530 $this->template
= $predefined_templates[$content];
536 $content = strip_tags(file_get_contents($content), '<span>');
538 $content = str_replace("\r\n", "\n", $content); // Unify newlines (because the output also only uses \n)
539 $template = explode('|', $content);
541 for ($i = 0; $i < count($template); $i++
) {
542 $this->template
[$i] = $template[$i];
547 * Starts parsing from URL
552 public function parse_from_url($url) {
553 return $this->parse(@file_get_contents
($url));
557 * Checks if there is a token at the current position
558 * @param string $string
563 public function is_token(&$string, $i) {
564 return (strpos($this->tokens_list
, $string[$i]) !== false && !$this->escaped($string, $i));
568 * Parses CSS in $string. The code is saved as array in $this->css
569 * @param string $string the CSS code
574 public function parse($string) {
575 // Temporarily set locale to en_US in order to handle floats properly
576 $old = @setlocale
(LC_ALL
, 0);
577 @setlocale
(LC_ALL
, 'C');
579 // PHP bug? Settings need to be refreshed in PHP4
580 $this->print = new csstidy_print($this);
581 $this->optimise
= new csstidy_optimise($this);
583 $all_properties = & $this->data
['csstidy']['all_properties'];
584 $at_rules = & $this->data
['csstidy']['at_rules'];
585 $quoted_string_properties = & $this->data
['csstidy']['quoted_string_properties'];
587 $this->css
= array();
588 $this->print->input_css
= $string;
589 $string = str_replace("\r\n", "\n", $string) . ' ';
593 for ($i = 0, $size = strlen($string); $i < $size; $i++
) {
594 if ($string[$i] === "\n" ||
$string[$i] === "\r") {
598 switch ($this->status
) {
599 /* Case in at-block */
601 if ($this->is_token($string, $i)) {
602 if ($string[$i] === '/' && @$string[$i +
1] === '*') {
603 $this->status
= 'ic';
605 $this->from
[] = 'at';
606 } elseif ($string[$i] === '{') {
607 $this->status
= 'is';
608 $this->at
= $this->css_new_media_section($this->at
, $cur_at);
609 $this->_add_token(AT_START
, $this->at
);
610 } elseif ($string[$i] === ',') {
611 $cur_at = trim($cur_at) . ',';
612 } elseif ($string[$i] === '\\') {
613 $cur_at .= $this->_unicode($string, $i);
615 // fix for complicated media, i.e @media screen and (-webkit-min-device-pixel-ratio:1.5)
616 elseif (in_array($string[$i], array('(', ')', ':', '.', '/'))) {
617 $cur_at .= $string[$i];
620 $lastpos = strlen($cur_at) - 1;
621 if (!( (ctype_space($cur_at[$lastpos]) ||
$this->is_token($cur_at, $lastpos) && $cur_at[$lastpos] === ',') && ctype_space($string[$i]))) {
622 $cur_at .= $string[$i];
627 /* Case in-selector */
629 if ($this->is_token($string, $i)) {
630 if ($string[$i] === '/' && @$string[$i +
1] === '*' && trim($this->selector
) == '') {
631 $this->status
= 'ic';
633 $this->from
[] = 'is';
634 } elseif ($string[$i] === '@' && trim($this->selector
) == '') {
636 $this->invalid_at
= true;
637 foreach ($at_rules as $name => $type) {
638 if (!strcasecmp(substr($string, $i +
1, strlen($name)), $name)) {
639 ($type === 'at') ?
$cur_at = '@' . $name : $this->selector
= '@' . $name;
640 if ($type === 'atis') {
641 $this->next_selector_at
= ($this->next_selector_at?
$this->next_selector_at
:($this->at?
$this->at
:DEFAULT_AT
));
642 $this->at
= $this->css_new_media_section($this->at
, ' ', true);
645 $this->status
= $type;
647 $this->invalid_at
= false;
652 if ($this->invalid_at
) {
653 $this->selector
= '@';
654 $invalid_at_name = '';
655 for ($j = $i +
1; $j < $size; ++
$j) {
656 if (!ctype_alpha($string[$j])) {
659 $invalid_at_name .= $string[$j];
661 $this->log('Invalid @-rule: ' . $invalid_at_name . ' (removed)', 'Warning');
663 } elseif (($string[$i] === '"' ||
$string[$i] === "'")) {
664 $this->cur_string
[] = $string[$i];
665 $this->status
= 'instr';
666 $this->str_char
[] = $string[$i];
667 $this->from
[] = 'is';
668 /* fixing CSS3 attribute selectors, i.e. a[href$=".mp3" */
669 $this->quoted_string
[] = ($string[$i - 1] === '=' );
670 } elseif ($this->invalid_at
&& $string[$i] === ';') {
671 $this->invalid_at
= false;
672 $this->status
= 'is';
673 if ($this->next_selector_at
) {
674 $this->at
= $this->css_close_media_section($this->at
);
675 $this->at
= $this->css_new_media_section($this->at
, $this->next_selector_at
);
676 $this->next_selector_at
= '';
678 } elseif ($string[$i] === '{') {
679 $this->status
= 'ip';
680 if ($this->at
== '') {
681 $this->at
= $this->css_new_media_section($this->at
, DEFAULT_AT
);
683 $this->selector
= $this->css_new_selector($this->at
,$this->selector
);
684 $this->_add_token(SEL_START
, $this->selector
);
685 $this->added
= false;
686 } elseif ($string[$i] === '}') {
687 $this->_add_token(AT_END
, $this->at
);
688 $this->at
= $this->css_close_media_section($this->at
);
689 $this->selector
= '';
690 $this->sel_separate
= array();
691 } elseif ($string[$i] === ',') {
692 $this->selector
= trim($this->selector
) . ',';
693 $this->sel_separate
[] = strlen($this->selector
);
694 } elseif ($string[$i] === '\\') {
695 $this->selector
.= $this->_unicode($string, $i);
696 } elseif ($string[$i] === '*' && @in_array
($string[$i +
1], array('.', '#', '[', ':')) && ($i==0 OR $string[$i - 1]!=='/')) {
697 // remove unnecessary universal selector, FS#147, but not comment in selector
699 $this->selector
.= $string[$i];
702 $lastpos = strlen($this->selector
) - 1;
703 if ($lastpos == -1 ||
!( (ctype_space($this->selector
[$lastpos]) ||
$this->is_token($this->selector
, $lastpos) && $this->selector
[$lastpos] === ',') && ctype_space($string[$i]))) {
704 $this->selector
.= $string[$i];
709 /* Case in-property */
711 if ($this->is_token($string, $i)) {
712 if (($string[$i] === ':' ||
$string[$i] === '=') && $this->property
!= '') {
713 $this->status
= 'iv';
714 if (!$this->get_cfg('discard_invalid_properties') ||
$this->property_is_valid($this->property
)) {
715 $this->property
= $this->css_new_property($this->at
,$this->selector
,$this->property
);
716 $this->_add_token(PROPERTY
, $this->property
);
718 } elseif ($string[$i] === '/' && @$string[$i +
1] === '*' && $this->property
== '') {
719 $this->status
= 'ic';
721 $this->from
[] = 'ip';
722 } elseif ($string[$i] === '}') {
723 $this->explode_selectors();
724 $this->status
= 'is';
725 $this->invalid_at
= false;
726 $this->_add_token(SEL_END
, $this->selector
);
727 $this->selector
= '';
728 $this->property
= '';
729 if ($this->next_selector_at
) {
730 $this->at
= $this->css_close_media_section($this->at
);
731 $this->at
= $this->css_new_media_section($this->at
, $this->next_selector_at
);
732 $this->next_selector_at
= '';
734 } elseif ($string[$i] === ';') {
735 $this->property
= '';
736 } elseif ($string[$i] === '\\') {
737 $this->property
.= $this->_unicode($string, $i);
739 // else this is dumb IE a hack, keep it
741 elseif (($this->property
=== '' && !ctype_space($string[$i]))
742 ||
($this->property
=== '/' ||
$string[$i] === '/')) {
743 $this->property
.= $string[$i];
745 } elseif (!ctype_space($string[$i])) {
746 $this->property
.= $string[$i];
752 $pn = (($string[$i] === "\n" ||
$string[$i] === "\r") && $this->property_is_next($string, $i +
1) ||
$i == strlen($string) - 1);
753 if ($this->is_token($string, $i) ||
$pn) {
754 if ($string[$i] === '/' && @$string[$i +
1] === '*') {
755 $this->status
= 'ic';
757 $this->from
[] = 'iv';
758 } elseif (($string[$i] === '"' ||
$string[$i] === "'" ||
$string[$i] === '(')) {
759 $this->cur_string
[] = $string[$i];
760 $this->str_char
[] = ($string[$i] === '(') ?
')' : $string[$i];
761 $this->status
= 'instr';
762 $this->from
[] = 'iv';
763 $this->quoted_string
[] = in_array(strtolower($this->property
), $quoted_string_properties);
764 } elseif ($string[$i] === ',') {
765 $this->sub_value
= trim($this->sub_value
) . ',';
766 } elseif ($string[$i] === '\\') {
767 $this->sub_value
.= $this->_unicode($string, $i);
768 } elseif ($string[$i] === ';' ||
$pn) {
769 if ($this->selector
[0] === '@' && isset($at_rules[substr($this->selector
, 1)]) && $at_rules[substr($this->selector
, 1)] === 'iv') {
770 /* Add quotes to charset, import, namespace */
771 $this->sub_value_arr
[] = trim($this->sub_value
);
773 $this->status
= 'is';
775 switch ($this->selector
) {
776 case '@charset': $this->charset
= '"'.$this->sub_value_arr
[0].'"';
778 case '@namespace': $this->namespace = implode(' ', $this->sub_value_arr
);
780 case '@import': $this->import
[] = implode(' ', $this->sub_value_arr
);
784 $this->sub_value_arr
= array();
785 $this->sub_value
= '';
786 $this->selector
= '';
787 $this->sel_separate
= array();
789 $this->status
= 'ip';
791 } elseif ($string[$i] !== '}') {
792 $this->sub_value
.= $string[$i];
794 if (($string[$i] === '}' ||
$string[$i] === ';' ||
$pn) && !empty($this->selector
)) {
795 if ($this->at
== '') {
796 $this->at
= $this->css_new_media_section($this->at
,DEFAULT_AT
);
800 if ($this->get_cfg('lowercase_s')) {
801 $this->selector
= strtolower($this->selector
);
803 $this->property
= strtolower($this->property
);
805 $this->optimise
->subvalue();
806 if ($this->sub_value
!= '') {
807 $this->sub_value_arr
[] = $this->sub_value
;
808 $this->sub_value
= '';
812 while (count($this->sub_value_arr
)) {
813 $sub = array_shift($this->sub_value_arr
);
814 if (strstr($this->selector
, 'font-face')) {
815 $sub = $this->quote_font_format($sub);
819 $this->value
.= ((!strlen($this->value
) ||
substr($this->value
,-1,1) === ',')?
'':' ').$sub;
822 $this->optimise
->value();
824 $valid = $this->property_is_valid($this->property
);
825 if ((!$this->invalid_at ||
$this->get_cfg('preserve_css')) && (!$this->get_cfg('discard_invalid_properties') ||
$valid)) {
826 $this->css_add_property($this->at
, $this->selector
, $this->property
, $this->value
);
827 $this->_add_token(VALUE
, $this->value
);
828 $this->optimise
->shorthands();
831 if ($this->get_cfg('discard_invalid_properties')) {
832 $this->log('Removed invalid property: ' . $this->property
, 'Warning');
834 $this->log('Invalid property in ' . strtoupper($this->get_cfg('css_level')) . ': ' . $this->property
, 'Warning');
838 $this->property
= '';
839 $this->sub_value_arr
= array();
842 if ($string[$i] === '}') {
843 $this->explode_selectors();
844 $this->_add_token(SEL_END
, $this->selector
);
845 $this->status
= 'is';
846 $this->invalid_at
= false;
847 $this->selector
= '';
848 if ($this->next_selector_at
) {
849 $this->at
= $this->css_close_media_section($this->at
);
850 $this->at
= $this->css_new_media_section($this->at
, $this->next_selector_at
);
851 $this->next_selector_at
= '';
855 $this->sub_value
.= $string[$i];
857 if (ctype_space($string[$i])) {
858 $this->optimise
->subvalue();
859 if ($this->sub_value
!= '') {
860 $this->sub_value_arr
[] = $this->sub_value
;
861 $this->sub_value
= '';
869 $_str_char = $this->str_char
[count($this->str_char
)-1];
870 $_cur_string = $this->cur_string
[count($this->cur_string
)-1];
871 $_quoted_string = $this->quoted_string
[count($this->quoted_string
)-1];
872 $temp_add = $string[$i];
874 // Add another string to the stack. Strings can't be nested inside of quotes, only parentheses, but
875 // parentheticals can be nested more than once.
876 if ($_str_char === ")" && ($string[$i] === "(" ||
$string[$i] === '"' ||
$string[$i] === '\'') && !$this->escaped($string, $i)) {
877 $this->cur_string
[] = $string[$i];
878 $this->str_char
[] = $string[$i] === '(' ?
')' : $string[$i];
879 $this->from
[] = 'instr';
880 $this->quoted_string
[] = ($_str_char === ')' && $string[$i] !== '(' && trim($_cur_string)==='(')?
$_quoted_string:!($string[$i] === '(');
884 if ($_str_char !== ")" && ($string[$i] === "\n" ||
$string[$i] === "\r") && !($string[$i - 1] === '\\' && !$this->escaped($string, $i - 1))) {
886 $this->log('Fixed incorrect newline in string', 'Warning');
889 $_cur_string .= $temp_add;
891 if ($string[$i] === $_str_char && !$this->escaped($string, $i)) {
892 $this->status
= array_pop($this->from
);
894 if (!preg_match('|[' . implode('', $this->data
['csstidy']['whitespace']) . ']|uis', $_cur_string) && $this->property
!== 'content') {
895 if (!$_quoted_string) {
896 if ($_str_char !== ')') {
897 // Convert properties like
898 // font-family: 'Arial';
900 // font-family: Arial;
905 $_cur_string = substr($_cur_string, 1, -1);
908 $_quoted_string = false;
912 array_pop($this->cur_string
);
913 array_pop($this->quoted_string
);
914 array_pop($this->str_char
);
916 if ($_str_char === ')') {
917 $_cur_string = '(' . trim(substr($_cur_string, 1, -1)) . ')';
920 if ($this->status
=== 'iv') {
921 if (!$_quoted_string) {
922 if (strpos($_cur_string,',') !== false)
923 // we can on only remove space next to ','
924 $_cur_string = implode(',', array_map('trim', explode(',',$_cur_string)));
925 // and multiple spaces (too expensive)
926 if (strpos($_cur_string, ' ') !== false)
927 $_cur_string = preg_replace(",\s+,", ' ', $_cur_string);
929 $this->sub_value
.= $_cur_string;
930 } elseif ($this->status
=== 'is') {
931 $this->selector
.= $_cur_string;
932 } elseif ($this->status
=== 'instr') {
933 $this->cur_string
[count($this->cur_string
)-1] .= $_cur_string;
936 $this->cur_string
[count($this->cur_string
)-1] = $_cur_string;
940 /* Case in-comment */
942 if ($string[$i] === '*' && $string[$i +
1] === '/') {
943 $this->status
= array_pop($this->from
);
945 if (strlen($cur_comment) > 1 and strncmp($cur_comment, '!', 1) === 0) {
946 $this->_add_token(IMPORTANT_COMMENT
, $cur_comment);
947 $this->css_add_important_comment($cur_comment);
950 $this->_add_token(COMMENT
, $cur_comment);
954 $cur_comment .= $string[$i];
960 $this->optimise
->postparse();
962 $this->print->_reset();
964 @setlocale
(LC_ALL
, $old); // Set locale back to original setting
966 return!(empty($this->css
) && empty($this->import
) && empty($this->charset
) && empty($this->tokens
) && empty($this->namespace));
971 * format() in font-face needs quoted values for somes browser (FF at least)
976 public function quote_font_format($value) {
977 if (strncmp($value,'format',6) == 0) {
978 $p = strpos($value,')',7);
979 $end = substr($value,$p);
980 $format_strings = $this->parse_string_list(substr($value, 7, $p-7));
981 if (!$format_strings) {
986 foreach ($format_strings as $format_string) {
987 $value .= '"' . str_replace('"', '\\"', $format_string) . '",';
990 $value = substr($value, 0, -1) . $end;
1001 public function explode_selectors() {
1002 // Explode multiple selectors
1003 if ($this->get_cfg('merge_selectors') === 1) {
1004 $new_sels = array();
1006 $this->sel_separate
[] = strlen($this->selector
);
1007 foreach ($this->sel_separate
as $num => $pos) {
1008 if ($num == count($this->sel_separate
) - 1) {
1012 $new_sels[] = substr($this->selector
, $lastpos, $pos - $lastpos - 1);
1016 if (count($new_sels) > 1) {
1017 foreach ($new_sels as $selector) {
1018 if (isset($this->css
[$this->at
][$this->selector
])) {
1019 $this->merge_css_blocks($this->at
, $selector, $this->css
[$this->at
][$this->selector
]);
1022 unset($this->css
[$this->at
][$this->selector
]);
1025 $this->sel_separate
= array();
1029 * Checks if a character is escaped (and returns true if it is)
1030 * @param string $string
1031 * @param integer $pos
1036 static function escaped(&$string, $pos) {
1037 return!(@($string[$pos - 1] !== '\\') || csstidy
::escaped($string, $pos - 1));
1042 * Add an important comment to the css code
1043 * (one we want to keep)
1046 public function css_add_important_comment($comment) {
1047 if ($this->get_cfg('preserve_css') ||
trim($comment) == '') {
1050 if (!isset($this->css
['!'])) {
1051 $this->css
['!'] = '';
1054 $this->css
['!'] .= "\n";
1056 $this->css
['!'] .= $comment;
1060 * Adds a property with value to the existing CSS code
1061 * @param string $media
1062 * @param string $selector
1063 * @param string $property
1064 * @param string $new_val
1068 public function css_add_property($media, $selector, $property, $new_val) {
1069 if ($this->get_cfg('preserve_css') ||
trim($new_val) == '') {
1073 $this->added
= true;
1074 if (isset($this->css
[$media][$selector][$property])) {
1075 if (($this->is_important($this->css
[$media][$selector][$property]) && $this->is_important($new_val)) ||
!$this->is_important($this->css
[$media][$selector][$property])) {
1076 $this->css
[$media][$selector][$property] = trim($new_val);
1079 $this->css
[$media][$selector][$property] = trim($new_val);
1084 * Check if a current media section is the continuation of the last one
1085 * if not inc the name of the media section to avoid a merging
1087 * @param int|string $media
1088 * @return int|string
1090 public function css_check_last_media_section_or_inc($media) {
1092 if (!$this->css ||
!is_array($this->css
) ||
empty($this->css
)) {
1096 // if the last @media is the same as this
1099 $at = key($this->css
);
1100 if ($at == $media) {
1104 // else inc the section in the array
1105 while (isset($this->css
[$media]))
1106 if (is_numeric($media))
1114 * Start a new media section.
1115 * Check if the media is not already known,
1116 * else rename it with extra spaces
1119 * @param string $current_media
1120 * @param string $media
1121 * @param bool $at_root
1124 public function css_new_media_section($current_media, $new_media, $at_root = false) {
1125 if ($this->get_cfg('preserve_css')) {
1129 // if we already are in a media and CSS level is 3, manage nested medias
1132 // numeric $current_media means DEFAULT_AT or inc
1133 && !is_numeric($current_media)
1134 && strncmp($this->get_cfg('css_level'), 'CSS3', 4) == 0) {
1136 $new_media = rtrim($current_media) . "{" . rtrim($new_media);
1139 return $this->css_check_last_media_section_or_inc($new_media);
1143 * Close a media section
1144 * Find the parent media we were in before or the root
1145 * @param $current_media
1148 public function css_close_media_section($current_media) {
1149 if ($this->get_cfg('preserve_css')) {
1153 if (strpos($current_media, '{') !== false) {
1154 $current_media = explode('{', $current_media);
1155 array_pop($current_media);
1156 $current_media = implode('{', $current_media);
1157 return $current_media;
1164 * Start a new selector.
1165 * If already referenced in this media section,
1166 * rename it with extra space to avoid merging
1167 * except if merging is required,
1168 * or last selector is the same (merge siblings)
1170 * never merge @font-face
1172 * @param string $media
1173 * @param string $selector
1176 public function css_new_selector($media,$selector) {
1177 if ($this->get_cfg('preserve_css')) {
1180 $selector = trim($selector);
1181 if (strncmp($selector,'@font-face',10)!=0) {
1182 if ($this->settings
['merge_selectors'] != false)
1185 if (!$this->css ||
!isset($this->css
[$media]) ||
!$this->css
[$media])
1188 // if last is the same, keep it
1189 end($this->css
[$media]);
1190 $sel = key($this->css
[$media]);
1191 if ($sel == $selector) {
1196 while (isset($this->css
[$media][$selector]))
1202 * Start a new propertie.
1203 * If already references in this selector,
1204 * rename it with extra space to avoid override
1206 * @param string $media
1207 * @param string $selector
1208 * @param string $property
1211 public function css_new_property($media, $selector, $property) {
1212 if ($this->get_cfg('preserve_css')) {
1215 if (!$this->css ||
!isset($this->css
[$media][$selector]) ||
!$this->css
[$media][$selector])
1218 while (isset($this->css
[$media][$selector][$property]))
1225 * Adds CSS to an existing media/selector
1226 * @param string $media
1227 * @param string $selector
1228 * @param array $css_add
1232 public function merge_css_blocks($media, $selector, $css_add) {
1233 foreach ($css_add as $property => $value) {
1234 $this->css_add_property($media, $selector, $property, $value, false);
1239 * Checks if $value is !important.
1240 * @param string $value
1245 public function is_important(&$value) {
1247 strpos($value, '!') !== false // quick test
1248 AND !strcasecmp(substr(str_replace($this->data
['csstidy']['whitespace'], '', $value), -10, 10), '!important'));
1252 * Returns a value without !important
1253 * @param string $value
1258 public function gvw_important($value) {
1259 if ($this->is_important($value)) {
1260 $value = trim($value);
1261 $value = substr($value, 0, -9);
1262 $value = trim($value);
1263 $value = substr($value, 0, -1);
1264 $value = trim($value);
1271 * Checks if the next word in a string from pos is a CSS property
1272 * @param string $istring
1273 * @param integer $pos
1278 public function property_is_next($istring, $pos) {
1279 $all_properties = & $this->data
['csstidy']['all_properties'];
1280 $istring = substr($istring, $pos, strlen($istring) - $pos);
1281 $pos = strpos($istring, ':');
1282 if ($pos === false) {
1285 $istring = strtolower(trim(substr($istring, 0, $pos)));
1286 if (isset($all_properties[$istring])) {
1287 $this->log('Added semicolon to the end of declaration', 'Warning');
1294 * Checks if a property is valid
1295 * @param string $property
1300 public function property_is_valid($property) {
1301 if (in_array(trim($property), $this->data
['csstidy']['multiple_properties'])) $property = trim($property);
1302 $all_properties = & $this->data
['csstidy']['all_properties'];
1303 return (isset($all_properties[$property]) && strpos($all_properties[$property], strtoupper($this->get_cfg('css_level'))) !== false );
1307 * Accepts a list of strings (e.g., the argument to format() in a @font-face src property)
1308 * and returns a list of the strings. Converts things like:
1310 * format(abc) => format("abc")
1311 * format(abc def) => format("abc","def")
1312 * format(abc "def") => format("abc","def")
1313 * format(abc, def, ghi) => format("abc","def","ghi")
1314 * format("abc",'def') => format("abc","def")
1315 * format("abc, def, ghi") => format("abc, def, ghi")
1320 public function parse_string_list($value) {
1321 $value = trim($value);
1324 if (!$value) return array();
1329 $current_string = '';
1331 for ($i = 0, $_len = strlen($value); $i < $_len; $i++
) {
1332 if (($value[$i] === ',' ||
$value[$i] === ' ') && $in_str === true) {
1334 $strings[] = $current_string;
1335 $current_string = '';
1336 } elseif ($value[$i] === '"' ||
$value[$i] === "'") {
1337 if ($in_str === $value[$i]) {
1338 $strings[] = $current_string;
1340 $current_string = '';
1342 } elseif (!$in_str) {
1343 $in_str = $value[$i];
1347 $current_string .= $value[$i];
1349 if (!preg_match("/[\s,]/", $value[$i])) {
1351 $current_string = $value[$i];
1357 if ($current_string) {
1358 $strings[] = $current_string;