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
34 * Defines ctype functions if required
38 require_once('class.csstidy_ctype.php');
41 * Various CSS data needed for correct optimisations etc.
45 require('data.inc.php');
48 * Contains a class for printing CSS code
52 require('class.csstidy_print.php');
55 * Contains a class for optimising CSS code
59 require('class.csstidy_optimise.php');
65 * This class represents a CSS parser which reads CSS code and saves it in an array.
66 * In opposite to most other CSS parsers, it does not use regular expressions and
67 * thus has full CSS2 support and a higher reliability.
68 * Additional to that it applies some optimisations and fixes to the CSS code.
69 * An online version should be available here: http://cdburnerxp.se/cssparse/css_optimiser.php
71 * @author Florian Schmitz (floele at gmail dot com) 2005-2006
77 * Saves the parsed CSS. This array is empty if preserve_css is on.
83 * Saves the parsed CSS (raw)
87 var $tokens = array();
97 * @see csstidy_optimise
103 * Saves the CSS charset (@charset)
109 * Saves all @import URLs
113 var $import = array();
115 * Saves the namespace
121 * Contains the version of csstidy
125 var $version = '1.3';
127 * Stores the settings
131 var $settings = array();
133 * Saves the parser-status.
139 * - instr = in string (started at " or ' or ( )
140 * - ic = in comment (ignore everything)
148 * Saves the current at rule (@media)
154 * Saves the at rule for next selector (during @font-face or other @)
158 var $next_selector_at = '';
161 * Saves the current selector
167 * Saves the current property
173 * Saves the position of , in selectors
177 var $sel_separate = array();
179 * Saves the current value
185 * Saves the current sub-value
187 * Example for a subvalue:
188 * background:url(foo.png) red no-repeat;
189 * "url(foo.png)", "red", and "no-repeat" are subvalues,
190 * seperated by whitespace
196 * Array which saves all subvalues for a property.
201 var $sub_value_arr = array();
203 * Saves the stack of characters that opened the current strings
207 var $str_char = array();
208 var $cur_string = array();
210 * Status from which the parser switched to ic or instr
217 * =true if in invalid at-rule
221 var $invalid_at = false;
223 * =true if something has been added to the current selector
229 * Array which saves the message log
235 * Saves the line number
241 * Marks if we need to leave quotes for a string
245 var $quoted_string = array();
251 var $tokens_list = "";
253 * Loads standard template and sets default settings
258 $this->settings
['remove_bslash'] = true;
259 $this->settings
['compress_colors'] = true;
260 $this->settings
['compress_font-weight'] = true;
261 $this->settings
['lowercase_s'] = false;
263 1 common shorthands optimization
264 2 + font property optimization
265 3 + background property optimization
267 $this->settings
['optimise_shorthands'] = 1;
268 $this->settings
['remove_last_;'] = true;
269 /* rewrite all properties with low case, better for later gzip OK, safe*/
270 $this->settings
['case_properties'] = 1;
271 /* sort properties in alpabetic order, better for later gzip
272 * but can cause trouble in case of overiding same propertie or using hack
274 $this->settings
['sort_properties'] = false;
276 1, 3, 5, etc -- enable sorting selectors inside @media: a{}b{}c{}
277 2, 5, 8, etc -- enable sorting selectors inside one CSS declaration: a,b,c{}
278 preserve order by default cause it can break functionnality
280 $this->settings
['sort_selectors'] = 0;
281 /* is dangeroues to be used: CSS is broken sometimes */
282 $this->settings
['merge_selectors'] = 0;
283 /* preserve or not browser hacks */
284 $this->settings
['discard_invalid_selectors'] = false;
285 $this->settings
['discard_invalid_properties'] = false;
286 $this->settings
['css_level'] = 'CSS3.0';
287 $this->settings
['preserve_css'] = false;
288 $this->settings
['timestamp'] = false;
289 $this->settings
['template'] = ''; // say that propertie exist
290 $this->set_cfg('template','default'); // call load_template
291 $this->optimise
= new csstidy_optimise($this);
293 $this->tokens_list
= & $GLOBALS['csstidy']['tokens'];
297 * Get the value of a setting.
298 * @param string $setting
303 function get_cfg($setting) {
304 if (isset($this->settings
[$setting])) {
305 return $this->settings
[$setting];
312 * @param string $template used by set_cfg to load a template via a configuration setting
316 function _load_template($template) {
319 $this->load_template('default');
323 $this->load_template('highest_compression');
327 $this->load_template('high_compression');
331 $this->load_template('low_compression');
335 $this->load_template($template);
341 * Set the value of a setting.
342 * @param string $setting
343 * @param mixed $value
348 function set_cfg($setting, $value=null) {
349 if (is_array($setting) && $value === null) {
350 foreach ($setting as $setprop => $setval) {
351 $this->settings
[$setprop] = $setval;
353 if (array_key_exists('template', $setting)) {
354 $this->_load_template($this->settings
['template']);
357 } else if (isset($this->settings
[$setting]) && $value !== '') {
358 $this->settings
[$setting] = $value;
359 if ($setting === 'template') {
360 $this->_load_template($this->settings
['template']);
368 * Adds a token to $this->tokens
370 * @param string $data
371 * @param bool $do add a token even if preserve_css is off
375 function _add_token($type, $data, $do = false) {
376 if ($this->get_cfg('preserve_css') ||
$do) {
377 $this->tokens
[] = array($type, ($type == COMMENT
) ?
$data : trim($data));
382 * Add a message to the message log
383 * @param string $message
384 * @param string $type
385 * @param integer $line
389 function log($message, $type, $line = -1) {
393 $line = intval($line);
394 $add = array('m' => $message, 't' => $type);
395 if (!isset($this->log
[$line]) ||
!in_array($add, $this->log
[$line])) {
396 $this->log
[$line][] = $add;
401 * Parse unicode notations and find a replacement character
402 * @param string $string
408 function _unicode(&$string, &$i) {
413 while ($i < strlen($string) && (ctype_xdigit($string{$i}) ||
ctype_space($string{$i})) && strlen($add) < 6) {
416 if (ctype_space($string{$i})) {
422 if (hexdec($add) > 47 && hexdec($add) < 58 ||
hexdec($add) > 64 && hexdec($add) < 91 ||
hexdec($add) > 96 && hexdec($add) < 123) {
423 $this->log('Replaced unicode notation: Changed \\' . $add . ' to ' . chr(hexdec($add)), 'Information');
424 $add = chr(hexdec($add));
427 $add = trim('\\' . $add);
430 if (@ctype_xdigit
($string{$i +
1}) && ctype_space($string{$i})
431 && !$replaced ||
!ctype_space($string{$i})) {
435 if ($add !== '\\' ||
!$this->get_cfg('remove_bslash') ||
strpos($this->tokens_list
, $string{$i +
1}) !== false) {
440 $this->log('Removed unnecessary backslash', 'Information');
446 * Write formatted output to a file
447 * @param string $filename
448 * @param string $doctype when printing formatted, is a shorthand for the document type
449 * @param bool $externalcss when printing formatted, indicates whether styles to be attached internally or as an external stylesheet
450 * @param string $title when printing formatted, is the title to be added in the head of the document
451 * @param string $lang when printing formatted, gives a two-letter language code to be added to the output
455 function write_page($filename, $doctype='xhtml1.1', $externalcss=true, $title='', $lang='en') {
456 $this->write($filename, true);
460 * Write plain output to a file
461 * @param string $filename
462 * @param bool $formatted whether to print formatted or not
463 * @param string $doctype when printing formatted, is a shorthand for the document type
464 * @param bool $externalcss when printing formatted, indicates whether styles to be attached internally or as an external stylesheet
465 * @param string $title when printing formatted, is the title to be added in the head of the document
466 * @param string $lang when printing formatted, gives a two-letter language code to be added to the output
467 * @param bool $pre_code whether to add pre and code tags around the code (for light HTML formatted templates)
471 function write($filename, $formatted=false, $doctype='xhtml1.1', $externalcss=true, $title='', $lang='en', $pre_code=true) {
472 $filename .= ( $formatted) ?
'.xhtml' : '.css';
474 if (!is_dir('temp')) {
475 $madedir = mkdir('temp');
477 print 'Could not make directory "temp" in ' . dirname(__FILE__
);
481 $handle = fopen('temp/' . $filename, 'w');
484 fwrite($handle, $this->print->plain());
486 fwrite($handle, $this->print->formatted_page($doctype, $externalcss, $title, $lang, $pre_code));
493 * Loads a new template
494 * @param string $content either filename (if $from_file == true), content of a template file, "high_compression", "highest_compression", "low_compression", or "default"
495 * @param bool $from_file uses $content as filename if true
498 * @see http://csstidy.sourceforge.net/templates.php
500 function load_template($content, $from_file=true) {
501 $predefined_templates = & $GLOBALS['csstidy']['predefined_templates'];
502 if ($content === 'high_compression' ||
$content === 'default' ||
$content === 'highest_compression' ||
$content === 'low_compression') {
503 $this->template
= $predefined_templates[$content];
509 $content = strip_tags(file_get_contents($content), '<span>');
511 $content = str_replace("\r\n", "\n", $content); // Unify newlines (because the output also only uses \n)
512 $template = explode('|', $content);
514 for ($i = 0; $i < count($template); $i++
) {
515 $this->template
[$i] = $template[$i];
520 * Starts parsing from URL
525 function parse_from_url($url) {
526 return $this->parse(@file_get_contents
($url));
530 * Checks if there is a token at the current position
531 * @param string $string
536 function is_token(&$string, $i) {
537 return (strpos($this->tokens_list
, $string{$i}) !== false && !csstidy
::escaped($string, $i));
541 * Parses CSS in $string. The code is saved as array in $this->css
542 * @param string $string the CSS code
547 function parse($string) {
548 // Temporarily set locale to en_US in order to handle floats properly
549 $old = @setlocale
(LC_ALL
, 0);
550 @setlocale
(LC_ALL
, 'C');
552 // PHP bug? Settings need to be refreshed in PHP4
553 $this->print = new csstidy_print($this);
554 $this->optimise
= new csstidy_optimise($this);
556 $all_properties = & $GLOBALS['csstidy']['all_properties'];
557 $at_rules = & $GLOBALS['csstidy']['at_rules'];
558 $quoted_string_properties = & $GLOBALS['csstidy']['quoted_string_properties'];
560 $this->css
= array();
561 $this->print->input_css
= $string;
562 $string = str_replace("\r\n", "\n", $string) . ' ';
565 for ($i = 0, $size = strlen($string); $i < $size; $i++
) {
566 if ($string{$i} === "\n" ||
$string{$i} === "\r") {
570 switch ($this->status
) {
571 /* Case in at-block */
573 if (csstidy
::is_token($string, $i)) {
574 if ($string{$i} === '/' && @$string{$i +
1} === '*') {
575 $this->status
= 'ic';
577 $this->from
[] = 'at';
578 } elseif ($string{$i} === '{') {
579 $this->status
= 'is';
580 $this->at
= $this->css_new_media_section($this->at
);
581 $this->_add_token(AT_START
, $this->at
);
582 } elseif ($string{$i} === ',') {
583 $this->at
= trim($this->at
) . ',';
584 } elseif ($string{$i} === '\\') {
585 $this->at
.= $this->_unicode($string, $i);
587 // fix for complicated media, i.e @media screen and (-webkit-min-device-pixel-ratio:1.5)
588 elseif (in_array($string{$i}, array('(', ')', ':', '.'))) {
589 $this->at
.= $string{$i};
592 $lastpos = strlen($this->at
) - 1;
593 if (!( (ctype_space($this->at
{$lastpos}) || csstidy
::is_token($this->at
, $lastpos) && $this->at
{$lastpos} === ',') && ctype_space($string{$i}))) {
594 $this->at
.= $string{$i};
599 /* Case in-selector */
601 if (csstidy
::is_token($string, $i)) {
602 if ($string{$i} === '/' && @$string{$i +
1} === '*' && trim($this->selector
) == '') {
603 $this->status
= 'ic';
605 $this->from
[] = 'is';
606 } elseif ($string{$i} === '@' && trim($this->selector
) == '') {
608 $this->invalid_at
= true;
609 foreach ($at_rules as $name => $type) {
610 if (!strcasecmp(substr($string, $i +
1, strlen($name)), $name)) {
611 ($type === 'at') ?
$this->at
= '@' . $name : $this->selector
= '@' . $name;
612 if ($type === "atis"){
613 $this->next_selector_at
= ($this->next_selector_at?
$this->next_selector_at
:($this->at?
$this->at
:DEFAULT_AT
));
614 $this->at
= $this->css_new_media_section(' ');
617 $this->status
= $type;
619 $this->invalid_at
= false;
623 if ($this->invalid_at
) {
624 $this->selector
= '@';
625 $invalid_at_name = '';
626 for ($j = $i +
1; $j < $size; ++
$j) {
627 if (!ctype_alpha($string{$j})) {
630 $invalid_at_name .= $string{$j};
632 $this->log('Invalid @-rule: ' . $invalid_at_name . ' (removed)', 'Warning');
634 } elseif (($string{$i} === '"' ||
$string{$i} === "'")) {
635 $this->cur_string
[] = $string{$i};
636 $this->status
= 'instr';
637 $this->str_char
[] = $string{$i};
638 $this->from
[] = 'is';
639 /* fixing CSS3 attribute selectors, i.e. a[href$=".mp3" */
640 $this->quoted_string
[] = ($string{$i - 1} == '=' );
641 } elseif ($this->invalid_at
&& $string{$i} === ';') {
642 $this->invalid_at
= false;
643 $this->status
= 'is';
644 if($this->next_selector_at
){
645 $this->at
= $this->css_new_media_section($this->next_selector_at
);
646 $this->next_selector_at
= '';
648 } elseif ($string{$i} === '{') {
649 $this->status
= 'ip';
650 if($this->at
== '') {
651 $this->at
= $this->css_new_media_section(DEFAULT_AT
);
653 $this->selector
= $this->css_new_selector($this->at
,$this->selector
);
654 $this->_add_token(SEL_START
, $this->selector
);
655 $this->added
= false;
656 } elseif ($string{$i} === '}') {
657 $this->_add_token(AT_END
, $this->at
);
659 $this->selector
= '';
660 $this->sel_separate
= array();
661 } elseif ($string{$i} === ',') {
662 $this->selector
= trim($this->selector
) . ',';
663 $this->sel_separate
[] = strlen($this->selector
);
664 } elseif ($string{$i} === '\\') {
665 $this->selector
.= $this->_unicode($string, $i);
666 } elseif ($string{$i} === '*' && @in_array
($string{$i +
1}, array('.', '#', '[', ':'))) {
667 // remove unnecessary universal selector, FS#147
669 $this->selector
.= $string{$i};
672 $lastpos = strlen($this->selector
) - 1;
673 if ($lastpos == -1 ||
!( (ctype_space($this->selector
{$lastpos}) || csstidy
::is_token($this->selector
, $lastpos) && $this->selector
{$lastpos} === ',') && ctype_space($string{$i}))) {
674 $this->selector
.= $string{$i};
679 /* Case in-property */
681 if (csstidy
::is_token($string, $i)) {
682 if (($string{$i} === ':' ||
$string{$i} === '=') && $this->property
!= '') {
683 $this->status
= 'iv';
684 if (!$this->get_cfg('discard_invalid_properties') || csstidy
::property_is_valid($this->property
)) {
685 $this->property
= $this->css_new_property($this->at
,$this->selector
,$this->property
);
686 $this->_add_token(PROPERTY
, $this->property
);
688 } elseif ($string{$i} === '/' && @$string{$i +
1} === '*' && $this->property
== '') {
689 $this->status
= 'ic';
691 $this->from
[] = 'ip';
692 } elseif ($string{$i} === '}') {
693 $this->explode_selectors();
694 $this->status
= 'is';
695 $this->invalid_at
= false;
696 $this->_add_token(SEL_END
, $this->selector
);
697 $this->selector
= '';
698 $this->property
= '';
699 if($this->next_selector_at
){
700 $this->at
= $this->css_new_media_section($this->next_selector_at
);
701 $this->next_selector_at
= '';
703 } elseif ($string{$i} === ';') {
704 $this->property
= '';
705 } elseif ($string{$i} === '\\') {
706 $this->property
.= $this->_unicode($string, $i);
708 // else this is dumb IE a hack, keep it
710 elseif (($this->property
=='' AND !ctype_space($string{$i}))
711 OR ($this->property
=='/' OR $string{$i}=="/")) {
712 $this->property
.= $string{$i};
715 elseif (!ctype_space($string{$i})) {
716 $this->property
.= $string{$i};
722 $pn = (($string{$i} === "\n" ||
$string{$i} === "\r") && $this->property_is_next($string, $i +
1) ||
$i == strlen($string) - 1);
723 if (csstidy
::is_token($string, $i) ||
$pn) {
724 if ($string{$i} === '/' && @$string{$i +
1} === '*') {
725 $this->status
= 'ic';
727 $this->from
[] = 'iv';
728 } elseif (($string{$i} === '"' ||
$string{$i} === "'" ||
$string{$i} === '(')) {
729 $this->cur_string
[] = $string{$i};
730 $this->str_char
[] = ($string{$i} === '(') ?
')' : $string{$i};
731 $this->status
= 'instr';
732 $this->from
[] = 'iv';
733 $this->quoted_string
[] = in_array(strtolower($this->property
), $quoted_string_properties);
734 } elseif ($string{$i} === ',') {
735 $this->sub_value
= trim($this->sub_value
) . ',';
736 } elseif ($string{$i} === '\\') {
737 $this->sub_value
.= $this->_unicode($string, $i);
738 } elseif ($string{$i} === ';' ||
$pn) {
739 if ($this->selector
{0} === '@' && isset($at_rules[substr($this->selector
, 1)]) && $at_rules[substr($this->selector
, 1)] === 'iv') {
740 /* Add quotes to charset, import, namespace */
741 $this->sub_value_arr
[] = trim($this->sub_value
);
743 $this->status
= 'is';
745 switch ($this->selector
) {
746 case '@charset': $this->charset
= '"'.$this->sub_value_arr
[0].'"';
748 case '@namespace': $this->namespace = implode(' ', $this->sub_value_arr
);
750 case '@import': $this->import
[] = implode(' ', $this->sub_value_arr
);
754 $this->sub_value_arr
= array();
755 $this->sub_value
= '';
756 $this->selector
= '';
757 $this->sel_separate
= array();
759 $this->status
= 'ip';
761 } elseif ($string{$i} !== '}') {
762 $this->sub_value
.= $string{$i};
764 if (($string{$i} === '}' ||
$string{$i} === ';' ||
$pn) && !empty($this->selector
)) {
766 $this->at
= $this->css_new_media_section(DEFAULT_AT
);
770 if ($this->get_cfg('lowercase_s')) {
771 $this->selector
= strtolower($this->selector
);
773 $this->property
= strtolower($this->property
);
775 $this->optimise
->subvalue();
776 if ($this->sub_value
!= '') {
777 $this->sub_value_arr
[] = $this->sub_value
;
778 $this->sub_value
= '';
782 while(count($this->sub_value_arr
)){
783 $sub = array_shift($this->sub_value_arr
);
784 if (strstr($this->selector
, "font-face")){
785 $sub = $this->quote_font_format($sub);
789 $this->value
.= ((!strlen($this->value
) OR substr($this->value
,-1,1)==',')?
'':' ').$sub;
792 $this->optimise
->value();
794 $valid = csstidy
::property_is_valid($this->property
);
795 if ((!$this->invalid_at ||
$this->get_cfg('preserve_css')) && (!$this->get_cfg('discard_invalid_properties') ||
$valid)) {
796 $this->css_add_property($this->at
, $this->selector
, $this->property
, $this->value
);
797 $this->_add_token(VALUE
, $this->value
);
798 $this->optimise
->shorthands();
801 if ($this->get_cfg('discard_invalid_properties')) {
802 $this->log('Removed invalid property: ' . $this->property
, 'Warning');
804 $this->log('Invalid property in ' . strtoupper($this->get_cfg('css_level')) . ': ' . $this->property
, 'Warning');
808 $this->property
= '';
809 $this->sub_value_arr
= array();
812 if ($string{$i} === '}') {
813 $this->explode_selectors();
814 $this->_add_token(SEL_END
, $this->selector
);
815 $this->status
= 'is';
816 $this->invalid_at
= false;
817 $this->selector
= '';
818 if($this->next_selector_at
){
819 $this->at
= $this->css_new_media_section($this->next_selector_at
);
820 $this->next_selector_at
= '';
824 $this->sub_value
.= $string{$i};
826 if (ctype_space($string{$i})) {
827 $this->optimise
->subvalue();
828 if ($this->sub_value
!= '') {
829 $this->sub_value_arr
[] = $this->sub_value
;
830 $this->sub_value
= '';
838 $_str_char = $this->str_char
[count($this->str_char
)-1];
839 $_cur_string = $this->cur_string
[count($this->cur_string
)-1];
840 $_quoted_string = $this->quoted_string
[count($this->quoted_string
)-1];
841 $temp_add = $string{$i};
843 // Add another string to the stack. Strings can't be nested inside of quotes, only parentheses, but
844 // parentheticals can be nested more than once.
845 if ($_str_char === ")" && ($string{$i} === "(" ||
$string{$i} === '"' ||
$string{$i} === '\'') && !csstidy
::escaped($string, $i)) {
846 $this->cur_string
[] = $string{$i};
847 $this->str_char
[] = $string{$i} == "(" ?
")" : $string{$i};
848 $this->from
[] = 'instr';
849 $this->quoted_string
[] = ($_str_char === ")" AND $string{$i} !== "(" AND trim($_cur_string)=="(")?
$_quoted_string:!($string{$i} === "(");
853 if ($_str_char !== ")" && ($string{$i} === "\n" ||
$string{$i} === "\r") && !($string{$i - 1} === '\\' && !csstidy
::escaped($string, $i - 1))) {
855 $this->log('Fixed incorrect newline in string', 'Warning');
858 $_cur_string .= $temp_add;
860 if ($string{$i} === $_str_char && !csstidy
::escaped($string, $i)) {
861 $this->status
= array_pop($this->from
);
863 if (!preg_match('|[' . implode('', $GLOBALS['csstidy']['whitespace']) . ']|uis', $_cur_string) && $this->property
!== 'content') {
864 if (!$_quoted_string) {
865 if ($_str_char !== ')') {
866 // Convert properties like
867 // font-family: 'Arial';
869 // font-family: Arial;
874 $_cur_string = substr($_cur_string, 1, -1);
877 $_quoted_string = false;
881 array_pop($this->cur_string
);
882 array_pop($this->quoted_string
);
883 array_pop($this->str_char
);
885 if ($_str_char === ")") {
886 $_cur_string = "(" . trim(substr($_cur_string, 1, -1)) . ")";
889 if ($this->status
=== 'iv') {
890 if (!$_quoted_string){
891 if (strpos($_cur_string,',')!==false)
892 // we can on only remove space next to ','
893 $_cur_string = implode(',',array_map('trim',explode(',',$_cur_string)));
894 // and multiple spaces (too expensive)
895 if (strpos($_cur_string,' ')!==false)
896 $_cur_string = preg_replace(",\s+,"," ",$_cur_string);
898 $this->sub_value
.= $_cur_string;
899 } elseif ($this->status
=== 'is') {
900 $this->selector
.= $_cur_string;
901 } elseif ($this->status
=== 'instr') {
902 $this->cur_string
[count($this->cur_string
)-1] .= $_cur_string;
906 $this->cur_string
[count($this->cur_string
)-1] = $_cur_string;
910 /* Case in-comment */
912 if ($string{$i} === '*' && $string{$i +
1} === '/') {
913 $this->status
= array_pop($this->from
);
915 $this->_add_token(COMMENT
, $cur_comment);
918 $cur_comment .= $string{$i};
924 $this->optimise
->postparse();
926 $this->print->_reset();
928 @setlocale
(LC_ALL
, $old); // Set locale back to original setting
930 return!(empty($this->css
) && empty($this->import
) && empty($this->charset
) && empty($this->tokens
) && empty($this->namespace));
935 * format() in font-face needs quoted values for somes browser (FF at least)
940 function quote_font_format($value){
941 if (strncmp($value,'format',6)==0) {
942 $p = strrpos($value,")");
943 $end = substr($value,$p);
944 $format_strings = csstidy
::parse_string_list(substr($value, 7, $p-7));
945 if (!$format_strings) {
951 foreach ($format_strings as $format_string) {
952 $value .= '"' . str_replace('"', '\\"', $format_string) . '",';
955 $value = substr($value, 0, -1) . $end;
966 function explode_selectors() {
967 // Explode multiple selectors
968 if ($this->get_cfg('merge_selectors') === 1) {
971 $this->sel_separate
[] = strlen($this->selector
);
972 foreach ($this->sel_separate
as $num => $pos) {
973 if ($num == count($this->sel_separate
) - 1) {
977 $new_sels[] = substr($this->selector
, $lastpos, $pos - $lastpos - 1);
981 if (count($new_sels) > 1) {
982 foreach ($new_sels as $selector) {
983 if (isset($this->css
[$this->at
][$this->selector
])) {
984 $this->merge_css_blocks($this->at
, $selector, $this->css
[$this->at
][$this->selector
]);
987 unset($this->css
[$this->at
][$this->selector
]);
990 $this->sel_separate
= array();
994 * Checks if a character is escaped (and returns true if it is)
995 * @param string $string
996 * @param integer $pos
1001 static function escaped(&$string, $pos) {
1002 return!(@($string{$pos - 1} !== '\\') || csstidy
::escaped($string, $pos - 1));
1006 * Adds a property with value to the existing CSS code
1007 * @param string $media
1008 * @param string $selector
1009 * @param string $property
1010 * @param string $new_val
1014 function css_add_property($media, $selector, $property, $new_val) {
1015 if ($this->get_cfg('preserve_css') ||
trim($new_val) == '') {
1019 $this->added
= true;
1020 if (isset($this->css
[$media][$selector][$property])) {
1021 if ((csstidy
::is_important($this->css
[$media][$selector][$property]) && csstidy
::is_important($new_val)) ||
!csstidy
::is_important($this->css
[$media][$selector][$property])) {
1022 $this->css
[$media][$selector][$property] = trim($new_val);
1025 $this->css
[$media][$selector][$property] = trim($new_val);
1030 * Start a new media section.
1031 * Check if the media is not already known,
1032 * else rename it with extra spaces
1035 * @param string $media
1038 function css_new_media_section($media){
1039 if($this->get_cfg('preserve_css')) {
1043 // if the last @media is the same as this
1045 if (!$this->css
OR !is_array($this->css
) OR empty($this->css
)){
1049 list($at,) = each($this->css
);
1053 while (isset($this->css
[$media]))
1054 if (is_numeric($media))
1062 * Start a new selector.
1063 * If already referenced in this media section,
1064 * rename it with extra space to avoid merging
1065 * except if merging is required,
1066 * or last selector is the same (merge siblings)
1068 * never merge @font-face
1070 * @param string $media
1071 * @param string $selector
1074 function css_new_selector($media,$selector){
1075 if($this->get_cfg('preserve_css')) {
1078 $selector = trim($selector);
1079 if (strncmp($selector,"@font-face",10)!=0){
1080 if ($this->settings
['merge_selectors'] != false)
1083 if (!$this->css
OR !isset($this->css
[$media]) OR !$this->css
[$media])
1086 // if last is the same, keep it
1087 end($this->css
[$media]);
1088 list($sel,) = each($this->css
[$media]);
1089 if ($sel == $selector){
1094 while (isset($this->css
[$media][$selector]))
1100 * Start a new propertie.
1101 * If already references in this selector,
1102 * rename it with extra space to avoid override
1104 * @param string $media
1105 * @param string $selector
1106 * @param string $property
1109 function css_new_property($media, $selector, $property){
1110 if($this->get_cfg('preserve_css')) {
1113 if (!$this->css
OR !isset($this->css
[$media][$selector]) OR !$this->css
[$media][$selector])
1116 while (isset($this->css
[$media][$selector][$property]))
1123 * Adds CSS to an existing media/selector
1124 * @param string $media
1125 * @param string $selector
1126 * @param array $css_add
1130 function merge_css_blocks($media, $selector, $css_add) {
1131 foreach ($css_add as $property => $value) {
1132 $this->css_add_property($media, $selector, $property, $value, false);
1137 * Checks if $value is !important.
1138 * @param string $value
1143 static function is_important(&$value) {
1145 strpos($value,"!")!==false // quick test
1146 AND !strcasecmp(substr(str_replace($GLOBALS['csstidy']['whitespace'], '', $value), -10, 10), '!important'));
1150 * Returns a value without !important
1151 * @param string $value
1156 static function gvw_important($value) {
1157 if (csstidy
::is_important($value)) {
1158 $value = trim($value);
1159 $value = substr($value, 0, -9);
1160 $value = trim($value);
1161 $value = substr($value, 0, -1);
1162 $value = trim($value);
1169 * Checks if the next word in a string from pos is a CSS property
1170 * @param string $istring
1171 * @param integer $pos
1176 function property_is_next($istring, $pos) {
1177 $all_properties = & $GLOBALS['csstidy']['all_properties'];
1178 $istring = substr($istring, $pos, strlen($istring) - $pos);
1179 $pos = strpos($istring, ':');
1180 if ($pos === false) {
1183 $istring = strtolower(trim(substr($istring, 0, $pos)));
1184 if (isset($all_properties[$istring])) {
1185 $this->log('Added semicolon to the end of declaration', 'Warning');
1192 * Checks if a property is valid
1193 * @param string $property
1198 function property_is_valid($property) {
1199 if (in_array(trim($property), $GLOBALS['csstidy']['multiple_properties'])) $property = trim($property);
1200 $all_properties = & $GLOBALS['csstidy']['all_properties'];
1201 return (isset($all_properties[$property]) && strpos($all_properties[$property], strtoupper($this->get_cfg('css_level'))) !== false );
1205 * Accepts a list of strings (e.g., the argument to format() in a @font-face src property)
1206 * and returns a list of the strings. Converts things like:
1208 * format(abc) => format("abc")
1209 * format(abc def) => format("abc","def")
1210 * format(abc "def") => format("abc","def")
1211 * format(abc, def, ghi) => format("abc","def","ghi")
1212 * format("abc",'def') => format("abc","def")
1213 * format("abc, def, ghi") => format("abc, def, ghi")
1219 function parse_string_list($value) {
1220 $value = trim($value);
1223 if (!$value) return array();
1228 $current_string = "";
1230 for ($i = 0, $_len = strlen($value); $i < $_len; $i++
) {
1231 if (($value{$i} == "," ||
$value{$i} === " ") && $in_str === true) {
1233 $strings[] = $current_string;
1234 $current_string = "";
1236 else if ($value{$i} == '"' ||
$value{$i} == "'"){
1237 if ($in_str === $value{$i}) {
1238 $strings[] = $current_string;
1240 $current_string = "";
1243 else if (!$in_str) {
1244 $in_str = $value{$i};
1249 $current_string .= $value{$i};
1252 if (!preg_match("/[\s,]/", $value{$i})) {
1254 $current_string = $value{$i};
1260 if ($current_string) {
1261 $strings[] = $current_string;