[SCRIPTS] ~fix nouveau chemin d installation de SPIP
[lhc/web/www.git] / www / plugins-dist / compresseur / lib / csstidy / class.csstidy_optimise.php
1 <?php
2
3 /**
4 * CSSTidy - CSS Parser and Optimiser
5 *
6 * CSS Optimising Class
7 * This class optimises CSS data generated by csstidy.
8 *
9 * Copyright 2005, 2006, 2007 Florian Schmitz
10 *
11 * This file is part of CSSTidy.
12 *
13 * CSSTidy is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU Lesser General Public License as published by
15 * the Free Software Foundation; either version 2.1 of the License, or
16 * (at your option) any later version.
17 *
18 * CSSTidy is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License for more details.
22 *
23 * You should have received a copy of the GNU Lesser General Public License
24 * along with this program. If not, see <http://www.gnu.org/licenses/>.
25 *
26 * @license http://opensource.org/licenses/lgpl-license.php GNU Lesser General Public License
27 * @package csstidy
28 * @author Florian Schmitz (floele at gmail dot com) 2005-2007
29 * @author Brett Zamir (brettz9 at yahoo dot com) 2007
30 * @author Nikolay Matsievsky (speed at webo dot name) 2009-2010
31 * @author Cedric Morin (cedric at yterium dot com) 2010-2012
32 */
33
34 /**
35 * CSS Optimising Class
36 *
37 * This class optimises CSS data generated by csstidy.
38 *
39 * @package csstidy
40 * @author Florian Schmitz (floele at gmail dot com) 2005-2006
41 * @version 1.0
42 */
43 class csstidy_optimise {
44
45 /**
46 * csstidy object
47 * @var object
48 */
49 public $parser;
50
51 /**
52 * Constructor
53 * @param array $css contains the class csstidy
54 * @access private
55 * @version 1.0
56 */
57 public function __construct($css) {
58 $this->parser = $css;
59 $this->css = & $css->css;
60 $this->sub_value = & $css->sub_value;
61 $this->at = & $css->at;
62 $this->selector = & $css->selector;
63 $this->property = & $css->property;
64 $this->value = & $css->value;
65 }
66
67 /**
68 * Optimises $css after parsing
69 * @access public
70 * @version 1.0
71 */
72 public function postparse() {
73
74 if ($this->parser->get_cfg('reverse_left_and_right') > 0) {
75
76 foreach ($this->css as $medium => $selectors) {
77 if (is_array($selectors)) {
78 foreach ($selectors as $selector => $properties) {
79 $this->css[$medium][$selector] = $this->reverse_left_and_right($this->css[$medium][$selector]);
80 }
81 }
82 }
83
84 }
85
86 if ($this->parser->get_cfg('preserve_css')) {
87 return;
88 }
89
90 if ((int)$this->parser->get_cfg('merge_selectors') === 2) {
91 foreach ($this->css as $medium => $value) {
92 if (is_array($value)) {
93 $this->merge_selectors($this->css[$medium]);
94 }
95 }
96 }
97
98 if ($this->parser->get_cfg('discard_invalid_selectors')) {
99 foreach ($this->css as $medium => $value) {
100 if (is_array($value)) {
101 $this->discard_invalid_selectors($this->css[$medium]);
102 }
103 }
104 }
105
106 if ($this->parser->get_cfg('optimise_shorthands') > 0) {
107 foreach ($this->css as $medium => $value) {
108 if (is_array($value)) {
109 foreach ($value as $selector => $value1) {
110 $this->css[$medium][$selector] = $this->merge_4value_shorthands($this->css[$medium][$selector]);
111 $this->css[$medium][$selector] = $this->merge_4value_radius_shorthands($this->css[$medium][$selector]);
112
113 if ($this->parser->get_cfg('optimise_shorthands') < 2) {
114 continue;
115 }
116
117 $this->css[$medium][$selector] = $this->merge_font($this->css[$medium][$selector]);
118
119 if ($this->parser->get_cfg('optimise_shorthands') < 3) {
120 continue;
121 }
122
123 $this->css[$medium][$selector] = $this->merge_bg($this->css[$medium][$selector]);
124 if (empty($this->css[$medium][$selector])) {
125 unset($this->css[$medium][$selector]);
126 }
127 }
128 }
129 }
130 }
131 }
132
133 /**
134 * Optimises values
135 * @access public
136 * @version 1.0
137 */
138 public function value() {
139 $shorthands = & $this->parser->data['csstidy']['shorthands'];
140
141 // optimise shorthand properties
142 if (isset($shorthands[$this->property])) {
143 $temp = $this->shorthand($this->value); // FIXME - move
144 if ($temp != $this->value) {
145 $this->parser->log('Optimised shorthand notation (' . $this->property . '): Changed "' . $this->value . '" to "' . $temp . '"', 'Information');
146 }
147 $this->value = $temp;
148 }
149
150 // Remove whitespace at ! important
151 if ($this->value != $this->compress_important($this->value)) {
152 $this->parser->log('Optimised !important', 'Information');
153 }
154 }
155
156 /**
157 * Optimises shorthands
158 * @access public
159 * @version 1.0
160 */
161 public function shorthands() {
162 $shorthands = & $this->parser->data['csstidy']['shorthands'];
163
164 if (!$this->parser->get_cfg('optimise_shorthands') || $this->parser->get_cfg('preserve_css')) {
165 return;
166 }
167
168 if ($this->property === 'font' && $this->parser->get_cfg('optimise_shorthands') > 1) {
169 $this->css[$this->at][$this->selector]['font']='';
170 $this->parser->merge_css_blocks($this->at, $this->selector, $this->dissolve_short_font($this->value));
171 }
172 if ($this->property === 'background' && $this->parser->get_cfg('optimise_shorthands') > 2) {
173 $this->css[$this->at][$this->selector]['background']='';
174 $this->parser->merge_css_blocks($this->at, $this->selector, $this->dissolve_short_bg($this->value));
175 }
176 if (isset($shorthands[$this->property])) {
177 $this->parser->merge_css_blocks($this->at, $this->selector, $this->dissolve_4value_shorthands($this->property, $this->value));
178 if (is_array($shorthands[$this->property])) {
179 $this->css[$this->at][$this->selector][$this->property] = '';
180 }
181 }
182 }
183
184 /**
185 * Optimises a sub-value
186 * @access public
187 * @version 1.0
188 */
189 public function subvalue() {
190 $replace_colors = & $this->parser->data['csstidy']['replace_colors'];
191
192 $this->sub_value = trim($this->sub_value);
193 if ($this->sub_value == '') { // caution : '0'
194 return;
195 }
196
197 $important = '';
198 if ($this->parser->is_important($this->sub_value)) {
199 $important = '!important';
200 }
201 $this->sub_value = $this->parser->gvw_important($this->sub_value);
202
203 // Compress font-weight
204 if ($this->property === 'font-weight' && $this->parser->get_cfg('compress_font-weight')) {
205 if ($this->sub_value === 'bold') {
206 $this->sub_value = '700';
207 $this->parser->log('Optimised font-weight: Changed "bold" to "700"', 'Information');
208 } elseif ($this->sub_value === 'normal') {
209 $this->sub_value = '400';
210 $this->parser->log('Optimised font-weight: Changed "normal" to "400"', 'Information');
211 }
212 }
213
214 $temp = $this->compress_numbers($this->sub_value);
215 if (strcasecmp($temp, $this->sub_value) !== 0) {
216 if (strlen($temp) > strlen($this->sub_value)) {
217 $this->parser->log('Fixed invalid number: Changed "' . $this->sub_value . '" to "' . $temp . '"', 'Warning');
218 } else {
219 $this->parser->log('Optimised number: Changed "' . $this->sub_value . '" to "' . $temp . '"', 'Information');
220 }
221 $this->sub_value = $temp;
222 }
223 if ($this->parser->get_cfg('compress_colors')) {
224 $temp = $this->cut_color($this->sub_value);
225 if ($temp !== $this->sub_value) {
226 if (isset($replace_colors[$this->sub_value])) {
227 $this->parser->log('Fixed invalid color name: Changed "' . $this->sub_value . '" to "' . $temp . '"', 'Warning');
228 } else {
229 $this->parser->log('Optimised color: Changed "' . $this->sub_value . '" to "' . $temp . '"', 'Information');
230 }
231 $this->sub_value = $temp;
232 }
233 }
234 $this->sub_value .= $important;
235 }
236
237 /**
238 * Compresses shorthand values. Example: margin:1px 1px 1px 1px -> margin:1px
239 * @param string $value
240 * @access public
241 * @return string
242 * @version 1.0
243 */
244 public function shorthand($value) {
245 $important = '';
246 if ($this->parser->is_important($value)) {
247 $values = $this->parser->gvw_important($value);
248 $important = '!important';
249 }
250 else
251 $values = $value;
252
253 $values = explode(' ', $values);
254 switch (count($values)) {
255 case 4:
256 if ($values[0] == $values[1] && $values[0] == $values[2] && $values[0] == $values[3]) {
257 return $values[0] . $important;
258 } elseif ($values[1] == $values[3] && $values[0] == $values[2]) {
259 return $values[0] . ' ' . $values[1] . $important;
260 } elseif ($values[1] == $values[3]) {
261 return $values[0] . ' ' . $values[1] . ' ' . $values[2] . $important;
262 }
263 break;
264
265 case 3:
266 if ($values[0] == $values[1] && $values[0] == $values[2]) {
267 return $values[0] . $important;
268 } elseif ($values[0] == $values[2]) {
269 return $values[0] . ' ' . $values[1] . $important;
270 }
271 break;
272
273 case 2:
274 if ($values[0] == $values[1]) {
275 return $values[0] . $important;
276 }
277 break;
278 }
279
280 return $value;
281 }
282
283 /**
284 * Removes unnecessary whitespace in ! important
285 * @param string $string
286 * @return string
287 * @access public
288 * @version 1.1
289 */
290 public function compress_important(&$string) {
291 if ($this->parser->is_important($string)) {
292 $important = $this->parser->get_cfg('space_before_important') ? ' !important' : '!important';
293 $string = $this->parser->gvw_important($string) . $important;
294 }
295 return $string;
296 }
297
298 /**
299 * Color compression function. Converts all rgb() values to #-values and uses the short-form if possible. Also replaces 4 color names by #-values.
300 * @param string $color
301 * @return string
302 * @version 1.1
303 */
304 public function cut_color($color) {
305 $replace_colors = & $this->parser->data['csstidy']['replace_colors'];
306
307 // if it's a string, don't touch !
308 if (strncmp($color, "'", 1) == 0 || strncmp($color, '"', 1) == 0)
309 return $color;
310
311 /* expressions complexes de type gradient */
312 if (strpos($color, '(') !== false && strncmp($color, 'rgb(' ,4) != 0) {
313 // on ne touche pas aux couleurs dans les expression ms, c'est trop sensible
314 if (stripos($color, 'progid:') !== false)
315 return $color;
316 preg_match_all(",rgb\([^)]+\),i", $color, $matches, PREG_SET_ORDER);
317 if (count($matches)) {
318 foreach ($matches as $m) {
319 $color = str_replace($m[0], $this->cut_color($m[0]), $color);
320 }
321 }
322 preg_match_all(",#[0-9a-f]{6}(?=[^0-9a-f]),i", $color, $matches, PREG_SET_ORDER);
323 if (count($matches)) {
324 foreach ($matches as $m) {
325 $color = str_replace($m[0],$this->cut_color($m[0]), $color);
326 }
327 }
328 return $color;
329 }
330
331 // rgb(0,0,0) -> #000000 (or #000 in this case later)
332 if (strncasecmp($color, 'rgb(', 4)==0) {
333 $color_tmp = substr($color, 4, strlen($color) - 5);
334 $color_tmp = explode(',', $color_tmp);
335 for ($i = 0; $i < count($color_tmp); $i++) {
336 $color_tmp[$i] = trim($color_tmp[$i]);
337 if (substr($color_tmp[$i], -1) === '%') {
338 $color_tmp[$i] = round((255 * $color_tmp[$i]) / 100);
339 }
340 if ($color_tmp[$i] > 255)
341 $color_tmp[$i] = 255;
342 }
343 $color = '#';
344 for ($i = 0; $i < 3; $i++) {
345 if ($color_tmp[$i] < 16) {
346 $color .= '0' . dechex($color_tmp[$i]);
347 } else {
348 $color .= dechex($color_tmp[$i]);
349 }
350 }
351 }
352
353 // Fix bad color names
354 if (isset($replace_colors[strtolower($color)])) {
355 $color = $replace_colors[strtolower($color)];
356 }
357
358 // #aabbcc -> #abc
359 if (strlen($color) == 7) {
360 $color_temp = strtolower($color);
361 if ($color_temp{0} === '#' && $color_temp{1} == $color_temp{2} && $color_temp{3} == $color_temp{4} && $color_temp{5} == $color_temp{6}) {
362 $color = '#' . $color{1} . $color{3} . $color{5};
363 }
364 }
365
366 switch (strtolower($color)) {
367 /* color name -> hex code */
368 case 'black': return '#000';
369 case 'fuchsia': return '#f0f';
370 case 'white': return '#fff';
371 case 'yellow': return '#ff0';
372
373 /* hex code -> color name */
374 case '#800000': return 'maroon';
375 case '#ffa500': return 'orange';
376 case '#808000': return 'olive';
377 case '#800080': return 'purple';
378 case '#008000': return 'green';
379 case '#000080': return 'navy';
380 case '#008080': return 'teal';
381 case '#c0c0c0': return 'silver';
382 case '#808080': return 'gray';
383 case '#f00': return 'red';
384 }
385
386 return $color;
387 }
388
389 /**
390 * Compresses numbers (ie. 1.0 becomes 1 or 1.100 becomes 1.1 )
391 * @param string $subvalue
392 * @return string
393 * @version 1.2
394 */
395 public function compress_numbers($subvalue) {
396 $unit_values = & $this->parser->data['csstidy']['unit_values'];
397 $color_values = & $this->parser->data['csstidy']['color_values'];
398
399 // for font:1em/1em sans-serif...;
400 if ($this->property === 'font') {
401 $temp = explode('/', $subvalue);
402 } else {
403 $temp = array($subvalue);
404 }
405
406 for ($l = 0; $l < count($temp); $l++) {
407 // if we are not dealing with a number at this point, do not optimise anything
408 $number = $this->AnalyseCssNumber($temp[$l]);
409 if ($number === false) {
410 return $subvalue;
411 }
412
413 // Fix bad colors
414 if (in_array($this->property, $color_values)) {
415 $temp[$l] = '#' . $temp[$l];
416 continue;
417 }
418
419 if (abs($number[0]) > 0) {
420 if ($number[1] == '' && in_array($this->property, $unit_values, true)) {
421 $number[1] = 'px';
422 }
423 } elseif ($number[1] != 's' && $number[1] != 'ms') {
424 $number[1] = '';
425 }
426
427 $temp[$l] = $number[0] . $number[1];
428 }
429
430 return ((count($temp) > 1) ? $temp[0] . '/' . $temp[1] : $temp[0]);
431 }
432
433 /**
434 * Checks if a given string is a CSS valid number. If it is,
435 * an array containing the value and unit is returned
436 * @param string $string
437 * @return array ('unit' if unit is found or '' if no unit exists, number value) or false if no number
438 */
439 public function AnalyseCssNumber($string) {
440 // most simple checks first
441 if (strlen($string) == 0 || ctype_alpha($string{0})) {
442 return false;
443 }
444
445 $units = & $this->parser->data['csstidy']['units'];
446 $return = array(0, '');
447
448 $return[0] = floatval($string);
449 if (abs($return[0]) > 0 && abs($return[0]) < 1) {
450 if ($return[0] < 0) {
451 $return[0] = '-' . ltrim(substr($return[0], 1), '0');
452 } else {
453 $return[0] = ltrim($return[0], '0');
454 }
455 }
456
457 // Look for unit and split from value if exists
458 foreach ($units as $unit) {
459 $expectUnitAt = strlen($string) - strlen($unit);
460 if (!($unitInString = stristr($string, $unit))) { // mb_strpos() fails with "false"
461 continue;
462 }
463 $actualPosition = strpos($string, $unitInString);
464 if ($expectUnitAt === $actualPosition) {
465 $return[1] = $unit;
466 $string = substr($string, 0, - strlen($unit));
467 break;
468 }
469 }
470 if (!is_numeric($string)) {
471 return false;
472 }
473 return $return;
474 }
475
476 /**
477 * Merges selectors with same properties. Example: a{color:red} b{color:red} -> a,b{color:red}
478 * Very basic and has at least one bug. Hopefully there is a replacement soon.
479 * @param array $array
480 * @return array
481 * @access public
482 * @version 1.2
483 */
484 public function merge_selectors(&$array) {
485 $css = $array;
486 foreach ($css as $key => $value) {
487 if (!isset($css[$key])) {
488 continue;
489 }
490 $newsel = '';
491
492 // Check if properties also exist in another selector
493 $keys = array();
494 // PHP bug (?) without $css = $array; here
495 foreach ($css as $selector => $vali) {
496 if ($selector == $key) {
497 continue;
498 }
499
500 if ($css[$key] === $vali) {
501 $keys[] = $selector;
502 }
503 }
504
505 if (!empty($keys)) {
506 $newsel = $key;
507 unset($css[$key]);
508 foreach ($keys as $selector) {
509 unset($css[$selector]);
510 $newsel .= ',' . $selector;
511 }
512 $css[$newsel] = $value;
513 }
514 }
515 $array = $css;
516 }
517
518 /**
519 * Removes invalid selectors and their corresponding rule-sets as
520 * defined by 4.1.7 in REC-CSS2. This is a very rudimentary check
521 * and should be replaced by a full-blown parsing algorithm or
522 * regular expression
523 * @version 1.4
524 */
525 public function discard_invalid_selectors(&$array) {
526 $invalid = array('+' => true, '~' => true, ',' => true, '>' => true);
527 foreach ($array as $selector => $decls) {
528 $ok = true;
529 $selectors = array_map('trim', explode(',', $selector));
530 foreach ($selectors as $s) {
531 $simple_selectors = preg_split('/\s*[+>~\s]\s*/', $s);
532 foreach ($simple_selectors as $ss) {
533 if ($ss === '')
534 $ok = false;
535 // could also check $ss for internal structure,
536 // but that probably would be too slow
537 }
538 }
539 if (!$ok)
540 unset($array[$selector]);
541 }
542 }
543
544 /**
545 * Dissolves properties like padding:10px 10px 10px to padding-top:10px;padding-bottom:10px;...
546 * @param string $property
547 * @param string $value
548 * @param array|null $shorthands
549 * @return array
550 * @version 1.0
551 * @see merge_4value_shorthands()
552 */
553 public function dissolve_4value_shorthands($property, $value, $shorthands = null) {
554 if (is_null($shorthands)) {
555 $shorthands = & $this->parser->data['csstidy']['shorthands'];
556 }
557 if (!is_array($shorthands[$property])) {
558 $return[$property] = $value;
559 return $return;
560 }
561
562 $important = '';
563 if ($this->parser->is_important($value)) {
564 $value = $this->parser->gvw_important($value);
565 $important = '!important';
566 }
567 $values = explode(' ', $value);
568
569
570 $return = array();
571 if (count($values) == 4) {
572 for ($i = 0; $i < 4; $i++) {
573 $return[$shorthands[$property][$i]] = $values[$i] . $important;
574 }
575 } elseif (count($values) == 3) {
576 $return[$shorthands[$property][0]] = $values[0] . $important;
577 $return[$shorthands[$property][1]] = $values[1] . $important;
578 $return[$shorthands[$property][3]] = $values[1] . $important;
579 $return[$shorthands[$property][2]] = $values[2] . $important;
580 } elseif (count($values) == 2) {
581 for ($i = 0; $i < 4; $i++) {
582 $return[$shorthands[$property][$i]] = (($i % 2 != 0)) ? $values[1] . $important : $values[0] . $important;
583 }
584 } else {
585 for ($i = 0; $i < 4; $i++) {
586 $return[$shorthands[$property][$i]] = $values[0] . $important;
587 }
588 }
589
590 return $return;
591 }
592
593 /**
594 * Dissolves radius properties like
595 * border-radius:10px 10px 10px / 1px 2px
596 * to border-top-left:10px 1px;border-top-right:10px 2x;...
597 * @param string $property
598 * @param string $value
599 * @return array
600 * @version 1.0
601 * @use dissolve_4value_shorthands()
602 * @see merge_4value_radius_shorthands()
603 */
604 public function dissolve_4value_radius_shorthands($property, $value) {
605 $shorthands = & $this->parser->data['csstidy']['radius_shorthands'];
606 if (!is_array($shorthands[$property])) {
607 $return[$property] = $value;
608 return $return;
609 }
610
611 if (strpos($value, '/') !== false) {
612 $values = $this->explode_ws('/', $value);
613 if (count($values) == 2) {
614 $r[0] = $this->dissolve_4value_shorthands($property, trim($values[0]), $shorthands);
615 $r[1] = $this->dissolve_4value_shorthands($property, trim($values[1]), $shorthands);
616 $return = array();
617 foreach ($r[0] as $p=>$v) {
618 $return[$p] = $v;
619 if ($r[1][$p] !== $v) {
620 $return[$p] .= ' ' . $r[1][$p];
621 }
622 }
623 return $return;
624 }
625 }
626
627 $return = $this->dissolve_4value_shorthands($property, $value, $shorthands);
628 return $return;
629 }
630
631 /**
632 * Explodes a string as explode() does, however, not if $sep is escaped or within a string.
633 * @param string $sep seperator
634 * @param string $string
635 * @param bool $explode_in_parenthesis
636 * @return array
637 * @version 1.0
638 */
639 public function explode_ws($sep, $string, $explode_in_parenthesis = false) {
640 $status = 'st';
641 $to = '';
642
643 $output = array(
644 0 => '',
645 );
646 $num = 0;
647 for ($i = 0, $len = strlen($string); $i < $len; $i++) {
648 switch ($status) {
649 case 'st':
650 if ($string{$i} == $sep && !$this->parser->escaped($string, $i)) {
651 ++$num;
652 } elseif ($string{$i} === '"' || $string{$i} === '\'' || (!$explode_in_parenthesis && $string{$i} === '(') && !$this->parser->escaped($string, $i)) {
653 $status = 'str';
654 $to = ($string{$i} === '(') ? ')' : $string{$i};
655 (isset($output[$num])) ? $output[$num] .= $string{$i} : $output[$num] = $string{$i};
656 } else {
657 (isset($output[$num])) ? $output[$num] .= $string{$i} : $output[$num] = $string{$i};
658 }
659 break;
660
661 case 'str':
662 if ($string{$i} == $to && !$this->parser->escaped($string, $i)) {
663 $status = 'st';
664 }
665 (isset($output[$num])) ? $output[$num] .= $string{$i} : $output[$num] = $string{$i};
666 break;
667 }
668 }
669
670 return $output;
671 }
672
673 /**
674 * Merges Shorthand properties again, the opposite of dissolve_4value_shorthands()
675 * @param array $array
676 * @param array|null $shorthands
677 * @return array
678 * @version 1.2
679 * @see dissolve_4value_shorthands()
680 */
681 public function merge_4value_shorthands($array, $shorthands = null) {
682 $return = $array;
683 if (is_null($shorthands)) {
684 $shorthands = & $this->parser->data['csstidy']['shorthands'];
685 }
686
687 foreach ($shorthands as $key => $value) {
688 if ($value !== 0 && isset($array[$value[0]]) && isset($array[$value[1]])
689 && isset($array[$value[2]]) && isset($array[$value[3]])) {
690 $return[$key] = '';
691
692 $important = '';
693 for ($i = 0; $i < 4; $i++) {
694 $val = $array[$value[$i]];
695 if ($this->parser->is_important($val)) {
696 $important = '!important';
697 $return[$key] .= $this->parser->gvw_important($val) . ' ';
698 } else {
699 $return[$key] .= $val . ' ';
700 }
701 unset($return[$value[$i]]);
702 }
703 $return[$key] = $this->shorthand(trim($return[$key] . $important));
704 }
705 }
706 return $return;
707 }
708
709 /**
710 * Merges Shorthand properties again, the opposite of dissolve_4value_shorthands()
711 * @param array $array
712 * @return array
713 * @version 1.2
714 * @use merge_4value_shorthands()
715 * @see dissolve_4value_radius_shorthands()
716 */
717 public function merge_4value_radius_shorthands($array) {
718 $return = $array;
719 $shorthands = & $this->parser->data['csstidy']['radius_shorthands'];
720
721 foreach ($shorthands as $key => $value) {
722 if (isset($array[$value[0]]) && isset($array[$value[1]])
723 && isset($array[$value[2]]) && isset($array[$value[3]]) && $value !== 0) {
724 $return[$key] = '';
725 $a = array();
726 for ($i = 0; $i < 4; $i++) {
727 $v = $this->explode_ws(' ', trim($array[$value[$i]]));
728 $a[0][$value[$i]] = reset($v);
729 $a[1][$value[$i]] = end($v);
730 }
731 $r = array();
732 $r[0] = $this->merge_4value_shorthands($a[0], $shorthands);
733 $r[1] = $this->merge_4value_shorthands($a[1], $shorthands);
734
735 if (isset($r[0][$key]) and isset($r[1][$key])) {
736 $return[$key] = $r[0][$key];
737 if ($r[1][$key] !== $r[0][$key]) {
738 $return[$key] .= ' / ' . $r[1][$key];
739 }
740 for ($i = 0; $i < 4; $i++) {
741 unset($return[$value[$i]]);
742 }
743 }
744 }
745 }
746 return $return;
747 }
748 /**
749 * Dissolve background property
750 * @param string $str_value
751 * @return array
752 * @version 1.0
753 * @see merge_bg()
754 * @todo full CSS 3 compliance
755 */
756 public function dissolve_short_bg($str_value) {
757 // don't try to explose background gradient !
758 if (stripos($str_value, 'gradient(')!== false)
759 return array('background'=>$str_value);
760
761 $background_prop_default = & $this->parser->data['csstidy']['background_prop_default'];
762 $repeat = array('repeat', 'repeat-x', 'repeat-y', 'no-repeat', 'space');
763 $attachment = array('scroll', 'fixed', 'local');
764 $clip = array('border', 'padding');
765 $origin = array('border', 'padding', 'content');
766 $pos = array('top', 'center', 'bottom', 'left', 'right');
767 $important = '';
768 $return = array('background-image' => null, 'background-size' => null, 'background-repeat' => null, 'background-position' => null, 'background-attachment' => null, 'background-clip' => null, 'background-origin' => null, 'background-color' => null);
769
770 if ($this->parser->is_important($str_value)) {
771 $important = ' !important';
772 $str_value = $this->parser->gvw_important($str_value);
773 }
774
775 $str_value = $this->explode_ws(',', $str_value);
776 for ($i = 0; $i < count($str_value); $i++) {
777 $have['clip'] = false;
778 $have['pos'] = false;
779 $have['color'] = false;
780 $have['bg'] = false;
781
782 if (is_array($str_value[$i])) {
783 $str_value[$i] = $str_value[$i][0];
784 }
785 $str_value[$i] = $this->explode_ws(' ', trim($str_value[$i]));
786
787 for ($j = 0; $j < count($str_value[$i]); $j++) {
788 if ($have['bg'] === false && (substr($str_value[$i][$j], 0, 4) === 'url(' || $str_value[$i][$j] === 'none')) {
789 $return['background-image'] .= $str_value[$i][$j] . ',';
790 $have['bg'] = true;
791 } elseif (in_array($str_value[$i][$j], $repeat, true)) {
792 $return['background-repeat'] .= $str_value[$i][$j] . ',';
793 } elseif (in_array($str_value[$i][$j], $attachment, true)) {
794 $return['background-attachment'] .= $str_value[$i][$j] . ',';
795 } elseif (in_array($str_value[$i][$j], $clip, true) && !$have['clip']) {
796 $return['background-clip'] .= $str_value[$i][$j] . ',';
797 $have['clip'] = true;
798 } elseif (in_array($str_value[$i][$j], $origin, true)) {
799 $return['background-origin'] .= $str_value[$i][$j] . ',';
800 } elseif ($str_value[$i][$j]{0} === '(') {
801 $return['background-size'] .= substr($str_value[$i][$j], 1, -1) . ',';
802 } elseif (in_array($str_value[$i][$j], $pos, true) || is_numeric($str_value[$i][$j]{0}) || $str_value[$i][$j]{0} === null || $str_value[$i][$j]{0} === '-' || $str_value[$i][$j]{0} === '.') {
803 $return['background-position'] .= $str_value[$i][$j];
804 if (!$have['pos'])
805 $return['background-position'] .= ' '; else
806 $return['background-position'].= ',';
807 $have['pos'] = true;
808 } elseif (!$have['color']) {
809 $return['background-color'] .= $str_value[$i][$j] . ',';
810 $have['color'] = true;
811 }
812 }
813 }
814
815 foreach ($background_prop_default as $bg_prop => $default_value) {
816 if ($return[$bg_prop] !== null) {
817 $return[$bg_prop] = substr($return[$bg_prop], 0, -1) . $important;
818 }
819 else
820 $return[$bg_prop] = $default_value . $important;
821 }
822 return $return;
823 }
824
825 /**
826 * Merges all background properties
827 * @param array $input_css
828 * @return array
829 * @version 1.0
830 * @see dissolve_short_bg()
831 * @todo full CSS 3 compliance
832 */
833 public function merge_bg($input_css) {
834 $background_prop_default = & $this->parser->data['csstidy']['background_prop_default'];
835 // Max number of background images. CSS3 not yet fully implemented
836 $number_of_values = @max(count($this->explode_ws(',', $input_css['background-image'])), count($this->explode_ws(',', $input_css['background-color'])), 1);
837 // Array with background images to check if BG image exists
838 $bg_img_array = @$this->explode_ws(',', $this->parser->gvw_important($input_css['background-image']));
839 $new_bg_value = '';
840 $important = '';
841
842 // if background properties is here and not empty, don't try anything
843 if (isset($input_css['background']) && $input_css['background'])
844 return $input_css;
845
846 for ($i = 0; $i < $number_of_values; $i++) {
847 foreach ($background_prop_default as $bg_property => $default_value) {
848 // Skip if property does not exist
849 if (!isset($input_css[$bg_property])) {
850 continue;
851 }
852
853 $cur_value = $input_css[$bg_property];
854 // skip all optimisation if gradient() somewhere
855 if (stripos($cur_value, 'gradient(') !== false)
856 return $input_css;
857
858 // Skip some properties if there is no background image
859 if ((!isset($bg_img_array[$i]) || $bg_img_array[$i] === 'none')
860 && ($bg_property === 'background-size' || $bg_property === 'background-position'
861 || $bg_property === 'background-attachment' || $bg_property === 'background-repeat')) {
862 continue;
863 }
864
865 // Remove !important
866 if ($this->parser->is_important($cur_value)) {
867 $important = ' !important';
868 $cur_value = $this->parser->gvw_important($cur_value);
869 }
870
871 // Do not add default values
872 if ($cur_value === $default_value) {
873 continue;
874 }
875
876 $temp = $this->explode_ws(',', $cur_value);
877
878 if (isset($temp[$i])) {
879 if ($bg_property === 'background-size') {
880 $new_bg_value .= '(' . $temp[$i] . ') ';
881 } else {
882 $new_bg_value .= $temp[$i] . ' ';
883 }
884 }
885 }
886
887 $new_bg_value = trim($new_bg_value);
888 if ($i != $number_of_values - 1)
889 $new_bg_value .= ',';
890 }
891
892 // Delete all background-properties
893 foreach ($background_prop_default as $bg_property => $default_value) {
894 unset($input_css[$bg_property]);
895 }
896
897 // Add new background property
898 if ($new_bg_value !== '')
899 $input_css['background'] = $new_bg_value . $important;
900 elseif(isset ($input_css['background']))
901 $input_css['background'] = 'none';
902
903 return $input_css;
904 }
905
906 /**
907 * Dissolve font property
908 * @param string $str_value
909 * @return array
910 * @version 1.3
911 * @see merge_font()
912 */
913 public function dissolve_short_font($str_value) {
914 $font_prop_default = & $this->parser->data['csstidy']['font_prop_default'];
915 $font_weight = array('normal', 'bold', 'bolder', 'lighter', 100, 200, 300, 400, 500, 600, 700, 800, 900);
916 $font_variant = array('normal', 'small-caps');
917 $font_style = array('normal', 'italic', 'oblique');
918 $important = '';
919 $return = array('font-style' => null, 'font-variant' => null, 'font-weight' => null, 'font-size' => null, 'line-height' => null, 'font-family' => null);
920
921 if ($this->parser->is_important($str_value)) {
922 $important = '!important';
923 $str_value = $this->parser->gvw_important($str_value);
924 }
925
926 $have['style'] = false;
927 $have['variant'] = false;
928 $have['weight'] = false;
929 $have['size'] = false;
930 // Detects if font-family consists of several words w/o quotes
931 $multiwords = false;
932
933 // Workaround with multiple font-family
934 $str_value = $this->explode_ws(',', trim($str_value));
935
936 $str_value[0] = $this->explode_ws(' ', trim($str_value[0]));
937
938 for ($j = 0; $j < count($str_value[0]); $j++) {
939 if ($have['weight'] === false && in_array($str_value[0][$j], $font_weight)) {
940 $return['font-weight'] = $str_value[0][$j];
941 $have['weight'] = true;
942 } elseif ($have['variant'] === false && in_array($str_value[0][$j], $font_variant)) {
943 $return['font-variant'] = $str_value[0][$j];
944 $have['variant'] = true;
945 } elseif ($have['style'] === false && in_array($str_value[0][$j], $font_style)) {
946 $return['font-style'] = $str_value[0][$j];
947 $have['style'] = true;
948 } elseif ($have['size'] === false && (is_numeric($str_value[0][$j]{0}) || $str_value[0][$j]{0} === null || $str_value[0][$j]{0} === '.')) {
949 $size = $this->explode_ws('/', trim($str_value[0][$j]));
950 $return['font-size'] = $size[0];
951 if (isset($size[1])) {
952 $return['line-height'] = $size[1];
953 } else {
954 $return['line-height'] = ''; // don't add 'normal' !
955 }
956 $have['size'] = true;
957 } else {
958 if (isset($return['font-family'])) {
959 $return['font-family'] .= ' ' . $str_value[0][$j];
960 $multiwords = true;
961 } else {
962 $return['font-family'] = $str_value[0][$j];
963 }
964 }
965 }
966 // add quotes if we have several qords in font-family
967 if ($multiwords !== false) {
968 $return['font-family'] = '"' . $return['font-family'] . '"';
969 }
970 $i = 1;
971 while (isset($str_value[$i])) {
972 $return['font-family'] .= ',' . trim($str_value[$i]);
973 $i++;
974 }
975
976 // Fix for 100 and more font-size
977 if ($have['size'] === false && isset($return['font-weight']) &&
978 is_numeric($return['font-weight']{0})) {
979 $return['font-size'] = $return['font-weight'];
980 unset($return['font-weight']);
981 }
982
983 foreach ($font_prop_default as $font_prop => $default_value) {
984 if ($return[$font_prop] !== null) {
985 $return[$font_prop] = $return[$font_prop] . $important;
986 }
987 else
988 $return[$font_prop] = $default_value . $important;
989 }
990 return $return;
991 }
992
993 /**
994 * Merges all fonts properties
995 * @param array $input_css
996 * @return array
997 * @version 1.3
998 * @see dissolve_short_font()
999 */
1000 public function merge_font($input_css) {
1001 $font_prop_default = & $this->parser->data['csstidy']['font_prop_default'];
1002 $new_font_value = '';
1003 $important = '';
1004 // Skip if not font-family and font-size set
1005 if (isset($input_css['font-family']) && isset($input_css['font-size']) && $input_css['font-family'] != 'inherit') {
1006 // fix several words in font-family - add quotes
1007 if (isset($input_css['font-family'])) {
1008 $families = explode(',', $input_css['font-family']);
1009 $result_families = array();
1010 foreach ($families as $family) {
1011 $family = trim($family);
1012 $len = strlen($family);
1013 if (strpos($family, ' ') &&
1014 !(($family{0} === '"' && $family{$len - 1} === '"') ||
1015 ($family{0} === "'" && $family{$len - 1} === "'"))) {
1016 $family = '"' . $family . '"';
1017 }
1018 $result_families[] = $family;
1019 }
1020 $input_css['font-family'] = implode(',', $result_families);
1021 }
1022 foreach ($font_prop_default as $font_property => $default_value) {
1023
1024 // Skip if property does not exist
1025 if (!isset($input_css[$font_property])) {
1026 continue;
1027 }
1028
1029 $cur_value = $input_css[$font_property];
1030
1031 // Skip if default value is used
1032 if ($cur_value === $default_value) {
1033 continue;
1034 }
1035
1036 // Remove !important
1037 if ($this->parser->is_important($cur_value)) {
1038 $important = '!important';
1039 $cur_value = $this->parser->gvw_important($cur_value);
1040 }
1041
1042 $new_font_value .= $cur_value;
1043 // Add delimiter
1044 $new_font_value .= ( $font_property === 'font-size' &&
1045 isset($input_css['line-height'])) ? '/' : ' ';
1046 }
1047
1048 $new_font_value = trim($new_font_value);
1049
1050 // Delete all font-properties
1051 foreach ($font_prop_default as $font_property => $default_value) {
1052 if ($font_property !== 'font' || !$new_font_value)
1053 unset($input_css[$font_property]);
1054 }
1055
1056 // Add new font property
1057 if ($new_font_value !== '') {
1058 $input_css['font'] = $new_font_value . $important;
1059 }
1060 }
1061
1062 return $input_css;
1063 }
1064
1065 /**
1066 * Reverse left vs right in a list of properties/values
1067 * @param array $array
1068 * @return array
1069 */
1070 public function reverse_left_and_right($array) {
1071 $return = array();
1072
1073 // change left <-> right in properties name and values
1074 foreach ($array as $propertie => $value) {
1075
1076 if (method_exists($this, $m = 'reverse_left_and_right_' . str_replace('-','_',trim($propertie)))) {
1077 $value = $this->$m($value);
1078 }
1079
1080 // simple replacement for properties
1081 $propertie = str_ireplace(array('left', 'right' ,"\x1"), array("\x1", 'left', 'right') , $propertie);
1082 // be careful for values, not modifying protected or quoted valued
1083 foreach (array('left' => "\x1", 'right' => 'left', "\x1" => 'right') as $v => $r) {
1084 if (strpos($value, $v) !== false) {
1085 // attraper les left et right separes du reste (pas au milieu d'un mot)
1086 if (in_array($v, array('left', 'right') )) {
1087 $value = preg_replace(",\\b$v\\b,", "\x0" , $value);
1088 }
1089 else {
1090 $value = str_replace($v, "\x0" , $value);
1091 }
1092 $value = $this->explode_ws("\x0", $value . ' ', true);
1093 $value = rtrim(implode($r, $value));
1094 $value = str_replace("\x0" , $v, $value);
1095 }
1096 }
1097 $return[$propertie] = $value;
1098 }
1099
1100 return $return;
1101 }
1102
1103 /**
1104 * Reversing 4 values shorthands properties
1105 * @param string $value
1106 * @return string
1107 */
1108 public function reverse_left_and_right_4value_shorthands($property, $value) {
1109 $shorthands = & $this->parser->data['csstidy']['shorthands'];
1110 if (isset($shorthands[$property])) {
1111 $property_right = $shorthands[$property][1];
1112 $property_left = $shorthands[$property][3];
1113 $v = $this->dissolve_4value_shorthands($property, $value);
1114 if ($v[$property_left] !== $v[$property_right]) {
1115 $r = $v[$property_right];
1116 $v[$property_right] = $v[$property_left];
1117 $v[$property_left] = $r;
1118 $v = $this->merge_4value_shorthands($v);
1119 if (isset($v[$property])) {
1120 return $v[$property];
1121 }
1122 }
1123 }
1124 return $value;
1125 }
1126
1127 /**
1128 * Reversing 4 values radius shorthands properties
1129 * @param string $value
1130 * @return string
1131 */
1132 public function reverse_left_and_right_4value_radius_shorthands($property, $value) {
1133 $shorthands = & $this->parser->data['csstidy']['radius_shorthands'];
1134 if (isset($shorthands[$property])) {
1135 $v = $this->dissolve_4value_radius_shorthands($property, $value);
1136 if ($v[$shorthands[$property][0]] !== $v[$shorthands[$property][1]]
1137 or $v[$shorthands[$property][2]] !== $v[$shorthands[$property][3]]) {
1138 $r = array(
1139 $shorthands[$property][0] => $v[$shorthands[$property][1]],
1140 $shorthands[$property][1] => $v[$shorthands[$property][0]],
1141 $shorthands[$property][2] => $v[$shorthands[$property][3]],
1142 $shorthands[$property][3] => $v[$shorthands[$property][2]],
1143 );
1144 $v = $this->merge_4value_radius_shorthands($r);
1145 if (isset($v[$property])) {
1146 return $v[$property];
1147 }
1148 }
1149 }
1150 return $value;
1151 }
1152
1153 /**
1154 * Reversing margin shorthands
1155 * @param string $value
1156 * @return string
1157 */
1158 public function reverse_left_and_right_margin($value) {
1159 return $this->reverse_left_and_right_4value_shorthands('margin', $value);
1160 }
1161
1162 /**
1163 * Reversing padding shorthands
1164 * @param string $value
1165 * @return string
1166 */
1167 public function reverse_left_and_right_padding($value) {
1168 return $this->reverse_left_and_right_4value_shorthands('padding', $value);
1169 }
1170
1171 /**
1172 * Reversing border-color shorthands
1173 * @param string $value
1174 * @return string
1175 */
1176 public function reverse_left_and_right_border_color($value) {
1177 return $this->reverse_left_and_right_4value_shorthands('border-color', $value);
1178 }
1179
1180 /**
1181 * Reversing border-style shorthands
1182 * @param string $value
1183 * @return string
1184 */
1185 public function reverse_left_and_right_border_style($value) {
1186 return $this->reverse_left_and_right_4value_shorthands('border-style', $value);
1187 }
1188
1189 /**
1190 * Reversing border-width shorthands
1191 * @param string $value
1192 * @return string
1193 */
1194 public function reverse_left_and_right_border_width($value) {
1195 return $this->reverse_left_and_right_4value_shorthands('border-width', $value);
1196 }
1197
1198 /**
1199 * Reversing border-radius shorthands
1200 * @param string $value
1201 * @return string
1202 */
1203 public function reverse_left_and_right_border_radius($value) {
1204 return $this->reverse_left_and_right_4value_radius_shorthands('border-radius', $value);
1205 }
1206
1207 /**
1208 * Reversing border-radius shorthands
1209 * @param string $value
1210 * @return string
1211 */
1212 public function reverse_left_and_right__moz_border_radius($value) {
1213 return $this->reverse_left_and_right_4value_radius_shorthands('border-radius', $value);
1214 }
1215
1216 /**
1217 * Reversing border-radius shorthands
1218 * @param string $value
1219 * @return string
1220 */
1221 public function reverse_left_and_right__webkit_border_radius($value) {
1222 return $this->reverse_left_and_right_4value_radius_shorthands('border-radius', $value);
1223 }
1224
1225
1226 /**
1227 * Reversing background shorthands
1228 * @param string $value
1229 * @return string
1230 */
1231 public function reverse_left_and_right_background($value) {
1232 $values = $this->dissolve_short_bg($value);
1233 if (isset($values['background-position']) and $values['background-position']) {
1234 $v = $this->reverse_left_and_right_background_position($values['background-position']);
1235 if ($v !== $values['background-position']) {
1236 if ($value == $values['background-position']) {
1237 return $v;
1238 }
1239 else {
1240 $values['background-position'] = $v;
1241 $x = $this->merge_bg($values);
1242 if (isset($x['background'])) {
1243 return $x['background'];
1244 }
1245 }
1246 }
1247 }
1248 return $value;
1249 }
1250
1251 /**
1252 * Reversing background position shorthands
1253 * @param string $value
1254 * @return string
1255 */
1256 public function reverse_left_and_right_background_position_x($value) {
1257 return $this->reverse_left_and_right_background_position($value);
1258 }
1259
1260 /**
1261 * Reversing background position shorthands
1262 * @param string $value
1263 * @return string
1264 */
1265 public function reverse_left_and_right_background_position($value) {
1266 // multiple background case
1267 if (strpos($value, ',') !== false) {
1268 $values = $this->explode_ws(',', $value);
1269 if (count($values) > 1) {
1270 foreach ($values as $k=>$v) {
1271 $values[$k] = $this->reverse_left_and_right_background_position($v);
1272 }
1273 return implode(',', $values);
1274 }
1275 }
1276
1277 // if no explicit left or right value
1278 if (stripos($value, 'left') === false and stripos($value, 'right') === false) {
1279 $values = $this->explode_ws(' ', trim($value));
1280 $values = array_map('trim', $values);
1281 $values = array_filter($values);
1282 $values = array_values($values);
1283 if (count($values) == 1) {
1284 return "left $value";
1285 }
1286 if ($values[1] == 'top' or $values[1] == 'bottom') {
1287 return 'left ' . implode(' ', $values);
1288 }
1289 else {
1290 $last = array_pop($values);
1291 return implode(' ', $values) . ' left ' . $last;
1292 }
1293 }
1294
1295 return $value;
1296 }
1297
1298 }