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