[SPIP] ~maj v3.0.14-->v3.0.17
[ptitvelo/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 if ($this->parser->get_cfg('preserve_css')) {
74 return;
75 }
76
77 if ($this->parser->get_cfg('merge_selectors') === 2) {
78 foreach ($this->css as $medium => $value) {
79 $this->merge_selectors($this->css[$medium]);
80 }
81 }
82
83 if ($this->parser->get_cfg('discard_invalid_selectors')) {
84 foreach ($this->css as $medium => $value) {
85 $this->discard_invalid_selectors($this->css[$medium]);
86 }
87 }
88
89 if ($this->parser->get_cfg('optimise_shorthands') > 0) {
90 foreach ($this->css as $medium => $value) {
91 foreach ($value as $selector => $value1) {
92 $this->css[$medium][$selector] = $this->merge_4value_shorthands($this->css[$medium][$selector]);
93
94 if ($this->parser->get_cfg('optimise_shorthands') < 2) {
95 continue;
96 }
97
98 $this->css[$medium][$selector] = $this->merge_font($this->css[$medium][$selector]);
99
100 if ($this->parser->get_cfg('optimise_shorthands') < 3) {
101 continue;
102 }
103
104 $this->css[$medium][$selector] = $this->merge_bg($this->css[$medium][$selector]);
105 if (empty($this->css[$medium][$selector])) {
106 unset($this->css[$medium][$selector]);
107 }
108 }
109 }
110 }
111 }
112
113 /**
114 * Optimises values
115 * @access public
116 * @version 1.0
117 */
118 public function value() {
119 $shorthands = & $this->parser->data['csstidy']['shorthands'];
120
121 // optimise shorthand properties
122 if (isset($shorthands[$this->property])) {
123 $temp = $this->shorthand($this->value); // FIXME - move
124 if ($temp != $this->value) {
125 $this->parser->log('Optimised shorthand notation (' . $this->property . '): Changed "' . $this->value . '" to "' . $temp . '"', 'Information');
126 }
127 $this->value = $temp;
128 }
129
130 // Remove whitespace at ! important
131 if ($this->value != $this->compress_important($this->value)) {
132 $this->parser->log('Optimised !important', 'Information');
133 }
134 }
135
136 /**
137 * Optimises shorthands
138 * @access public
139 * @version 1.0
140 */
141 public function shorthands() {
142 $shorthands = & $this->parser->data['csstidy']['shorthands'];
143
144 if (!$this->parser->get_cfg('optimise_shorthands') || $this->parser->get_cfg('preserve_css')) {
145 return;
146 }
147
148 if ($this->property === 'font' && $this->parser->get_cfg('optimise_shorthands') > 1) {
149 $this->css[$this->at][$this->selector]['font']='';
150 $this->parser->merge_css_blocks($this->at, $this->selector, $this->dissolve_short_font($this->value));
151 }
152 if ($this->property === 'background' && $this->parser->get_cfg('optimise_shorthands') > 2) {
153 $this->css[$this->at][$this->selector]['background']='';
154 $this->parser->merge_css_blocks($this->at, $this->selector, $this->dissolve_short_bg($this->value));
155 }
156 if (isset($shorthands[$this->property])) {
157 $this->parser->merge_css_blocks($this->at, $this->selector, $this->dissolve_4value_shorthands($this->property, $this->value));
158 if (is_array($shorthands[$this->property])) {
159 $this->css[$this->at][$this->selector][$this->property] = '';
160 }
161 }
162 }
163
164 /**
165 * Optimises a sub-value
166 * @access public
167 * @version 1.0
168 */
169 public function subvalue() {
170 $replace_colors = & $this->parser->data['csstidy']['replace_colors'];
171
172 $this->sub_value = trim($this->sub_value);
173 if ($this->sub_value == '') { // caution : '0'
174 return;
175 }
176
177 $important = '';
178 if ($this->parser->is_important($this->sub_value)) {
179 $important = '!important';
180 }
181 $this->sub_value = $this->parser->gvw_important($this->sub_value);
182
183 // Compress font-weight
184 if ($this->property === 'font-weight' && $this->parser->get_cfg('compress_font-weight')) {
185 if ($this->sub_value === 'bold') {
186 $this->sub_value = '700';
187 $this->parser->log('Optimised font-weight: Changed "bold" to "700"', 'Information');
188 } elseif ($this->sub_value === 'normal') {
189 $this->sub_value = '400';
190 $this->parser->log('Optimised font-weight: Changed "normal" to "400"', 'Information');
191 }
192 }
193
194 $temp = $this->compress_numbers($this->sub_value);
195 if (strcasecmp($temp, $this->sub_value) !== 0) {
196 if (strlen($temp) > strlen($this->sub_value)) {
197 $this->parser->log('Fixed invalid number: Changed "' . $this->sub_value . '" to "' . $temp . '"', 'Warning');
198 } else {
199 $this->parser->log('Optimised number: Changed "' . $this->sub_value . '" to "' . $temp . '"', 'Information');
200 }
201 $this->sub_value = $temp;
202 }
203 if ($this->parser->get_cfg('compress_colors')) {
204 $temp = $this->cut_color($this->sub_value);
205 if ($temp !== $this->sub_value) {
206 if (isset($replace_colors[$this->sub_value])) {
207 $this->parser->log('Fixed invalid color name: Changed "' . $this->sub_value . '" to "' . $temp . '"', 'Warning');
208 } else {
209 $this->parser->log('Optimised color: Changed "' . $this->sub_value . '" to "' . $temp . '"', 'Information');
210 }
211 $this->sub_value = $temp;
212 }
213 }
214 $this->sub_value .= $important;
215 }
216
217 /**
218 * Compresses shorthand values. Example: margin:1px 1px 1px 1px -> margin:1px
219 * @param string $value
220 * @access public
221 * @return string
222 * @version 1.0
223 */
224 public function shorthand($value) {
225 $important = '';
226 if ($this->parser->is_important($value)) {
227 $values = $this->parser->gvw_important($value);
228 $important = '!important';
229 }
230 else
231 $values = $value;
232
233 $values = explode(' ', $values);
234 switch (count($values)) {
235 case 4:
236 if ($values[0] == $values[1] && $values[0] == $values[2] && $values[0] == $values[3]) {
237 return $values[0] . $important;
238 } elseif ($values[1] == $values[3] && $values[0] == $values[2]) {
239 return $values[0] . ' ' . $values[1] . $important;
240 } elseif ($values[1] == $values[3]) {
241 return $values[0] . ' ' . $values[1] . ' ' . $values[2] . $important;
242 }
243 break;
244
245 case 3:
246 if ($values[0] == $values[1] && $values[0] == $values[2]) {
247 return $values[0] . $important;
248 } elseif ($values[0] == $values[2]) {
249 return $values[0] . ' ' . $values[1] . $important;
250 }
251 break;
252
253 case 2:
254 if ($values[0] == $values[1]) {
255 return $values[0] . $important;
256 }
257 break;
258 }
259
260 return $value;
261 }
262
263 /**
264 * Removes unnecessary whitespace in ! important
265 * @param string $string
266 * @return string
267 * @access public
268 * @version 1.1
269 */
270 public function compress_important(&$string) {
271 if ($this->parser->is_important($string)) {
272 $string = $this->parser->gvw_important($string) . '!important';
273 }
274 return $string;
275 }
276
277 /**
278 * Color compression function. Converts all rgb() values to #-values and uses the short-form if possible. Also replaces 4 color names by #-values.
279 * @param string $color
280 * @return string
281 * @version 1.1
282 */
283 public function cut_color($color) {
284 $replace_colors = & $this->parser->data['csstidy']['replace_colors'];
285
286 // if it's a string, don't touch !
287 if (strncmp($color, "'", 1) == 0 || strncmp($color, '"', 1) == 0)
288 return $color;
289
290 /* expressions complexes de type gradient */
291 if (strpos($color, '(') !== false && strncmp($color, 'rgb(' ,4) != 0) {
292 // on ne touche pas aux couleurs dans les expression ms, c'est trop sensible
293 if (stripos($color, 'progid:') !== false)
294 return $color;
295 preg_match_all(",rgb\([^)]+\),i", $color, $matches, PREG_SET_ORDER);
296 if (count($matches)) {
297 foreach ($matches as $m) {
298 $color = str_replace($m[0], $this->cut_color($m[0]), $color);
299 }
300 }
301 preg_match_all(",#[0-9a-f]{6}(?=[^0-9a-f]),i", $color, $matches, PREG_SET_ORDER);
302 if (count($matches)) {
303 foreach ($matches as $m) {
304 $color = str_replace($m[0],$this->cut_color($m[0]), $color);
305 }
306 }
307 return $color;
308 }
309
310 // rgb(0,0,0) -> #000000 (or #000 in this case later)
311 if (strncasecmp($color, 'rgb(', 4)==0) {
312 $color_tmp = substr($color, 4, strlen($color) - 5);
313 $color_tmp = explode(',', $color_tmp);
314 for ($i = 0; $i < count($color_tmp); $i++) {
315 $color_tmp[$i] = trim($color_tmp[$i]);
316 if (substr($color_tmp[$i], -1) === '%') {
317 $color_tmp[$i] = round((255 * $color_tmp[$i]) / 100);
318 }
319 if ($color_tmp[$i] > 255)
320 $color_tmp[$i] = 255;
321 }
322 $color = '#';
323 for ($i = 0; $i < 3; $i++) {
324 if ($color_tmp[$i] < 16) {
325 $color .= '0' . dechex($color_tmp[$i]);
326 } else {
327 $color .= dechex($color_tmp[$i]);
328 }
329 }
330 }
331
332 // Fix bad color names
333 if (isset($replace_colors[strtolower($color)])) {
334 $color = $replace_colors[strtolower($color)];
335 }
336
337 // #aabbcc -> #abc
338 if (strlen($color) == 7) {
339 $color_temp = strtolower($color);
340 if ($color_temp{0} === '#' && $color_temp{1} == $color_temp{2} && $color_temp{3} == $color_temp{4} && $color_temp{5} == $color_temp{6}) {
341 $color = '#' . $color{1} . $color{3} . $color{5};
342 }
343 }
344
345 switch (strtolower($color)) {
346 /* color name -> hex code */
347 case 'black': return '#000';
348 case 'fuchsia': return '#f0f';
349 case 'white': return '#fff';
350 case 'yellow': return '#ff0';
351
352 /* hex code -> color name */
353 case '#800000': return 'maroon';
354 case '#ffa500': return 'orange';
355 case '#808000': return 'olive';
356 case '#800080': return 'purple';
357 case '#008000': return 'green';
358 case '#000080': return 'navy';
359 case '#008080': return 'teal';
360 case '#c0c0c0': return 'silver';
361 case '#808080': return 'gray';
362 case '#f00': return 'red';
363 }
364
365 return $color;
366 }
367
368 /**
369 * Compresses numbers (ie. 1.0 becomes 1 or 1.100 becomes 1.1 )
370 * @param string $subvalue
371 * @return string
372 * @version 1.2
373 */
374 public function compress_numbers($subvalue) {
375 $unit_values = & $this->parser->data['csstidy']['unit_values'];
376 $color_values = & $this->parser->data['csstidy']['color_values'];
377
378 // for font:1em/1em sans-serif...;
379 if ($this->property === 'font') {
380 $temp = explode('/', $subvalue);
381 } else {
382 $temp = array($subvalue);
383 }
384
385 for ($l = 0; $l < count($temp); $l++) {
386 // if we are not dealing with a number at this point, do not optimise anything
387 $number = $this->AnalyseCssNumber($temp[$l]);
388 if ($number === false) {
389 return $subvalue;
390 }
391
392 // Fix bad colors
393 if (in_array($this->property, $color_values)) {
394 $temp[$l] = '#' . $temp[$l];
395 continue;
396 }
397
398 if (abs($number[0]) > 0) {
399 if ($number[1] == '' && in_array($this->property, $unit_values, true)) {
400 $number[1] = 'px';
401 }
402 } else {
403 $number[1] = '';
404 }
405
406 $temp[$l] = $number[0] . $number[1];
407 }
408
409 return ((count($temp) > 1) ? $temp[0] . '/' . $temp[1] : $temp[0]);
410 }
411
412 /**
413 * Checks if a given string is a CSS valid number. If it is,
414 * an array containing the value and unit is returned
415 * @param string $string
416 * @return array ('unit' if unit is found or '' if no unit exists, number value) or false if no number
417 */
418 public function AnalyseCssNumber($string) {
419 // most simple checks first
420 if (strlen($string) == 0 || ctype_alpha($string{0})) {
421 return false;
422 }
423
424 $units = & $this->parser->data['csstidy']['units'];
425 $return = array(0, '');
426
427 $return[0] = floatval($string);
428 if (abs($return[0]) > 0 && abs($return[0]) < 1) {
429 if ($return[0] < 0) {
430 $return[0] = '-' . ltrim(substr($return[0], 1), '0');
431 } else {
432 $return[0] = ltrim($return[0], '0');
433 }
434 }
435
436 // Look for unit and split from value if exists
437 foreach ($units as $unit) {
438 $expectUnitAt = strlen($string) - strlen($unit);
439 if (!($unitInString = stristr($string, $unit))) { // mb_strpos() fails with "false"
440 continue;
441 }
442 $actualPosition = strpos($string, $unitInString);
443 if ($expectUnitAt === $actualPosition) {
444 $return[1] = $unit;
445 $string = substr($string, 0, - strlen($unit));
446 break;
447 }
448 }
449 if (!is_numeric($string)) {
450 return false;
451 }
452 return $return;
453 }
454
455 /**
456 * Merges selectors with same properties. Example: a{color:red} b{color:red} -> a,b{color:red}
457 * Very basic and has at least one bug. Hopefully there is a replacement soon.
458 * @param array $array
459 * @return array
460 * @access public
461 * @version 1.2
462 */
463 public function merge_selectors(&$array) {
464 $css = $array;
465 foreach ($css as $key => $value) {
466 if (!isset($css[$key])) {
467 continue;
468 }
469 $newsel = '';
470
471 // Check if properties also exist in another selector
472 $keys = array();
473 // PHP bug (?) without $css = $array; here
474 foreach ($css as $selector => $vali) {
475 if ($selector == $key) {
476 continue;
477 }
478
479 if ($css[$key] === $vali) {
480 $keys[] = $selector;
481 }
482 }
483
484 if (!empty($keys)) {
485 $newsel = $key;
486 unset($css[$key]);
487 foreach ($keys as $selector) {
488 unset($css[$selector]);
489 $newsel .= ',' . $selector;
490 }
491 $css[$newsel] = $value;
492 }
493 }
494 $array = $css;
495 }
496
497 /**
498 * Removes invalid selectors and their corresponding rule-sets as
499 * defined by 4.1.7 in REC-CSS2. This is a very rudimentary check
500 * and should be replaced by a full-blown parsing algorithm or
501 * regular expression
502 * @version 1.4
503 */
504 public function discard_invalid_selectors(&$array) {
505 $invalid = array('+' => true, '~' => true, ',' => true, '>' => true);
506 foreach ($array as $selector => $decls) {
507 $ok = true;
508 $selectors = array_map('trim', explode(',', $selector));
509 foreach ($selectors as $s) {
510 $simple_selectors = preg_split('/\s*[+>~\s]\s*/', $s);
511 foreach ($simple_selectors as $ss) {
512 if ($ss === '')
513 $ok = false;
514 // could also check $ss for internal structure,
515 // but that probably would be too slow
516 }
517 }
518 if (!$ok)
519 unset($array[$selector]);
520 }
521 }
522
523 /**
524 * Dissolves properties like padding:10px 10px 10px to padding-top:10px;padding-bottom:10px;...
525 * @param string $property
526 * @param string $value
527 * @return array
528 * @version 1.0
529 * @see merge_4value_shorthands()
530 */
531 public function dissolve_4value_shorthands($property, $value) {
532 $shorthands = & $this->parser->data['csstidy']['shorthands'];
533 if (!is_array($shorthands[$property])) {
534 $return[$property] = $value;
535 return $return;
536 }
537
538 $important = '';
539 if ($this->parser->is_important($value)) {
540 $value = $this->parser->gvw_important($value);
541 $important = '!important';
542 }
543 $values = explode(' ', $value);
544
545
546 $return = array();
547 if (count($values) == 4) {
548 for ($i = 0; $i < 4; $i++) {
549 $return[$shorthands[$property][$i]] = $values[$i] . $important;
550 }
551 } elseif (count($values) == 3) {
552 $return[$shorthands[$property][0]] = $values[0] . $important;
553 $return[$shorthands[$property][1]] = $values[1] . $important;
554 $return[$shorthands[$property][3]] = $values[1] . $important;
555 $return[$shorthands[$property][2]] = $values[2] . $important;
556 } elseif (count($values) == 2) {
557 for ($i = 0; $i < 4; $i++) {
558 $return[$shorthands[$property][$i]] = (($i % 2 != 0)) ? $values[1] . $important : $values[0] . $important;
559 }
560 } else {
561 for ($i = 0; $i < 4; $i++) {
562 $return[$shorthands[$property][$i]] = $values[0] . $important;
563 }
564 }
565
566 return $return;
567 }
568
569 /**
570 * Explodes a string as explode() does, however, not if $sep is escaped or within a string.
571 * @param string $sep seperator
572 * @param string $string
573 * @return array
574 * @version 1.0
575 */
576 public function explode_ws($sep, $string) {
577 $status = 'st';
578 $to = '';
579
580 $output = array();
581 $num = 0;
582 for ($i = 0, $len = strlen($string); $i < $len; $i++) {
583 switch ($status) {
584 case 'st':
585 if ($string{$i} == $sep && !$this->parser->escaped($string, $i)) {
586 ++$num;
587 } elseif ($string{$i} === '"' || $string{$i} === '\'' || $string{$i} === '(' && !$this->parser->escaped($string, $i)) {
588 $status = 'str';
589 $to = ($string{$i} === '(') ? ')' : $string{$i};
590 (isset($output[$num])) ? $output[$num] .= $string{$i} : $output[$num] = $string{$i};
591 } else {
592 (isset($output[$num])) ? $output[$num] .= $string{$i} : $output[$num] = $string{$i};
593 }
594 break;
595
596 case 'str':
597 if ($string{$i} == $to && !$this->parser->escaped($string, $i)) {
598 $status = 'st';
599 }
600 (isset($output[$num])) ? $output[$num] .= $string{$i} : $output[$num] = $string{$i};
601 break;
602 }
603 }
604
605 if (isset($output[0])) {
606 return $output;
607 } else {
608 return array($output);
609 }
610 }
611
612 /**
613 * Merges Shorthand properties again, the opposite of dissolve_4value_shorthands()
614 * @param array $array
615 * @return array
616 * @version 1.2
617 * @see dissolve_4value_shorthands()
618 */
619 public function merge_4value_shorthands($array) {
620 $return = $array;
621 $shorthands = & $this->parser->data['csstidy']['shorthands'];
622
623 foreach ($shorthands as $key => $value) {
624 if (isset($array[$value[0]]) && isset($array[$value[1]])
625 && isset($array[$value[2]]) && isset($array[$value[3]]) && $value !== 0) {
626 $return[$key] = '';
627
628 $important = '';
629 for ($i = 0; $i < 4; $i++) {
630 $val = $array[$value[$i]];
631 if ($this->parser->is_important($val)) {
632 $important = '!important';
633 $return[$key] .= $this->parser->gvw_important($val) . ' ';
634 } else {
635 $return[$key] .= $val . ' ';
636 }
637 unset($return[$value[$i]]);
638 }
639 $return[$key] = $this->shorthand(trim($return[$key] . $important));
640 }
641 }
642 return $return;
643 }
644
645 /**
646 * Dissolve background property
647 * @param string $str_value
648 * @return array
649 * @version 1.0
650 * @see merge_bg()
651 * @todo full CSS 3 compliance
652 */
653 public function dissolve_short_bg($str_value) {
654 // don't try to explose background gradient !
655 if (stripos($str_value, 'gradient(')!== false)
656 return array('background'=>$str_value);
657
658 $background_prop_default = & $this->parser->data['csstidy']['background_prop_default'];
659 $repeat = array('repeat', 'repeat-x', 'repeat-y', 'no-repeat', 'space');
660 $attachment = array('scroll', 'fixed', 'local');
661 $clip = array('border', 'padding');
662 $origin = array('border', 'padding', 'content');
663 $pos = array('top', 'center', 'bottom', 'left', 'right');
664 $important = '';
665 $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);
666
667 if ($this->parser->is_important($str_value)) {
668 $important = ' !important';
669 $str_value = $this->parser->gvw_important($str_value);
670 }
671
672 $str_value = $this->explode_ws(',', $str_value);
673 for ($i = 0; $i < count($str_value); $i++) {
674 $have['clip'] = false;
675 $have['pos'] = false;
676 $have['color'] = false;
677 $have['bg'] = false;
678
679 if (is_array($str_value[$i])) {
680 $str_value[$i] = $str_value[$i][0];
681 }
682 $str_value[$i] = $this->explode_ws(' ', trim($str_value[$i]));
683
684 for ($j = 0; $j < count($str_value[$i]); $j++) {
685 if ($have['bg'] === false && (substr($str_value[$i][$j], 0, 4) === 'url(' || $str_value[$i][$j] === 'none')) {
686 $return['background-image'] .= $str_value[$i][$j] . ',';
687 $have['bg'] = true;
688 } elseif (in_array($str_value[$i][$j], $repeat, true)) {
689 $return['background-repeat'] .= $str_value[$i][$j] . ',';
690 } elseif (in_array($str_value[$i][$j], $attachment, true)) {
691 $return['background-attachment'] .= $str_value[$i][$j] . ',';
692 } elseif (in_array($str_value[$i][$j], $clip, true) && !$have['clip']) {
693 $return['background-clip'] .= $str_value[$i][$j] . ',';
694 $have['clip'] = true;
695 } elseif (in_array($str_value[$i][$j], $origin, true)) {
696 $return['background-origin'] .= $str_value[$i][$j] . ',';
697 } elseif ($str_value[$i][$j]{0} === '(') {
698 $return['background-size'] .= substr($str_value[$i][$j], 1, -1) . ',';
699 } 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} === '.') {
700 $return['background-position'] .= $str_value[$i][$j];
701 if (!$have['pos'])
702 $return['background-position'] .= ' '; else
703 $return['background-position'].= ',';
704 $have['pos'] = true;
705 } elseif (!$have['color']) {
706 $return['background-color'] .= $str_value[$i][$j] . ',';
707 $have['color'] = true;
708 }
709 }
710 }
711
712 foreach ($background_prop_default as $bg_prop => $default_value) {
713 if ($return[$bg_prop] !== null) {
714 $return[$bg_prop] = substr($return[$bg_prop], 0, -1) . $important;
715 }
716 else
717 $return[$bg_prop] = $default_value . $important;
718 }
719 return $return;
720 }
721
722 /**
723 * Merges all background properties
724 * @param array $input_css
725 * @return array
726 * @version 1.0
727 * @see dissolve_short_bg()
728 * @todo full CSS 3 compliance
729 */
730 public function merge_bg($input_css) {
731 $background_prop_default = & $this->parser->data['csstidy']['background_prop_default'];
732 // Max number of background images. CSS3 not yet fully implemented
733 $number_of_values = @max(count($this->explode_ws(',', $input_css['background-image'])), count($this->explode_ws(',', $input_css['background-color'])), 1);
734 // Array with background images to check if BG image exists
735 $bg_img_array = @$this->explode_ws(',', $this->parser->gvw_important($input_css['background-image']));
736 $new_bg_value = '';
737 $important = '';
738
739 // if background properties is here and not empty, don't try anything
740 if (isset($input_css['background']) && $input_css['background'])
741 return $input_css;
742
743 for ($i = 0; $i < $number_of_values; $i++) {
744 foreach ($background_prop_default as $bg_property => $default_value) {
745 // Skip if property does not exist
746 if (!isset($input_css[$bg_property])) {
747 continue;
748 }
749
750 $cur_value = $input_css[$bg_property];
751 // skip all optimisation if gradient() somewhere
752 if (stripos($cur_value, 'gradient(') !== false)
753 return $input_css;
754
755 // Skip some properties if there is no background image
756 if ((!isset($bg_img_array[$i]) || $bg_img_array[$i] === 'none')
757 && ($bg_property === 'background-size' || $bg_property === 'background-position'
758 || $bg_property === 'background-attachment' || $bg_property === 'background-repeat')) {
759 continue;
760 }
761
762 // Remove !important
763 if ($this->parser->is_important($cur_value)) {
764 $important = ' !important';
765 $cur_value = $this->parser->gvw_important($cur_value);
766 }
767
768 // Do not add default values
769 if ($cur_value === $default_value) {
770 continue;
771 }
772
773 $temp = $this->explode_ws(',', $cur_value);
774
775 if (isset($temp[$i])) {
776 if ($bg_property === 'background-size') {
777 $new_bg_value .= '(' . $temp[$i] . ') ';
778 } else {
779 $new_bg_value .= $temp[$i] . ' ';
780 }
781 }
782 }
783
784 $new_bg_value = trim($new_bg_value);
785 if ($i != $number_of_values - 1)
786 $new_bg_value .= ',';
787 }
788
789 // Delete all background-properties
790 foreach ($background_prop_default as $bg_property => $default_value) {
791 unset($input_css[$bg_property]);
792 }
793
794 // Add new background property
795 if ($new_bg_value !== '')
796 $input_css['background'] = $new_bg_value . $important;
797 elseif(isset ($input_css['background']))
798 $input_css['background'] = 'none';
799
800 return $input_css;
801 }
802
803 /**
804 * Dissolve font property
805 * @param string $str_value
806 * @return array
807 * @version 1.3
808 * @see merge_font()
809 */
810 public function dissolve_short_font($str_value) {
811 $font_prop_default = & $this->parser->data['csstidy']['font_prop_default'];
812 $font_weight = array('normal', 'bold', 'bolder', 'lighter', 100, 200, 300, 400, 500, 600, 700, 800, 900);
813 $font_variant = array('normal', 'small-caps');
814 $font_style = array('normal', 'italic', 'oblique');
815 $important = '';
816 $return = array('font-style' => null, 'font-variant' => null, 'font-weight' => null, 'font-size' => null, 'line-height' => null, 'font-family' => null);
817
818 if ($this->parser->is_important($str_value)) {
819 $important = '!important';
820 $str_value = $this->parser->gvw_important($str_value);
821 }
822
823 $have['style'] = false;
824 $have['variant'] = false;
825 $have['weight'] = false;
826 $have['size'] = false;
827 // Detects if font-family consists of several words w/o quotes
828 $multiwords = false;
829
830 // Workaround with multiple font-family
831 $str_value = $this->explode_ws(',', trim($str_value));
832
833 $str_value[0] = $this->explode_ws(' ', trim($str_value[0]));
834
835 for ($j = 0; $j < count($str_value[0]); $j++) {
836 if ($have['weight'] === false && in_array($str_value[0][$j], $font_weight)) {
837 $return['font-weight'] = $str_value[0][$j];
838 $have['weight'] = true;
839 } elseif ($have['variant'] === false && in_array($str_value[0][$j], $font_variant)) {
840 $return['font-variant'] = $str_value[0][$j];
841 $have['variant'] = true;
842 } elseif ($have['style'] === false && in_array($str_value[0][$j], $font_style)) {
843 $return['font-style'] = $str_value[0][$j];
844 $have['style'] = true;
845 } elseif ($have['size'] === false && (is_numeric($str_value[0][$j]{0}) || $str_value[0][$j]{0} === null || $str_value[0][$j]{0} === '.')) {
846 $size = $this->explode_ws('/', trim($str_value[0][$j]));
847 $return['font-size'] = $size[0];
848 if (isset($size[1])) {
849 $return['line-height'] = $size[1];
850 } else {
851 $return['line-height'] = ''; // don't add 'normal' !
852 }
853 $have['size'] = true;
854 } else {
855 if (isset($return['font-family'])) {
856 $return['font-family'] .= ' ' . $str_value[0][$j];
857 $multiwords = true;
858 } else {
859 $return['font-family'] = $str_value[0][$j];
860 }
861 }
862 }
863 // add quotes if we have several qords in font-family
864 if ($multiwords !== false) {
865 $return['font-family'] = '"' . $return['font-family'] . '"';
866 }
867 $i = 1;
868 while (isset($str_value[$i])) {
869 $return['font-family'] .= ',' . trim($str_value[$i]);
870 $i++;
871 }
872
873 // Fix for 100 and more font-size
874 if ($have['size'] === false && isset($return['font-weight']) &&
875 is_numeric($return['font-weight']{0})) {
876 $return['font-size'] = $return['font-weight'];
877 unset($return['font-weight']);
878 }
879
880 foreach ($font_prop_default as $font_prop => $default_value) {
881 if ($return[$font_prop] !== null) {
882 $return[$font_prop] = $return[$font_prop] . $important;
883 }
884 else
885 $return[$font_prop] = $default_value . $important;
886 }
887 return $return;
888 }
889
890 /**
891 * Merges all fonts properties
892 * @param array $input_css
893 * @return array
894 * @version 1.3
895 * @see dissolve_short_font()
896 */
897 public function merge_font($input_css) {
898 $font_prop_default = & $this->parser->data['csstidy']['font_prop_default'];
899 $new_font_value = '';
900 $important = '';
901 // Skip if not font-family and font-size set
902 if (isset($input_css['font-family']) && isset($input_css['font-size'])) {
903 // fix several words in font-family - add quotes
904 if (isset($input_css['font-family'])) {
905 $families = explode(',', $input_css['font-family']);
906 $result_families = array();
907 foreach ($families as $family) {
908 $family = trim($family);
909 $len = strlen($family);
910 if (strpos($family, ' ') &&
911 !(($family{0} === '"' && $family{$len - 1} === '"') ||
912 ($family{0} === "'" && $family{$len - 1} === "'"))) {
913 $family = '"' . $family . '"';
914 }
915 $result_families[] = $family;
916 }
917 $input_css['font-family'] = implode(',', $result_families);
918 }
919 foreach ($font_prop_default as $font_property => $default_value) {
920
921 // Skip if property does not exist
922 if (!isset($input_css[$font_property])) {
923 continue;
924 }
925
926 $cur_value = $input_css[$font_property];
927
928 // Skip if default value is used
929 if ($cur_value === $default_value) {
930 continue;
931 }
932
933 // Remove !important
934 if ($this->parser->is_important($cur_value)) {
935 $important = '!important';
936 $cur_value = $this->parser->gvw_important($cur_value);
937 }
938
939 $new_font_value .= $cur_value;
940 // Add delimiter
941 $new_font_value .= ( $font_property === 'font-size' &&
942 isset($input_css['line-height'])) ? '/' : ' ';
943 }
944
945 $new_font_value = trim($new_font_value);
946
947 // Delete all font-properties
948 foreach ($font_prop_default as $font_property => $default_value) {
949 if ($font_property !== 'font' || !$new_font_value)
950 unset($input_css[$font_property]);
951 }
952
953 // Add new font property
954 if ($new_font_value !== '') {
955 $input_css['font'] = $new_font_value . $important;
956 }
957 }
958
959 return $input_css;
960 }
961
962 }