[3052] | 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 | for ( $i = 0; $i < count($color_tmp); $i++ ) |
---|
| 305 | { |
---|
| 306 | $color_tmp[$i] = trim ($color_tmp[$i]); |
---|
| 307 | if(substr($color_tmp[$i],-1) == '%') |
---|
| 308 | { |
---|
| 309 | $color_tmp[$i] = round((255*$color_tmp[$i])/100); |
---|
| 310 | } |
---|
| 311 | if($color_tmp[$i]>255) $color_tmp[$i] = 255; |
---|
| 312 | } |
---|
| 313 | $color = '#'; |
---|
| 314 | for ($i = 0; $i < 3; $i++ ) |
---|
| 315 | { |
---|
| 316 | if($color_tmp[$i]<16) { |
---|
| 317 | $color .= '0' . dechex($color_tmp[$i]); |
---|
| 318 | } else { |
---|
| 319 | $color .= dechex($color_tmp[$i]); |
---|
| 320 | } |
---|
| 321 | } |
---|
| 322 | } |
---|
| 323 | |
---|
| 324 | // Fix bad color names |
---|
| 325 | if(isset($replace_colors[strtolower($color)])) |
---|
| 326 | { |
---|
| 327 | $color = $replace_colors[strtolower($color)]; |
---|
| 328 | } |
---|
| 329 | |
---|
| 330 | // #aabbcc -> #abc |
---|
| 331 | if(strlen($color) == 7) |
---|
| 332 | { |
---|
| 333 | $color_temp = strtolower($color); |
---|
| 334 | if($color_temp{0} == '#' && $color_temp{1} == $color_temp{2} && $color_temp{3} == $color_temp{4} && $color_temp{5} == $color_temp{6}) |
---|
| 335 | { |
---|
| 336 | $color = '#'.$color{1}.$color{3}.$color{5}; |
---|
| 337 | } |
---|
| 338 | } |
---|
| 339 | |
---|
| 340 | switch(strtolower($color)) |
---|
| 341 | { |
---|
| 342 | /* color name -> hex code */ |
---|
| 343 | case 'black': return '#000'; |
---|
| 344 | case 'fuchsia': return '#F0F'; |
---|
| 345 | case 'white': return '#FFF'; |
---|
| 346 | case 'yellow': return '#FF0'; |
---|
| 347 | |
---|
| 348 | /* hex code -> color name */ |
---|
| 349 | case '#800000': return 'maroon'; |
---|
| 350 | case '#ffa500': return 'orange'; |
---|
| 351 | case '#808000': return 'olive'; |
---|
| 352 | case '#800080': return 'purple'; |
---|
| 353 | case '#008000': return 'green'; |
---|
| 354 | case '#000080': return 'navy'; |
---|
| 355 | case '#008080': return 'teal'; |
---|
| 356 | case '#c0c0c0': return 'silver'; |
---|
| 357 | case '#808080': return 'gray'; |
---|
| 358 | case '#f00': return 'red'; |
---|
| 359 | } |
---|
| 360 | |
---|
| 361 | return $color; |
---|
| 362 | } |
---|
| 363 | |
---|
| 364 | /** |
---|
| 365 | * Compresses numbers (ie. 1.0 becomes 1 or 1.100 becomes 1.1 ) |
---|
| 366 | * @param string $subvalue |
---|
| 367 | * @return string |
---|
| 368 | * @version 1.2 |
---|
| 369 | */ |
---|
| 370 | function compress_numbers($subvalue) |
---|
| 371 | { |
---|
| 372 | $units =& $GLOBALS['csstidy']['units']; |
---|
| 373 | $unit_values =& $GLOBALS['csstidy']['unit_values']; |
---|
| 374 | $color_values =& $GLOBALS['csstidy']['color_values']; |
---|
| 375 | |
---|
| 376 | // for font:1em/1em sans-serif...; |
---|
| 377 | if($this->property == 'font') |
---|
| 378 | { |
---|
| 379 | $temp = explode('/',$subvalue); |
---|
| 380 | } |
---|
| 381 | else |
---|
| 382 | { |
---|
| 383 | $temp = array($subvalue); |
---|
| 384 | } |
---|
| 385 | for ($l = 0; $l < count($temp); $l++) |
---|
| 386 | { |
---|
| 387 | // continue if no numeric value |
---|
| 388 | if (!(strlen($temp[$l]) > 0 && ( is_numeric($temp[$l]{0}) || $temp[$l]{0} == '+' || $temp[$l]{0} == '-' ) )) |
---|
| 389 | { |
---|
| 390 | continue; |
---|
| 391 | } |
---|
| 392 | |
---|
| 393 | // Fix bad colors |
---|
| 394 | if (in_array($this->property, $color_values)) |
---|
| 395 | { |
---|
| 396 | $temp[$l] = '#'.$temp[$l]; |
---|
| 397 | } |
---|
| 398 | |
---|
| 399 | if (floatval($temp[$l]) == 0) |
---|
| 400 | { |
---|
| 401 | $temp[$l] = '0'; |
---|
| 402 | } |
---|
| 403 | else |
---|
| 404 | { |
---|
| 405 | $unit_found = FALSE; |
---|
| 406 | for ($m = 0, $size_4 = count($units); $m < $size_4; $m++) |
---|
| 407 | { |
---|
| 408 | if (strpos(strtolower($temp[$l]),$units[$m]) !== FALSE) |
---|
| 409 | { |
---|
| 410 | $temp[$l] = floatval($temp[$l]).$units[$m]; |
---|
| 411 | $unit_found = TRUE; |
---|
| 412 | break; |
---|
| 413 | } |
---|
| 414 | } |
---|
| 415 | if (!$unit_found && in_array($this->property,$unit_values,TRUE)) |
---|
| 416 | { |
---|
| 417 | $temp[$l] = floatval($temp[$l]).'px'; |
---|
| 418 | } |
---|
| 419 | else if (!$unit_found) |
---|
| 420 | { |
---|
| 421 | $temp[$l] = floatval($temp[$l]); |
---|
| 422 | } |
---|
| 423 | // Remove leading zero |
---|
| 424 | if (abs(floatval($temp[$l])) < 1) { |
---|
| 425 | if (floatval($temp[$l]) < 0) { |
---|
| 426 | $temp[$l] = '-' . substr($temp[$l], 2); |
---|
| 427 | } else { |
---|
| 428 | $temp[$l] = substr($temp[$l], 1); |
---|
| 429 | } |
---|
| 430 | } |
---|
| 431 | } |
---|
| 432 | } |
---|
| 433 | |
---|
| 434 | return ((count($temp) > 1) ? $temp[0].'/'.$temp[1] : $temp[0]); |
---|
| 435 | } |
---|
| 436 | |
---|
| 437 | /** |
---|
| 438 | * Merges selectors with same properties. Example: a{color:red} b{color:red} -> a,b{color:red} |
---|
| 439 | * Very basic and has at least one bug. Hopefully there is a replacement soon. |
---|
| 440 | * @param array $array |
---|
| 441 | * @return array |
---|
| 442 | * @access public |
---|
| 443 | * @version 1.2 |
---|
| 444 | */ |
---|
| 445 | function merge_selectors(&$array) |
---|
| 446 | { |
---|
| 447 | $css = $array; |
---|
| 448 | foreach($css as $key => $value) |
---|
| 449 | { |
---|
| 450 | if(!isset($css[$key])) |
---|
| 451 | { |
---|
| 452 | continue; |
---|
| 453 | } |
---|
| 454 | $newsel = ''; |
---|
| 455 | |
---|
| 456 | // Check if properties also exist in another selector |
---|
| 457 | $keys = array(); |
---|
| 458 | // PHP bug (?) without $css = $array; here |
---|
| 459 | foreach($css as $selector => $vali) |
---|
| 460 | { |
---|
| 461 | if($selector == $key) |
---|
| 462 | { |
---|
| 463 | continue; |
---|
| 464 | } |
---|
| 465 | |
---|
| 466 | if($css[$key] === $vali) |
---|
| 467 | { |
---|
| 468 | $keys[] = $selector; |
---|
| 469 | } |
---|
| 470 | } |
---|
| 471 | |
---|
| 472 | if(!empty($keys)) |
---|
| 473 | { |
---|
| 474 | $newsel = $key; |
---|
| 475 | unset($css[$key]); |
---|
| 476 | foreach($keys as $selector) |
---|
| 477 | { |
---|
| 478 | unset($css[$selector]); |
---|
| 479 | $newsel .= ','.$selector; |
---|
| 480 | } |
---|
| 481 | $css[$newsel] = $value; |
---|
| 482 | } |
---|
| 483 | } |
---|
| 484 | $array = $css; |
---|
| 485 | } |
---|
| 486 | |
---|
| 487 | /** |
---|
| 488 | * Dissolves properties like padding:10px 10px 10px to padding-top:10px;padding-bottom:10px;... |
---|
| 489 | * @param string $property |
---|
| 490 | * @param string $value |
---|
| 491 | * @return array |
---|
| 492 | * @version 1.0 |
---|
| 493 | * @see merge_4value_shorthands() |
---|
| 494 | */ |
---|
| 495 | function dissolve_4value_shorthands($property,$value) |
---|
| 496 | { |
---|
| 497 | $shorthands =& $GLOBALS['csstidy']['shorthands']; |
---|
| 498 | if(!is_array($shorthands[$property])) |
---|
| 499 | { |
---|
| 500 | $return[$property] = $value; |
---|
| 501 | return $return; |
---|
| 502 | } |
---|
| 503 | |
---|
| 504 | $important = ''; |
---|
| 505 | if(csstidy::is_important($value)) |
---|
| 506 | { |
---|
| 507 | $value = csstidy::gvw_important($value); |
---|
| 508 | $important = '!important'; |
---|
| 509 | } |
---|
| 510 | $values = explode(' ',$value); |
---|
| 511 | |
---|
| 512 | |
---|
| 513 | $return = array(); |
---|
| 514 | if(count($values) == 4) |
---|
| 515 | { |
---|
| 516 | for($i=0;$i<4;$i++) |
---|
| 517 | { |
---|
| 518 | $return[$shorthands[$property][$i]] = $values[$i].$important; |
---|
| 519 | } |
---|
| 520 | } |
---|
| 521 | elseif(count($values) == 3) |
---|
| 522 | { |
---|
| 523 | $return[$shorthands[$property][0]] = $values[0].$important; |
---|
| 524 | $return[$shorthands[$property][1]] = $values[1].$important; |
---|
| 525 | $return[$shorthands[$property][3]] = $values[1].$important; |
---|
| 526 | $return[$shorthands[$property][2]] = $values[2].$important; |
---|
| 527 | } |
---|
| 528 | elseif(count($values) == 2) |
---|
| 529 | { |
---|
| 530 | for($i=0;$i<4;$i++) |
---|
| 531 | { |
---|
| 532 | $return[$shorthands[$property][$i]] = (($i % 2 != 0)) ? $values[1].$important : $values[0].$important; |
---|
| 533 | } |
---|
| 534 | } |
---|
| 535 | else |
---|
| 536 | { |
---|
| 537 | for($i=0;$i<4;$i++) |
---|
| 538 | { |
---|
| 539 | $return[$shorthands[$property][$i]] = $values[0].$important; |
---|
| 540 | } |
---|
| 541 | } |
---|
| 542 | |
---|
| 543 | return $return; |
---|
| 544 | } |
---|
| 545 | |
---|
| 546 | /** |
---|
| 547 | * Explodes a string as explode() does, however, not if $sep is escaped or within a string. |
---|
| 548 | * @param string $sep seperator |
---|
| 549 | * @param string $string |
---|
| 550 | * @return array |
---|
| 551 | * @version 1.0 |
---|
| 552 | */ |
---|
| 553 | function explode_ws($sep,$string) |
---|
| 554 | { |
---|
| 555 | $status = 'st'; |
---|
| 556 | $to = ''; |
---|
| 557 | |
---|
| 558 | $output = array(); |
---|
| 559 | $num = 0; |
---|
| 560 | for($i = 0, $len = strlen($string);$i < $len; $i++) |
---|
| 561 | { |
---|
| 562 | switch($status) |
---|
| 563 | { |
---|
| 564 | case 'st': |
---|
| 565 | if($string{$i} == $sep && !csstidy::escaped($string,$i)) |
---|
| 566 | { |
---|
| 567 | ++$num; |
---|
| 568 | } |
---|
| 569 | elseif($string{$i} == '"' || $string{$i} == '\'' || $string{$i} == '(' && !csstidy::escaped($string,$i)) |
---|
| 570 | { |
---|
| 571 | $status = 'str'; |
---|
| 572 | $to = ($string{$i} == '(') ? ')' : $string{$i}; |
---|
| 573 | (isset($output[$num])) ? $output[$num] .= $string{$i} : $output[$num] = $string{$i}; |
---|
| 574 | } |
---|
| 575 | else |
---|
| 576 | { |
---|
| 577 | (isset($output[$num])) ? $output[$num] .= $string{$i} : $output[$num] = $string{$i}; |
---|
| 578 | } |
---|
| 579 | break; |
---|
| 580 | |
---|
| 581 | case 'str': |
---|
| 582 | if($string{$i} == $to && !csstidy::escaped($string,$i)) |
---|
| 583 | { |
---|
| 584 | $status = 'st'; |
---|
| 585 | } |
---|
| 586 | (isset($output[$num])) ? $output[$num] .= $string{$i} : $output[$num] = $string{$i}; |
---|
| 587 | break; |
---|
| 588 | } |
---|
| 589 | } |
---|
| 590 | |
---|
| 591 | if(isset($output[0])) |
---|
| 592 | { |
---|
| 593 | return $output; |
---|
| 594 | } |
---|
| 595 | else |
---|
| 596 | { |
---|
| 597 | return array($output); |
---|
| 598 | } |
---|
| 599 | } |
---|
| 600 | |
---|
| 601 | /** |
---|
| 602 | * Merges Shorthand properties again, the opposite of dissolve_4value_shorthands() |
---|
| 603 | * @param array $array |
---|
| 604 | * @return array |
---|
| 605 | * @version 1.2 |
---|
| 606 | * @see dissolve_4value_shorthands() |
---|
| 607 | */ |
---|
| 608 | function merge_4value_shorthands($array) |
---|
| 609 | { |
---|
| 610 | $return = $array; |
---|
| 611 | $shorthands =& $GLOBALS['csstidy']['shorthands']; |
---|
| 612 | |
---|
| 613 | foreach($shorthands as $key => $value) |
---|
| 614 | { |
---|
| 615 | if(isset($array[$value[0]]) && isset($array[$value[1]]) |
---|
| 616 | && isset($array[$value[2]]) && isset($array[$value[3]]) && $value !== 0) |
---|
| 617 | { |
---|
| 618 | $return[$key] = ''; |
---|
| 619 | |
---|
| 620 | $important = ''; |
---|
| 621 | for($i = 0; $i < 4; $i++) |
---|
| 622 | { |
---|
| 623 | $val = $array[$value[$i]]; |
---|
| 624 | if(csstidy::is_important($val)) |
---|
| 625 | { |
---|
| 626 | $important = '!important'; |
---|
| 627 | $return[$key] .= csstidy::gvw_important($val).' '; |
---|
| 628 | } |
---|
| 629 | else |
---|
| 630 | { |
---|
| 631 | $return[$key] .= $val.' '; |
---|
| 632 | } |
---|
| 633 | unset($return[$value[$i]]); |
---|
| 634 | } |
---|
| 635 | $return[$key] = csstidy_optimise::shorthand(trim($return[$key].$important)); |
---|
| 636 | } |
---|
| 637 | } |
---|
| 638 | return $return; |
---|
| 639 | } |
---|
| 640 | |
---|
| 641 | /** |
---|
| 642 | * Dissolve background property |
---|
| 643 | * @param string $str_value |
---|
| 644 | * @return array |
---|
| 645 | * @version 1.0 |
---|
| 646 | * @see merge_bg() |
---|
| 647 | * @todo full CSS 3 compliance |
---|
| 648 | */ |
---|
| 649 | function dissolve_short_bg($str_value) |
---|
| 650 | { |
---|
| 651 | $background_prop_default =& $GLOBALS['csstidy']['background_prop_default']; |
---|
| 652 | $repeat = array('repeat','repeat-x','repeat-y','no-repeat','space'); |
---|
| 653 | $attachment = array('scroll','fixed','local'); |
---|
| 654 | $clip = array('border','padding'); |
---|
| 655 | $origin = array('border','padding','content'); |
---|
| 656 | $pos = array('top','center','bottom','left','right'); |
---|
| 657 | $important = ''; |
---|
| 658 | $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); |
---|
| 659 | |
---|
| 660 | if(csstidy::is_important($str_value)) |
---|
| 661 | { |
---|
| 662 | $important = ' !important'; |
---|
| 663 | $str_value = csstidy::gvw_important($str_value); |
---|
| 664 | } |
---|
| 665 | |
---|
| 666 | $str_value = csstidy_optimise::explode_ws(',',$str_value); |
---|
| 667 | for($i = 0; $i < count($str_value); $i++) |
---|
| 668 | { |
---|
| 669 | $have['clip'] = FALSE; $have['pos'] = FALSE; |
---|
| 670 | $have['color'] = FALSE; $have['bg'] = FALSE; |
---|
| 671 | |
---|
| 672 | $str_value[$i] = csstidy_optimise::explode_ws(' ',trim($str_value[$i])); |
---|
| 673 | |
---|
| 674 | for($j = 0; $j < count($str_value[$i]); $j++) |
---|
| 675 | { |
---|
| 676 | if($have['bg'] === FALSE && (substr($str_value[$i][$j],0,4) == 'url(' || $str_value[$i][$j] === 'none')) |
---|
| 677 | { |
---|
| 678 | $return['background-image'] .= $str_value[$i][$j].','; |
---|
| 679 | $have['bg'] = TRUE; |
---|
| 680 | } |
---|
| 681 | elseif(in_array($str_value[$i][$j],$repeat,TRUE)) |
---|
| 682 | { |
---|
| 683 | $return['background-repeat'] .= $str_value[$i][$j].','; |
---|
| 684 | } |
---|
| 685 | elseif(in_array($str_value[$i][$j],$attachment,TRUE)) |
---|
| 686 | { |
---|
| 687 | $return['background-attachment'] .= $str_value[$i][$j].','; |
---|
| 688 | } |
---|
| 689 | elseif(in_array($str_value[$i][$j],$clip,TRUE) && !$have['clip']) |
---|
| 690 | { |
---|
| 691 | $return['background-clip'] .= $str_value[$i][$j].','; |
---|
| 692 | $have['clip'] = TRUE; |
---|
| 693 | } |
---|
| 694 | elseif(in_array($str_value[$i][$j],$origin,TRUE)) |
---|
| 695 | { |
---|
| 696 | $return['background-origin'] .= $str_value[$i][$j].','; |
---|
| 697 | } |
---|
| 698 | elseif($str_value[$i][$j]{0} == '(') |
---|
| 699 | { |
---|
| 700 | $return['background-size'] .= substr($str_value[$i][$j],1,-1).','; |
---|
| 701 | } |
---|
| 702 | elseif(in_array($str_value[$i][$j],$pos,TRUE) || is_numeric($str_value[$i][$j]{0}) || $str_value[$i][$j]{0} === NULL) |
---|
| 703 | { |
---|
| 704 | $return['background-position'] .= $str_value[$i][$j]; |
---|
| 705 | if(!$have['pos']) $return['background-position'] .= ' '; else $return['background-position'].= ','; |
---|
| 706 | $have['pos'] = TRUE; |
---|
| 707 | } |
---|
| 708 | elseif(!$have['color']) |
---|
| 709 | { |
---|
| 710 | $return['background-color'] .= $str_value[$i][$j].','; |
---|
| 711 | $have['color'] = TRUE; |
---|
| 712 | } |
---|
| 713 | } |
---|
| 714 | } |
---|
| 715 | |
---|
| 716 | foreach($background_prop_default as $bg_prop => $default_value) |
---|
| 717 | { |
---|
| 718 | if($return[$bg_prop] !== NULL) |
---|
| 719 | { |
---|
| 720 | $return[$bg_prop] = substr($return[$bg_prop],0,-1).$important; |
---|
| 721 | } |
---|
| 722 | else $return[$bg_prop] = $default_value.$important; |
---|
| 723 | } |
---|
| 724 | return $return; |
---|
| 725 | } |
---|
| 726 | |
---|
| 727 | /** |
---|
| 728 | * Merges all background properties |
---|
| 729 | * @param array $input_css |
---|
| 730 | * @return array |
---|
| 731 | * @version 1.0 |
---|
| 732 | * @see dissolve_short_bg() |
---|
| 733 | * @todo full CSS 3 compliance |
---|
| 734 | */ |
---|
| 735 | function merge_bg($input_css) |
---|
| 736 | { |
---|
| 737 | $background_prop_default =& $GLOBALS['csstidy']['background_prop_default']; |
---|
| 738 | // Max number of background images. CSS3 not yet fully implemented |
---|
| 739 | $number_of_values = @max(count(csstidy_optimise::explode_ws(',',$input_css['background-image'])),count(csstidy_optimise::explode_ws(',',$input_css['background-color'])),1); |
---|
| 740 | // Array with background images to check if BG image exists |
---|
| 741 | $bg_img_array = @csstidy_optimise::explode_ws(',',csstidy::gvw_important($input_css['background-image'])); |
---|
| 742 | $new_bg_value = ''; |
---|
| 743 | $important = ''; |
---|
| 744 | |
---|
| 745 | for($i = 0; $i < $number_of_values; $i++) |
---|
| 746 | { |
---|
| 747 | foreach($background_prop_default as $bg_property => $default_value) |
---|
| 748 | { |
---|
| 749 | // Skip if property does not exist |
---|
| 750 | if(!isset($input_css[$bg_property])) |
---|
| 751 | { |
---|
| 752 | continue; |
---|
| 753 | } |
---|
| 754 | |
---|
| 755 | $cur_value = $input_css[$bg_property]; |
---|
| 756 | |
---|
| 757 | // Skip some properties if there is no background image |
---|
| 758 | if((!isset($bg_img_array[$i]) || $bg_img_array[$i] === 'none') |
---|
| 759 | && ($bg_property === 'background-size' || $bg_property === 'background-position' |
---|
| 760 | || $bg_property === 'background-attachment' || $bg_property === 'background-repeat')) |
---|
| 761 | { |
---|
| 762 | continue; |
---|
| 763 | } |
---|
| 764 | |
---|
| 765 | // Remove !important |
---|
| 766 | if(csstidy::is_important($cur_value)) |
---|
| 767 | { |
---|
| 768 | $important = ' !important'; |
---|
| 769 | $cur_value = csstidy::gvw_important($cur_value); |
---|
| 770 | } |
---|
| 771 | |
---|
| 772 | // Do not add default values |
---|
| 773 | if($cur_value === $default_value) |
---|
| 774 | { |
---|
| 775 | continue; |
---|
| 776 | } |
---|
| 777 | |
---|
| 778 | $temp = csstidy_optimise::explode_ws(',',$cur_value); |
---|
| 779 | |
---|
| 780 | if(isset($temp[$i])) |
---|
| 781 | { |
---|
| 782 | if($bg_property == 'background-size') |
---|
| 783 | { |
---|
| 784 | $new_bg_value .= '('.$temp[$i].') '; |
---|
| 785 | } |
---|
| 786 | else |
---|
| 787 | { |
---|
| 788 | $new_bg_value .= $temp[$i].' '; |
---|
| 789 | } |
---|
| 790 | } |
---|
| 791 | } |
---|
| 792 | |
---|
| 793 | $new_bg_value = trim($new_bg_value); |
---|
| 794 | if($i != $number_of_values-1) $new_bg_value .= ','; |
---|
| 795 | } |
---|
| 796 | |
---|
| 797 | // Delete all background-properties |
---|
| 798 | foreach($background_prop_default as $bg_property => $default_value) |
---|
| 799 | { |
---|
| 800 | unset($input_css[$bg_property]); |
---|
| 801 | } |
---|
| 802 | |
---|
| 803 | // Add new background property |
---|
| 804 | if($new_bg_value !== '') $input_css['background'] = $new_bg_value.$important; |
---|
| 805 | |
---|
| 806 | return $input_css; |
---|
| 807 | } |
---|
| 808 | } |
---|
| 809 | ?> |
---|