xref: /plugin/pagecss/vendor/csstidy-2.2.1/class.csstidy.php (revision 7d6669007238fef7e8728f167d637ed824899eb0)
1<?php
2
3/**
4 * CSSTidy - CSS Parser and Optimiser
5 *
6 * CSS Parser class
7 *
8 * Copyright 2005, 2006, 2007 Florian Schmitz
9 *
10 * This file is part of CSSTidy.
11 *
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.
16 *
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.
21 *
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/>.
24 *
25 * @license http://opensource.org/licenses/lgpl-license.php GNU Lesser General Public License
26 * @package csstidy
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
33 */
34
35/**
36 * Defines constants
37 * @todo //TODO: make them class constants of csstidy
38 */
39define('AT_START',         1);
40define('AT_END',           2);
41define('SEL_START',        3);
42define('SEL_END',          4);
43define('PROPERTY',         5);
44define('VALUE',            6);
45define('COMMENT',          7);
46define('IMPORTANT_COMMENT',8);
47define('DEFAULT_AT',      41);
48
49/**
50 * Contains a class for printing CSS code
51 *
52 * @version 1.1.0
53 */
54require(__DIR__ . DIRECTORY_SEPARATOR . 'class.csstidy_print.php');
55
56/**
57 * Contains a class for optimising CSS code
58 *
59 * @version 1.0
60 */
61require(__DIR__ . DIRECTORY_SEPARATOR . 'class.csstidy_optimise.php');
62
63/**
64 * CSS Parser class
65 *
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
71 * @package csstidy
72 * @author Florian Schmitz (floele at gmail dot com) 2005-2006
73 * @version 2.2.1
74 */
75class csstidy {
76
77	/**
78	 * Saves the parsed CSS. This array is empty if preserve_css is on.
79	 * @var array
80	 * @access public
81	 */
82	public $css = array();
83	/**
84	 * Saves the parsed CSS (raw)
85	 * @var array
86	 * @access private
87	 */
88	public $tokens = array();
89	/**
90	 * Printer class
91	 * @see csstidy_print
92	 * @var object
93	 * @access public
94	 */
95	public $print;
96	/**
97	 * Optimiser class
98	 * @see csstidy_optimise
99	 * @var object
100	 * @access private
101	 */
102	public $optimise;
103	/**
104	 * Saves the CSS charset (@charset)
105	 * @var string
106	 * @access private
107	 */
108	public $charset = '';
109	/**
110	 * Saves all @import URLs
111	 * @var array
112	 * @access private
113	 */
114	public $import = array();
115	/**
116	 * Saves the namespace
117	 * @var string
118	 * @access private
119	 */
120	public $namespace = '';
121	/**
122	 * Contains the version of csstidy
123	 * @var string
124	 * @access private
125	 */
126	public $version = '2.0.3';
127	/**
128	 * Stores the settings
129	 * @var array
130	 * @access private
131	 */
132	public $settings = array();
133	/**
134	 * Saves the parser-status.
135	 *
136	 * Possible values:
137	 * - is = in selector
138	 * - ip = in property
139	 * - iv = in value
140	 * - instr = in string (started at " or ' or ( )
141	 * - ic = in comment (ignore everything)
142	 * - at = in @-block
143	 *
144	 * @var string
145	 * @access private
146	 */
147	public $status = 'is';
148	/**
149	 * Saves the current at rule (@media)
150	 * @var string
151	 * @access private
152	 */
153	public $at = '';
154	/**
155	 * Saves the at rule for next selector (during @font-face or other @)
156	 * @var string
157	 * @access private
158	 */
159	public $next_selector_at = '';
160
161	/**
162	 * Saves the current selector
163	 * @var string
164	 * @access private
165	 */
166	public $selector = '';
167	/**
168	 * Saves the current property
169	 * @var string
170	 * @access private
171	 */
172	public $property = '';
173	/**
174	 * Saves the position of , in selectors
175	 * @var array
176	 * @access private
177	 */
178	public $sel_separate = array();
179	/**
180	 * Saves the current value
181	 * @var string
182	 * @access private
183	 */
184	public $value = '';
185	/**
186	 * Saves the current sub-value
187	 *
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
192	 * @var string
193	 * @access private
194	 */
195	public $sub_value = '';
196	/**
197	 * Array which saves all subvalues for a property.
198	 * @var array
199	 * @see sub_value
200	 * @access private
201	 */
202	public $sub_value_arr = array();
203	/**
204	 * Saves the stack of characters that opened the current strings
205	 * @var array
206	 * @access private
207	 */
208	public $str_char = array();
209	public $cur_string = array();
210	/**
211	 * Status from which the parser switched to ic or instr
212	 * @var array
213	 * @access private
214	 */
215	public $from = array();
216	/**
217	/**
218	 * =true if in invalid at-rule
219	 * @var bool
220	 * @access private
221	 */
222	public $invalid_at = false;
223	/**
224	 * =true if something has been added to the current selector
225	 * @var bool
226	 * @access private
227	 */
228	public $added = false;
229	/**
230	 * Array which saves the message log
231	 * @var array
232	 * @access private
233	 */
234	public $log = array();
235	/**
236	 * Saves the line number
237	 * @var integer
238	 * @access private
239	 */
240	public $line = 1;
241	/**
242	 * Marks if we need to leave quotes for a string
243	 * @var array
244	 * @access private
245	 */
246	public $quoted_string = array();
247
248	/**
249	 * List of tokens
250	 * @var string
251	 */
252	public $tokens_list = "";
253
254	/**
255	 * Various CSS Data for CSSTidy
256	 * @var array
257	 */
258	public $data = array();
259
260	public $template;
261
262	/**
263	 * Loads standard template and sets default settings
264	 * @access private
265	 * @version 1.3
266	 */
267	public function __construct() {
268		$data = array();
269		include(__DIR__ . DIRECTORY_SEPARATOR . 'data.inc.php');
270		$this->data = $data;
271
272		$this->settings['remove_bslash'] = true;
273		$this->settings['compress_colors'] = true;
274		$this->settings['compress_font-weight'] = true;
275		$this->settings['lowercase_s'] = false;
276		/*
277			1 common shorthands optimization
278			2 + font property optimization
279			3 + background property optimization
280		 */
281		$this->settings['optimise_shorthands'] = 1;
282		$this->settings['remove_last_;'] = true;
283		$this->settings['space_before_important'] = false;
284		/* rewrite all properties with low case, better for later gzip OK, safe*/
285		$this->settings['case_properties'] = 1;
286		/* sort properties in alpabetic order, better for later gzip
287		 * but can cause trouble in case of overiding same propertie or using hack
288		 */
289		$this->settings['sort_properties'] = false;
290		/*
291			1, 3, 5, etc -- enable sorting selectors inside @media: a{}b{}c{}
292			2, 5, 8, etc -- enable sorting selectors inside one CSS declaration: a,b,c{}
293			preserve order by default cause it can break functionnality
294		 */
295		$this->settings['sort_selectors'] = 0;
296		/* is dangeroues to be used: CSS is broken sometimes */
297		$this->settings['merge_selectors'] = 0;
298		/* preserve or not browser hacks */
299
300		/* Useful to produce a rtl css from a ltr one (or the opposite) */
301		$this->settings['reverse_left_and_right'] = 0;
302
303		$this->settings['discard_invalid_selectors'] = false;
304		$this->settings['discard_invalid_properties'] = false;
305		$this->settings['css_level'] = 'CSS3.0';
306		$this->settings['preserve_css'] = false;
307		$this->settings['timestamp'] = false;
308		$this->settings['template'] = ''; // say that propertie exist
309		$this->set_cfg('template','default'); // call load_template
310		$this->optimise = new csstidy_optimise($this);
311
312		$this->tokens_list = & $this->data['csstidy']['tokens'];
313	}
314
315	/**
316	 * Get the value of a setting.
317	 * @param string $setting
318	 * @access public
319	 * @return mixed
320	 * @version 1.0
321	 */
322	public function get_cfg($setting) {
323		if (isset($this->settings[$setting])) {
324			return $this->settings[$setting];
325		}
326		return false;
327	}
328
329	/**
330	 * Load a template
331	 * @param string $template used by set_cfg to load a template via a configuration setting
332	 * @access private
333	 * @version 1.4
334	 */
335	public function _load_template($template) {
336		switch ($template) {
337			case 'default':
338				$this->load_template('default');
339				break;
340
341			case 'highest':
342				$this->load_template('highest_compression');
343				break;
344
345			case 'high':
346				$this->load_template('high_compression');
347				break;
348
349			case 'low':
350				$this->load_template('low_compression');
351				break;
352
353			default:
354				$this->load_template($template);
355				break;
356		}
357	}
358
359	/**
360	 * Set the value of a setting.
361	 * @param string $setting
362	 * @param mixed $value
363	 * @access public
364	 * @return bool
365	 * @version 1.0
366	 */
367	public function set_cfg($setting, $value=null) {
368		if (is_array($setting) && $value === null) {
369			foreach ($setting as $setprop => $setval) {
370				$this->settings[$setprop] = $setval;
371			}
372			if (array_key_exists('template', $setting)) {
373				$this->_load_template($this->settings['template']);
374			}
375			return true;
376		} elseif (isset($this->settings[$setting]) && $value !== '') {
377			$this->settings[$setting] = $value;
378			if ($setting === 'template') {
379				$this->_load_template($this->settings['template']);
380			}
381			return true;
382		}
383		return false;
384	}
385
386	/**
387	 * Adds a token to $this->tokens
388	 * @param mixed $type
389	 * @param string $data
390	 * @param bool $do add a token even if preserve_css is off
391	 * @access private
392	 * @version 1.0
393	 */
394	public function _add_token($type, $data, $do = false) {
395		if ($this->get_cfg('preserve_css') || $do) {
396			// nested @... : if opening a new part we just closed, remove the previous closing instead of adding opening
397			if ($type === AT_START
398				and count($this->tokens)
399				and $last = end($this->tokens)
400				and $last[0] === AT_END
401				and $last[1] === trim($data)) {
402				array_pop($this->tokens);
403			}
404			else {
405				$this->tokens[] = array($type, ($type == COMMENT or $type == IMPORTANT_COMMENT) ? $data : trim($data));
406			}
407		}
408	}
409
410	/**
411	 * Add a message to the message log
412	 * @param string $message
413	 * @param string $type
414	 * @param integer $line
415	 * @access private
416	 * @version 1.0
417	 */
418	public function log($message, $type, $line = -1) {
419		if ($line === -1) {
420			$line = $this->line;
421		}
422		$line = intval($line);
423		$add = array('m' => $message, 't' => $type);
424		if (!isset($this->log[$line]) || !in_array($add, $this->log[$line])) {
425			$this->log[$line][] = $add;
426		}
427	}
428
429	/**
430	 * Parse unicode notations and find a replacement character
431	 * @param string $string
432	 * @param integer $i
433	 * @access private
434	 * @return string
435	 * @version 1.2
436	 */
437	public function _unicode(&$string, &$i) {
438		++$i;
439		$add = '';
440		$replaced = false;
441
442		while ($i < strlen($string) && (ctype_xdigit($string[$i]) || ctype_space($string[$i])) && strlen($add) < 6) {
443			$add .= $string[$i];
444
445			if (ctype_space($string[$i])) {
446				break;
447			}
448			$i++;
449		}
450
451		if (hexdec($add) > 47 && hexdec($add) < 58 || hexdec($add) > 64 && hexdec($add) < 91 || hexdec($add) > 96 && hexdec($add) < 123) {
452			$this->log('Replaced unicode notation: Changed \\' . $add . ' to ' . chr(hexdec($add)), 'Information');
453			$add = chr(hexdec($add));
454			$replaced = true;
455		} else {
456			$add = trim('\\' . $add);
457		}
458
459		if (@ctype_xdigit($string[$i + 1]) && ctype_space($string[$i])
460						&& !$replaced || !ctype_space($string[$i])) {
461			$i--;
462		}
463
464		if ($add !== '\\' || !$this->get_cfg('remove_bslash') || strpos($this->tokens_list, $string[$i + 1]) !== false) {
465			return $add;
466		}
467
468		if ($add === '\\') {
469			$this->log('Removed unnecessary backslash', 'Information');
470		}
471		return '';
472	}
473
474	/**
475	 * Write formatted output to a file
476	 * @param string $filename
477	 * @param string $doctype when printing formatted, is a shorthand for the document type
478	 * @param bool $externalcss when printing formatted, indicates whether styles to be attached internally or as an external stylesheet
479	 * @param string $title when printing formatted, is the title to be added in the head of the document
480	 * @param string $lang when printing formatted, gives a two-letter language code to be added to the output
481	 * @access public
482	 * @version 1.4
483	 */
484	public function write_page($filename, $doctype='xhtml1.1', $externalcss=true, $title='', $lang='en') {
485		$this->write($filename, true);
486	}
487
488	/**
489	 * Write plain output to a file
490	 * @param string $filename
491	 * @param bool $formatted whether to print formatted or not
492	 * @param string $doctype when printing formatted, is a shorthand for the document type
493	 * @param bool $externalcss when printing formatted, indicates whether styles to be attached internally or as an external stylesheet
494	 * @param string $title when printing formatted, is the title to be added in the head of the document
495	 * @param string $lang when printing formatted, gives a two-letter language code to be added to the output
496	 * @param bool $pre_code whether to add pre and code tags around the code (for light HTML formatted templates)
497	 * @access public
498	 * @version 1.4
499	 */
500	public function write($filename, $formatted=false, $doctype='xhtml1.1', $externalcss=true, $title='', $lang='en', $pre_code=true) {
501		$filename .= ( $formatted) ? '.xhtml' : '.css';
502
503		if (!is_dir('temp')) {
504			$madedir = mkdir('temp');
505			if (!$madedir) {
506				print 'Could not make directory "temp" in ' . dirname(__FILE__);
507				exit;
508			}
509		}
510		$handle = fopen('temp/' . $filename, 'w');
511		if ($handle) {
512			if (!$formatted) {
513				fwrite($handle, $this->print->plain());
514			} else {
515				fwrite($handle, $this->print->formatted_page($doctype, $externalcss, $title, $lang, $pre_code));
516			}
517		}
518		fclose($handle);
519	}
520
521	/**
522	 * Loads a new template
523	 * @param string $content either filename (if $from_file == true), content of a template file, "high_compression", "highest_compression", "low_compression", or "default"
524	 * @param bool $from_file uses $content as filename if true
525	 * @access public
526	 * @version 1.1
527	 * @see http://csstidy.sourceforge.net/templates.php
528	 */
529	public function load_template($content, $from_file=true) {
530		$predefined_templates = & $this->data['csstidy']['predefined_templates'];
531		if ($content === 'high_compression' || $content === 'default' || $content === 'highest_compression' || $content === 'low_compression') {
532			$this->template = $predefined_templates[$content];
533			return;
534		}
535
536
537		if ($from_file) {
538			$content = strip_tags(file_get_contents($content), '<span>');
539		}
540		$content = str_replace("\r\n", "\n", $content); // Unify newlines (because the output also only uses \n)
541		$template = explode('|', $content);
542
543		for ($i = 0; $i < count($template); $i++) {
544			$this->template[$i] = $template[$i];
545		}
546	}
547
548	/**
549	 * Starts parsing from URL
550	 * @param string $url
551	 * @access public
552	 * @version 1.0
553	 */
554	public function parse_from_url($url) {
555		return $this->parse(@file_get_contents($url));
556	}
557
558	/**
559	 * Checks if there is a token at the current position
560	 * @param string $string
561	 * @param integer $i
562	 * @access public
563	 * @version 1.11
564	 */
565	public function is_token(&$string, $i) {
566		return (strpos($this->tokens_list, $string[$i]) !== false && !$this->escaped($string, $i));
567	}
568
569	/**
570	 * Parses CSS in $string. The code is saved as array in $this->css
571	 * @param string $string the CSS code
572	 * @access public
573	 * @return bool
574	 * @version 1.1
575	 */
576	public function parse($string) {
577		// Temporarily set locale to en_US in order to handle floats properly
578		$old = @setlocale(LC_ALL, 0);
579		@setlocale(LC_ALL, 'C');
580
581		// PHP bug? Settings need to be refreshed in PHP4
582		$this->print = new csstidy_print($this);
583		$this->optimise = new csstidy_optimise($this);
584
585		$all_properties = & $this->data['csstidy']['all_properties'];
586		$at_rules = & $this->data['csstidy']['at_rules'];
587		$quoted_string_properties = & $this->data['csstidy']['quoted_string_properties'];
588
589		$this->css = array();
590		$this->print->input_css = $string;
591		$string = str_replace("\r\n", "\n", $string) . ' ';
592		$cur_comment = '';
593		$cur_at = '';
594
595		for ($i = 0, $size = strlen($string); $i < $size; $i++) {
596			if ($string[$i] === "\n" || $string[$i] === "\r") {
597				++$this->line;
598			}
599
600			switch ($this->status) {
601				/* Case in at-block */
602				case 'at':
603					if ($this->is_token($string, $i)) {
604						if ($string[$i] === '/' && @$string[$i + 1] === '*') {
605							$this->status = 'ic';
606							++$i;
607							$this->from[] = 'at';
608						} elseif ($string[$i] === '{') {
609							$this->status = 'is';
610							$this->at = $this->css_new_media_section($this->at, $cur_at);
611							$this->_add_token(AT_START, $this->at);
612						} elseif ($string[$i] === ',') {
613							$cur_at = trim($cur_at) . ',';
614						} elseif ($string[$i] === '\\') {
615							$cur_at .= $this->_unicode($string, $i);
616						}
617						// fix for complicated media, i.e @media screen and (-webkit-min-device-pixel-ratio:1.5)
618						elseif (in_array($string[$i], array('(', ')', ':', '.', '/'))) {
619							$cur_at .= $string[$i];
620						}
621					} else {
622						$lastpos = strlen($cur_at) - 1;
623						if (!( (ctype_space($cur_at[$lastpos]) || $this->is_token($cur_at, $lastpos) && $cur_at[$lastpos] === ',') && ctype_space($string[$i]))) {
624							$cur_at .= $string[$i];
625						}
626					}
627					break;
628
629				/* Case in-selector */
630				case 'is':
631					if ($this->is_token($string, $i)) {
632						if ($string[$i] === '/' && @$string[$i + 1] === '*' && trim($this->selector) == '') {
633							$this->status = 'ic';
634							++$i;
635							$this->from[] = 'is';
636						} elseif ($string[$i] === '@' && trim($this->selector) == '') {
637							// Check for at-rule
638							$this->invalid_at = true;
639							foreach ($at_rules as $name => $type) {
640								if (!strcasecmp(substr($string, $i + 1, strlen($name)), $name)) {
641									($type === 'at') ? $cur_at = '@' . $name : $this->selector = '@' . $name;
642									if ($type === 'atis') {
643										$this->next_selector_at = ($this->next_selector_at?$this->next_selector_at:($this->at?$this->at:DEFAULT_AT));
644										$this->at = $this->css_new_media_section($this->at, ' ', true);
645										$type = 'is';
646									}
647									$this->status = $type;
648									$i += strlen($name);
649									$this->invalid_at = false;
650									break;
651								}
652							}
653
654							if ($this->invalid_at) {
655								$this->selector = '@';
656								$invalid_at_name = '';
657								for ($j = $i + 1; $j < $size; ++$j) {
658									if (!ctype_alpha($string[$j])) {
659										break;
660									}
661									$invalid_at_name .= $string[$j];
662								}
663								$this->log('Invalid @-rule: ' . $invalid_at_name . ' (removed)', 'Warning');
664							}
665						} elseif (($string[$i] === '"' || $string[$i] === "'")) {
666							$this->cur_string[] = $string[$i];
667							$this->status = 'instr';
668							$this->str_char[] = $string[$i];
669							$this->from[] = 'is';
670							/* fixing CSS3 attribute selectors, i.e. a[href$=".mp3" */
671							$this->quoted_string[] = ($string[$i - 1] === '=' );
672						} elseif ($this->invalid_at && $string[$i] === ';') {
673							$this->invalid_at = false;
674							$this->status = 'is';
675							if ($this->next_selector_at) {
676								$this->at = $this->css_close_media_section($this->at);
677								$this->at = $this->css_new_media_section($this->at, $this->next_selector_at);
678								$this->next_selector_at = '';
679							}
680						} elseif ($string[$i] === '{') {
681							$this->status = 'ip';
682							if ($this->at == '') {
683								$this->at = $this->css_new_media_section($this->at, DEFAULT_AT);
684							}
685							$this->selector = $this->css_new_selector($this->at,$this->selector);
686							$this->_add_token(SEL_START, $this->selector);
687							$this->added = false;
688						} elseif ($string[$i] === '}') {
689							$this->_add_token(AT_END, $this->at);
690							$this->at = $this->css_close_media_section($this->at);
691							$this->selector = '';
692							$this->sel_separate = array();
693						} elseif ($string[$i] === ',') {
694							$this->selector = trim($this->selector) . ',';
695							$this->sel_separate[] = strlen($this->selector);
696						} elseif ($string[$i] === '\\') {
697							$this->selector .= $this->_unicode($string, $i);
698						} elseif ($string[$i] === '*' && @in_array($string[$i + 1], array('.', '#', '[', ':')) && ($i==0 OR $string[$i - 1]!=='/')) {
699							// remove unnecessary universal selector, FS#147, but not comment in selector
700						} else {
701							$this->selector .= $string[$i];
702						}
703					} else {
704						$lastpos = strlen($this->selector) - 1;
705						if ($lastpos == -1 || !( (ctype_space($this->selector[$lastpos]) || $this->is_token($this->selector, $lastpos) && $this->selector[$lastpos] === ',') && ctype_space($string[$i]))) {
706							$this->selector .= $string[$i];
707						}
708					}
709					break;
710
711				/* Case in-property */
712				case 'ip':
713					if ($this->is_token($string, $i)) {
714						if (($string[$i] === ':' || $string[$i] === '=') && $this->property != '') {
715							$this->status = 'iv';
716							if (!$this->get_cfg('discard_invalid_properties') || $this->property_is_valid($this->property)) {
717								$this->property = $this->css_new_property($this->at,$this->selector,$this->property);
718								$this->_add_token(PROPERTY, $this->property);
719							}
720						} elseif ($string[$i] === '/' && @$string[$i + 1] === '*' && $this->property == '') {
721							$this->status = 'ic';
722							++$i;
723							$this->from[] = 'ip';
724						} elseif ($string[$i] === '}') {
725							$this->explode_selectors();
726							$this->status = 'is';
727							$this->invalid_at = false;
728							$this->_add_token(SEL_END, $this->selector);
729							$this->selector = '';
730							$this->property = '';
731							if ($this->next_selector_at) {
732								$this->at = $this->css_close_media_section($this->at);
733								$this->at = $this->css_new_media_section($this->at, $this->next_selector_at);
734								$this->next_selector_at = '';
735							}
736						} elseif ($string[$i] === ';') {
737							$this->property = '';
738						} elseif ($string[$i] === '\\') {
739							$this->property .= $this->_unicode($string, $i);
740						}
741						// else this is dumb IE a hack, keep it
742						// including //
743						elseif (($this->property === '' && !ctype_space($string[$i]))
744							|| ($this->property === '/' || $string[$i] === '/')) {
745							$this->property .= $string[$i];
746						}
747					} elseif (!ctype_space($string[$i])) {
748						$this->property .= $string[$i];
749					}
750					break;
751
752				/* Case in-value */
753				case 'iv':
754					$pn = (($string[$i] === "\n" || $string[$i] === "\r") && $this->property_is_next($string, $i + 1) || $i == strlen($string) - 1);
755					if ($this->is_token($string, $i) || $pn) {
756						if ($string[$i] === '/' && @$string[$i + 1] === '*') {
757							$this->status = 'ic';
758							++$i;
759							$this->from[] = 'iv';
760						} elseif (($string[$i] === '"' || $string[$i] === "'" || $string[$i] === '(')) {
761							$this->cur_string[] = $string[$i];
762							$this->str_char[] = ($string[$i] === '(') ? ')' : $string[$i];
763							$this->status = 'instr';
764							$this->from[] = 'iv';
765							$this->quoted_string[] = in_array(strtolower($this->property), $quoted_string_properties);
766						} elseif ($string[$i] === ',') {
767							$this->sub_value = trim($this->sub_value) . ',';
768						} elseif ($string[$i] === '\\') {
769							$this->sub_value .= $this->_unicode($string, $i);
770						} elseif ($string[$i] === ';' || $pn) {
771							if ($this->selector[0] === '@' && isset($at_rules[substr($this->selector, 1)]) && $at_rules[substr($this->selector, 1)] === 'iv') {
772								/* Add quotes to charset, import, namespace */
773								$this->sub_value_arr[] = trim($this->sub_value);
774
775								$this->status = 'is';
776
777								switch ($this->selector) {
778									case '@charset': $this->charset = '"'.$this->sub_value_arr[0].'"';
779										break;
780									case '@namespace': $this->namespace = implode(' ', $this->sub_value_arr);
781										break;
782									case '@import': $this->import[] = implode(' ', $this->sub_value_arr);
783										break;
784								}
785
786								$this->sub_value_arr = array();
787								$this->sub_value = '';
788								$this->selector = '';
789								$this->sel_separate = array();
790							} else {
791								$this->status = 'ip';
792							}
793						} elseif ($string[$i] !== '}') {
794							$this->sub_value .= $string[$i];
795						}
796						if (($string[$i] === '}' || $string[$i] === ';' || $pn) && !empty($this->selector)) {
797							if ($this->at == '') {
798								$this->at = $this->css_new_media_section($this->at,DEFAULT_AT);
799							}
800
801							// case settings
802							if ($this->get_cfg('lowercase_s')) {
803								$this->selector = strtolower($this->selector);
804							}
805							$this->property = strtolower($this->property);
806
807							$this->optimise->subvalue();
808							if ($this->sub_value != '') {
809								$this->sub_value_arr[] = $this->sub_value;
810								$this->sub_value = '';
811							}
812
813							$this->value = '';
814							while (count($this->sub_value_arr)) {
815								$sub = array_shift($this->sub_value_arr);
816								if (strstr($this->selector, 'font-face')) {
817									$sub = $this->quote_font_format($sub);
818								}
819
820								if ($sub != '')
821									$this->value .= ((!strlen($this->value) || substr($this->value,-1,1) === ',')?'':' ').$sub;
822							}
823
824							$this->optimise->value();
825
826							$valid = $this->property_is_valid($this->property);
827							if ((!$this->invalid_at || $this->get_cfg('preserve_css')) && (!$this->get_cfg('discard_invalid_properties') || $valid)) {
828								$this->css_add_property($this->at, $this->selector, $this->property, $this->value);
829								$this->_add_token(VALUE, $this->value);
830								$this->optimise->shorthands();
831							}
832							if (!$valid) {
833								if ($this->get_cfg('discard_invalid_properties')) {
834									$this->log('Removed invalid property: ' . $this->property, 'Warning');
835								} else {
836									$this->log('Invalid property in ' . strtoupper($this->get_cfg('css_level')) . ': ' . $this->property, 'Warning');
837								}
838							}
839
840							$this->property = '';
841							$this->sub_value_arr = array();
842							$this->value = '';
843						}
844						if ($string[$i] === '}') {
845							$this->explode_selectors();
846							$this->_add_token(SEL_END, $this->selector);
847							$this->status = 'is';
848							$this->invalid_at = false;
849							$this->selector = '';
850							if ($this->next_selector_at) {
851								$this->at = $this->css_close_media_section($this->at);
852								$this->at = $this->css_new_media_section($this->at, $this->next_selector_at);
853								$this->next_selector_at = '';
854							}
855						}
856					} elseif (!$pn) {
857						$this->sub_value .= $string[$i];
858
859						if (ctype_space($string[$i])) {
860							$this->optimise->subvalue();
861							if ($this->sub_value != '') {
862								$this->sub_value_arr[] = $this->sub_value;
863								$this->sub_value = '';
864							}
865						}
866					}
867					break;
868
869				/* Case in string */
870				case 'instr':
871					$_str_char = $this->str_char[count($this->str_char)-1];
872					$_cur_string = $this->cur_string[count($this->cur_string)-1];
873					$_quoted_string = $this->quoted_string[count($this->quoted_string)-1];
874					$temp_add = $string[$i];
875
876					// Add another string to the stack. Strings can't be nested inside of quotes, only parentheses, but
877					// parentheticals can be nested more than once.
878					if ($_str_char === ")" && ($string[$i] === "(" || $string[$i] === '"' || $string[$i] === '\'') && !$this->escaped($string, $i)) {
879						$this->cur_string[] = $string[$i];
880						$this->str_char[] = $string[$i] === '(' ? ')' : $string[$i];
881						$this->from[] = 'instr';
882						$this->quoted_string[] = ($_str_char === ')' && $string[$i] !== '(' && trim($_cur_string)==='(')?$_quoted_string:!($string[$i] === '(');
883						continue 2;
884					}
885
886					if ($_str_char !== ")" && ($string[$i] === "\n" || $string[$i] === "\r") && !($string[$i - 1] === '\\' && !$this->escaped($string, $i - 1))) {
887						$temp_add = "\\A";
888						$this->log('Fixed incorrect newline in string', 'Warning');
889					}
890
891					$_cur_string .= $temp_add;
892
893					if ($string[$i] === $_str_char && !$this->escaped($string, $i)) {
894						$this->status = array_pop($this->from);
895
896						if (!preg_match('|[' . implode('', $this->data['csstidy']['whitespace']) . ']|uis', $_cur_string) && $this->property !== 'content') {
897							if (!$_quoted_string) {
898								if ($_str_char !== ')') {
899									// Convert properties like
900									// font-family: 'Arial';
901									// to
902									// font-family: Arial;
903									// or
904									// url("abc")
905									// to
906									// url(abc)
907									$_cur_string = substr($_cur_string, 1, -1);
908								}
909							} else {
910								$_quoted_string = false;
911							}
912						}
913
914						array_pop($this->cur_string);
915						array_pop($this->quoted_string);
916						array_pop($this->str_char);
917
918						if ($_str_char === ')') {
919							$_cur_string = '(' . trim(substr($_cur_string, 1, -1)) . ')';
920						}
921
922						if ($this->status === 'iv') {
923							if (!$_quoted_string) {
924								if (strpos($_cur_string,',') !== false)
925									// we can on only remove space next to ','
926									$_cur_string = implode(',', array_map('trim', explode(',',$_cur_string)));
927								// and multiple spaces (too expensive)
928								if (strpos($_cur_string, '  ') !== false)
929									$_cur_string = preg_replace(",\s+,", ' ', $_cur_string);
930							}
931							$this->sub_value .= $_cur_string;
932						} elseif ($this->status === 'is') {
933							$this->selector .= $_cur_string;
934						} elseif ($this->status === 'instr') {
935							$this->cur_string[count($this->cur_string)-1] .= $_cur_string;
936						}
937					} else {
938						$this->cur_string[count($this->cur_string)-1] = $_cur_string;
939					}
940					break;
941
942				/* Case in-comment */
943				case 'ic':
944					if ($string[$i] === '*' && $string[$i + 1] === '/') {
945						$this->status = array_pop($this->from);
946						$i++;
947						if (strlen($cur_comment) > 1 and strncmp($cur_comment, '!', 1) === 0) {
948							$this->_add_token(IMPORTANT_COMMENT, $cur_comment);
949							$this->css_add_important_comment($cur_comment);
950						}
951						else {
952							$this->_add_token(COMMENT, $cur_comment);
953						}
954						$cur_comment = '';
955					} else {
956						$cur_comment .= $string[$i];
957					}
958					break;
959			}
960		}
961
962		$this->optimise->postparse();
963
964		$this->print->_reset();
965
966		@setlocale(LC_ALL, $old); // Set locale back to original setting
967
968		return!(empty($this->css) && empty($this->import) && empty($this->charset) && empty($this->tokens) && empty($this->namespace));
969	}
970
971
972	/**
973	 * format() in font-face needs quoted values for somes browser (FF at least)
974	 *
975	 * @param $value
976	 * @return string
977	 */
978	public function quote_font_format($value) {
979		if (strncmp($value,'format',6) == 0) {
980			$p = strpos($value,')',7);
981			$end = substr($value,$p);
982			$format_strings = $this->parse_string_list(substr($value, 7, $p-7));
983			if (!$format_strings) {
984				$value = '';
985			} else {
986				$value = 'format(';
987
988				foreach ($format_strings as $format_string) {
989					$value .= '"' . str_replace('"', '\\"', $format_string) . '",';
990				}
991
992				$value = substr($value, 0, -1) . $end;
993			}
994		}
995		return $value;
996	}
997
998	/**
999	 * Explodes selectors
1000	 * @access private
1001	 * @version 1.0
1002	 */
1003	public function explode_selectors() {
1004		// Explode multiple selectors
1005		if ($this->get_cfg('merge_selectors') === 1) {
1006			$new_sels = array();
1007			$lastpos = 0;
1008			$this->sel_separate[] = strlen($this->selector);
1009			foreach ($this->sel_separate as $num => $pos) {
1010				if ($num == count($this->sel_separate) - 1) {
1011					$pos += 1;
1012				}
1013
1014				$new_sels[] = substr($this->selector, $lastpos, $pos - $lastpos - 1);
1015				$lastpos = $pos;
1016			}
1017
1018			if (count($new_sels) > 1) {
1019				foreach ($new_sels as $selector) {
1020					if (isset($this->css[$this->at][$this->selector])) {
1021						$this->merge_css_blocks($this->at, $selector, $this->css[$this->at][$this->selector]);
1022					}
1023				}
1024				unset($this->css[$this->at][$this->selector]);
1025			}
1026		}
1027		$this->sel_separate = array();
1028	}
1029
1030	/**
1031	 * Checks if a character is escaped (and returns true if it is)
1032	 * @param string $string
1033	 * @param integer $pos
1034	 * @access public
1035	 * @return bool
1036	 * @version 1.02
1037	 */
1038	static function escaped(&$string, $pos) {
1039		return!(@($string[$pos - 1] !== '\\') || csstidy::escaped($string, $pos - 1));
1040	}
1041
1042
1043	/**
1044	 * Add an important comment to the css code
1045	 * (one we want to keep)
1046	 * @param $comment
1047	 */
1048	public function css_add_important_comment($comment) {
1049		if ($this->get_cfg('preserve_css') || trim($comment) == '') {
1050			return;
1051		}
1052		if (!isset($this->css['!'])) {
1053			$this->css['!'] = '';
1054		}
1055		else {
1056			$this->css['!'] .= "\n";
1057		}
1058		$this->css['!'] .= $comment;
1059	}
1060
1061	/**
1062	 * Adds a property with value to the existing CSS code
1063	 * @param string $media
1064	 * @param string $selector
1065	 * @param string $property
1066	 * @param string $new_val
1067	 * @access private
1068	 * @version 1.2
1069	 */
1070	public function css_add_property($media, $selector, $property, $new_val) {
1071		if ($this->get_cfg('preserve_css') || trim($new_val) == '') {
1072			return;
1073		}
1074
1075		$this->added = true;
1076		if (isset($this->css[$media][$selector][$property])) {
1077			if (($this->is_important($this->css[$media][$selector][$property]) && $this->is_important($new_val)) || !$this->is_important($this->css[$media][$selector][$property])) {
1078				$this->css[$media][$selector][$property] = trim($new_val);
1079			}
1080		} else {
1081			$this->css[$media][$selector][$property] = trim($new_val);
1082		}
1083	}
1084
1085	/**
1086	 * Check if a current media section is the continuation of the last one
1087	 * if not inc the name of the media section to avoid a merging
1088	 *
1089	 * @param int|string $media
1090	 * @return int|string
1091	 */
1092	public function css_check_last_media_section_or_inc($media) {
1093		// are we starting?
1094		if (!$this->css || !is_array($this->css) || empty($this->css)) {
1095			return $media;
1096		}
1097
1098		// if the last @media is the same as this
1099		// keep it
1100		end($this->css);
1101		$at = key($this->css);
1102		if ($at == $media) {
1103			return $media;
1104		}
1105
1106		// else inc the section in the array
1107		while (isset($this->css[$media]))
1108			if (is_numeric($media))
1109				$media++;
1110			else
1111				$media .= ' ';
1112		return $media;
1113	}
1114
1115	/**
1116	 * Start a new media section.
1117	 * Check if the media is not already known,
1118	 * else rename it with extra spaces
1119	 * to avoid merging
1120	 *
1121	 * @param string $current_media
1122	 * @param string $media
1123	 * @param bool $at_root
1124	 * @return string
1125	 */
1126	public function css_new_media_section($current_media, $new_media, $at_root = false) {
1127		if ($this->get_cfg('preserve_css')) {
1128			return $new_media;
1129		}
1130
1131		// if we already are in a media and CSS level is 3, manage nested medias
1132		if ($current_media
1133			&& !$at_root
1134			// numeric $current_media means DEFAULT_AT or inc
1135			&& !is_numeric($current_media)
1136			&& strncmp($this->get_cfg('css_level'), 'CSS3', 4) == 0) {
1137
1138			$new_media = rtrim($current_media) . "{" . rtrim($new_media);
1139		}
1140
1141		return $this->css_check_last_media_section_or_inc($new_media);
1142	}
1143
1144	/**
1145	 * Close a media section
1146	 * Find the parent media we were in before or the root
1147	 * @param $current_media
1148	 * @return string
1149	 */
1150	public function css_close_media_section($current_media) {
1151		if ($this->get_cfg('preserve_css')) {
1152			return '';
1153		}
1154
1155		if (strpos($current_media, '{') !== false) {
1156			$current_media = explode('{', $current_media);
1157			array_pop($current_media);
1158			$current_media = implode('{', $current_media);
1159			return $current_media;
1160		}
1161
1162		return '';
1163	}
1164
1165	/**
1166	 * Start a new selector.
1167	 * If already referenced in this media section,
1168	 * rename it with extra space to avoid merging
1169	 * except if merging is required,
1170	 * or last selector is the same (merge siblings)
1171	 *
1172	 * never merge @font-face
1173	 *
1174	 * @param string $media
1175	 * @param string $selector
1176	 * @return string
1177	 */
1178	public function css_new_selector($media,$selector) {
1179		if ($this->get_cfg('preserve_css')) {
1180			return $selector;
1181		}
1182		$selector = trim($selector);
1183		if (strncmp($selector,'@font-face',10)!=0) {
1184			if ($this->settings['merge_selectors'] != false)
1185				return $selector;
1186
1187			if (!$this->css || !isset($this->css[$media]) || !$this->css[$media])
1188				return $selector;
1189
1190			// if last is the same, keep it
1191			end($this->css[$media]);
1192			$sel = key($this->css[$media]);
1193			if ($sel == $selector) {
1194				return $selector;
1195			}
1196		}
1197
1198		while (isset($this->css[$media][$selector]))
1199			$selector .= ' ';
1200		return $selector;
1201	}
1202
1203	/**
1204	 * Start a new propertie.
1205	 * If already references in this selector,
1206	 * rename it with extra space to avoid override
1207	 *
1208	 * @param string $media
1209	 * @param string $selector
1210	 * @param string $property
1211	 * @return string
1212	 */
1213	public function css_new_property($media, $selector, $property) {
1214		if ($this->get_cfg('preserve_css')) {
1215			return $property;
1216		}
1217		if (!$this->css || !isset($this->css[$media][$selector]) || !$this->css[$media][$selector])
1218			return $property;
1219
1220		while (isset($this->css[$media][$selector][$property]))
1221			$property .= ' ';
1222
1223		return $property;
1224	}
1225
1226	/**
1227	 * Adds CSS to an existing media/selector
1228	 * @param string $media
1229	 * @param string $selector
1230	 * @param array $css_add
1231	 * @access private
1232	 * @version 1.1
1233	 */
1234	public function merge_css_blocks($media, $selector, $css_add) {
1235		foreach ($css_add as $property => $value) {
1236			$this->css_add_property($media, $selector, $property, $value, false);
1237		}
1238	}
1239
1240	/**
1241	 * Checks if $value is !important.
1242	 * @param string $value
1243	 * @return bool
1244	 * @access public
1245	 * @version 1.0
1246	 */
1247	public function is_important(&$value) {
1248		return (
1249			strpos($value, '!') !== false // quick test
1250			AND !strcasecmp(substr(str_replace($this->data['csstidy']['whitespace'], '', $value), -10, 10), '!important'));
1251	}
1252
1253	/**
1254	 * Returns a value without !important
1255	 * @param string $value
1256	 * @return string
1257	 * @access public
1258	 * @version 1.0
1259	 */
1260	public function gvw_important($value) {
1261		if ($this->is_important($value)) {
1262			$value = trim($value);
1263			$value = substr($value, 0, -9);
1264			$value = trim($value);
1265			$value = substr($value, 0, -1);
1266			$value = trim($value);
1267			return $value;
1268		}
1269		return $value;
1270	}
1271
1272	/**
1273	 * Checks if the next word in a string from pos is a CSS property
1274	 * @param string $istring
1275	 * @param integer $pos
1276	 * @return bool
1277	 * @access private
1278	 * @version 1.2
1279	 */
1280	public function property_is_next($istring, $pos) {
1281		$all_properties = & $this->data['csstidy']['all_properties'];
1282		$istring = substr($istring, $pos, strlen($istring) - $pos);
1283		$pos = strpos($istring, ':');
1284		if ($pos === false) {
1285			return false;
1286		}
1287		$istring = strtolower(trim(substr($istring, 0, $pos)));
1288		if (isset($all_properties[$istring])) {
1289			$this->log('Added semicolon to the end of declaration', 'Warning');
1290			return true;
1291		}
1292		return false;
1293	}
1294
1295	/**
1296	 * Checks if a property is valid
1297	 * @param string $property
1298	 * @return bool
1299	 * @access public
1300	 * @version 1.0
1301	 */
1302	public function property_is_valid($property) {
1303		if (strpos($property, '--') === 0) {
1304			$property = "--custom";
1305		}
1306		elseif (in_array(trim($property), $this->data['csstidy']['multiple_properties'])) {
1307			$property = trim($property);
1308		}
1309		$all_properties = & $this->data['csstidy']['all_properties'];
1310		return (isset($all_properties[$property]) && strpos($all_properties[$property], strtoupper($this->get_cfg('css_level'))) !== false );
1311	}
1312
1313	/**
1314	 * Accepts a list of strings (e.g., the argument to format() in a @font-face src property)
1315	 * and returns a list of the strings.  Converts things like:
1316	 *
1317	 * format(abc) => format("abc")
1318	 * format(abc def) => format("abc","def")
1319	 * format(abc "def") => format("abc","def")
1320	 * format(abc, def, ghi) => format("abc","def","ghi")
1321	 * format("abc",'def') => format("abc","def")
1322	 * format("abc, def, ghi") => format("abc, def, ghi")
1323	 *
1324	 * @param string
1325	 * @return array
1326	 */
1327	public function parse_string_list($value) {
1328		$value = trim($value);
1329
1330		// Case: empty
1331		if (!$value) return array();
1332
1333		$strings = array();
1334
1335		$in_str = false;
1336		$current_string = '';
1337
1338		for ($i = 0, $_len = strlen($value); $i < $_len; $i++) {
1339			if (($value[$i] === ',' || $value[$i] === ' ') && $in_str === true) {
1340				$in_str = false;
1341				$strings[] = $current_string;
1342				$current_string = '';
1343			} elseif ($value[$i] === '"' || $value[$i] === "'") {
1344				if ($in_str === $value[$i]) {
1345					$strings[] = $current_string;
1346					$in_str = false;
1347					$current_string = '';
1348					continue;
1349				} elseif (!$in_str) {
1350					$in_str = $value[$i];
1351				}
1352			} else {
1353				if ($in_str) {
1354					$current_string .= $value[$i];
1355				} else {
1356					if (!preg_match("/[\s,]/", $value[$i])) {
1357						$in_str = true;
1358						$current_string = $value[$i];
1359					}
1360				}
1361			}
1362		}
1363
1364		if ($current_string) {
1365			$strings[] = $current_string;
1366		}
1367
1368		return $strings;
1369	}
1370}