1 | <?php |
---|
2 | /** |
---|
3 | * CSSTidy - CSS Parser and Optimiser |
---|
4 | * |
---|
5 | * CSS Optimising Class |
---|
6 | * This class optimises CSS data generated by csstidy. |
---|
7 | * |
---|
8 | * This file is part of CSSTidy. |
---|
9 | * |
---|
10 | * CSSTidy is free software; you can redistribute it and/or modify |
---|
11 | * it under the terms of the GNU General Public License as published by |
---|
12 | * the Free Software Foundation; either version 2 of the License, or |
---|
13 | * (at your option) any later version. |
---|
14 | * |
---|
15 | * CSSTidy is distributed in the hope that it will be useful, |
---|
16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
18 | * GNU General Public License for more details. |
---|
19 | * |
---|
20 | * You should have received a copy of the GNU General Public License |
---|
21 | * along with CSSTidy; if not, write to the Free Software |
---|
22 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
---|
23 | * |
---|
24 | * @license http://opensource.org/licenses/gpl-license.php GNU Public License |
---|
25 | * @package csstidy |
---|
26 | * @author Florian Schmitz (floele at gmail dot com) 2005-2006 |
---|
27 | */ |
---|
28 | |
---|
29 | /** |
---|
30 | * CSS Optimising Class |
---|
31 | * |
---|
32 | * This class optimises CSS data generated by csstidy. |
---|
33 | * |
---|
34 | * @package csstidy |
---|
35 | * @author Florian Schmitz (floele at gmail dot com) 2005-2006 |
---|
36 | * @version 1.0 |
---|
37 | */ |
---|
38 | |
---|
39 | class csstidy_optimise |
---|
40 | { |
---|
41 | /** |
---|
42 | * Constructor |
---|
43 | * @param array $css contains the class csstidy |
---|
44 | * @access private |
---|
45 | * @version 1.0 |
---|
46 | */ |
---|
47 | function csstidy_optimise(&$css) |
---|
48 | { |
---|
49 | $this->parser =& $css; |
---|
50 | $this->css =& $css->css; |
---|
51 | $this->sub_value =& $css->sub_value; |
---|
52 | $this->at =& $css->at; |
---|
53 | $this->selector =& $css->selector; |
---|
54 | $this->property =& $css->property; |
---|
55 | $this->value =& $css->value; |
---|
56 | } |
---|
57 | |
---|
58 | /** |
---|
59 | * Optimises $css after parsing |
---|
60 | * @access public |
---|
61 | * @version 1.0 |
---|
62 | */ |
---|
63 | function postparse() |
---|
64 | { |
---|
65 | if ($this->parser->get_cfg('preserve_css')) { |
---|
66 | return; |
---|
67 | } |
---|
68 | |
---|
69 | if ($this->parser->get_cfg('merge_selectors') == 2) |
---|
70 | { |
---|
71 | foreach ($this->css as $medium => $value) |
---|
72 | { |
---|
73 | $this->merge_selectors($this->css[$medium]); |
---|
74 | } |
---|
75 | } |
---|
76 | |
---|
77 | if ($this->parser->get_cfg('optimise_shorthands') > 0) |
---|
78 | { |
---|
79 | foreach ($this->css as $medium => $value) |
---|
80 | { |
---|
81 | foreach ($value as $selector => $value1) |
---|
82 | { |
---|
83 | $this->css[$medium][$selector] = csstidy_optimise::merge_4value_shorthands($this->css[$medium][$selector]); |
---|
84 | |
---|
85 | if ($this->parser->get_cfg('optimise_shorthands') < 2) { |
---|
86 | continue; |
---|
87 | } |
---|
88 | |
---|
89 | $this->css[$medium][$selector] = csstidy_optimise::merge_bg($this->css[$medium][$selector]); |
---|
90 | if (empty($this->css[$medium][$selector])) { |
---|
91 | unset($this->css[$medium][$selector]); |
---|
92 | } |
---|
93 | } |
---|
94 | } |
---|
95 | } |
---|
96 | } |
---|
97 | |
---|
98 | /** |
---|
99 | * Optimises values |
---|
100 | * @access public |
---|
101 | * @version 1.0 |
---|
102 | */ |
---|
103 | function value() |
---|
104 | { |
---|
105 | $shorthands =& $GLOBALS['csstidy']['shorthands']; |
---|
106 | |
---|
107 | // optimise shorthand properties |
---|
108 | if(isset($shorthands[$this->property])) |
---|
109 | { |
---|
110 | $temp = csstidy_optimise::shorthand($this->value); // FIXME - move |
---|
111 | if($temp != $this->value) |
---|
112 | { |
---|
113 | $this->parser->log('Optimised shorthand notation ('.$this->property.'): Changed "'.$this->value.'" to "'.$temp.'"','Information'); |
---|
114 | } |
---|
115 | $this->value = $temp; |
---|
116 | } |
---|
117 | |
---|
118 | // Remove whitespace at ! important |
---|
119 | if($this->value != $this->compress_important($this->value)) |
---|
120 | { |
---|
121 | $this->parser->log('Optimised !important','Information'); |
---|
122 | } |
---|
123 | } |
---|
124 | |
---|
125 | /** |
---|
126 | * Optimises shorthands |
---|
127 | * @access public |
---|
128 | * @version 1.0 |
---|
129 | */ |
---|
130 | function shorthands() |
---|
131 | { |
---|
132 | $shorthands =& $GLOBALS['csstidy']['shorthands']; |
---|
133 | |
---|
134 | if(!$this->parser->get_cfg('optimise_shorthands') || $this->parser->get_cfg('preserve_css')) { |
---|
135 | return; |
---|
136 | } |
---|
137 | |
---|
138 | if($this->property == 'background' && $this->parser->get_cfg('optimise_shorthands') > 1) |
---|
139 | { |
---|
140 | unset($this->css[$this->at][$this->selector]['background']); |
---|
141 | $this->parser->merge_css_blocks($this->at,$this->selector,csstidy_optimise::dissolve_short_bg($this->value)); |
---|
142 | } |
---|
143 | if(isset($shorthands[$this->property])) |
---|
144 | { |
---|
145 | $this->parser->merge_css_blocks($this->at,$this->selector,csstidy_optimise::dissolve_4value_shorthands($this->property,$this->value)); |
---|
146 | if(is_array($shorthands[$this->property])) |
---|
147 | { |
---|
148 | unset($this->css[$this->at][$this->selector][$this->property]); |
---|
149 | } |
---|
150 | } |
---|
151 | } |
---|
152 | |
---|
153 | /** |
---|
154 | * Optimises a sub-value |
---|
155 | * @access public |
---|
156 | * @version 1.0 |
---|
157 | */ |
---|
158 | function subvalue() |
---|
159 | { |
---|
160 | $replace_colors =& $GLOBALS['csstidy']['replace_colors']; |
---|
161 | |
---|
162 | $this->sub_value = trim($this->sub_value); |
---|
163 | if($this->sub_value == '') // caution : '0' |
---|
164 | { |
---|
165 | return; |
---|
166 | } |
---|
167 | |
---|
168 | $important = ''; |
---|
169 | if(csstidy::is_important($this->sub_value)) |
---|
170 | { |
---|
171 | $important = '!important'; |
---|
172 | } |
---|
173 | $this->sub_value = csstidy::gvw_important($this->sub_value); |
---|
174 | |
---|
175 | // Compress font-weight |
---|
176 | if($this->property == 'font-weight' && $this->parser->get_cfg('compress_font-weight')) |
---|
177 | { |
---|
178 | if($this->sub_value == 'bold') |
---|
179 | { |
---|
180 | $this->sub_value = '700'; |
---|
181 | $this->parser->log('Optimised font-weight: Changed "bold" to "700"','Information'); |
---|
182 | } |
---|
183 | else if($this->sub_value == 'normal') |
---|
184 | { |
---|
185 | $this->sub_value = '400'; |
---|
186 | $this->parser->log('Optimised font-weight: Changed "normal" to "400"','Information'); |
---|
187 | } |
---|
188 | } |
---|
189 | |
---|
190 | $temp = $this->compress_numbers($this->sub_value); |
---|
191 | if($temp != $this->sub_value) |
---|
192 | { |
---|
193 | if(strlen($temp) > strlen($this->sub_value)) { |
---|
194 | $this->parser->log('Fixed invalid number: Changed "'.$this->sub_value.'" to "'.$temp.'"','Warning'); |
---|
195 | } else { |
---|
196 | $this->parser->log('Optimised number: Changed "'.$this->sub_value.'" to "'.$temp.'"','Information'); |
---|
197 | } |
---|
198 | $this->sub_value = $temp; |
---|
199 | } |
---|
200 | if($this->parser->get_cfg('compress_colors')) |
---|
201 | { |
---|
202 | $temp = $this->cut_color($this->sub_value); |
---|
203 | if($temp !== $this->sub_value) |
---|
204 | { |
---|
205 | if(isset($replace_colors[$this->sub_value])) { |
---|
206 | $this->parser->log('Fixed invalid color name: Changed "'.$this->sub_value.'" to "'.$temp.'"','Warning'); |
---|
207 | } else { |
---|
208 | $this->parser->log('Optimised color: Changed "'.$this->sub_value.'" to "'.$temp.'"','Information'); |
---|
209 | } |
---|
210 | $this->sub_value = $temp; |
---|
211 | } |
---|
212 | } |
---|
213 | $this->sub_value .= $important; |
---|
214 | } |
---|
215 | |
---|
216 | /** |
---|
217 | * Compresses shorthand values. Example: margin:1px 1px 1px 1px -> margin:1px |
---|
218 | * @param string $value |
---|
219 | * @access public |
---|
220 | * @return string |
---|
221 | * @version 1.0 |
---|
222 | */ |
---|
223 | function shorthand($value) |
---|
224 | { |
---|
225 | $important = ''; |
---|
226 | if(csstidy::is_important($value)) |
---|
227 | { |
---|
228 | $values = csstidy::gvw_important($value); |
---|
229 | $important = '!important'; |
---|
230 | } |
---|
231 | else $values = $value; |
---|
232 | |
---|
233 | $values = explode(' ',$values); |
---|
234 | switch(count($values)) |
---|
235 | { |
---|
236 | case 4: |
---|
237 | if($values[0] == $values[1] && $values[0] == $values[2] && $values[0] == $values[3]) |
---|
238 | { |
---|
239 | return $values[0].$important; |
---|
240 | } |
---|
241 | elseif($values[1] == $values[3] && $values[0] == $values[2]) |
---|
242 | { |
---|
243 | return $values[0].' '.$values[1].$important; |
---|
244 | } |
---|
245 | elseif($values[1] == $values[3]) |
---|
246 | { |
---|
247 | return $values[0].' '.$values[1].' '.$values[2].$important; |
---|
248 | } |
---|
249 | break; |
---|
250 | |
---|
251 | case 3: |
---|
252 | if($values[0] == $values[1] && $values[0] == $values[2]) |
---|
253 | { |
---|
254 | return $values[0].$important; |
---|
255 | } |
---|
256 | elseif($values[0] == $values[2]) |
---|
257 | { |
---|
258 | return $values[0].' '.$values[1].$important; |
---|
259 | } |
---|
260 | break; |
---|
261 | |
---|
262 | case 2: |
---|
263 | if($values[0] == $values[1]) |
---|
264 | { |
---|
265 | return $values[0].$important; |
---|
266 | } |
---|
267 | break; |
---|
268 | } |
---|
269 | |
---|
270 | return $value; |
---|
271 | } |
---|
272 | |
---|
273 | /** |
---|
274 | * Removes unnecessary whitespace in ! important |
---|
275 | * @param string $string |
---|
276 | * @return string |
---|
277 | * @access public |
---|
278 | * @version 1.1 |
---|
279 | */ |
---|
280 | function compress_important(&$string) |
---|
281 | { |
---|
282 | if(csstidy::is_important($string)) |
---|
283 | { |
---|
284 | $string = csstidy::gvw_important($string) . '!important'; |
---|
285 | } |
---|
286 | return $string; |
---|
287 | } |
---|
288 | |
---|
289 | /** |
---|
290 | * Color compression function. Converts all rgb() values to #-values and uses the short-form if possible. Also replaces 4 color names by #-values. |
---|
291 | * @param string $color |
---|
292 | * @return string |
---|
293 | * @version 1.1 |
---|
294 | */ |
---|
295 | function cut_color($color) |
---|
296 | { |
---|
297 | $replace_colors =& $GLOBALS['csstidy']['replace_colors']; |
---|
298 | |
---|
299 | // rgb(0,0,0) -> #000000 (or #000 in this case later) |
---|
300 | if(strtolower(substr($color,0,4)) == 'rgb(') |
---|
301 | { |
---|
302 | $color_tmp = substr($color,4,strlen($color)-5); |
---|
303 | $color_tmp = explode(',',$color_tmp); |
---|
304 | $color_tmp_count = count($color_tmp); |
---|
305 | for ( $i = 0; $i < $color_tmp_count; ++$i ) |
---|
306 | { |
---|
307 | $color_tmp[$i] = trim ($color_tmp[$i]); |
---|
308 | if(substr($color_tmp[$i],-1) == '%') |
---|
309 | { |
---|
310 | $color_tmp[$i] = round((255*$color_tmp[$i])/100); |
---|
311 | } |
---|
312 | if($color_tmp[$i]>255) $color_tmp[$i] = 255; |
---|
313 | } |
---|
314 | $color = '#'; |
---|
315 | for ($i = 0; $i < 3; ++$i ) |
---|
316 | { |
---|
317 | if($color_tmp[$i]<16) { |
---|
318 | $color .= '0' . dechex($color_tmp[$i]); |
---|
319 | } else { |
---|
320 | $color .= dechex($color_tmp[$i]); |
---|
321 | } |
---|
322 | } |
---|
323 | } |
---|
324 | |
---|
325 | // Fix bad color names |
---|
326 | if(isset($replace_colors[strtolower($color)])) |
---|
327 | { |
---|
328 | $color = $replace_colors[strtolower($color)]; |
---|
329 | } |
---|
330 | |
---|
331 | // #aabbcc -> #abc |
---|
332 | if(strlen($color) == 7) |
---|
333 | { |
---|
334 | $color_temp = strtolower($color); |
---|
335 | if($color_temp{0} == '#' && $color_temp{1} == $color_temp{2} && $color_temp{3} == $color_temp{4} && $color_temp{5} == $color_temp{6}) |
---|
336 | { |
---|
337 | $color = '#'.$color{1}.$color{3}.$color{5}; |
---|
338 | } |
---|
339 | } |
---|
340 | |
---|
341 | switch(strtolower($color)) |
---|
342 | { |
---|
343 | /* color name -> hex code */ |
---|
344 | case 'black': return '#000'; |
---|
345 | case 'fuchsia': return '#F0F'; |
---|
346 | case 'white': return '#FFF'; |
---|
347 | case 'yellow': return '#FF0'; |
---|
348 | |
---|
349 | /* hex code -> color name */ |
---|
350 | case '#800000': return 'maroon'; |
---|
351 | case '#ffa500': return 'orange'; |
---|
352 | case '#808000': return 'olive'; |
---|
353 | case '#800080': return 'purple'; |
---|
354 | case '#008000': return 'green'; |
---|
355 | case '#000080': return 'navy'; |
---|
356 | case '#008080': return 'teal'; |
---|
357 | case '#c0c0c0': return 'silver'; |
---|
358 | case '#808080': return 'gray'; |
---|
359 | case '#f00': return 'red'; |
---|
360 | } |
---|
361 | |
---|
362 | return $color; |
---|
363 | } |
---|
364 | |
---|
365 | /** |
---|
366 | * Compresses numbers (ie. 1.0 becomes 1 or 1.100 becomes 1.1 ) |
---|
367 | * @param string $subvalue |
---|
368 | * @return string |
---|
369 | * @version 1.2 |
---|
370 | */ |
---|
371 | function compress_numbers($subvalue) |
---|
372 | { |
---|
373 | $units =& $GLOBALS['csstidy']['units']; |
---|
374 | $unit_values =& $GLOBALS['csstidy']['unit_values']; |
---|
375 | $color_values =& $GLOBALS['csstidy']['color_values']; |
---|
376 | |
---|
377 | // for font:1em/1em sans-serif...; |
---|
378 | if($this->property == 'font') |
---|
379 | { |
---|
380 | $temp = explode('/',$subvalue); |
---|
381 | } |
---|
382 | else |
---|
383 | { |
---|
384 | $temp = array($subvalue); |
---|
385 | } |
---|
386 | $temp_count = count($temp); |
---|
387 | for ($l = 0; $l < $temp_count; ++$l) |
---|
388 | { |
---|
389 | // continue if no numeric value |
---|
390 | if (!(strlen($temp[$l]) > 0 && ( is_numeric($temp[$l]{0}) || $temp[$l]{0} == '+' || $temp[$l]{0} == '-' ) )) |
---|
391 | { |
---|
392 | continue; |
---|
393 | } |
---|
394 | |
---|
395 | // Fix bad colors |
---|
396 | if (in_array($this->property, $color_values)) |
---|
397 | { |
---|
398 | $temp[$l] = '#'.$temp[$l]; |
---|
399 | } |
---|
400 | |
---|
401 | if (floatval($temp[$l]) == 0) |
---|
402 | { |
---|
403 | $temp[$l] = '0'; |
---|
404 | } |
---|
405 | else |
---|
406 | { |
---|
407 | $unit_found = FALSE; |
---|
408 | for ($m = 0, $size_4 = count($units); $m < $size_4; ++$m) |
---|
409 | { |
---|
410 | if (strpos(strtolower($temp[$l]),$units[$m]) !== FALSE) |
---|
411 | { |
---|
412 | $temp[$l] = floatval($temp[$l]).$units[$m]; |
---|
413 | $unit_found = TRUE; |
---|
414 | break; |
---|
415 | } |
---|
416 | } |
---|
417 | if (!$unit_found && in_array($this->property,$unit_values,TRUE)) |
---|
418 | { |
---|
419 | $temp[$l] = floatval($temp[$l]).'px'; |
---|
420 | } |
---|
421 | else if (!$unit_found) |
---|
422 | { |
---|
423 | $temp[$l] = floatval($temp[$l]); |
---|
424 | } |
---|
425 | // Remove leading zero |
---|
426 | if (abs(floatval($temp[$l])) < 1) { |
---|
427 | if (floatval($temp[$l]) < 0) { |
---|
428 | $temp[$l] = '-' . substr($temp[$l], 2); |
---|
429 | } else { |
---|
430 | $temp[$l] = substr($temp[$l], 1); |
---|
431 | } |
---|
432 | } |
---|
433 | } |
---|
434 | } |
---|
435 | |
---|
436 | return ((count($temp) > 1) ? $temp[0].'/'.$temp[1] : $temp[0]); |
---|
437 | } |
---|
438 | |
---|
439 | /** |
---|
440 | * Merges selectors with same properties. Example: a{color:red} b{color:red} -> a,b{color:red} |
---|
441 | * Very basic and has at least one bug. Hopefully there is a replacement soon. |
---|
442 | * @param array $array |
---|
443 | * @return array |
---|
444 | * @access public |
---|
445 | * @version 1.2 |
---|
446 | */ |
---|
447 | function merge_selectors(&$array) |
---|
448 | { |
---|
449 | $css = $array; |
---|
450 | foreach($css as $key => $value) |
---|
451 | { |
---|
452 | if(!isset($css[$key])) |
---|
453 | { |
---|
454 | continue; |
---|
455 | } |
---|
456 | $newsel = ''; |
---|
457 | |
---|
458 | // Check if properties also exist in another selector |
---|
459 | $keys = array(); |
---|
460 | // PHP bug (?) without $css = $array; here |
---|
461 | foreach($css as $selector => $vali) |
---|
462 | { |
---|
463 | if($selector == $key) |
---|
464 | { |
---|
465 | continue; |
---|
466 | } |
---|
467 | |
---|
468 | if($css[$key] === $vali) |
---|
469 | { |
---|
470 | $keys[] = $selector; |
---|
471 | } |
---|
472 | } |
---|
473 | |
---|
474 | if(!empty($keys)) |
---|
475 | { |
---|
476 | $newsel = $key; |
---|
477 | unset($css[$key]); |
---|
478 | foreach($keys as $selector) |
---|
479 | { |
---|
480 | unset($css[$selector]); |
---|
481 | $newsel .= ','.$selector; |
---|
482 | } |
---|
483 | $css[$newsel] = $value; |
---|
484 | } |
---|
485 | } |
---|
486 | $array = $css; |
---|
487 | } |
---|
488 | |
---|
489 | /** |
---|
490 | * Dissolves properties like padding:10px 10px 10px to padding-top:10px;padding-bottom:10px;... |
---|
491 | * @param string $property |
---|
492 | * @param string $value |
---|
493 | * @return array |
---|
494 | * @version 1.0 |
---|
495 | * @see merge_4value_shorthands() |
---|
496 | */ |
---|
497 | function dissolve_4value_shorthands($property,$value) |
---|
498 | { |
---|
499 | $shorthands =& $GLOBALS['csstidy']['shorthands']; |
---|
500 | if(!is_array($shorthands[$property])) |
---|
501 | { |
---|
502 | $return[$property] = $value; |
---|
503 | return $return; |
---|
504 | } |
---|
505 | |
---|
506 | $important = ''; |
---|
507 | if(csstidy::is_important($value)) |
---|
508 | { |
---|
509 | $value = csstidy::gvw_important($value); |
---|
510 | $important = '!important'; |
---|
511 | } |
---|
512 | $values = explode(' ',$value); |
---|
513 | |
---|
514 | |
---|
515 | $return = array(); |
---|
516 | if(count($values) == 4) |
---|
517 | { |
---|
518 | for($i=0;$i<4;++$i) |
---|
519 | { |
---|
520 | $return[$shorthands[$property][$i]] = $values[$i].$important; |
---|
521 | } |
---|
522 | } |
---|
523 | elseif(count($values) == 3) |
---|
524 | { |
---|
525 | $return[$shorthands[$property][0]] = $values[0].$important; |
---|
526 | $return[$shorthands[$property][1]] = $values[1].$important; |
---|
527 | $return[$shorthands[$property][3]] = $values[1].$important; |
---|
528 | $return[$shorthands[$property][2]] = $values[2].$important; |
---|
529 | } |
---|
530 | elseif(count($values) == 2) |
---|
531 | { |
---|
532 | for($i=0;$i<4;++$i) |
---|
533 | { |
---|
534 | $return[$shorthands[$property][$i]] = (($i % 2 != 0)) ? $values[1].$important : $values[0].$important; |
---|
535 | } |
---|
536 | } |
---|
537 | else |
---|
538 | { |
---|
539 | for($i=0;$i<4;++$i) |
---|
540 | { |
---|
541 | $return[$shorthands[$property][$i]] = $values[0].$important; |
---|
542 | } |
---|
543 | } |
---|
544 | |
---|
545 | return $return; |
---|
546 | } |
---|
547 | |
---|
548 | /** |
---|
549 | * Explodes a string as explode() does, however, not if $sep is escaped or within a string. |
---|
550 | * @param string $sep seperator |
---|
551 | * @param string $string |
---|
552 | * @return array |
---|
553 | * @version 1.0 |
---|
554 | */ |
---|
555 | function explode_ws($sep,$string) |
---|
556 | { |
---|
557 | $status = 'st'; |
---|
558 | $to = ''; |
---|
559 | |
---|
560 | $output = array(); |
---|
561 | $num = 0; |
---|
562 | for($i = 0, $len = strlen($string);$i < $len; ++$i) |
---|
563 | { |
---|
564 | switch($status) |
---|
565 | { |
---|
566 | case 'st': |
---|
567 | if($string{$i} == $sep && !csstidy::escaped($string,$i)) |
---|
568 | { |
---|
569 | ++$num; |
---|
570 | } |
---|
571 | elseif($string{$i} == '"' || $string{$i} == '\'' || $string{$i} == '(' && !csstidy::escaped($string,$i)) |
---|
572 | { |
---|
573 | $status = 'str'; |
---|
574 | $to = ($string{$i} == '(') ? ')' : $string{$i}; |
---|
575 | (isset($output[$num])) ? $output[$num] .= $string{$i} : $output[$num] = $string{$i}; |
---|
576 | } |
---|
577 | else |
---|
578 | { |
---|
579 | (isset($output[$num])) ? $output[$num] .= $string{$i} : $output[$num] = $string{$i}; |
---|
580 | } |
---|
581 | break; |
---|
582 | |
---|
583 | case 'str': |
---|
584 | if($string{$i} == $to && !csstidy::escaped($string,$i)) |
---|
585 | { |
---|
586 | $status = 'st'; |
---|
587 | } |
---|
588 | (isset($output[$num])) ? $output[$num] .= $string{$i} : $output[$num] = $string{$i}; |
---|
589 | break; |
---|
590 | } |
---|
591 | } |
---|
592 | |
---|
593 | if(isset($output[0])) |
---|
594 | { |
---|
595 | return $output; |
---|
596 | } |
---|
597 | else |
---|
598 | { |
---|
599 | return array($output); |
---|
600 | } |
---|
601 | } |
---|
602 | |
---|
603 | /** |
---|
604 | * Merges Shorthand properties again, the opposite of dissolve_4value_shorthands() |
---|
605 | * @param array $array |
---|
606 | * @return array |
---|
607 | * @version 1.2 |
---|
608 | * @see dissolve_4value_shorthands() |
---|
609 | */ |
---|
610 | function merge_4value_shorthands($array) |
---|
611 | { |
---|
612 | $return = $array; |
---|
613 | $shorthands =& $GLOBALS['csstidy']['shorthands']; |
---|
614 | |
---|
615 | foreach($shorthands as $key => $value) |
---|
616 | { |
---|
617 | if(isset($array[$value[0]]) && isset($array[$value[1]]) |
---|
618 | && isset($array[$value[2]]) && isset($array[$value[3]]) && $value !== 0) |
---|
619 | { |
---|
620 | $return[$key] = ''; |
---|
621 | |
---|
622 | $important = ''; |
---|
623 | for($i = 0; $i < 4; ++$i) |
---|
624 | { |
---|
625 | $val = $array[$value[$i]]; |
---|
626 | if(csstidy::is_important($val)) |
---|
627 | { |
---|
628 | $important = '!important'; |
---|
629 | $return[$key] .= csstidy::gvw_important($val).' '; |
---|
630 | } |
---|
631 | else |
---|
632 | { |
---|
633 | $return[$key] .= $val.' '; |
---|
634 | } |
---|
635 | unset($return[$value[$i]]); |
---|
636 | } |
---|
637 | $return[$key] = csstidy_optimise::shorthand(trim($return[$key].$important)); |
---|
638 | } |
---|
639 | } |
---|
640 | return $return; |
---|
641 | } |
---|
642 | |
---|
643 | /** |
---|
644 | * Dissolve background property |
---|
645 | * @param string $str_value |
---|
646 | * @return array |
---|
647 | * @version 1.0 |
---|
648 | * @see merge_bg() |
---|
649 | * @todo full CSS 3 compliance |
---|
650 | */ |
---|
651 | function dissolve_short_bg($str_value) |
---|
652 | { |
---|
653 | $background_prop_default =& $GLOBALS['csstidy']['background_prop_default']; |
---|
654 | $repeat = array('repeat','repeat-x','repeat-y','no-repeat','space'); |
---|
655 | $attachment = array('scroll','fixed','local'); |
---|
656 | $clip = array('border','padding'); |
---|
657 | $origin = array('border','padding','content'); |
---|
658 | $pos = array('top','center','bottom','left','right'); |
---|
659 | $important = ''; |
---|
660 | $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); |
---|
661 | |
---|
662 | if(csstidy::is_important($str_value)) |
---|
663 | { |
---|
664 | $important = ' !important'; |
---|
665 | $str_value = csstidy::gvw_important($str_value); |
---|
666 | } |
---|
667 | |
---|
668 | $str_value = csstidy_optimise::explode_ws(',',$str_value); |
---|
669 | $str_value_count = count($str_value); |
---|
670 | for($i = 0; $i < $str_value_count; ++$i) |
---|
671 | { |
---|
672 | $have['clip'] = FALSE; $have['pos'] = FALSE; |
---|
673 | $have['color'] = FALSE; $have['bg'] = FALSE; |
---|
674 | |
---|
675 | $str_value[$i] = csstidy_optimise::explode_ws(' ',trim($str_value[$i])); |
---|
676 | |
---|
677 | $str_value_count = count($str_value[$i]); |
---|
678 | for($j = 0; $j < $str_value_count; ++$j) |
---|
679 | { |
---|
680 | if($have['bg'] === FALSE && (substr($str_value[$i][$j],0,4) == 'url(' || $str_value[$i][$j] === 'none')) |
---|
681 | { |
---|
682 | $return['background-image'] .= $str_value[$i][$j].','; |
---|
683 | $have['bg'] = TRUE; |
---|
684 | } |
---|
685 | elseif(in_array($str_value[$i][$j],$repeat,TRUE)) |
---|
686 | { |
---|
687 | $return['background-repeat'] .= $str_value[$i][$j].','; |
---|
688 | } |
---|
689 | elseif(in_array($str_value[$i][$j],$attachment,TRUE)) |
---|
690 | { |
---|
691 | $return['background-attachment'] .= $str_value[$i][$j].','; |
---|
692 | } |
---|
693 | elseif(in_array($str_value[$i][$j],$clip,TRUE) && !$have['clip']) |
---|
694 | { |
---|
695 | $return['background-clip'] .= $str_value[$i][$j].','; |
---|
696 | $have['clip'] = TRUE; |
---|
697 | } |
---|
698 | elseif(in_array($str_value[$i][$j],$origin,TRUE)) |
---|
699 | { |
---|
700 | $return['background-origin'] .= $str_value[$i][$j].','; |
---|
701 | } |
---|
702 | elseif($str_value[$i][$j]{0} == '(') |
---|
703 | { |
---|
704 | $return['background-size'] .= substr($str_value[$i][$j],1,-1).','; |
---|
705 | } |
---|
706 | elseif(in_array($str_value[$i][$j],$pos,TRUE) || is_numeric($str_value[$i][$j]{0}) || $str_value[$i][$j]{0} === NULL) |
---|
707 | { |
---|
708 | $return['background-position'] .= $str_value[$i][$j]; |
---|
709 | if(!$have['pos']) $return['background-position'] .= ' '; else $return['background-position'].= ','; |
---|
710 | $have['pos'] = TRUE; |
---|
711 | } |
---|
712 | elseif(!$have['color']) |
---|
713 | { |
---|
714 | $return['background-color'] .= $str_value[$i][$j].','; |
---|
715 | $have['color'] = TRUE; |
---|
716 | } |
---|
717 | } |
---|
718 | } |
---|
719 | |
---|
720 | foreach($background_prop_default as $bg_prop => $default_value) |
---|
721 | { |
---|
722 | if($return[$bg_prop] !== NULL) |
---|
723 | { |
---|
724 | $return[$bg_prop] = substr($return[$bg_prop],0,-1).$important; |
---|
725 | } |
---|
726 | else $return[$bg_prop] = $default_value.$important; |
---|
727 | } |
---|
728 | return $return; |
---|
729 | } |
---|
730 | |
---|
731 | /** |
---|
732 | * Merges all background properties |
---|
733 | * @param array $input_css |
---|
734 | * @return array |
---|
735 | * @version 1.0 |
---|
736 | * @see dissolve_short_bg() |
---|
737 | * @todo full CSS 3 compliance |
---|
738 | */ |
---|
739 | function merge_bg($input_css) |
---|
740 | { |
---|
741 | $background_prop_default =& $GLOBALS['csstidy']['background_prop_default']; |
---|
742 | // Max number of background images. CSS3 not yet fully implemented |
---|
743 | $number_of_values = @max(count(csstidy_optimise::explode_ws(',',$input_css['background-image'])),count(csstidy_optimise::explode_ws(',',$input_css['background-color'])),1); |
---|
744 | // Array with background images to check if BG image exists |
---|
745 | $bg_img_array = @csstidy_optimise::explode_ws(',',csstidy::gvw_important($input_css['background-image'])); |
---|
746 | $new_bg_value = ''; |
---|
747 | $important = ''; |
---|
748 | |
---|
749 | for($i = 0; $i < $number_of_values; ++$i) |
---|
750 | { |
---|
751 | foreach($background_prop_default as $bg_property => $default_value) |
---|
752 | { |
---|
753 | // Skip if property does not exist |
---|
754 | if(!isset($input_css[$bg_property])) |
---|
755 | { |
---|
756 | continue; |
---|
757 | } |
---|
758 | |
---|
759 | $cur_value = $input_css[$bg_property]; |
---|
760 | |
---|
761 | // Skip some properties if there is no background image |
---|
762 | if((!isset($bg_img_array[$i]) || $bg_img_array[$i] === 'none') |
---|
763 | && ($bg_property === 'background-size' || $bg_property === 'background-position' |
---|
764 | || $bg_property === 'background-attachment' || $bg_property === 'background-repeat')) |
---|
765 | { |
---|
766 | continue; |
---|
767 | } |
---|
768 | |
---|
769 | // Remove !important |
---|
770 | if(csstidy::is_important($cur_value)) |
---|
771 | { |
---|
772 | $important = ' !important'; |
---|
773 | $cur_value = csstidy::gvw_important($cur_value); |
---|
774 | } |
---|
775 | |
---|
776 | // Do not add default values |
---|
777 | if($cur_value === $default_value) |
---|
778 | { |
---|
779 | continue; |
---|
780 | } |
---|
781 | |
---|
782 | $temp = csstidy_optimise::explode_ws(',',$cur_value); |
---|
783 | |
---|
784 | if(isset($temp[$i])) |
---|
785 | { |
---|
786 | if($bg_property == 'background-size') |
---|
787 | { |
---|
788 | $new_bg_value .= '('.$temp[$i].') '; |
---|
789 | } |
---|
790 | else |
---|
791 | { |
---|
792 | $new_bg_value .= $temp[$i].' '; |
---|
793 | } |
---|
794 | } |
---|
795 | } |
---|
796 | |
---|
797 | $new_bg_value = trim($new_bg_value); |
---|
798 | if($i != $number_of_values-1) $new_bg_value .= ','; |
---|
799 | } |
---|
800 | |
---|
801 | // Delete all background-properties |
---|
802 | foreach($background_prop_default as $bg_property => $default_value) |
---|
803 | { |
---|
804 | unset($input_css[$bg_property]); |
---|
805 | } |
---|
806 | |
---|
807 | // Add new background property |
---|
808 | if($new_bg_value !== '') $input_css['background'] = $new_bg_value.$important; |
---|
809 | |
---|
810 | return $input_css; |
---|
811 | } |
---|
812 | } |
---|
813 | ?> |
---|