csstidy
[ class tree: csstidy ] [ index: csstidy ] [ all elements ]

Source for file functions.inc.php

Documentation is available at functions.inc.php

  1. <?php
  2. /**
  3. * Various functions
  4. *
  5. * This file contains a few functions which are needed to optimise CSS Code. These
  6. * functions are not part of the main class since they are not directly related to the
  7. * parsing process.
  8. *
  9. * This file is part of CSSTidy.
  10. *
  11. * CSSTidy is free software; you can redistribute it and/or modify
  12. * it under the terms of the GNU General Public License as published by
  13. * the Free Software Foundation; either version 2 of the License, or
  14. * (at your option) any later version.
  15. *
  16. * CSSTidy is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU General Public License
  22. * along with CSSTidy; if not, write to the Free Software
  23. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  24. *
  25. * @license http://opensource.org/licenses/gpl-license.php GNU Public License
  26. * @package csstidy
  27. * @author Florian Schmitz (floele at gmail dot com) 2005
  28. */
  29.  
  30.  
  31. /**
  32. * Color compression function. Converts all rgb() values to #-values and uses the short-form if possible. Also replaces 4 color names by #-values.
  33. * @param string $color
  34. * @return string
  35. * @version 1.1
  36. */
  37. function cut_color($color)
  38. {
  39. $replace_colors =& $GLOBALS['csstidy']['replace_colors'];
  40. // rgb(0,0,0) -> #000000 (or #000 in this case later)
  41. if(strtolower(substr($color,0,4)) == 'rgb(')
  42. {
  43. $color_tmp = substr($color,4,strlen($color)-5);
  44. $color_tmp = explode(',',$color_tmp);
  45. for ( $i = 0; $i < count($color_tmp); $i++ )
  46. {
  47. $color_tmp[$i] = trim ($color_tmp[$i]);
  48. if(substr($color_tmp[$i],-1) == '%')
  49. {
  50. $color_tmp[$i] = round((255*$color_tmp[$i])/100);
  51. }
  52. if($color_tmp[$i]>255) $color_tmp[$i] = 255;
  53. }
  54. $color = '#';
  55. for ($i = 0; $i < 3; $i++ )
  56. {
  57. if($color_tmp[$i]<16) $color .= '0'.dechex($color_tmp[$i]);
  58. else $color .= dechex($color_tmp[$i]);
  59. }
  60. }
  61. // Fix bad color names
  62. if(isset($replace_colors[strtolower($color)]))
  63. {
  64. $color = $replace_colors[strtolower($color)];
  65. }
  66. // #aabbcc -> #abc
  67. if(strlen($color) == 7)
  68. {
  69. $color_temp = strtolower($color);
  70. if($color_temp{0} == '#' && $color_temp{1} == $color_temp{2} && $color_temp{3} == $color_temp{4} && $color_temp{5} == $color_temp{6})
  71. {
  72. $color = '#'.$color{1}.$color{3}.$color{5};
  73. }
  74. }
  75. switch(strtolower($color))
  76. {
  77. /* color name -> hex code */
  78. case 'black': return '#000';
  79. case 'fuchsia': return '#F0F';
  80. case 'white': return '#FFF';
  81. case 'yellow': return '#FF0';
  82. /* hex code -> color name */
  83. case '#800000': return 'maroon';
  84. case '#ffa500': return 'orange';
  85. case '#808000': return 'olive';
  86. case '#800080': return 'purple';
  87. case '#008000': return 'green';
  88. case '#000080': return 'navy';
  89. case '#008080': return 'teal';
  90. case '#c0c0c0': return 'silver';
  91. case '#808080': return 'gray';
  92. case '#f00': return 'red';
  93. }
  94.  
  95. return $color;
  96. }
  97.  
  98. /**
  99. * Compresses numbers (ie. 1.0 becomes 1 or 1.100 becomes 1.1 )
  100. * @param string $subvalue
  101. * @param string property needed to check wheter <number>-values are allowed or not
  102. * @return string
  103. * @version 1.1
  104. */
  105. function compress_numbers($subvalue, $property = NULL)
  106. {
  107. $units =& $GLOBALS['csstidy']['units'];
  108. $number_values =& $GLOBALS['csstidy']['number_values'];
  109. $color_values =& $GLOBALS['csstidy']['color_values'];
  110.  
  111. // for font:1em/1em sans-serif...;
  112. if($property == 'font')
  113. {
  114. $temp = explode('/',$subvalue);
  115. }
  116. else
  117. {
  118. $temp = array($subvalue);
  119. }
  120. for ($l = 0; $l < count($temp); $l++)
  121. {
  122. // continue if no numeric value
  123. if(!(strlen($temp[$l]) > 0 && ( is_numeric($temp[$l]{0}) || $temp[$l]{0} == "+" || $temp[$l]{0} == "-" ) ))
  124. {
  125. continue;
  126. }
  127.  
  128. // Fix bad colors
  129. if(in_array($property,$color_values))
  130. {
  131. $temp[$l] = '#'.$temp[$l];
  132. }
  133. if(floatval($temp[$l]) == 0 && ( is_numeric($temp[$l]{0}) || $temp[$l]{0} == "+" || $temp[$l]{0} == "-" ) )
  134. {
  135. $temp[$l] = 0;
  136. }
  137. elseif(is_numeric($temp[$l]{0}) || $temp[$l]{0} == "+" || $temp[$l]{0} == "-")
  138. {
  139. $unit_found = FALSE;
  140. for( $m = 0, $size_4 = count($units); $m < $size_4; $m++ )
  141. {
  142. if(strpos(strtolower($temp[$l]),$units[$m]) !== FALSE)
  143. {
  144. $temp[$l] = floatval($temp[$l]).$units[$m];
  145. $unit_found = TRUE;
  146. break;
  147. }
  148. }
  149. if(!$unit_found && !in_array($property,$number_values,TRUE))
  150. {
  151. $temp[$l] = floatval($temp[$l]).'px';
  152. }
  153. else if(!$unit_found)
  154. {
  155. $temp[$l] = floatval($temp[$l]);
  156. }
  157. }
  158. }
  159. $subvalue = (count($temp) > 1) ? $temp[0].'/'.$temp[1] : $temp[0];
  160. return "$subvalue";
  161. }
  162.  
  163. /**
  164. * Dissolves properties like padding:10px 10px 10px to padding-top:10px;padding-bottom:10px;...
  165. * @param string $property
  166. * @param string $value
  167. * @return array
  168. * @version 1.0
  169. * @see merge_4value_shorthands()
  170. */
  171. function dissolve_4value_shorthands($property,$value)
  172. {
  173. $shorthands =& $GLOBALS['csstidy']['shorthands'];
  174. if(!is_array($shorthands[$property]))
  175. {
  176. $return[$property] = $value;
  177. return $return;
  178. }
  179. $important = '';
  180. if(csstidy::is_important($value))
  181. {
  182. $value = csstidy::gvw_important($value);
  183. $important = ' !important';
  184. }
  185. $values = explode(' ',$value);
  186.  
  187.  
  188. $return = array();
  189. if(count($values) == 4)
  190. {
  191. for($i=0;$i<4;$i++)
  192. {
  193. $return[$shorthands[$property][$i]] = $values[$i].$important;
  194. }
  195. }
  196. elseif(count($values) == 3)
  197. {
  198. $return[$shorthands[$property][0]] = $values[0].$important;
  199. $return[$shorthands[$property][1]] = $values[1].$important;
  200. $return[$shorthands[$property][3]] = $values[1].$important;
  201. $return[$shorthands[$property][2]] = $values[2].$important;
  202. }
  203. elseif(count($values) == 2)
  204. {
  205. for($i=0;$i<4;$i++)
  206. {
  207. $return[$shorthands[$property][$i]] = (($i % 2 != 0)) ? $values[1].$important : $values[0].$important;
  208. }
  209. }
  210. else
  211. {
  212. for($i=0;$i<4;$i++)
  213. {
  214. $return[$shorthands[$property][$i]] = $values[0].$important;
  215. }
  216. }
  217. return $return;
  218. }
  219.  
  220. /**
  221. * Explodes a string as explode() does, however, not if $sep is escaped or within a string.
  222. * @param string $sep seperator
  223. * @param string $string
  224. * @return array
  225. * @version 1.0
  226. */
  227. function explode_ws($sep,$string)
  228. {
  229. $status = 'st';
  230. $to = '';
  231. $output = array();
  232. $num = 0;
  233. for($i = 0, $len = strlen($string);$i < $len; $i++)
  234. {
  235. switch($status)
  236. {
  237. case 'st':
  238. if($string{$i} == $sep && !csstidy::escaped($string,$i))
  239. {
  240. ++$num;
  241. }
  242. elseif($string{$i} == '"' || $string{$i} == '\'' || $string{$i} == '(' && !csstidy::escaped($string,$i))
  243. {
  244. $status = 'str';
  245. $to = ($string{$i} == '(') ? ')' : $string{$i};
  246. (isset($output[$num])) ? $output[$num] .= $string{$i} : $output[$num] = $string{$i};
  247. }
  248. else
  249. {
  250. (isset($output[$num])) ? $output[$num] .= $string{$i} : $output[$num] = $string{$i};
  251. }
  252. break;
  253. case 'str':
  254. if($string{$i} == $to && !csstidy::escaped($string,$i))
  255. {
  256. $status = 'st';
  257. }
  258. (isset($output[$num])) ? $output[$num] .= $string{$i} : $output[$num] = $string{$i};
  259. break;
  260. }
  261. }
  262. if(isset($output[0]))
  263. {
  264. return $output;
  265. }
  266. else
  267. {
  268. return array($output);
  269. }
  270. }
  271.  
  272. /**
  273. * Merges Shorthand properties again, the opposite of dissolve_4value_shorthands()
  274. * @param array $array
  275. * @return array
  276. * @version 1.2
  277. * @see dissolve_4value_shorthands()
  278. */
  279. function merge_4value_shorthands($array)
  280. {
  281. $return = $array;
  282. $shorthands =& $GLOBALS['csstidy']['shorthands'];
  283. foreach($shorthands as $key => $value)
  284. {
  285. if(isset($array[$value[0]]) && isset($array[$value[1]])
  286. && isset($array[$value[2]]) && isset($array[$value[3]]) && $value !== 0)
  287. {
  288. $return[$key] = '';
  289. $important = '';
  290. for($i = 0; $i < 4; $i++)
  291. {
  292. $val = $array[$value[$i]];
  293. if(csstidy::is_important($val))
  294. {
  295. $important = '!important';
  296. $return[$key] .= csstidy::gvw_important($val).' ';
  297. }
  298. else
  299. {
  300. $return[$key] .= $val.' ';
  301. }
  302. unset($return[$value[$i]]);
  303. }
  304. $return[$key] = csstidy::shorthand(trim($return[$key].$important));
  305. }
  306. }
  307. return $return;
  308. }
  309.  
  310. /**
  311. * Dissolve background property
  312. * @param string $str_value
  313. * @return array
  314. * @version 1.0
  315. * @see merge_bg()
  316. * @todo full CSS 3 compliance
  317. */
  318. function dissolve_short_bg($str_value)
  319. {
  320. $background_prop_default =& $GLOBALS['csstidy']['background_prop_default'];
  321. $repeat = array('repeat','repeat-x','repeat-y','no-repeat','space');
  322. $attachment = array('scroll','fixed','local');
  323. $clip = array('border','padding');
  324. $origin = array('border','padding','content');
  325. $pos = array('top','center','bottom','left','right');
  326. $important = '';
  327. $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);
  328. if(csstidy::is_important($str_value))
  329. {
  330. $important = ' !important';
  331. $str_value = csstidy::gvw_important($str_value);
  332. }
  333. $str_value = explode_ws(',',$str_value);
  334. for($i = 0; $i < count($str_value); $i++)
  335. {
  336. $have['clip'] = FALSE; $have['pos'] = FALSE;
  337. $have['color'] = FALSE; $have['bg'] = FALSE;
  338. $str_value[$i] = explode_ws(' ',trim($str_value[$i]));
  339. for($j = 0; $j < count($str_value[$i]); $j++)
  340. {
  341. if($have['bg'] === FALSE && (substr($str_value[$i][$j],0,4) == 'url(' || $str_value[$i][$j] === 'none'))
  342. {
  343. $return['background-image'] .= $str_value[$i][$j].',';
  344. $have['bg'] = TRUE;
  345. }
  346. elseif(in_array($str_value[$i][$j],$repeat,TRUE))
  347. {
  348. $return['background-repeat'] .= $str_value[$i][$j].',';
  349. }
  350. elseif(in_array($str_value[$i][$j],$attachment,TRUE))
  351. {
  352. $return['background-attachment'] .= $str_value[$i][$j].',';
  353. }
  354. elseif(in_array($str_value[$i][$j],$clip,TRUE) && !$have['clip'])
  355. {
  356. $return['background-clip'] .= $str_value[$i][$j].',';
  357. $have['clip'] = TRUE;
  358. }
  359. elseif(in_array($str_value[$i][$j],$origin,TRUE))
  360. {
  361. $return['background-origin'] .= $str_value[$i][$j].',';
  362. }
  363. elseif($str_value[$i][$j]{0} == '(')
  364. {
  365. $return['background-size'] .= substr($str_value[$i][$j],1,-1).',';
  366. }
  367. elseif(in_array($str_value[$i][$j],$pos,TRUE) || is_numeric($str_value[$i][$j]{0}) || $str_value[$i][$j]{0} === NULL)
  368. {
  369. $return['background-position'] .= $str_value[$i][$j];
  370. if(!$have['pos']) $return['background-position'] .= ' '; else $return['background-position'].= ',';
  371. $have['pos'] = TRUE;
  372. }
  373. elseif(!$have['color'])
  374. {
  375. $return['background-color'] .= $str_value[$i][$j].',';
  376. $have['color'] = TRUE;
  377. }
  378. }
  379. }
  380. foreach($background_prop_default as $bg_prop => $default_value)
  381. {
  382. if($return[$bg_prop] !== NULL)
  383. {
  384. $return[$bg_prop] = substr($return[$bg_prop],0,-1).$important;
  385. }
  386. else $return[$bg_prop] = $default_value.$important;
  387. }
  388. return $return;
  389. }
  390.  
  391. /**
  392. * Merges all background properties
  393. * @param array $input_css
  394. * @return array
  395. * @version 1.0
  396. * @see dissolve_short_bg()
  397. * @todo full CSS 3 compliance
  398. */
  399. function merge_bg($input_css)
  400. {
  401. $background_prop_default =& $GLOBALS['csstidy']['background_prop_default'];
  402. // Max number of background images. CSS3 not yet fully implemented
  403. $number_of_values = @max(count(explode_ws(',',$input_css['background-image'])),count(explode_ws(',',$input_css['background-color'])),1);
  404. // Array with background images to check if BG image exists
  405. $bg_img_array = @explode_ws(',',csstidy::gvw_important($input_css['background-image']));
  406. $new_bg_value = '';
  407. $important = '';
  408. for($i = 0; $i < $number_of_values; $i++)
  409. {
  410. foreach($background_prop_default as $bg_property => $default_value)
  411. {
  412. // Skip if property does not exist
  413. if(!isset($input_css[$bg_property]))
  414. {
  415. continue;
  416. }
  417. $cur_value = $input_css[$bg_property];
  418. // Skip some properties if there is no background image
  419. if((!isset($bg_img_array[$i]) || $bg_img_array[$i] === 'none')
  420. && ($bg_property === 'background-size' || $bg_property === 'background-position'
  421. || $bg_property === 'background-attachment' || $bg_property === 'background-repeat'))
  422. {
  423. continue;
  424. }
  425. // Remove !important
  426. if(csstidy::is_important($cur_value))
  427. {
  428. $important = ' !important';
  429. $cur_value = csstidy::gvw_important($cur_value);
  430. }
  431. // Do not add default values
  432. if($cur_value === $default_value)
  433. {
  434. continue;
  435. }
  436. $temp = explode_ws(',',$cur_value);
  437.  
  438. if(isset($temp[$i]))
  439. {
  440. if($bg_property == 'background-size')
  441. {
  442. $new_bg_value .= '('.$temp[$i].') ';
  443. }
  444. else
  445. {
  446. $new_bg_value .= $temp[$i].' ';
  447. }
  448. }
  449. }
  450. $new_bg_value = trim($new_bg_value);
  451. if($i != $number_of_values-1) $new_bg_value .= ',';
  452. }
  453. // Delete all background-properties
  454. foreach($background_prop_default as $bg_property => $default_value)
  455. {
  456. unset($input_css[$bg_property]);
  457. }
  458. // Add new background property
  459. if($new_bg_value !== '') $input_css['background'] = $new_bg_value.$important;
  460. return $input_css;
  461. }
  462.  
  463. ?>

Documentation generated on Fri, 3 Feb 2006 16:22:16 +0100 by phpDocumentor 1.3.0RC3