Brak opisu

class.csstidy_optimise.php 28KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944
  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 <https://www.gnu.org/licenses/>.
  24. *
  25. * @license https://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] = (float) $string;
  359. if (abs($return[0]) > 0 && abs($return[0]) < 1) {
  360. // Removes the initial `0` from a decimal number, e.g., `0.7 => .7` or `-0.666 => -.666`.
  361. if ( ! $this->parser->get_cfg( 'preserve_leading_zeros' ) ) {
  362. if ( $return[0] < 0 ) {
  363. $return[0] = '-' . ltrim( substr( $return[0], 1 ), '0' );
  364. } else {
  365. $return[0] = ltrim( $return[0], '0' );
  366. }
  367. }
  368. }
  369. // Look for unit and split from value if exists
  370. foreach ($units as $unit) {
  371. $expectUnitAt = strlen($string) - strlen($unit);
  372. if (!($unitInString = stristr($string, $unit))) { // mb_strpos() fails with "false"
  373. continue;
  374. }
  375. $actualPosition = strpos($string, $unitInString);
  376. if ($expectUnitAt === $actualPosition) {
  377. $return[1] = $unit;
  378. $string = substr($string, 0, - strlen($unit));
  379. break;
  380. }
  381. }
  382. if (!is_numeric($string)) {
  383. return false;
  384. }
  385. return $return;
  386. }
  387. /**
  388. * Merges selectors with same properties. Example: a{color:red} b{color:red} -> a,b{color:red}
  389. * Very basic and has at least one bug. Hopefully there is a replacement soon.
  390. * @param array $array
  391. * @return array
  392. * @access public
  393. * @version 1.2
  394. */
  395. function merge_selectors(&$array) {
  396. $css = $array;
  397. foreach ($css as $key => $value) {
  398. if (!isset($css[$key])) {
  399. continue;
  400. }
  401. $newsel = '';
  402. // Check if properties also exist in another selector
  403. $keys = array();
  404. // PHP bug (?) without $css = $array; here
  405. foreach ($css as $selector => $vali) {
  406. if ($selector == $key) {
  407. continue;
  408. }
  409. if ($css[$key] === $vali) {
  410. $keys[] = $selector;
  411. }
  412. }
  413. if (!empty($keys)) {
  414. $newsel = $key;
  415. unset($css[$key]);
  416. foreach ($keys as $selector) {
  417. unset($css[$selector]);
  418. $newsel .= ',' . $selector;
  419. }
  420. $css[$newsel] = $value;
  421. }
  422. }
  423. $array = $css;
  424. }
  425. /**
  426. * Removes invalid selectors and their corresponding rule-sets as
  427. * defined by 4.1.7 in REC-CSS2. This is a very rudimentary check
  428. * and should be replaced by a full-blown parsing algorithm or
  429. * regular expression
  430. * @version 1.4
  431. */
  432. function discard_invalid_selectors(&$array) {
  433. $invalid = array('+' => true, '~' => true, ',' => true, '>' => true);
  434. foreach ($array as $selector => $decls) {
  435. $ok = true;
  436. $selectors = array_map('trim', explode(',', $selector));
  437. foreach ($selectors as $s) {
  438. $simple_selectors = preg_split('/\s*[+>~\s]\s*/', $s);
  439. foreach ($simple_selectors as $ss) {
  440. if ($ss === '')
  441. $ok = false;
  442. // could also check $ss for internal structure,
  443. // but that probably would be too slow
  444. }
  445. }
  446. if (!$ok)
  447. unset($array[$selector]);
  448. }
  449. }
  450. /**
  451. * Dissolves properties like padding:10px 10px 10px to padding-top:10px;padding-bottom:10px;...
  452. * @param string $property
  453. * @param string $value
  454. * @return array
  455. * @version 1.0
  456. * @see merge_4value_shorthands()
  457. */
  458. static function dissolve_4value_shorthands($property, $value) {
  459. $shorthands = & $GLOBALS['csstidy']['shorthands'];
  460. if (!is_array($shorthands[$property])) {
  461. $return[$property] = $value;
  462. return $return;
  463. }
  464. $important = '';
  465. if (csstidy::is_important($value)) {
  466. $value = csstidy::gvw_important($value);
  467. $important = '!important';
  468. }
  469. $values = explode(' ', $value);
  470. $return = array();
  471. if (count($values) == 4) {
  472. for ($i = 0; $i < 4; $i++) {
  473. $return[$shorthands[$property][$i]] = $values[$i] . $important;
  474. }
  475. } elseif (count($values) == 3) {
  476. $return[$shorthands[$property][0]] = $values[0] . $important;
  477. $return[$shorthands[$property][1]] = $values[1] . $important;
  478. $return[$shorthands[$property][3]] = $values[1] . $important;
  479. $return[$shorthands[$property][2]] = $values[2] . $important;
  480. } elseif (count($values) == 2) {
  481. for ($i = 0; $i < 4; $i++) {
  482. $return[$shorthands[$property][$i]] = (($i % 2 != 0)) ? $values[1] . $important : $values[0] . $important;
  483. }
  484. } else {
  485. for ($i = 0; $i < 4; $i++) {
  486. $return[$shorthands[$property][$i]] = $values[0] . $important;
  487. }
  488. }
  489. return $return;
  490. }
  491. /**
  492. * Explodes a string as explode() does, however, not if $sep is escaped or within a string.
  493. * @param string $sep seperator
  494. * @param string $string
  495. * @return array
  496. * @version 1.0
  497. */
  498. static function explode_ws($sep, $string) {
  499. $status = 'st';
  500. $to = '';
  501. $output = array();
  502. $num = 0;
  503. for ($i = 0, $len = strlen($string); $i < $len; $i++) {
  504. switch ($status) {
  505. case 'st':
  506. if ($string[$i] == $sep && !csstidy::escaped($string, $i)) {
  507. ++$num;
  508. } elseif ($string[$i] === '"' || $string[$i] === '\'' || $string[$i] === '(' && !csstidy::escaped($string, $i)) {
  509. $status = 'str';
  510. $to = ($string[$i] === '(') ? ')' : $string[$i];
  511. (isset($output[$num])) ? $output[$num] .= $string[$i] : $output[$num] = $string[$i];
  512. } else {
  513. (isset($output[$num])) ? $output[$num] .= $string[$i] : $output[$num] = $string[$i];
  514. }
  515. break;
  516. case 'str':
  517. if ($string[$i] == $to && !csstidy::escaped($string, $i)) {
  518. $status = 'st';
  519. }
  520. (isset($output[$num])) ? $output[$num] .= $string[$i] : $output[$num] = $string[$i];
  521. break;
  522. }
  523. }
  524. if (isset($output[0])) {
  525. return $output;
  526. } else {
  527. return array($output);
  528. }
  529. }
  530. /**
  531. * Merges Shorthand properties again, the opposite of dissolve_4value_shorthands()
  532. * @param array $array
  533. * @return array
  534. * @version 1.2
  535. * @see dissolve_4value_shorthands()
  536. */
  537. static function merge_4value_shorthands($array) {
  538. $return = $array;
  539. $shorthands = & $GLOBALS['csstidy']['shorthands'];
  540. foreach ($shorthands as $key => $value) {
  541. if (isset($array[$value[0]]) && isset($array[$value[1]])
  542. && isset($array[$value[2]]) && isset($array[$value[3]]) && $value !== 0) {
  543. $return[$key] = '';
  544. $important = '';
  545. for ($i = 0; $i < 4; $i++) {
  546. $val = $array[$value[$i]];
  547. if (csstidy::is_important($val)) {
  548. $important = '!important';
  549. $return[$key] .= csstidy::gvw_important($val) . ' ';
  550. } else {
  551. $return[$key] .= $val . ' ';
  552. }
  553. unset($return[$value[$i]]);
  554. }
  555. $return[$key] = csstidy_optimise::shorthand(trim($return[$key] . $important));
  556. }
  557. }
  558. return $return;
  559. }
  560. /**
  561. * Dissolve background property
  562. * @param string $str_value
  563. * @return array
  564. * @version 1.0
  565. * @see merge_bg()
  566. * @todo full CSS 3 compliance
  567. */
  568. static function dissolve_short_bg($str_value) {
  569. $have = array();
  570. // don't try to explose background gradient !
  571. if (stripos($str_value, "gradient(")!==FALSE)
  572. return array('background'=>$str_value);
  573. $background_prop_default = & $GLOBALS['csstidy']['background_prop_default'];
  574. $repeat = array('repeat', 'repeat-x', 'repeat-y', 'no-repeat', 'space');
  575. $attachment = array('scroll', 'fixed', 'local');
  576. $clip = array('border', 'padding');
  577. $origin = array('border', 'padding', 'content');
  578. $pos = array('top', 'center', 'bottom', 'left', 'right');
  579. $important = '';
  580. $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);
  581. if (csstidy::is_important($str_value)) {
  582. $important = ' !important';
  583. $str_value = csstidy::gvw_important($str_value);
  584. }
  585. $str_value = csstidy_optimise::explode_ws(',', $str_value);
  586. for ($i = 0; $i < count($str_value); $i++) {
  587. $have['clip'] = false;
  588. $have['pos'] = false;
  589. $have['color'] = false;
  590. $have['bg'] = false;
  591. if (is_array($str_value[$i])) {
  592. $str_value[$i] = $str_value[$i][0];
  593. }
  594. $str_value[$i] = csstidy_optimise::explode_ws(' ', trim($str_value[$i]));
  595. for ($j = 0; $j < count($str_value[$i]); $j++) {
  596. if ($have['bg'] === false && (substr($str_value[$i][$j], 0, 4) === 'url(' || $str_value[$i][$j] === 'none')) {
  597. $return['background-image'] .= $str_value[$i][$j] . ',';
  598. $have['bg'] = true;
  599. } elseif (in_array($str_value[$i][$j], $repeat, true)) {
  600. $return['background-repeat'] .= $str_value[$i][$j] . ',';
  601. } elseif (in_array($str_value[$i][$j], $attachment, true)) {
  602. $return['background-attachment'] .= $str_value[$i][$j] . ',';
  603. } elseif (in_array($str_value[$i][$j], $clip, true) && !$have['clip']) {
  604. $return['background-clip'] .= $str_value[$i][$j] . ',';
  605. $have['clip'] = true;
  606. } elseif (in_array($str_value[$i][$j], $origin, true)) {
  607. $return['background-origin'] .= $str_value[$i][$j] . ',';
  608. } elseif ($str_value[$i][$j][0] === '(') {
  609. $return['background-size'] .= substr($str_value[$i][$j], 1, -1) . ',';
  610. } 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] === '.') {
  611. $return['background-position'] .= $str_value[$i][$j];
  612. if (!$have['pos'])
  613. $return['background-position'] .= ' '; else
  614. $return['background-position'].= ',';
  615. $have['pos'] = true;
  616. }
  617. elseif (!$have['color']) {
  618. $return['background-color'] .= $str_value[$i][$j] . ',';
  619. $have['color'] = true;
  620. }
  621. }
  622. }
  623. foreach ($background_prop_default as $bg_prop => $default_value) {
  624. if ($return[$bg_prop] !== null) {
  625. $return[$bg_prop] = substr($return[$bg_prop], 0, -1) . $important;
  626. }
  627. else
  628. $return[$bg_prop] = $default_value . $important;
  629. }
  630. return $return;
  631. }
  632. /**
  633. * Merges all background properties
  634. * @param array $input_css
  635. * @return array
  636. * @version 1.0
  637. * @see dissolve_short_bg()
  638. * @todo full CSS 3 compliance
  639. */
  640. static function merge_bg($input_css) {
  641. $background_prop_default = & $GLOBALS['csstidy']['background_prop_default'];
  642. // Max number of background images. CSS3 not yet fully implemented
  643. $number_of_values = @max(count(csstidy_optimise::explode_ws(',', $input_css['background-image'])), count(csstidy_optimise::explode_ws(',', $input_css['background-color'])), 1);
  644. // Array with background images to check if BG image exists
  645. $bg_img_array = @csstidy_optimise::explode_ws(',', csstidy::gvw_important($input_css['background-image']));
  646. $new_bg_value = '';
  647. $important = '';
  648. // if background properties is here and not empty, don't try anything
  649. if (isset($input_css['background']) AND $input_css['background'])
  650. return $input_css;
  651. for ($i = 0; $i < $number_of_values; $i++) {
  652. foreach ($background_prop_default as $bg_property => $default_value) {
  653. // Skip if property does not exist
  654. if (!isset($input_css[$bg_property])) {
  655. continue;
  656. }
  657. $cur_value = $input_css[$bg_property];
  658. // skip all optimisation if gradient() somewhere
  659. if (stripos($cur_value, "gradient(")!==FALSE)
  660. return $input_css;
  661. // Skip some properties if there is no background image
  662. if ((!isset($bg_img_array[$i]) || $bg_img_array[$i] === 'none')
  663. && ($bg_property === 'background-size' || $bg_property === 'background-position'
  664. || $bg_property === 'background-attachment' || $bg_property === 'background-repeat')) {
  665. continue;
  666. }
  667. // Remove !important
  668. if (csstidy::is_important($cur_value)) {
  669. $important = ' !important';
  670. $cur_value = csstidy::gvw_important($cur_value);
  671. }
  672. // Do not add default values
  673. if ($cur_value === $default_value) {
  674. continue;
  675. }
  676. $temp = csstidy_optimise::explode_ws(',', $cur_value);
  677. if (isset($temp[$i])) {
  678. if ($bg_property === 'background-size') {
  679. $new_bg_value .= '(' . $temp[$i] . ') ';
  680. } else {
  681. $new_bg_value .= $temp[$i] . ' ';
  682. }
  683. }
  684. }
  685. $new_bg_value = trim($new_bg_value);
  686. if ($i != $number_of_values - 1)
  687. $new_bg_value .= ',';
  688. }
  689. // Delete all background-properties
  690. foreach ($background_prop_default as $bg_property => $default_value) {
  691. unset($input_css[$bg_property]);
  692. }
  693. // Add new background property
  694. if ($new_bg_value !== '')
  695. $input_css['background'] = $new_bg_value . $important;
  696. elseif(isset ($input_css['background']))
  697. $input_css['background'] = 'none';
  698. return $input_css;
  699. }
  700. /**
  701. * Dissolve font property
  702. * @param string $str_value
  703. * @return array
  704. * @version 1.3
  705. * @see merge_font()
  706. */
  707. static function dissolve_short_font($str_value) {
  708. $have = array();
  709. $font_prop_default = & $GLOBALS['csstidy']['font_prop_default'];
  710. $font_weight = array('normal', 'bold', 'bolder', 'lighter', 100, 200, 300, 400, 500, 600, 700, 800, 900);
  711. $font_variant = array('normal', 'small-caps');
  712. $font_style = array('normal', 'italic', 'oblique');
  713. $important = '';
  714. $return = array('font-style' => null, 'font-variant' => null, 'font-weight' => null, 'font-size' => null, 'line-height' => null, 'font-family' => null);
  715. if (csstidy::is_important($str_value)) {
  716. $important = '!important';
  717. $str_value = csstidy::gvw_important($str_value);
  718. }
  719. $have['style'] = false;
  720. $have['variant'] = false;
  721. $have['weight'] = false;
  722. $have['size'] = false;
  723. // Detects if font-family consists of several words w/o quotes
  724. $multiwords = false;
  725. // Workaround with multiple font-family
  726. $str_value = csstidy_optimise::explode_ws(',', trim($str_value));
  727. $str_value[0] = csstidy_optimise::explode_ws(' ', trim($str_value[0]));
  728. for ($j = 0; $j < count($str_value[0]); $j++) {
  729. if ($have['weight'] === false && in_array($str_value[0][$j], $font_weight)) {
  730. $return['font-weight'] = $str_value[0][$j];
  731. $have['weight'] = true;
  732. } elseif ($have['variant'] === false && in_array($str_value[0][$j], $font_variant)) {
  733. $return['font-variant'] = $str_value[0][$j];
  734. $have['variant'] = true;
  735. } elseif ($have['style'] === false && in_array($str_value[0][$j], $font_style)) {
  736. $return['font-style'] = $str_value[0][$j];
  737. $have['style'] = true;
  738. } elseif ($have['size'] === false && (is_numeric($str_value[0][$j][0]) || $str_value[0][$j][0] === null || $str_value[0][$j][0] === '.')) {
  739. $size = csstidy_optimise::explode_ws('/', trim($str_value[0][$j]));
  740. $return['font-size'] = $size[0];
  741. if (isset($size[1])) {
  742. $return['line-height'] = $size[1];
  743. } else {
  744. $return['line-height'] = ''; // don't add 'normal' !
  745. }
  746. $have['size'] = true;
  747. } else {
  748. if (isset($return['font-family'])) {
  749. $return['font-family'] .= ' ' . $str_value[0][$j];
  750. $multiwords = true;
  751. } else {
  752. $return['font-family'] = $str_value[0][$j];
  753. }
  754. }
  755. }
  756. // add quotes if we have several qords in font-family
  757. if ($multiwords !== false) {
  758. $return['font-family'] = '"' . $return['font-family'] . '"';
  759. }
  760. $i = 1;
  761. while (isset($str_value[$i])) {
  762. $return['font-family'] .= ',' . trim($str_value[$i]);
  763. $i++;
  764. }
  765. // Fix for 100 and more font-size
  766. if ($have['size'] === false && isset($return['font-weight']) &&
  767. is_numeric($return['font-weight'][0])) {
  768. $return['font-size'] = $return['font-weight'];
  769. unset($return['font-weight']);
  770. }
  771. foreach ($font_prop_default as $font_prop => $default_value) {
  772. if ($return[$font_prop] !== null) {
  773. $return[$font_prop] = $return[$font_prop] . $important;
  774. }
  775. else
  776. $return[$font_prop] = $default_value . $important;
  777. }
  778. return $return;
  779. }
  780. /**
  781. * Merges all fonts properties
  782. * @param array $input_css
  783. * @return array
  784. * @version 1.3
  785. * @see dissolve_short_font()
  786. */
  787. static function merge_font($input_css) {
  788. $font_prop_default = & $GLOBALS['csstidy']['font_prop_default'];
  789. $new_font_value = '';
  790. $important = '';
  791. // Skip if not font-family and font-size set
  792. if (isset($input_css['font-family']) && isset($input_css['font-size'])) {
  793. // fix several words in font-family - add quotes
  794. if (isset($input_css['font-family'])) {
  795. $families = explode(",", $input_css['font-family']);
  796. $result_families = array();
  797. foreach ($families as $family) {
  798. $family = trim($family);
  799. $len = strlen($family);
  800. if (strpos($family, " ") &&
  801. !(($family[0] == '"' && $family[$len - 1] == '"') ||
  802. ($family[0] == "'" && $family[$len - 1] == "'"))) {
  803. $family = '"' . $family . '"';
  804. }
  805. $result_families[] = $family;
  806. }
  807. $input_css['font-family'] = implode(",", $result_families);
  808. }
  809. foreach ($font_prop_default as $font_property => $default_value) {
  810. // Skip if property does not exist
  811. if (!isset($input_css[$font_property])) {
  812. continue;
  813. }
  814. $cur_value = $input_css[$font_property];
  815. // Skip if default value is used
  816. if ($cur_value === $default_value) {
  817. continue;
  818. }
  819. // Remove !important
  820. if (csstidy::is_important($cur_value)) {
  821. $important = '!important';
  822. $cur_value = csstidy::gvw_important($cur_value);
  823. }
  824. $new_font_value .= $cur_value;
  825. // Add delimiter
  826. $new_font_value .= ( $font_property === 'font-size' &&
  827. isset($input_css['line-height'])) ? '/' : ' ';
  828. }
  829. $new_font_value = trim($new_font_value);
  830. // Delete all font-properties
  831. foreach ($font_prop_default as $font_property => $default_value) {
  832. if ($font_property!=='font' OR !$new_font_value)
  833. unset($input_css[$font_property]);
  834. }
  835. // Add new font property
  836. if ($new_font_value !== '') {
  837. $input_css['font'] = $new_font_value . $important;
  838. }
  839. }
  840. return $input_css;
  841. }
  842. }