class.csstidy_optimise.php 28 KB

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