49724369c83be22f73e79447d677cfbdb129bbc9
4 * CSSTidy - CSS Parser and Optimiser
7 * This class optimises CSS data generated by csstidy.
9 * Copyright 2005, 2006, 2007 Florian Schmitz
11 * This file is part of CSSTidy.
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.
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.
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/>.
26 * @license http://opensource.org/licenses/lgpl-license.php GNU Lesser General Public License
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
35 * CSS Optimising Class
37 * This class optimises CSS data generated by csstidy.
40 * @author Florian Schmitz (floele at gmail dot com) 2005-2006
43 class csstidy_optimise
{
53 * @param array $css contains the class csstidy
57 public function __construct($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
;
68 * Optimises $css after parsing
72 public function postparse() {
74 if ($this->parser
->get_cfg('reverse_left_and_right') > 0) {
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]);
86 if ($this->parser
->get_cfg('preserve_css')) {
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]);
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]);
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]);
113 if ($this->parser
->get_cfg('optimise_shorthands') < 2) {
117 $this->css
[$medium][$selector] = $this->merge_font($this->css
[$medium][$selector]);
119 if ($this->parser
->get_cfg('optimise_shorthands') < 3) {
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]);
138 public function value() {
139 $shorthands = & $this->parser
->data
['csstidy']['shorthands'];
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');
147 $this->value
= $temp;
150 // Remove whitespace at ! important
151 if ($this->value
!= $this->compress_important($this->value
)) {
152 $this->parser
->log('Optimised !important', 'Information');
157 * Optimises shorthands
161 public function shorthands() {
162 $shorthands = & $this->parser
->data
['csstidy']['shorthands'];
164 if (!$this->parser
->get_cfg('optimise_shorthands') ||
$this->parser
->get_cfg('preserve_css')) {
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
));
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
));
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
] = '';
185 * Optimises a sub-value
189 public function subvalue() {
190 $replace_colors = & $this->parser
->data
['csstidy']['replace_colors'];
192 $this->sub_value
= trim($this->sub_value
);
193 if ($this->sub_value
== '') { // caution : '0'
198 if ($this->parser
->is_important($this->sub_value
)) {
199 $important = '!important';
201 $this->sub_value
= $this->parser
->gvw_important($this->sub_value
);
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');
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');
219 $this->parser
->log('Optimised number: Changed "' . $this->sub_value
. '" to "' . $temp . '"', 'Information');
221 $this->sub_value
= $temp;
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');
229 $this->parser
->log('Optimised color: Changed "' . $this->sub_value
. '" to "' . $temp . '"', 'Information');
231 $this->sub_value
= $temp;
234 $this->sub_value
.= $important;
238 * Compresses shorthand values. Example: margin:1px 1px 1px 1px -> margin:1px
239 * @param string $value
244 public function shorthand($value) {
246 if ($this->parser
->is_important($value)) {
247 $values = $this->parser
->gvw_important($value);
248 $important = '!important';
253 $values = explode(' ', $values);
254 switch (count($values)) {
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;
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;
274 if ($values[0] == $values[1]) {
275 return $values[0] . $important;
284 * Removes unnecessary whitespace in ! important
285 * @param string $string
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;
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
304 public function cut_color($color) {
305 $replace_colors = & $this->parser
->data
['csstidy']['replace_colors'];
307 // if it's a string, don't touch !
308 if (strncmp($color, "'", 1) == 0 ||
strncmp($color, '"', 1) == 0)
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)
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);
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);
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);
340 if ($color_tmp[$i] > 255)
341 $color_tmp[$i] = 255;
344 for ($i = 0; $i < 3; $i++
) {
345 if ($color_tmp[$i] < 16) {
346 $color .= '0' . dechex($color_tmp[$i]);
348 $color .= dechex($color_tmp[$i]);
353 // Fix bad color names
354 if (isset($replace_colors[strtolower($color)])) {
355 $color = $replace_colors[strtolower($color)];
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};
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';
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';
390 * Compresses numbers (ie. 1.0 becomes 1 or 1.100 becomes 1.1 )
391 * @param string $subvalue
395 public function compress_numbers($subvalue) {
396 $unit_values = & $this->parser
->data
['csstidy']['unit_values'];
397 $color_values = & $this->parser
->data
['csstidy']['color_values'];
399 // for font:1em/1em sans-serif...;
400 if ($this->property
=== 'font') {
401 $temp = explode('/', $subvalue);
403 $temp = array($subvalue);
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) {
414 if (in_array($this->property
, $color_values)) {
415 $temp[$l] = '#' . $temp[$l];
419 if (abs($number[0]) > 0) {
420 if ($number[1] == '' && in_array($this->property
, $unit_values, true)) {
423 } elseif ($number[1] != 's' && $number[1] != 'ms') {
427 $temp[$l] = $number[0] . $number[1];
430 return ((count($temp) > 1) ?
$temp[0] . '/' . $temp[1] : $temp[0]);
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
439 public function AnalyseCssNumber($string) {
440 // most simple checks first
441 if (strlen($string) == 0 ||
ctype_alpha($string{0})) {
445 $units = & $this->parser
->data
['csstidy']['units'];
446 $return = array(0, '');
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');
453 $return[0] = ltrim($return[0], '0');
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"
463 $actualPosition = strpos($string, $unitInString);
464 if ($expectUnitAt === $actualPosition) {
466 $string = substr($string, 0, - strlen($unit));
470 if (!is_numeric($string)) {
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
484 public function merge_selectors(&$array) {
486 foreach ($css as $key => $value) {
487 if (!isset($css[$key])) {
492 // Check if properties also exist in another selector
494 // PHP bug (?) without $css = $array; here
495 foreach ($css as $selector => $vali) {
496 if ($selector == $key) {
500 if ($css[$key] === $vali) {
508 foreach ($keys as $selector) {
509 unset($css[$selector]);
510 $newsel .= ',' . $selector;
512 $css[$newsel] = $value;
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
525 public function discard_invalid_selectors(&$array) {
526 $invalid = array('+' => true, '~' => true, ',' => true, '>' => true);
527 foreach ($array as $selector => $decls) {
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) {
535 // could also check $ss for internal structure,
536 // but that probably would be too slow
540 unset($array[$selector]);
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
551 * @see merge_4value_shorthands()
553 public function dissolve_4value_shorthands($property, $value, $shorthands = null) {
554 if (is_null($shorthands)) {
555 $shorthands = & $this->parser
->data
['csstidy']['shorthands'];
557 if (!is_array($shorthands[$property])) {
558 $return[$property] = $value;
563 if ($this->parser
->is_important($value)) {
564 $value = $this->parser
->gvw_important($value);
565 $important = '!important';
567 $values = explode(' ', $value);
571 if (count($values) == 4) {
572 for ($i = 0; $i < 4; $i++
) {
573 $return[$shorthands[$property][$i]] = $values[$i] . $important;
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;
585 for ($i = 0; $i < 4; $i++
) {
586 $return[$shorthands[$property][$i]] = $values[0] . $important;
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
601 * @use dissolve_4value_shorthands()
602 * @see merge_4value_radius_shorthands()
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;
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);
617 foreach ($r[0] as $p=>$v) {
619 if ($r[1][$p] !== $v) {
620 $return[$p] .= ' ' . $r[1][$p];
627 $return = $this->dissolve_4value_shorthands($property, $value, $shorthands);
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
639 public function explode_ws($sep, $string, $explode_in_parenthesis = false) {
647 for ($i = 0, $len = strlen($string); $i < $len; $i++
) {
650 if ($string{$i} == $sep && !$this->parser
->escaped($string, $i)) {
652 } elseif ($string{$i} === '"' ||
$string{$i} === '\'' ||
(!$explode_in_parenthesis && $string{$i} === '(') && !$this->parser
->escaped($string, $i)) {
654 $to = ($string{$i} === '(') ?
')' : $string{$i};
655 (isset($output[$num])) ?
$output[$num] .= $string{$i} : $output[$num] = $string{$i};
657 (isset($output[$num])) ?
$output[$num] .= $string{$i} : $output[$num] = $string{$i};
662 if ($string{$i} == $to && !$this->parser
->escaped($string, $i)) {
665 (isset($output[$num])) ?
$output[$num] .= $string{$i} : $output[$num] = $string{$i};
674 * Merges Shorthand properties again, the opposite of dissolve_4value_shorthands()
675 * @param array $array
676 * @param array|null $shorthands
679 * @see dissolve_4value_shorthands()
681 public function merge_4value_shorthands($array, $shorthands = null) {
683 if (is_null($shorthands)) {
684 $shorthands = & $this->parser
->data
['csstidy']['shorthands'];
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]])) {
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) . ' ';
699 $return[$key] .= $val . ' ';
701 unset($return[$value[$i]]);
703 $return[$key] = $this->shorthand(trim($return[$key] . $important));
710 * Merges Shorthand properties again, the opposite of dissolve_4value_shorthands()
711 * @param array $array
714 * @use merge_4value_shorthands()
715 * @see dissolve_4value_radius_shorthands()
717 public function merge_4value_radius_shorthands($array) {
719 $shorthands = & $this->parser
->data
['csstidy']['radius_shorthands'];
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) {
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);
732 $r[0] = $this->merge_4value_shorthands($a[0], $shorthands);
733 $r[1] = $this->merge_4value_shorthands($a[1], $shorthands);
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];
740 for ($i = 0; $i < 4; $i++
) {
741 unset($return[$value[$i]]);
749 * Dissolve background property
750 * @param string $str_value
754 * @todo full CSS 3 compliance
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);
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');
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);
770 if ($this->parser
->is_important($str_value)) {
771 $important = ' !important';
772 $str_value = $this->parser
->gvw_important($str_value);
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;
782 if (is_array($str_value[$i])) {
783 $str_value[$i] = $str_value[$i][0];
785 $str_value[$i] = $this->explode_ws(' ', trim($str_value[$i]));
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] . ',';
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];
805 $return['background-position'] .= ' '; else
806 $return['background-position'].= ',';
808 } elseif (!$have['color']) {
809 $return['background-color'] .= $str_value[$i][$j] . ',';
810 $have['color'] = true;
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;
820 $return[$bg_prop] = $default_value . $important;
826 * Merges all background properties
827 * @param array $input_css
830 * @see dissolve_short_bg()
831 * @todo full CSS 3 compliance
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']));
842 // if background properties is here and not empty, don't try anything
843 if (isset($input_css['background']) && $input_css['background'])
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])) {
853 $cur_value = $input_css[$bg_property];
854 // skip all optimisation if gradient() somewhere
855 if (stripos($cur_value, 'gradient(') !== false)
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')) {
866 if ($this->parser
->is_important($cur_value)) {
867 $important = ' !important';
868 $cur_value = $this->parser
->gvw_important($cur_value);
871 // Do not add default values
872 if ($cur_value === $default_value) {
876 $temp = $this->explode_ws(',', $cur_value);
878 if (isset($temp[$i])) {
879 if ($bg_property === 'background-size') {
880 $new_bg_value .= '(' . $temp[$i] . ') ';
882 $new_bg_value .= $temp[$i] . ' ';
887 $new_bg_value = trim($new_bg_value);
888 if ($i != $number_of_values - 1)
889 $new_bg_value .= ',';
892 // Delete all background-properties
893 foreach ($background_prop_default as $bg_property => $default_value) {
894 unset($input_css[$bg_property]);
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';
907 * Dissolve font property
908 * @param string $str_value
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');
919 $return = array('font-style' => null, 'font-variant' => null, 'font-weight' => null, 'font-size' => null, 'line-height' => null, 'font-family' => null);
921 if ($this->parser
->is_important($str_value)) {
922 $important = '!important';
923 $str_value = $this->parser
->gvw_important($str_value);
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
933 // Workaround with multiple font-family
934 $str_value = $this->explode_ws(',', trim($str_value));
936 $str_value[0] = $this->explode_ws(' ', trim($str_value[0]));
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];
954 $return['line-height'] = ''; // don't add 'normal' !
956 $have['size'] = true;
958 if (isset($return['font-family'])) {
959 $return['font-family'] .= ' ' . $str_value[0][$j];
962 $return['font-family'] = $str_value[0][$j];
966 // add quotes if we have several qords in font-family
967 if ($multiwords !== false) {
968 $return['font-family'] = '"' . $return['font-family'] . '"';
971 while (isset($str_value[$i])) {
972 $return['font-family'] .= ',' . trim($str_value[$i]);
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']);
983 foreach ($font_prop_default as $font_prop => $default_value) {
984 if ($return[$font_prop] !== null) {
985 $return[$font_prop] = $return[$font_prop] . $important;
988 $return[$font_prop] = $default_value . $important;
994 * Merges all fonts properties
995 * @param array $input_css
998 * @see dissolve_short_font()
1000 public function merge_font($input_css) {
1001 $font_prop_default = & $this->parser
->data
['csstidy']['font_prop_default'];
1002 $new_font_value = '';
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 . '"';
1018 $result_families[] = $family;
1020 $input_css['font-family'] = implode(',', $result_families);
1022 foreach ($font_prop_default as $font_property => $default_value) {
1024 // Skip if property does not exist
1025 if (!isset($input_css[$font_property])) {
1029 $cur_value = $input_css[$font_property];
1031 // Skip if default value is used
1032 if ($cur_value === $default_value) {
1036 // Remove !important
1037 if ($this->parser
->is_important($cur_value)) {
1038 $important = '!important';
1039 $cur_value = $this->parser
->gvw_important($cur_value);
1042 $new_font_value .= $cur_value;
1044 $new_font_value .= ( $font_property === 'font-size' &&
1045 isset($input_css['line-height'])) ?
'/' : ' ';
1048 $new_font_value = trim($new_font_value);
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]);
1056 // Add new font property
1057 if ($new_font_value !== '') {
1058 $input_css['font'] = $new_font_value . $important;
1066 * Reverse left vs right in a list of properties/values
1067 * @param array $array
1070 public function reverse_left_and_right($array) {
1073 // change left <-> right in properties name and values
1074 foreach ($array as $propertie => $value) {
1076 if (method_exists($this, $m = 'reverse_left_and_right_' . str_replace('-','_',trim($propertie)))) {
1077 $value = $this->$m($value);
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);
1090 $value = str_replace($v, "\x0" , $value);
1092 $value = $this->explode_ws("\x0", $value . ' ', true);
1093 $value = rtrim(implode($r, $value));
1094 $value = str_replace("\x0" , $v, $value);
1097 $return[$propertie] = $value;
1104 * Reversing 4 values shorthands properties
1105 * @param string $value
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];
1128 * Reversing 4 values radius shorthands properties
1129 * @param string $value
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]]) {
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]],
1144 $v = $this->merge_4value_radius_shorthands($r);
1145 if (isset($v[$property])) {
1146 return $v[$property];
1154 * Reversing margin shorthands
1155 * @param string $value
1158 public function reverse_left_and_right_margin($value) {
1159 return $this->reverse_left_and_right_4value_shorthands('margin', $value);
1163 * Reversing padding shorthands
1164 * @param string $value
1167 public function reverse_left_and_right_padding($value) {
1168 return $this->reverse_left_and_right_4value_shorthands('padding', $value);
1172 * Reversing border-color shorthands
1173 * @param string $value
1176 public function reverse_left_and_right_border_color($value) {
1177 return $this->reverse_left_and_right_4value_shorthands('border-color', $value);
1181 * Reversing border-style shorthands
1182 * @param string $value
1185 public function reverse_left_and_right_border_style($value) {
1186 return $this->reverse_left_and_right_4value_shorthands('border-style', $value);
1190 * Reversing border-width shorthands
1191 * @param string $value
1194 public function reverse_left_and_right_border_width($value) {
1195 return $this->reverse_left_and_right_4value_shorthands('border-width', $value);
1199 * Reversing border-radius shorthands
1200 * @param string $value
1203 public function reverse_left_and_right_border_radius($value) {
1204 return $this->reverse_left_and_right_4value_radius_shorthands('border-radius', $value);
1208 * Reversing border-radius shorthands
1209 * @param string $value
1212 public function reverse_left_and_right__moz_border_radius($value) {
1213 return $this->reverse_left_and_right_4value_radius_shorthands('border-radius', $value);
1217 * Reversing border-radius shorthands
1218 * @param string $value
1221 public function reverse_left_and_right__webkit_border_radius($value) {
1222 return $this->reverse_left_and_right_4value_radius_shorthands('border-radius', $value);
1227 * Reversing background shorthands
1228 * @param string $value
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']) {
1240 $values['background-position'] = $v;
1241 $x = $this->merge_bg($values);
1242 if (isset($x['background'])) {
1243 return $x['background'];
1252 * Reversing background position shorthands
1253 * @param string $value
1256 public function reverse_left_and_right_background_position_x($value) {
1257 return $this->reverse_left_and_right_background_position($value);
1261 * Reversing background position shorthands
1262 * @param string $value
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);
1273 return implode(',', $values);
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";
1286 if ($values[1] == 'top' or $values[1] == 'bottom') {
1287 return 'left ' . implode(' ', $values);
1290 $last = array_pop($values);
1291 return implode(' ', $values) . ' left ' . $last;