csstidy_optimise

CSS Optimising Class.

Defined (1)

The class is defined in the following location(s).

/lib/CSSTidy/class.csstidy_optimise.php  
  1. class csstidy_optimise { 
  2.  
  3. /** 
  4. * csstidy object 
  5. * @var object 
  6. */ 
  7. public $parser; 
  8.  
  9. /** 
  10. * Constructor 
  11. * @param array $css contains the class csstidy 
  12. * @access private 
  13. * @version 1.0 
  14. */ 
  15. public function __construct($css) { 
  16. $this->parser = $css; 
  17. $this->css = & $css->css; 
  18. $this->sub_value = & $css->sub_value; 
  19. $this->at = & $css->at; 
  20. $this->selector = & $css->selector; 
  21. $this->property = & $css->property; 
  22. $this->value = & $css->value; 
  23.  
  24. /** 
  25. * Optimises $css after parsing 
  26. * @access public 
  27. * @version 1.0 
  28. */ 
  29. public function postparse() { 
  30. if ($this->parser->get_cfg('preserve_css')) { 
  31. return; 
  32.  
  33. if ((int)$this->parser->get_cfg('merge_selectors') === 2) { 
  34. foreach ($this->css as $medium => $value) { 
  35. $this->merge_selectors($this->css[$medium]); 
  36.  
  37. if ($this->parser->get_cfg('discard_invalid_selectors')) { 
  38. foreach ($this->css as $medium => $value) { 
  39. $this->discard_invalid_selectors($this->css[$medium]); 
  40.  
  41. if ($this->parser->get_cfg('optimise_shorthands') > 0) { 
  42. foreach ($this->css as $medium => $value) { 
  43. foreach ($value as $selector => $value1) { 
  44. $this->css[$medium][$selector] = $this->merge_4value_shorthands($this->css[$medium][$selector]); 
  45.  
  46. if ($this->parser->get_cfg('optimise_shorthands') < 2) { 
  47. continue; 
  48.  
  49. $this->css[$medium][$selector] = $this->merge_font($this->css[$medium][$selector]); 
  50.  
  51. if ($this->parser->get_cfg('optimise_shorthands') < 3) { 
  52. continue; 
  53.  
  54. $this->css[$medium][$selector] = $this->merge_bg($this->css[$medium][$selector]); 
  55. if (empty($this->css[$medium][$selector])) { 
  56. unset($this->css[$medium][$selector]); 
  57.  
  58. /** 
  59. * Optimises values 
  60. * @access public 
  61. * @version 1.0 
  62. */ 
  63. public function value() { 
  64. $shorthands = & $this->parser->data['csstidy']['shorthands']; 
  65.  
  66. // optimise shorthand properties 
  67. if (isset($shorthands[$this->property])) { 
  68. $temp = $this->shorthand($this->value); // FIXME - move 
  69. if ($temp != $this->value) { 
  70. $this->parser->log('Optimised shorthand notation (' . $this->property . '): Changed "' . $this->value . '" to "' . $temp . '"', 'Information'); 
  71. $this->value = $temp; 
  72.  
  73. // Remove whitespace at ! important 
  74. if ($this->value != $this->compress_important($this->value)) { 
  75. $this->parser->log('Optimised !important', 'Information'); 
  76.  
  77. /** 
  78. * Optimises shorthands 
  79. * @access public 
  80. * @version 1.0 
  81. */ 
  82. public function shorthands() { 
  83. $shorthands = & $this->parser->data['csstidy']['shorthands']; 
  84.  
  85. if (!$this->parser->get_cfg('optimise_shorthands') || $this->parser->get_cfg('preserve_css')) { 
  86. return; 
  87.  
  88. if ($this->property === 'font' && $this->parser->get_cfg('optimise_shorthands') > 1) { 
  89. $this->css[$this->at][$this->selector]['font']=''; 
  90. $this->parser->merge_css_blocks($this->at, $this->selector, $this->dissolve_short_font($this->value)); 
  91. if ($this->property === 'background' && $this->parser->get_cfg('optimise_shorthands') > 2) { 
  92. $this->css[$this->at][$this->selector]['background']=''; 
  93. $this->parser->merge_css_blocks($this->at, $this->selector, $this->dissolve_short_bg($this->value)); 
  94. if (isset($shorthands[$this->property])) { 
  95. $this->parser->merge_css_blocks($this->at, $this->selector, $this->dissolve_4value_shorthands($this->property, $this->value)); 
  96. if (is_array($shorthands[$this->property])) { 
  97. $this->css[$this->at][$this->selector][$this->property] = ''; 
  98.  
  99. /** 
  100. * Optimises a sub-value 
  101. * @access public 
  102. * @version 1.0 
  103. */ 
  104. public function subvalue() { 
  105. $replace_colors = & $this->parser->data['csstidy']['replace_colors']; 
  106.  
  107. $this->sub_value = trim($this->sub_value); 
  108. if ($this->sub_value == '') { // caution : '0' 
  109. return; 
  110.  
  111. $important = ''; 
  112. if ($this->parser->is_important($this->sub_value)) { 
  113. $important = '!important'; 
  114. $this->sub_value = $this->parser->gvw_important($this->sub_value); 
  115.  
  116. // Compress font-weight 
  117. if ($this->property === 'font-weight' && $this->parser->get_cfg('compress_font-weight')) { 
  118. if ($this->sub_value === 'bold') { 
  119. $this->sub_value = '700'; 
  120. $this->parser->log('Optimised font-weight: Changed "bold" to "700"', 'Information'); 
  121. } elseif ($this->sub_value === 'normal') { 
  122. $this->sub_value = '400'; 
  123. $this->parser->log('Optimised font-weight: Changed "normal" to "400"', 'Information'); 
  124.  
  125. $temp = $this->compress_numbers($this->sub_value); 
  126. if (strcasecmp($temp, $this->sub_value) !== 0) { 
  127. if (strlen($temp) > strlen($this->sub_value)) { 
  128. $this->parser->log('Fixed invalid number: Changed "' . $this->sub_value . '" to "' . $temp . '"', 'Warning'); 
  129. } else { 
  130. $this->parser->log('Optimised number: Changed "' . $this->sub_value . '" to "' . $temp . '"', 'Information'); 
  131. $this->sub_value = $temp; 
  132. if ($this->parser->get_cfg('compress_colors')) { 
  133. $temp = $this->cut_color($this->sub_value); 
  134. if ($temp !== $this->sub_value) { 
  135. if (isset($replace_colors[$this->sub_value])) { 
  136. $this->parser->log('Fixed invalid color name: Changed "' . $this->sub_value . '" to "' . $temp . '"', 'Warning'); 
  137. } else { 
  138. $this->parser->log('Optimised color: Changed "' . $this->sub_value . '" to "' . $temp . '"', 'Information'); 
  139. $this->sub_value = $temp; 
  140. $this->sub_value .= $important; 
  141.  
  142. /** 
  143. * Compresses shorthand values. Example: margin:1px 1px 1px 1px -> margin:1px 
  144. * @param string $value 
  145. * @access public 
  146. * @return string 
  147. * @version 1.0 
  148. */ 
  149. public function shorthand($value) { 
  150. $important = ''; 
  151. if ($this->parser->is_important($value)) { 
  152. $values = $this->parser->gvw_important($value); 
  153. $important = '!important'; 
  154. else 
  155. $values = $value; 
  156.  
  157. $values = explode(' ', $values); 
  158. switch (count($values)) { 
  159. case 4: 
  160. if ($values[0] == $values[1] && $values[0] == $values[2] && $values[0] == $values[3]) { 
  161. return $values[0] . $important; 
  162. } elseif ($values[1] == $values[3] && $values[0] == $values[2]) { 
  163. return $values[0] . ' ' . $values[1] . $important; 
  164. } elseif ($values[1] == $values[3]) { 
  165. return $values[0] . ' ' . $values[1] . ' ' . $values[2] . $important; 
  166. break; 
  167.  
  168. case 3: 
  169. if ($values[0] == $values[1] && $values[0] == $values[2]) { 
  170. return $values[0] . $important; 
  171. } elseif ($values[0] == $values[2]) { 
  172. return $values[0] . ' ' . $values[1] . $important; 
  173. break; 
  174.  
  175. case 2: 
  176. if ($values[0] == $values[1]) { 
  177. return $values[0] . $important; 
  178. break; 
  179.  
  180. return $value; 
  181.  
  182. /** 
  183. * Removes unnecessary whitespace in ! important 
  184. * @param string $string 
  185. * @return string 
  186. * @access public 
  187. * @version 1.1 
  188. */ 
  189. public function compress_important(&$string) { 
  190. if ($this->parser->is_important($string)) { 
  191. $important = $this->parser->get_cfg('space_before_important') ? ' !important' : '!important'; 
  192. $string = $this->parser->gvw_important($string) . $important; 
  193. return $string; 
  194.  
  195. /** 
  196. * Color compression function. Converts all rgb() values to #-values and uses the short-form if possible. Also replaces 4 color names by #-values. 
  197. * @param string $color 
  198. * @return string 
  199. * @version 1.1 
  200. */ 
  201. public function cut_color($color) { 
  202. $replace_colors = & $this->parser->data['csstidy']['replace_colors']; 
  203.  
  204. // if it's a string, don't touch ! 
  205. if (strncmp($color, "'", 1) == 0 || strncmp($color, '"', 1) == 0) 
  206. return $color; 
  207.  
  208. /** expressions complexes de type gradient */ 
  209. if (strpos($color, '(') !== false && strncmp($color, 'rgb(' , 4) != 0) { 
  210. // on ne touche pas aux couleurs dans les expression ms, c'est trop sensible 
  211. if (stripos($color, 'progid:') !== false) 
  212. return $color; 
  213. preg_match_all(", rgb\([^)]+\), i", $color, $matches, PREG_SET_ORDER); 
  214. if (count($matches)) { 
  215. foreach ($matches as $m) { 
  216. $color = str_replace($m[0], $this->cut_color($m[0]), $color); 
  217. preg_match_all(", #[0-9a-f]{6}(?=[^0-9a-f]), i", $color, $matches, PREG_SET_ORDER); 
  218. if (count($matches)) { 
  219. foreach ($matches as $m) { 
  220. $color = str_replace($m[0], $this->cut_color($m[0]), $color); 
  221. return $color; 
  222.  
  223. // rgb(0, 0, 0) -> #000000 (or #000 in this case later) 
  224. if (strncasecmp($color, 'rgb(', 4)==0) { 
  225. $color_tmp = substr($color, 4, strlen($color) - 5); 
  226. $color_tmp = explode(', ', $color_tmp); 
  227. for ($i = 0; $i < count($color_tmp); $i++) { 
  228. $color_tmp[$i] = trim($color_tmp[$i]); 
  229. if (substr($color_tmp[$i], -1) === '%') { 
  230. $color_tmp[$i] = round((255 * $color_tmp[$i]) / 100); 
  231. if ($color_tmp[$i] > 255) 
  232. $color_tmp[$i] = 255; 
  233. $color = '#'; 
  234. for ($i = 0; $i < 3; $i++) { 
  235. if ($color_tmp[$i] < 16) { 
  236. $color .= '0' . dechex($color_tmp[$i]); 
  237. } else { 
  238. $color .= dechex($color_tmp[$i]); 
  239.  
  240. // Fix bad color names 
  241. if (isset($replace_colors[strtolower($color)])) { 
  242. $color = $replace_colors[strtolower($color)]; 
  243.  
  244. // #aabbcc -> #abc 
  245. if (strlen($color) == 7) { 
  246. $color_temp = strtolower($color); 
  247. if ($color_temp{0} === '#' && $color_temp{1} == $color_temp{2} && $color_temp{3} == $color_temp{4} && $color_temp{5} == $color_temp{6}) { 
  248. $color = '#' . $color{1} . $color{3} . $color{5}; 
  249.  
  250. switch (strtolower($color)) { 
  251. /** color name -> hex code */ 
  252. case 'black': return '#000'; 
  253. case 'fuchsia': return '#f0f'; 
  254. case 'white': return '#fff'; 
  255. case 'yellow': return '#ff0'; 
  256.  
  257. /** hex code -> color name */ 
  258. case '#800000': return 'maroon'; 
  259. case '#ffa500': return 'orange'; 
  260. case '#808000': return 'olive'; 
  261. case '#800080': return 'purple'; 
  262. case '#008000': return 'green'; 
  263. case '#000080': return 'navy'; 
  264. case '#008080': return 'teal'; 
  265. case '#c0c0c0': return 'silver'; 
  266. case '#808080': return 'gray'; 
  267. case '#f00': return 'red'; 
  268.  
  269. return $color; 
  270.  
  271. /** 
  272. * Compresses numbers (ie. 1.0 becomes 1 or 1.100 becomes 1.1 ) 
  273. * @param string $subvalue 
  274. * @return string 
  275. * @version 1.2 
  276. */ 
  277. public function compress_numbers($subvalue) { 
  278. $unit_values = & $this->parser->data['csstidy']['unit_values']; 
  279. $color_values = & $this->parser->data['csstidy']['color_values']; 
  280.  
  281. // for font:1em/1em sans-serif...; 
  282. if ($this->property === 'font') { 
  283. $temp = explode('/', $subvalue); 
  284. } else { 
  285. $temp = array($subvalue); 
  286.  
  287. for ($l = 0; $l < count($temp); $l++) { 
  288. // if we are not dealing with a number at this point, do not optimise anything 
  289. $number = $this->AnalyseCssNumber($temp[$l]); 
  290. if ($number === false) { 
  291. return $subvalue; 
  292.  
  293. // Fix bad colors 
  294. if (in_array($this->property, $color_values)) { 
  295. $temp[$l] = '#' . $temp[$l]; 
  296. continue; 
  297.  
  298. if (abs($number[0]) > 0) { 
  299. if ($number[1] == '' && in_array($this->property, $unit_values, true)) { 
  300. $number[1] = 'px'; 
  301. } elseif ($number[1] != 's' && $number[1] != 'ms') { 
  302. $number[1] = ''; 
  303.  
  304. $temp[$l] = $number[0] . $number[1]; 
  305.  
  306. return ((count($temp) > 1) ? $temp[0] . '/' . $temp[1] : $temp[0]); 
  307.  
  308. /** 
  309. * Checks if a given string is a CSS valid number. If it is,  
  310. * an array containing the value and unit is returned 
  311. * @param string $string 
  312. * @return array ('unit' if unit is found or '' if no unit exists, number value) or false if no number 
  313. */ 
  314. public function AnalyseCssNumber($string) { 
  315. // most simple checks first 
  316. if (strlen($string) == 0 || ctype_alpha($string{0})) { 
  317. return false; 
  318.  
  319. $units = & $this->parser->data['csstidy']['units']; 
  320. $return = array(0, ''); 
  321.  
  322. $return[0] = floatval($string); 
  323. if (abs($return[0]) > 0 && abs($return[0]) < 1) { 
  324. if ($return[0] < 0) { 
  325. $return[0] = '-' . ltrim(substr($return[0], 1), '0'); 
  326. } else { 
  327. $return[0] = ltrim($return[0], '0'); 
  328.  
  329. // Look for unit and split from value if exists 
  330. foreach ($units as $unit) { 
  331. $expectUnitAt = strlen($string) - strlen($unit); 
  332. if (!($unitInString = stristr($string, $unit))) { // mb_strpos() fails with "false" 
  333. continue; 
  334. $actualPosition = strpos($string, $unitInString); 
  335. if ($expectUnitAt === $actualPosition) { 
  336. $return[1] = $unit; 
  337. $string = substr($string, 0, - strlen($unit)); 
  338. break; 
  339. if (!is_numeric($string)) { 
  340. return false; 
  341. return $return; 
  342.  
  343. /** 
  344. * Merges selectors with same properties. Example: a{color:red} b{color:red} -> a, b{color:red} 
  345. * Very basic and has at least one bug. Hopefully there is a replacement soon. 
  346. * @param array $array 
  347. * @return array 
  348. * @access public 
  349. * @version 1.2 
  350. */ 
  351. public function merge_selectors(&$array) { 
  352. $css = $array; 
  353. foreach ($css as $key => $value) { 
  354. if (!isset($css[$key])) { 
  355. continue; 
  356. $newsel = ''; 
  357.  
  358. // Check if properties also exist in another selector 
  359. $keys = array(); 
  360. // PHP bug (?) without $css = $array; here 
  361. foreach ($css as $selector => $vali) { 
  362. if ($selector == $key) { 
  363. continue; 
  364.  
  365. if ($css[$key] === $vali) { 
  366. $keys[] = $selector; 
  367.  
  368. if (!empty($keys)) { 
  369. $newsel = $key; 
  370. unset($css[$key]); 
  371. foreach ($keys as $selector) { 
  372. unset($css[$selector]); 
  373. $newsel .= ', ' . $selector; 
  374. $css[$newsel] = $value; 
  375. $array = $css; 
  376.  
  377. /** 
  378. * Removes invalid selectors and their corresponding rule-sets as 
  379. * defined by 4.1.7 in REC-CSS2. This is a very rudimentary check 
  380. * and should be replaced by a full-blown parsing algorithm or 
  381. * regular expression 
  382. * @version 1.4 
  383. */ 
  384. public function discard_invalid_selectors(&$array) { 
  385. $invalid = array('+' => true, '~' => true, ', ' => true, '>' => true); 
  386. foreach ($array as $selector => $decls) { 
  387. $ok = true; 
  388. $selectors = array_map('trim', explode(', ', $selector)); 
  389. foreach ($selectors as $s) { 
  390. $simple_selectors = preg_split('/\s*[+>~\s]\s*/', $s); 
  391. foreach ($simple_selectors as $ss) { 
  392. if ($ss === '') 
  393. $ok = false; 
  394. // could also check $ss for internal structure,  
  395. // but that probably would be too slow 
  396. if (!$ok) 
  397. unset($array[$selector]); 
  398.  
  399. /** 
  400. * Dissolves properties like padding:10px 10px 10px to padding-top:10px;padding-bottom:10px;... 
  401. * @param string $property 
  402. * @param string $value 
  403. * @return array 
  404. * @version 1.0 
  405. * @see merge_4value_shorthands() 
  406. */ 
  407. public function dissolve_4value_shorthands($property, $value) { 
  408. $shorthands = & $this->parser->data['csstidy']['shorthands']; 
  409. if (!is_array($shorthands[$property])) { 
  410. $return[$property] = $value; 
  411. return $return; 
  412.  
  413. $important = ''; 
  414. if ($this->parser->is_important($value)) { 
  415. $value = $this->parser->gvw_important($value); 
  416. $important = '!important'; 
  417. $values = explode(' ', $value); 
  418.  
  419.  
  420. $return = array(); 
  421. if (count($values) == 4) { 
  422. for ($i = 0; $i < 4; $i++) { 
  423. $return[$shorthands[$property][$i]] = $values[$i] . $important; 
  424. } elseif (count($values) == 3) { 
  425. $return[$shorthands[$property][0]] = $values[0] . $important; 
  426. $return[$shorthands[$property][1]] = $values[1] . $important; 
  427. $return[$shorthands[$property][3]] = $values[1] . $important; 
  428. $return[$shorthands[$property][2]] = $values[2] . $important; 
  429. } elseif (count($values) == 2) { 
  430. for ($i = 0; $i < 4; $i++) { 
  431. $return[$shorthands[$property][$i]] = (($i % 2 != 0)) ? $values[1] . $important : $values[0] . $important; 
  432. } else { 
  433. for ($i = 0; $i < 4; $i++) { 
  434. $return[$shorthands[$property][$i]] = $values[0] . $important; 
  435.  
  436. return $return; 
  437.  
  438. /** 
  439. * Explodes a string as explode() does, however, not if $sep is escaped or within a string. 
  440. * @param string $sep seperator 
  441. * @param string $string 
  442. * @return array 
  443. * @version 1.0 
  444. */ 
  445. public function explode_ws($sep, $string) { 
  446. $status = 'st'; 
  447. $to = ''; 
  448.  
  449. $output = array(); 
  450. $num = 0; 
  451. for ($i = 0, $len = strlen($string); $i < $len; $i++) { 
  452. switch ($status) { 
  453. case 'st': 
  454. if ($string{$i} == $sep && !$this->parser->escaped($string, $i)) { 
  455. ++$num; 
  456. } elseif ($string{$i} === '"' || $string{$i} === '\'' || $string{$i} === '(' && !$this->parser->escaped($string, $i)) { 
  457. $status = 'str'; 
  458. $to = ($string{$i} === '(') ? ')' : $string{$i}; 
  459. (isset($output[$num])) ? $output[$num] .= $string{$i} : $output[$num] = $string{$i}; 
  460. } else { 
  461. (isset($output[$num])) ? $output[$num] .= $string{$i} : $output[$num] = $string{$i}; 
  462. break; 
  463.  
  464. case 'str': 
  465. if ($string{$i} == $to && !$this->parser->escaped($string, $i)) { 
  466. $status = 'st'; 
  467. (isset($output[$num])) ? $output[$num] .= $string{$i} : $output[$num] = $string{$i}; 
  468. break; 
  469.  
  470. if (isset($output[0])) { 
  471. return $output; 
  472. } else { 
  473. return array($output); 
  474.  
  475. /** 
  476. * Merges Shorthand properties again, the opposite of dissolve_4value_shorthands() 
  477. * @param array $array 
  478. * @return array 
  479. * @version 1.2 
  480. * @see dissolve_4value_shorthands() 
  481. */ 
  482. public function merge_4value_shorthands($array) { 
  483. $return = $array; 
  484. $shorthands = & $this->parser->data['csstidy']['shorthands']; 
  485.  
  486. foreach ($shorthands as $key => $value) { 
  487. if (isset($array[$value[0]]) && isset($array[$value[1]]) 
  488. && isset($array[$value[2]]) && isset($array[$value[3]]) && $value !== 0) { 
  489. $return[$key] = ''; 
  490.  
  491. $important = ''; 
  492. for ($i = 0; $i < 4; $i++) { 
  493. $val = $array[$value[$i]]; 
  494. if ($this->parser->is_important($val)) { 
  495. $important = '!important'; 
  496. $return[$key] .= $this->parser->gvw_important($val) . ' '; 
  497. } else { 
  498. $return[$key] .= $val . ' '; 
  499. unset($return[$value[$i]]); 
  500. $return[$key] = $this->shorthand(trim($return[$key] . $important)); 
  501. return $return; 
  502.  
  503. /** 
  504. * Dissolve background property 
  505. * @param string $str_value 
  506. * @return array 
  507. * @version 1.0 
  508. * @see merge_bg() 
  509. * @todo full CSS 3 compliance 
  510. */ 
  511. public function dissolve_short_bg($str_value) { 
  512. // don't try to explose background gradient ! 
  513. if (stripos($str_value, 'gradient(')!== false) 
  514. return array('background'=>$str_value); 
  515.  
  516. $background_prop_default = & $this->parser->data['csstidy']['background_prop_default']; 
  517. $repeat = array('repeat', 'repeat-x', 'repeat-y', 'no-repeat', 'space'); 
  518. $attachment = array('scroll', 'fixed', 'local'); 
  519. $clip = array('border', 'padding'); 
  520. $origin = array('border', 'padding', 'content'); 
  521. $pos = array('top', 'center', 'bottom', 'left', 'right'); 
  522. $important = ''; 
  523. $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); 
  524.  
  525. if ($this->parser->is_important($str_value)) { 
  526. $important = ' !important'; 
  527. $str_value = $this->parser->gvw_important($str_value); 
  528.  
  529. $str_value = $this->explode_ws(', ', $str_value); 
  530. for ($i = 0; $i < count($str_value); $i++) { 
  531. $have['clip'] = false; 
  532. $have['pos'] = false; 
  533. $have['color'] = false; 
  534. $have['bg'] = false; 
  535.  
  536. if (is_array($str_value[$i])) { 
  537. $str_value[$i] = $str_value[$i][0]; 
  538. $str_value[$i] = $this->explode_ws(' ', trim($str_value[$i])); 
  539.  
  540. for ($j = 0; $j < count($str_value[$i]); $j++) { 
  541. if ($have['bg'] === false && (substr($str_value[$i][$j], 0, 4) === 'url(' || $str_value[$i][$j] === 'none')) { 
  542. $return['background-image'] .= $str_value[$i][$j] . ', '; 
  543. $have['bg'] = true; 
  544. } elseif (in_array($str_value[$i][$j], $repeat, true)) { 
  545. $return['background-repeat'] .= $str_value[$i][$j] . ', '; 
  546. } elseif (in_array($str_value[$i][$j], $attachment, true)) { 
  547. $return['background-attachment'] .= $str_value[$i][$j] . ', '; 
  548. } elseif (in_array($str_value[$i][$j], $clip, true) && !$have['clip']) { 
  549. $return['background-clip'] .= $str_value[$i][$j] . ', '; 
  550. $have['clip'] = true; 
  551. } elseif (in_array($str_value[$i][$j], $origin, true)) { 
  552. $return['background-origin'] .= $str_value[$i][$j] . ', '; 
  553. } elseif ($str_value[$i][$j]{0} === '(') { 
  554. $return['background-size'] .= substr($str_value[$i][$j], 1, -1) . ', '; 
  555. } 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} === '.') { 
  556. $return['background-position'] .= $str_value[$i][$j]; 
  557. if (!$have['pos']) 
  558. $return['background-position'] .= ' '; else 
  559. $return['background-position'].= ', '; 
  560. $have['pos'] = true; 
  561. } elseif (!$have['color']) { 
  562. $return['background-color'] .= $str_value[$i][$j] . ', '; 
  563. $have['color'] = true; 
  564.  
  565. foreach ($background_prop_default as $bg_prop => $default_value) { 
  566. if ($return[$bg_prop] !== null) { 
  567. $return[$bg_prop] = substr($return[$bg_prop], 0, -1) . $important; 
  568. else 
  569. $return[$bg_prop] = $default_value . $important; 
  570. return $return; 
  571.  
  572. /** 
  573. * Merges all background properties 
  574. * @param array $input_css 
  575. * @return array 
  576. * @version 1.0 
  577. * @see dissolve_short_bg() 
  578. * @todo full CSS 3 compliance 
  579. */ 
  580. public function merge_bg($input_css) { 
  581. $background_prop_default = & $this->parser->data['csstidy']['background_prop_default']; 
  582. // Max number of background images. CSS3 not yet fully implemented 
  583. $number_of_values = @max(count($this->explode_ws(', ', $input_css['background-image'])), count($this->explode_ws(', ', $input_css['background-color'])), 1); 
  584. // Array with background images to check if BG image exists 
  585. $bg_img_array = @$this->explode_ws(', ', $this->parser->gvw_important($input_css['background-image'])); 
  586. $new_bg_value = ''; 
  587. $important = ''; 
  588.  
  589. // if background properties is here and not empty, don't try anything 
  590. if (isset($input_css['background']) && $input_css['background']) 
  591. return $input_css; 
  592.  
  593. for ($i = 0; $i < $number_of_values; $i++) { 
  594. foreach ($background_prop_default as $bg_property => $default_value) { 
  595. // Skip if property does not exist 
  596. if (!isset($input_css[$bg_property])) { 
  597. continue; 
  598.  
  599. $cur_value = $input_css[$bg_property]; 
  600. // skip all optimisation if gradient() somewhere 
  601. if (stripos($cur_value, 'gradient(') !== false) 
  602. return $input_css; 
  603.  
  604. // Skip some properties if there is no background image 
  605. if ((!isset($bg_img_array[$i]) || $bg_img_array[$i] === 'none') 
  606. && ($bg_property === 'background-size' || $bg_property === 'background-position' 
  607. || $bg_property === 'background-attachment' || $bg_property === 'background-repeat')) { 
  608. continue; 
  609.  
  610. // Remove !important 
  611. if ($this->parser->is_important($cur_value)) { 
  612. $important = ' !important'; 
  613. $cur_value = $this->parser->gvw_important($cur_value); 
  614.  
  615. // Do not add default values 
  616. if ($cur_value === $default_value) { 
  617. continue; 
  618.  
  619. $temp = $this->explode_ws(', ', $cur_value); 
  620.  
  621. if (isset($temp[$i])) { 
  622. if ($bg_property === 'background-size') { 
  623. $new_bg_value .= '(' . $temp[$i] . ') '; 
  624. } else { 
  625. $new_bg_value .= $temp[$i] . ' '; 
  626.  
  627. $new_bg_value = trim($new_bg_value); 
  628. if ($i != $number_of_values - 1) 
  629. $new_bg_value .= ', '; 
  630.  
  631. // Delete all background-properties 
  632. foreach ($background_prop_default as $bg_property => $default_value) { 
  633. unset($input_css[$bg_property]); 
  634.  
  635. // Add new background property 
  636. if ($new_bg_value !== '') 
  637. $input_css['background'] = $new_bg_value . $important; 
  638. elseif(isset ($input_css['background'])) 
  639. $input_css['background'] = 'none'; 
  640.  
  641. return $input_css; 
  642.  
  643. /** 
  644. * Dissolve font property 
  645. * @param string $str_value 
  646. * @return array 
  647. * @version 1.3 
  648. * @see merge_font() 
  649. */ 
  650. public function dissolve_short_font($str_value) { 
  651. $font_prop_default = & $this->parser->data['csstidy']['font_prop_default']; 
  652. $font_weight = array('normal', 'bold', 'bolder', 'lighter', 100, 200, 300, 400, 500, 600, 700, 800, 900); 
  653. $font_variant = array('normal', 'small-caps'); 
  654. $font_style = array('normal', 'italic', 'oblique'); 
  655. $important = ''; 
  656. $return = array('font-style' => null, 'font-variant' => null, 'font-weight' => null, 'font-size' => null, 'line-height' => null, 'font-family' => null); 
  657.  
  658. if ($this->parser->is_important($str_value)) { 
  659. $important = '!important'; 
  660. $str_value = $this->parser->gvw_important($str_value); 
  661.  
  662. $have['style'] = false; 
  663. $have['variant'] = false; 
  664. $have['weight'] = false; 
  665. $have['size'] = false; 
  666. // Detects if font-family consists of several words w/o quotes 
  667. $multiwords = false; 
  668.  
  669. // Workaround with multiple font-family 
  670. $str_value = $this->explode_ws(', ', trim($str_value)); 
  671.  
  672. $str_value[0] = $this->explode_ws(' ', trim($str_value[0])); 
  673.  
  674. for ($j = 0; $j < count($str_value[0]); $j++) { 
  675. if ($have['weight'] === false && in_array($str_value[0][$j], $font_weight)) { 
  676. $return['font-weight'] = $str_value[0][$j]; 
  677. $have['weight'] = true; 
  678. } elseif ($have['variant'] === false && in_array($str_value[0][$j], $font_variant)) { 
  679. $return['font-variant'] = $str_value[0][$j]; 
  680. $have['variant'] = true; 
  681. } elseif ($have['style'] === false && in_array($str_value[0][$j], $font_style)) { 
  682. $return['font-style'] = $str_value[0][$j]; 
  683. $have['style'] = true; 
  684. } elseif ($have['size'] === false && (is_numeric($str_value[0][$j]{0}) || $str_value[0][$j]{0} === null || $str_value[0][$j]{0} === '.')) { 
  685. $size = $this->explode_ws('/', trim($str_value[0][$j])); 
  686. $return['font-size'] = $size[0]; 
  687. if (isset($size[1])) { 
  688. $return['line-height'] = $size[1]; 
  689. } else { 
  690. $return['line-height'] = ''; // don't add 'normal' ! 
  691. $have['size'] = true; 
  692. } else { 
  693. if (isset($return['font-family'])) { 
  694. $return['font-family'] .= ' ' . $str_value[0][$j]; 
  695. $multiwords = true; 
  696. } else { 
  697. $return['font-family'] = $str_value[0][$j]; 
  698. // add quotes if we have several qords in font-family 
  699. if ($multiwords !== false) { 
  700. $return['font-family'] = '"' . $return['font-family'] . '"'; 
  701. $i = 1; 
  702. while (isset($str_value[$i])) { 
  703. $return['font-family'] .= ', ' . trim($str_value[$i]); 
  704. $i++; 
  705.  
  706. // Fix for 100 and more font-size 
  707. if ($have['size'] === false && isset($return['font-weight']) && 
  708. is_numeric($return['font-weight']{0})) { 
  709. $return['font-size'] = $return['font-weight']; 
  710. unset($return['font-weight']); 
  711.  
  712. foreach ($font_prop_default as $font_prop => $default_value) { 
  713. if ($return[$font_prop] !== null) { 
  714. $return[$font_prop] = $return[$font_prop] . $important; 
  715. else 
  716. $return[$font_prop] = $default_value . $important; 
  717. return $return; 
  718.  
  719. /** 
  720. * Merges all fonts properties 
  721. * @param array $input_css 
  722. * @return array 
  723. * @version 1.3 
  724. * @see dissolve_short_font() 
  725. */ 
  726. public function merge_font($input_css) { 
  727. $font_prop_default = & $this->parser->data['csstidy']['font_prop_default']; 
  728. $new_font_value = ''; 
  729. $important = ''; 
  730. // Skip if not font-family and font-size set 
  731. if (isset($input_css['font-family']) && isset($input_css['font-size']) && $input_css['font-family'] != 'inherit') { 
  732. // fix several words in font-family - add quotes 
  733. if (isset($input_css['font-family'])) { 
  734. $families = explode(', ', $input_css['font-family']); 
  735. $result_families = array(); 
  736. foreach ($families as $family) { 
  737. $family = trim($family); 
  738. $len = strlen($family); 
  739. if (strpos($family, ' ') && 
  740. !(($family{0} === '"' && $family{$len - 1} === '"') || 
  741. ($family{0} === "'" && $family{$len - 1} === "'"))) { 
  742. $family = '"' . $family . '"'; 
  743. $result_families[] = $family; 
  744. $input_css['font-family'] = implode(', ', $result_families); 
  745. foreach ($font_prop_default as $font_property => $default_value) { 
  746.  
  747. // Skip if property does not exist 
  748. if (!isset($input_css[$font_property])) { 
  749. continue; 
  750.  
  751. $cur_value = $input_css[$font_property]; 
  752.  
  753. // Skip if default value is used 
  754. if ($cur_value === $default_value) { 
  755. continue; 
  756.  
  757. // Remove !important 
  758. if ($this->parser->is_important($cur_value)) { 
  759. $important = '!important'; 
  760. $cur_value = $this->parser->gvw_important($cur_value); 
  761.  
  762. $new_font_value .= $cur_value; 
  763. // Add delimiter 
  764. $new_font_value .= ( $font_property === 'font-size' && 
  765. isset($input_css['line-height'])) ? '/' : ' '; 
  766.  
  767. $new_font_value = trim($new_font_value); 
  768.  
  769. // Delete all font-properties 
  770. foreach ($font_prop_default as $font_property => $default_value) { 
  771. if ($font_property !== 'font' || !$new_font_value) 
  772. unset($input_css[$font_property]); 
  773.  
  774. // Add new font property 
  775. if ($new_font_value !== '') { 
  776. $input_css['font'] = $new_font_value . $important; 
  777.  
  778. return $input_css; 
  779.