d42341ce78efd5a68bd27431f36685fecf149b29
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() {
73 if ($this->parser
->get_cfg('preserve_css')) {
77 if ($this->parser
->get_cfg('merge_selectors') === 2) {
78 foreach ($this->css
as $medium => $value) {
79 $this->merge_selectors($this->css
[$medium]);
83 if ($this->parser
->get_cfg('discard_invalid_selectors')) {
84 foreach ($this->css
as $medium => $value) {
85 $this->discard_invalid_selectors($this->css
[$medium]);
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]);
94 if ($this->parser
->get_cfg('optimise_shorthands') < 2) {
98 $this->css
[$medium][$selector] = $this->merge_font($this->css
[$medium][$selector]);
100 if ($this->parser
->get_cfg('optimise_shorthands') < 3) {
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]);
118 public function value() {
119 $shorthands = & $this->parser
->data
['csstidy']['shorthands'];
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');
127 $this->value
= $temp;
130 // Remove whitespace at ! important
131 if ($this->value
!= $this->compress_important($this->value
)) {
132 $this->parser
->log('Optimised !important', 'Information');
137 * Optimises shorthands
141 public function shorthands() {
142 $shorthands = & $this->parser
->data
['csstidy']['shorthands'];
144 if (!$this->parser
->get_cfg('optimise_shorthands') ||
$this->parser
->get_cfg('preserve_css')) {
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
));
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
));
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
] = '';
165 * Optimises a sub-value
169 public function subvalue() {
170 $replace_colors = & $this->parser
->data
['csstidy']['replace_colors'];
172 $this->sub_value
= trim($this->sub_value
);
173 if ($this->sub_value
== '') { // caution : '0'
178 if ($this->parser
->is_important($this->sub_value
)) {
179 $important = '!important';
181 $this->sub_value
= $this->parser
->gvw_important($this->sub_value
);
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');
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');
199 $this->parser
->log('Optimised number: Changed "' . $this->sub_value
. '" to "' . $temp . '"', 'Information');
201 $this->sub_value
= $temp;
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');
209 $this->parser
->log('Optimised color: Changed "' . $this->sub_value
. '" to "' . $temp . '"', 'Information');
211 $this->sub_value
= $temp;
214 $this->sub_value
.= $important;
218 * Compresses shorthand values. Example: margin:1px 1px 1px 1px -> margin:1px
219 * @param string $value
224 public function shorthand($value) {
226 if ($this->parser
->is_important($value)) {
227 $values = $this->parser
->gvw_important($value);
228 $important = '!important';
233 $values = explode(' ', $values);
234 switch (count($values)) {
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;
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;
254 if ($values[0] == $values[1]) {
255 return $values[0] . $important;
264 * Removes unnecessary whitespace in ! important
265 * @param string $string
270 public function compress_important(&$string) {
271 if ($this->parser
->is_important($string)) {
272 $string = $this->parser
->gvw_important($string) . '!important';
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
283 public function cut_color($color) {
284 $replace_colors = & $this->parser
->data
['csstidy']['replace_colors'];
286 // if it's a string, don't touch !
287 if (strncmp($color, "'", 1) == 0 ||
strncmp($color, '"', 1) == 0)
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)
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);
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);
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);
319 if ($color_tmp[$i] > 255)
320 $color_tmp[$i] = 255;
323 for ($i = 0; $i < 3; $i++
) {
324 if ($color_tmp[$i] < 16) {
325 $color .= '0' . dechex($color_tmp[$i]);
327 $color .= dechex($color_tmp[$i]);
332 // Fix bad color names
333 if (isset($replace_colors[strtolower($color)])) {
334 $color = $replace_colors[strtolower($color)];
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};
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';
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';
369 * Compresses numbers (ie. 1.0 becomes 1 or 1.100 becomes 1.1 )
370 * @param string $subvalue
374 public function compress_numbers($subvalue) {
375 $unit_values = & $this->parser
->data
['csstidy']['unit_values'];
376 $color_values = & $this->parser
->data
['csstidy']['color_values'];
378 // for font:1em/1em sans-serif...;
379 if ($this->property
=== 'font') {
380 $temp = explode('/', $subvalue);
382 $temp = array($subvalue);
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) {
393 if (in_array($this->property
, $color_values)) {
394 $temp[$l] = '#' . $temp[$l];
398 if (abs($number[0]) > 0) {
399 if ($number[1] == '' && in_array($this->property
, $unit_values, true)) {
406 $temp[$l] = $number[0] . $number[1];
409 return ((count($temp) > 1) ?
$temp[0] . '/' . $temp[1] : $temp[0]);
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
418 public function AnalyseCssNumber($string) {
419 // most simple checks first
420 if (strlen($string) == 0 ||
ctype_alpha($string{0})) {
424 $units = & $this->parser
->data
['csstidy']['units'];
425 $return = array(0, '');
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');
432 $return[0] = ltrim($return[0], '0');
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"
442 $actualPosition = strpos($string, $unitInString);
443 if ($expectUnitAt === $actualPosition) {
445 $string = substr($string, 0, - strlen($unit));
449 if (!is_numeric($string)) {
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
463 public function merge_selectors(&$array) {
465 foreach ($css as $key => $value) {
466 if (!isset($css[$key])) {
471 // Check if properties also exist in another selector
473 // PHP bug (?) without $css = $array; here
474 foreach ($css as $selector => $vali) {
475 if ($selector == $key) {
479 if ($css[$key] === $vali) {
487 foreach ($keys as $selector) {
488 unset($css[$selector]);
489 $newsel .= ',' . $selector;
491 $css[$newsel] = $value;
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
504 public function discard_invalid_selectors(&$array) {
505 $invalid = array('+' => true, '~' => true, ',' => true, '>' => true);
506 foreach ($array as $selector => $decls) {
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) {
514 // could also check $ss for internal structure,
515 // but that probably would be too slow
519 unset($array[$selector]);
524 * Dissolves properties like padding:10px 10px 10px to padding-top:10px;padding-bottom:10px;...
525 * @param string $property
526 * @param string $value
529 * @see merge_4value_shorthands()
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;
539 if ($this->parser
->is_important($value)) {
540 $value = $this->parser
->gvw_important($value);
541 $important = '!important';
543 $values = explode(' ', $value);
547 if (count($values) == 4) {
548 for ($i = 0; $i < 4; $i++
) {
549 $return[$shorthands[$property][$i]] = $values[$i] . $important;
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;
561 for ($i = 0; $i < 4; $i++
) {
562 $return[$shorthands[$property][$i]] = $values[0] . $important;
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
576 public function explode_ws($sep, $string) {
582 for ($i = 0, $len = strlen($string); $i < $len; $i++
) {
585 if ($string{$i} == $sep && !$this->parser
->escaped($string, $i)) {
587 } elseif ($string{$i} === '"' ||
$string{$i} === '\'' ||
$string{$i} === '(' && !$this->parser
->escaped($string, $i)) {
589 $to = ($string{$i} === '(') ?
')' : $string{$i};
590 (isset($output[$num])) ?
$output[$num] .= $string{$i} : $output[$num] = $string{$i};
592 (isset($output[$num])) ?
$output[$num] .= $string{$i} : $output[$num] = $string{$i};
597 if ($string{$i} == $to && !$this->parser
->escaped($string, $i)) {
600 (isset($output[$num])) ?
$output[$num] .= $string{$i} : $output[$num] = $string{$i};
605 if (isset($output[0])) {
608 return array($output);
613 * Merges Shorthand properties again, the opposite of dissolve_4value_shorthands()
614 * @param array $array
617 * @see dissolve_4value_shorthands()
619 public function merge_4value_shorthands($array) {
621 $shorthands = & $this->parser
->data
['csstidy']['shorthands'];
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) {
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) . ' ';
635 $return[$key] .= $val . ' ';
637 unset($return[$value[$i]]);
639 $return[$key] = $this->shorthand(trim($return[$key] . $important));
646 * Dissolve background property
647 * @param string $str_value
651 * @todo full CSS 3 compliance
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);
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');
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);
667 if ($this->parser
->is_important($str_value)) {
668 $important = ' !important';
669 $str_value = $this->parser
->gvw_important($str_value);
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;
679 if (is_array($str_value[$i])) {
680 $str_value[$i] = $str_value[$i][0];
682 $str_value[$i] = $this->explode_ws(' ', trim($str_value[$i]));
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] . ',';
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];
702 $return['background-position'] .= ' '; else
703 $return['background-position'].= ',';
705 } elseif (!$have['color']) {
706 $return['background-color'] .= $str_value[$i][$j] . ',';
707 $have['color'] = true;
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;
717 $return[$bg_prop] = $default_value . $important;
723 * Merges all background properties
724 * @param array $input_css
727 * @see dissolve_short_bg()
728 * @todo full CSS 3 compliance
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']));
739 // if background properties is here and not empty, don't try anything
740 if (isset($input_css['background']) && $input_css['background'])
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])) {
750 $cur_value = $input_css[$bg_property];
751 // skip all optimisation if gradient() somewhere
752 if (stripos($cur_value, 'gradient(') !== false)
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')) {
763 if ($this->parser
->is_important($cur_value)) {
764 $important = ' !important';
765 $cur_value = $this->parser
->gvw_important($cur_value);
768 // Do not add default values
769 if ($cur_value === $default_value) {
773 $temp = $this->explode_ws(',', $cur_value);
775 if (isset($temp[$i])) {
776 if ($bg_property === 'background-size') {
777 $new_bg_value .= '(' . $temp[$i] . ') ';
779 $new_bg_value .= $temp[$i] . ' ';
784 $new_bg_value = trim($new_bg_value);
785 if ($i != $number_of_values - 1)
786 $new_bg_value .= ',';
789 // Delete all background-properties
790 foreach ($background_prop_default as $bg_property => $default_value) {
791 unset($input_css[$bg_property]);
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';
804 * Dissolve font property
805 * @param string $str_value
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');
816 $return = array('font-style' => null, 'font-variant' => null, 'font-weight' => null, 'font-size' => null, 'line-height' => null, 'font-family' => null);
818 if ($this->parser
->is_important($str_value)) {
819 $important = '!important';
820 $str_value = $this->parser
->gvw_important($str_value);
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
830 // Workaround with multiple font-family
831 $str_value = $this->explode_ws(',', trim($str_value));
833 $str_value[0] = $this->explode_ws(' ', trim($str_value[0]));
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];
851 $return['line-height'] = ''; // don't add 'normal' !
853 $have['size'] = true;
855 if (isset($return['font-family'])) {
856 $return['font-family'] .= ' ' . $str_value[0][$j];
859 $return['font-family'] = $str_value[0][$j];
863 // add quotes if we have several qords in font-family
864 if ($multiwords !== false) {
865 $return['font-family'] = '"' . $return['font-family'] . '"';
868 while (isset($str_value[$i])) {
869 $return['font-family'] .= ',' . trim($str_value[$i]);
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']);
880 foreach ($font_prop_default as $font_prop => $default_value) {
881 if ($return[$font_prop] !== null) {
882 $return[$font_prop] = $return[$font_prop] . $important;
885 $return[$font_prop] = $default_value . $important;
891 * Merges all fonts properties
892 * @param array $input_css
895 * @see dissolve_short_font()
897 public function merge_font($input_css) {
898 $font_prop_default = & $this->parser
->data
['csstidy']['font_prop_default'];
899 $new_font_value = '';
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 . '"';
915 $result_families[] = $family;
917 $input_css['font-family'] = implode(',', $result_families);
919 foreach ($font_prop_default as $font_property => $default_value) {
921 // Skip if property does not exist
922 if (!isset($input_css[$font_property])) {
926 $cur_value = $input_css[$font_property];
928 // Skip if default value is used
929 if ($cur_value === $default_value) {
934 if ($this->parser
->is_important($cur_value)) {
935 $important = '!important';
936 $cur_value = $this->parser
->gvw_important($cur_value);
939 $new_font_value .= $cur_value;
941 $new_font_value .= ( $font_property === 'font-size' &&
942 isset($input_css['line-height'])) ?
'/' : ' ';
945 $new_font_value = trim($new_font_value);
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]);
953 // Add new font property
954 if ($new_font_value !== '') {
955 $input_css['font'] = $new_font_value . $important;