[SPIP] +installation version 3.0.10
[lhc/web/www.git] / www / plugins-dist / compresseur / lib / csstidy / class.csstidy.php
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 */
33 /**
34 * Defines ctype functions if required
35 *
36 * @version 1.0
37 */
38 require_once('class.csstidy_ctype.php');
39
40 /**
41 * Various CSS data needed for correct optimisations etc.
42 *
43 * @version 1.3
44 */
45 require('data.inc.php');
46
47 /**
48 * Contains a class for printing CSS code
49 *
50 * @version 1.0
51 */
52 require('class.csstidy_print.php');
53
54 /**
55 * Contains a class for optimising CSS code
56 *
57 * @version 1.0
58 */
59 require('class.csstidy_optimise.php');
60
61 /**
62 * CSS Parser class
63 *
64
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
70 * @package csstidy
71 * @author Florian Schmitz (floele at gmail dot com) 2005-2006
72 * @version 1.4.0
73 */
74 class csstidy {
75
76 /**
77 * Saves the parsed CSS. This array is empty if preserve_css is on.
78 * @var array
79 * @access public
80 */
81 var $css = array();
82 /**
83 * Saves the parsed CSS (raw)
84 * @var array
85 * @access private
86 */
87 var $tokens = array();
88 /**
89 * Printer class
90 * @see csstidy_print
91 * @var object
92 * @access public
93 */
94 var $print;
95 /**
96 * Optimiser class
97 * @see csstidy_optimise
98 * @var object
99 * @access private
100 */
101 var $optimise;
102 /**
103 * Saves the CSS charset (@charset)
104 * @var string
105 * @access private
106 */
107 var $charset = '';
108 /**
109 * Saves all @import URLs
110 * @var array
111 * @access private
112 */
113 var $import = array();
114 /**
115 * Saves the namespace
116 * @var string
117 * @access private
118 */
119 var $namespace = '';
120 /**
121 * Contains the version of csstidy
122 * @var string
123 * @access private
124 */
125 var $version = '1.3';
126 /**
127 * Stores the settings
128 * @var array
129 * @access private
130 */
131 var $settings = array();
132 /**
133 * Saves the parser-status.
134 *
135 * Possible values:
136 * - is = in selector
137 * - ip = in property
138 * - iv = in value
139 * - instr = in string (started at " or ' or ( )
140 * - ic = in comment (ignore everything)
141 * - at = in @-block
142 *
143 * @var string
144 * @access private
145 */
146 var $status = 'is';
147 /**
148 * Saves the current at rule (@media)
149 * @var string
150 * @access private
151 */
152 var $at = '';
153 /**
154 * Saves the at rule for next selector (during @font-face or other @)
155 * @var string
156 * @access private
157 */
158 var $next_selector_at = '';
159
160 /**
161 * Saves the current selector
162 * @var string
163 * @access private
164 */
165 var $selector = '';
166 /**
167 * Saves the current property
168 * @var string
169 * @access private
170 */
171 var $property = '';
172 /**
173 * Saves the position of , in selectors
174 * @var array
175 * @access private
176 */
177 var $sel_separate = array();
178 /**
179 * Saves the current value
180 * @var string
181 * @access private
182 */
183 var $value = '';
184 /**
185 * Saves the current sub-value
186 *
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
191 * @var string
192 * @access private
193 */
194 var $sub_value = '';
195 /**
196 * Array which saves all subvalues for a property.
197 * @var array
198 * @see sub_value
199 * @access private
200 */
201 var $sub_value_arr = array();
202 /**
203 * Saves the stack of characters that opened the current strings
204 * @var array
205 * @access private
206 */
207 var $str_char = array();
208 var $cur_string = array();
209 /**
210 * Status from which the parser switched to ic or instr
211 * @var array
212 * @access private
213 */
214 var $from = array();
215 /**
216 /**
217 * =true if in invalid at-rule
218 * @var bool
219 * @access private
220 */
221 var $invalid_at = false;
222 /**
223 * =true if something has been added to the current selector
224 * @var bool
225 * @access private
226 */
227 var $added = false;
228 /**
229 * Array which saves the message log
230 * @var array
231 * @access private
232 */
233 var $log = array();
234 /**
235 * Saves the line number
236 * @var integer
237 * @access private
238 */
239 var $line = 1;
240 /**
241 * Marks if we need to leave quotes for a string
242 * @var array
243 * @access private
244 */
245 var $quoted_string = array();
246
247 /**
248 * List of tokens
249 * @var string
250 */
251 var $tokens_list = "";
252 /**
253 * Loads standard template and sets default settings
254 * @access private
255 * @version 1.3
256 */
257 function csstidy() {
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;
262 /*
263 1 common shorthands optimization
264 2 + font property optimization
265 3 + background property optimization
266 */
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
273 */
274 $this->settings['sort_properties'] = false;
275 /*
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
279 */
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);
292
293 $this->tokens_list = & $GLOBALS['csstidy']['tokens'];
294 }
295
296 /**
297 * Get the value of a setting.
298 * @param string $setting
299 * @access public
300 * @return mixed
301 * @version 1.0
302 */
303 function get_cfg($setting) {
304 if (isset($this->settings[$setting])) {
305 return $this->settings[$setting];
306 }
307 return false;
308 }
309
310 /**
311 * Load a template
312 * @param string $template used by set_cfg to load a template via a configuration setting
313 * @access private
314 * @version 1.4
315 */
316 function _load_template($template) {
317 switch ($template) {
318 case 'default':
319 $this->load_template('default');
320 break;
321
322 case 'highest':
323 $this->load_template('highest_compression');
324 break;
325
326 case 'high':
327 $this->load_template('high_compression');
328 break;
329
330 case 'low':
331 $this->load_template('low_compression');
332 break;
333
334 default:
335 $this->load_template($template);
336 break;
337 }
338 }
339
340 /**
341 * Set the value of a setting.
342 * @param string $setting
343 * @param mixed $value
344 * @access public
345 * @return bool
346 * @version 1.0
347 */
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;
352 }
353 if (array_key_exists('template', $setting)) {
354 $this->_load_template($this->settings['template']);
355 }
356 return true;
357 } else if (isset($this->settings[$setting]) && $value !== '') {
358 $this->settings[$setting] = $value;
359 if ($setting === 'template') {
360 $this->_load_template($this->settings['template']);
361 }
362 return true;
363 }
364 return false;
365 }
366
367 /**
368 * Adds a token to $this->tokens
369 * @param mixed $type
370 * @param string $data
371 * @param bool $do add a token even if preserve_css is off
372 * @access private
373 * @version 1.0
374 */
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));
378 }
379 }
380
381 /**
382 * Add a message to the message log
383 * @param string $message
384 * @param string $type
385 * @param integer $line
386 * @access private
387 * @version 1.0
388 */
389 function log($message, $type, $line = -1) {
390 if ($line === -1) {
391 $line = $this->line;
392 }
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;
397 }
398 }
399
400 /**
401 * Parse unicode notations and find a replacement character
402 * @param string $string
403 * @param integer $i
404 * @access private
405 * @return string
406 * @version 1.2
407 */
408 function _unicode(&$string, &$i) {
409 ++$i;
410 $add = '';
411 $replaced = false;
412
413 while ($i < strlen($string) && (ctype_xdigit($string{$i}) || ctype_space($string{$i})) && strlen($add) < 6) {
414 $add .= $string{$i};
415
416 if (ctype_space($string{$i})) {
417 break;
418 }
419 $i++;
420 }
421
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));
425 $replaced = true;
426 } else {
427 $add = trim('\\' . $add);
428 }
429
430 if (@ctype_xdigit($string{$i + 1}) && ctype_space($string{$i})
431 && !$replaced || !ctype_space($string{$i})) {
432 $i--;
433 }
434
435 if ($add !== '\\' || !$this->get_cfg('remove_bslash') || strpos($this->tokens_list, $string{$i + 1}) !== false) {
436 return $add;
437 }
438
439 if ($add === '\\') {
440 $this->log('Removed unnecessary backslash', 'Information');
441 }
442 return '';
443 }
444
445 /**
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
452 * @access public
453 * @version 1.4
454 */
455 function write_page($filename, $doctype='xhtml1.1', $externalcss=true, $title='', $lang='en') {
456 $this->write($filename, true);
457 }
458
459 /**
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)
468 * @access public
469 * @version 1.4
470 */
471 function write($filename, $formatted=false, $doctype='xhtml1.1', $externalcss=true, $title='', $lang='en', $pre_code=true) {
472 $filename .= ( $formatted) ? '.xhtml' : '.css';
473
474 if (!is_dir('temp')) {
475 $madedir = mkdir('temp');
476 if (!$madedir) {
477 print 'Could not make directory "temp" in ' . dirname(__FILE__);
478 exit;
479 }
480 }
481 $handle = fopen('temp/' . $filename, 'w');
482 if ($handle) {
483 if (!$formatted) {
484 fwrite($handle, $this->print->plain());
485 } else {
486 fwrite($handle, $this->print->formatted_page($doctype, $externalcss, $title, $lang, $pre_code));
487 }
488 }
489 fclose($handle);
490 }
491
492 /**
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
496 * @access public
497 * @version 1.1
498 * @see http://csstidy.sourceforge.net/templates.php
499 */
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];
504 return;
505 }
506
507
508 if ($from_file) {
509 $content = strip_tags(file_get_contents($content), '<span>');
510 }
511 $content = str_replace("\r\n", "\n", $content); // Unify newlines (because the output also only uses \n)
512 $template = explode('|', $content);
513
514 for ($i = 0; $i < count($template); $i++) {
515 $this->template[$i] = $template[$i];
516 }
517 }
518
519 /**
520 * Starts parsing from URL
521 * @param string $url
522 * @access public
523 * @version 1.0
524 */
525 function parse_from_url($url) {
526 return $this->parse(@file_get_contents($url));
527 }
528
529 /**
530 * Checks if there is a token at the current position
531 * @param string $string
532 * @param integer $i
533 * @access public
534 * @version 1.11
535 */
536 function is_token(&$string, $i) {
537 return (strpos($this->tokens_list, $string{$i}) !== false && !csstidy::escaped($string, $i));
538 }
539
540 /**
541 * Parses CSS in $string. The code is saved as array in $this->css
542 * @param string $string the CSS code
543 * @access public
544 * @return bool
545 * @version 1.1
546 */
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');
551
552 // PHP bug? Settings need to be refreshed in PHP4
553 $this->print = new csstidy_print($this);
554 $this->optimise = new csstidy_optimise($this);
555
556 $all_properties = & $GLOBALS['csstidy']['all_properties'];
557 $at_rules = & $GLOBALS['csstidy']['at_rules'];
558 $quoted_string_properties = & $GLOBALS['csstidy']['quoted_string_properties'];
559
560 $this->css = array();
561 $this->print->input_css = $string;
562 $string = str_replace("\r\n", "\n", $string) . ' ';
563 $cur_comment = '';
564
565 for ($i = 0, $size = strlen($string); $i < $size; $i++) {
566 if ($string{$i} === "\n" || $string{$i} === "\r") {
567 ++$this->line;
568 }
569
570 switch ($this->status) {
571 /* Case in at-block */
572 case 'at':
573 if (csstidy::is_token($string, $i)) {
574 if ($string{$i} === '/' && @$string{$i + 1} === '*') {
575 $this->status = 'ic';
576 ++$i;
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);
586 }
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};
590 }
591 } else {
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};
595 }
596 }
597 break;
598
599 /* Case in-selector */
600 case 'is':
601 if (csstidy::is_token($string, $i)) {
602 if ($string{$i} === '/' && @$string{$i + 1} === '*' && trim($this->selector) == '') {
603 $this->status = 'ic';
604 ++$i;
605 $this->from[] = 'is';
606 } elseif ($string{$i} === '@' && trim($this->selector) == '') {
607 // Check for at-rule
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(' ');
615 $type = "is";
616 }
617 $this->status = $type;
618 $i += strlen($name);
619 $this->invalid_at = false;
620 }
621 }
622
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})) {
628 break;
629 }
630 $invalid_at_name .= $string{$j};
631 }
632 $this->log('Invalid @-rule: ' . $invalid_at_name . ' (removed)', 'Warning');
633 }
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 = '';
647 }
648 } elseif ($string{$i} === '{') {
649 $this->status = 'ip';
650 if($this->at == '') {
651 $this->at = $this->css_new_media_section(DEFAULT_AT);
652 }
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);
658 $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
668 } else {
669 $this->selector .= $string{$i};
670 }
671 } else {
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};
675 }
676 }
677 break;
678
679 /* Case in-property */
680 case 'ip':
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);
687 }
688 } elseif ($string{$i} === '/' && @$string{$i + 1} === '*' && $this->property == '') {
689 $this->status = 'ic';
690 ++$i;
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 = '';
702 }
703 } elseif ($string{$i} === ';') {
704 $this->property = '';
705 } elseif ($string{$i} === '\\') {
706 $this->property .= $this->_unicode($string, $i);
707 }
708 // else this is dumb IE a hack, keep it
709 // including //
710 elseif (($this->property=='' AND !ctype_space($string{$i}))
711 OR ($this->property=='/' OR $string{$i}=="/")) {
712 $this->property .= $string{$i};
713 }
714 }
715 elseif (!ctype_space($string{$i})) {
716 $this->property .= $string{$i};
717 }
718 break;
719
720 /* Case in-value */
721 case 'iv':
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';
726 ++$i;
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);
742
743 $this->status = 'is';
744
745 switch ($this->selector) {
746 case '@charset': $this->charset = '"'.$this->sub_value_arr[0].'"';
747 break;
748 case '@namespace': $this->namespace = implode(' ', $this->sub_value_arr);
749 break;
750 case '@import': $this->import[] = implode(' ', $this->sub_value_arr);
751 break;
752 }
753
754 $this->sub_value_arr = array();
755 $this->sub_value = '';
756 $this->selector = '';
757 $this->sel_separate = array();
758 } else {
759 $this->status = 'ip';
760 }
761 } elseif ($string{$i} !== '}') {
762 $this->sub_value .= $string{$i};
763 }
764 if (($string{$i} === '}' || $string{$i} === ';' || $pn) && !empty($this->selector)) {
765 if($this->at == ''){
766 $this->at = $this->css_new_media_section(DEFAULT_AT);
767 }
768
769 // case settings
770 if ($this->get_cfg('lowercase_s')) {
771 $this->selector = strtolower($this->selector);
772 }
773 $this->property = strtolower($this->property);
774
775 $this->optimise->subvalue();
776 if ($this->sub_value != '') {
777 $this->sub_value_arr[] = $this->sub_value;
778 $this->sub_value = '';
779 }
780
781 $this->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);
786 }
787
788 if ($sub != '')
789 $this->value .= ((!strlen($this->value) OR substr($this->value,-1,1)==',')?'':' ').$sub;
790 }
791
792 $this->optimise->value();
793
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();
799 }
800 if (!$valid) {
801 if ($this->get_cfg('discard_invalid_properties')) {
802 $this->log('Removed invalid property: ' . $this->property, 'Warning');
803 } else {
804 $this->log('Invalid property in ' . strtoupper($this->get_cfg('css_level')) . ': ' . $this->property, 'Warning');
805 }
806 }
807
808 $this->property = '';
809 $this->sub_value_arr = array();
810 $this->value = '';
811 }
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 = '';
821 }
822 }
823 } elseif (!$pn) {
824 $this->sub_value .= $string{$i};
825
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 = '';
831 }
832 }
833 }
834 break;
835
836 /* Case in string */
837 case 'instr':
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};
842
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} === "(");
850 continue;
851 }
852
853 if ($_str_char !== ")" && ($string{$i} === "\n" || $string{$i} === "\r") && !($string{$i - 1} === '\\' && !csstidy::escaped($string, $i - 1))) {
854 $temp_add = "\\A";
855 $this->log('Fixed incorrect newline in string', 'Warning');
856 }
857
858 $_cur_string .= $temp_add;
859
860 if ($string{$i} === $_str_char && !csstidy::escaped($string, $i)) {
861 $this->status = array_pop($this->from);
862
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';
868 // to
869 // font-family: Arial;
870 // or
871 // url("abc")
872 // to
873 // url(abc)
874 $_cur_string = substr($_cur_string, 1, -1);
875 }
876 } else {
877 $_quoted_string = false;
878 }
879 }
880
881 array_pop($this->cur_string);
882 array_pop($this->quoted_string);
883 array_pop($this->str_char);
884
885 if ($_str_char === ")") {
886 $_cur_string = "(" . trim(substr($_cur_string, 1, -1)) . ")";
887 }
888
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);
897 }
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;
903 }
904 }
905 else {
906 $this->cur_string[count($this->cur_string)-1] = $_cur_string;
907 }
908 break;
909
910 /* Case in-comment */
911 case 'ic':
912 if ($string{$i} === '*' && $string{$i + 1} === '/') {
913 $this->status = array_pop($this->from);
914 $i++;
915 $this->_add_token(COMMENT, $cur_comment);
916 $cur_comment = '';
917 } else {
918 $cur_comment .= $string{$i};
919 }
920 break;
921 }
922 }
923
924 $this->optimise->postparse();
925
926 $this->print->_reset();
927
928 @setlocale(LC_ALL, $old); // Set locale back to original setting
929
930 return!(empty($this->css) && empty($this->import) && empty($this->charset) && empty($this->tokens) && empty($this->namespace));
931 }
932
933
934 /**
935 * format() in font-face needs quoted values for somes browser (FF at least)
936 *
937 * @param $value
938 * @return string
939 */
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) {
946 $value = "";
947 }
948 else {
949 $value = "format(";
950
951 foreach ($format_strings as $format_string) {
952 $value .= '"' . str_replace('"', '\\"', $format_string) . '",';
953 }
954
955 $value = substr($value, 0, -1) . $end;
956 }
957 }
958 return $value;
959 }
960
961 /**
962 * Explodes selectors
963 * @access private
964 * @version 1.0
965 */
966 function explode_selectors() {
967 // Explode multiple selectors
968 if ($this->get_cfg('merge_selectors') === 1) {
969 $new_sels = array();
970 $lastpos = 0;
971 $this->sel_separate[] = strlen($this->selector);
972 foreach ($this->sel_separate as $num => $pos) {
973 if ($num == count($this->sel_separate) - 1) {
974 $pos += 1;
975 }
976
977 $new_sels[] = substr($this->selector, $lastpos, $pos - $lastpos - 1);
978 $lastpos = $pos;
979 }
980
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]);
985 }
986 }
987 unset($this->css[$this->at][$this->selector]);
988 }
989 }
990 $this->sel_separate = array();
991 }
992
993 /**
994 * Checks if a character is escaped (and returns true if it is)
995 * @param string $string
996 * @param integer $pos
997 * @access public
998 * @return bool
999 * @version 1.02
1000 */
1001 static function escaped(&$string, $pos) {
1002 return!(@($string{$pos - 1} !== '\\') || csstidy::escaped($string, $pos - 1));
1003 }
1004
1005 /**
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
1011 * @access private
1012 * @version 1.2
1013 */
1014 function css_add_property($media, $selector, $property, $new_val) {
1015 if ($this->get_cfg('preserve_css') || trim($new_val) == '') {
1016 return;
1017 }
1018
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);
1023 }
1024 } else {
1025 $this->css[$media][$selector][$property] = trim($new_val);
1026 }
1027 }
1028
1029 /**
1030 * Start a new media section.
1031 * Check if the media is not already known,
1032 * else rename it with extra spaces
1033 * to avoid merging
1034 *
1035 * @param string $media
1036 * @return string
1037 */
1038 function css_new_media_section($media){
1039 if($this->get_cfg('preserve_css')) {
1040 return $media;
1041 }
1042
1043 // if the last @media is the same as this
1044 // keep it
1045 if (!$this->css OR !is_array($this->css) OR empty($this->css)){
1046 return $media;
1047 }
1048 end($this->css);
1049 list($at,) = each($this->css);
1050 if ($at == $media){
1051 return $media;
1052 }
1053 while (isset($this->css[$media]))
1054 if (is_numeric($media))
1055 $media++;
1056 else
1057 $media .= " ";
1058 return $media;
1059 }
1060
1061 /**
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)
1067 *
1068 * never merge @font-face
1069 *
1070 * @param string $media
1071 * @param string $selector
1072 * @return string
1073 */
1074 function css_new_selector($media,$selector){
1075 if($this->get_cfg('preserve_css')) {
1076 return $selector;
1077 }
1078 $selector = trim($selector);
1079 if (strncmp($selector,"@font-face",10)!=0){
1080 if ($this->settings['merge_selectors'] != false)
1081 return $selector;
1082
1083 if (!$this->css OR !isset($this->css[$media]) OR !$this->css[$media])
1084 return $selector;
1085
1086 // if last is the same, keep it
1087 end($this->css[$media]);
1088 list($sel,) = each($this->css[$media]);
1089 if ($sel == $selector){
1090 return $selector;
1091 }
1092 }
1093
1094 while (isset($this->css[$media][$selector]))
1095 $selector .= " ";
1096 return $selector;
1097 }
1098
1099 /**
1100 * Start a new propertie.
1101 * If already references in this selector,
1102 * rename it with extra space to avoid override
1103 *
1104 * @param string $media
1105 * @param string $selector
1106 * @param string $property
1107 * @return string
1108 */
1109 function css_new_property($media, $selector, $property){
1110 if($this->get_cfg('preserve_css')) {
1111 return $property;
1112 }
1113 if (!$this->css OR !isset($this->css[$media][$selector]) OR !$this->css[$media][$selector])
1114 return $property;
1115
1116 while (isset($this->css[$media][$selector][$property]))
1117 $property .= " ";
1118
1119 return $property;
1120 }
1121
1122 /**
1123 * Adds CSS to an existing media/selector
1124 * @param string $media
1125 * @param string $selector
1126 * @param array $css_add
1127 * @access private
1128 * @version 1.1
1129 */
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);
1133 }
1134 }
1135
1136 /**
1137 * Checks if $value is !important.
1138 * @param string $value
1139 * @return bool
1140 * @access public
1141 * @version 1.0
1142 */
1143 static function is_important(&$value) {
1144 return (
1145 strpos($value,"!")!==false // quick test
1146 AND !strcasecmp(substr(str_replace($GLOBALS['csstidy']['whitespace'], '', $value), -10, 10), '!important'));
1147 }
1148
1149 /**
1150 * Returns a value without !important
1151 * @param string $value
1152 * @return string
1153 * @access public
1154 * @version 1.0
1155 */
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);
1163 return $value;
1164 }
1165 return $value;
1166 }
1167
1168 /**
1169 * Checks if the next word in a string from pos is a CSS property
1170 * @param string $istring
1171 * @param integer $pos
1172 * @return bool
1173 * @access private
1174 * @version 1.2
1175 */
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) {
1181 return false;
1182 }
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');
1186 return true;
1187 }
1188 return false;
1189 }
1190
1191 /**
1192 * Checks if a property is valid
1193 * @param string $property
1194 * @return bool;
1195 * @access public
1196 * @version 1.0
1197 */
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 );
1202 }
1203
1204 /**
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:
1207 *
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")
1214 *
1215 * @param string
1216 * @return array
1217 */
1218
1219 function parse_string_list($value) {
1220 $value = trim($value);
1221
1222 // Case: empty
1223 if (!$value) return array();
1224
1225 $strings = array();
1226
1227 $in_str = false;
1228 $current_string = "";
1229
1230 for ($i = 0, $_len = strlen($value); $i < $_len; $i++) {
1231 if (($value{$i} == "," || $value{$i} === " ") && $in_str === true) {
1232 $in_str = false;
1233 $strings[] = $current_string;
1234 $current_string = "";
1235 }
1236 else if ($value{$i} == '"' || $value{$i} == "'"){
1237 if ($in_str === $value{$i}) {
1238 $strings[] = $current_string;
1239 $in_str = false;
1240 $current_string = "";
1241 continue;
1242 }
1243 else if (!$in_str) {
1244 $in_str = $value{$i};
1245 }
1246 }
1247 else {
1248 if ($in_str){
1249 $current_string .= $value{$i};
1250 }
1251 else {
1252 if (!preg_match("/[\s,]/", $value{$i})) {
1253 $in_str = true;
1254 $current_string = $value{$i};
1255 }
1256 }
1257 }
1258 }
1259
1260 if ($current_string) {
1261 $strings[] = $current_string;
1262 }
1263
1264 return $strings;
1265 }
1266 }