X-Git-Url: http://git.cyclocoop.org/?p=ptitvelo%2Fweb%2Fwww.git;a=blobdiff_plain;f=www%2Fplugins-dist%2Fcompresseur%2Flib%2Fcsstidy%2FDocs%2F__filesource%2Ffsource_csstidy__class.csstidy_optimise.php.html;fp=www%2Fplugins-dist%2Fcompresseur%2Flib%2Fcsstidy%2FDocs%2F__filesource%2Ffsource_csstidy__class.csstidy_optimise.php.html;h=0000000000000000000000000000000000000000;hp=f13419ce0a1298bd1b5e153382776d4d024aad72;hb=e847eea4a82a7396dd0abf860f9b30d654f38629;hpb=d686c3e22e97dd46ef42bcd0f138c65083f8f232 diff --git a/www/plugins-dist/compresseur/lib/csstidy/Docs/__filesource/fsource_csstidy__class.csstidy_optimise.php.html b/www/plugins-dist/compresseur/lib/csstidy/Docs/__filesource/fsource_csstidy__class.csstidy_optimise.php.html deleted file mode 100644 index f13419c..0000000 --- a/www/plugins-dist/compresseur/lib/csstidy/Docs/__filesource/fsource_csstidy__class.csstidy_optimise.php.html +++ /dev/null @@ -1,851 +0,0 @@ - - -File Source for class.csstidy_optimise.php - - - - - - - - - - - - - -
csstidy
- [ class tree: csstidy ] - [ index: csstidy ] - [ all elements ] -
- - - - - - -
-
- -

Source for file class.csstidy_optimise.php

-

Documentation is available at class.csstidy_optimise.php

-
-
  1. <?php
  2. -
  3. /**
  4. -
  5. * CSSTidy - CSS Parser and Optimiser
  6. -
  7. *
  8. -
  9. * CSS Optimising Class
  10. -
  11. * This class optimises CSS data generated by csstidy.
  12. -
  13. *
  14. -
  15. * This file is part of CSSTidy.
  16. -
  17. *
  18. -
  19. * CSSTidy is free software; you can redistribute it and/or modify
  20. -
  21. * it under the terms of the GNU General Public License as published by
  22. -
  23. * the Free Software Foundation; either version 2 of the License, or
  24. -
  25. * (at your option) any later version.
  26. -
  27. *
  28. -
  29. * CSSTidy is distributed in the hope that it will be useful,
  30. -
  31. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  32. -
  33. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  34. -
  35. * GNU General Public License for more details.
  36. -
  37. *
  38. -
  39. * You should have received a copy of the GNU General Public License
  40. -
  41. * along with CSSTidy; if not, write to the Free Software
  42. -
  43. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  44. -
  45. *
  46. -
  47. * @license http://opensource.org/licenses/gpl-license.php GNU Public License
  48. -
  49. * @package csstidy
  50. -
  51. * @author Florian Schmitz (floele at gmail dot com) 2005-2006
  52. -
  53. */
  54. -
  55. -
  56. /**
  57. -
  58. * CSS Optimising Class
  59. -
  60. *
  61. -
  62. * This class optimises CSS data generated by csstidy.
  63. -
  64. *
  65. -
  66. * @package csstidy
  67. -
  68. * @author Florian Schmitz (floele at gmail dot com) 2005-2006
  69. -
  70. * @version 1.0
  71. -
  72. */
  73. -
  74. -
  75. class csstidy_optimise
  76. -
  77. {
  78. -
  79. /**
  80. -
  81. * Constructor
  82. -
  83. * @param array $css contains the class csstidy
  84. -
  85. * @access private
  86. -
  87. * @version 1.0
  88. -
  89. */
  90. -
  91. function csstidy_optimise(&$css)
  92. -
  93. {
  94. -
  95. $this->parser =& $css;
  96. -
  97. $this->css =& $css->css;
  98. -
  99. $this->sub_value =& $css->sub_value;
  100. -
  101. $this->at =& $css->at;
  102. -
  103. $this->selector =& $css->selector;
  104. -
  105. $this->property =& $css->property;
  106. -
  107. $this->value =& $css->value;
  108. -
  109. }
  110. -
  111.  
  112. -
  113. /**
  114. -
  115. * Optimises $css after parsing
  116. -
  117. * @access public
  118. -
  119. * @version 1.0
  120. -
  121. */
  122. -
  123. function postparse()
  124. -
  125. {
  126. -
  127. if ($this->parser->get_cfg('preserve_css')) {
  128. -
  129. return;
  130. -
  131. }
  132. -
  133. -
  134. if ($this->parser->get_cfg('merge_selectors') == 2)
  135. -
  136. {
  137. -
  138. foreach ($this->css as $medium => $value)
  139. -
  140. {
  141. -
  142. $this->merge_selectors($this->css[$medium]);
  143. -
  144. }
  145. -
  146. }
  147. -
  148.  
  149. -
  150. if ($this->parser->get_cfg('optimise_shorthands') > 0)
  151. -
  152. {
  153. -
  154. foreach ($this->css as $medium => $value)
  155. -
  156. {
  157. -
  158. foreach ($value as $selector => $value1)
  159. -
  160. {
  161. -
  162. $this->css[$medium][$selector] = csstidy_optimise::merge_4value_shorthands($this->css[$medium][$selector]);
  163. -
  164. -
  165. if ($this->parser->get_cfg('optimise_shorthands') < 2) {
  166. -
  167. continue;
  168. -
  169. }
  170. -
  171. -
  172. $this->css[$medium][$selector] = csstidy_optimise::merge_bg($this->css[$medium][$selector]);
  173. -
  174. if (empty($this->css[$medium][$selector])) {
  175. -
  176. unset($this->css[$medium][$selector]);
  177. -
  178. }
  179. -
  180. }
  181. -
  182. }
  183. -
  184. }
  185. -
  186. }
  187. -
  188.  
  189. -
  190. /**
  191. -
  192. * Optimises values
  193. -
  194. * @access public
  195. -
  196. * @version 1.0
  197. -
  198. */
  199. -
  200. function value()
  201. -
  202. {
  203. -
  204. $shorthands =& $GLOBALS['csstidy']['shorthands'];
  205. -
  206. -
  207. // optimise shorthand properties
  208. -
  209. if(isset($shorthands[$this->property]))
  210. -
  211. {
  212. -
  213. $temp = csstidy_optimise::shorthand($this->value); // FIXME - move
  214. -
  215. if($temp != $this->value)
  216. -
  217. {
  218. -
  219. $this->parser->log('Optimised shorthand notation ('.$this->property.'): Changed "'.$this->value.'" to "'.$temp.'"','Information');
  220. -
  221. }
  222. -
  223. $this->value = $temp;
  224. -
  225. }
  226. -
  227. -
  228. // Remove whitespace at ! important
  229. -
  230. if($this->value != $this->compress_important($this->value))
  231. -
  232. {
  233. -
  234. $this->parser->log('Optimised !important','Information');
  235. -
  236. }
  237. -
  238. }
  239. -
  240. -
  241. /**
  242. -
  243. * Optimises shorthands
  244. -
  245. * @access public
  246. -
  247. * @version 1.0
  248. -
  249. */
  250. -
  251. function shorthands()
  252. -
  253. {
  254. -
  255. $shorthands =& $GLOBALS['csstidy']['shorthands'];
  256. -
  257. -
  258. if(!$this->parser->get_cfg('optimise_shorthands') || $this->parser->get_cfg('preserve_css')) {
  259. -
  260. return;
  261. -
  262. }
  263. -
  264. -
  265. if($this->property == 'background' && $this->parser->get_cfg('optimise_shorthands') > 1)
  266. -
  267. {
  268. -
  269. unset($this->css[$this->at][$this->selector]['background']);
  270. -
  271. $this->parser->merge_css_blocks($this->at,$this->selector,csstidy_optimise::dissolve_short_bg($this->value));
  272. -
  273. }
  274. -
  275. if(isset($shorthands[$this->property]))
  276. -
  277. {
  278. -
  279. $this->parser->merge_css_blocks($this->at,$this->selector,csstidy_optimise::dissolve_4value_shorthands($this->property,$this->value));
  280. -
  281. if(is_array($shorthands[$this->property]))
  282. -
  283. {
  284. -
  285. unset($this->css[$this->at][$this->selector][$this->property]);
  286. -
  287. }
  288. -
  289. }
  290. -
  291. }
  292. -
  293. -
  294. /**
  295. -
  296. * Optimises a sub-value
  297. -
  298. * @access public
  299. -
  300. * @version 1.0
  301. -
  302. */
  303. -
  304. function subvalue()
  305. -
  306. {
  307. -
  308. $replace_colors =& $GLOBALS['csstidy']['replace_colors'];
  309. -
  310.  
  311. -
  312. $this->sub_value = trim($this->sub_value);
  313. -
  314. if($this->sub_value == '') // caution : '0'
  315. -
  316. {
  317. -
  318. return;
  319. -
  320. }
  321. -
  322. -
  323. // Compress font-weight
  324. -
  325. if($this->property == 'font-weight' && $this->parser->get_cfg('compress_font-weight'))
  326. -
  327. {
  328. -
  329. $important = '';
  330. -
  331. if(csstidy::is_important($this->sub_value))
  332. -
  333. {
  334. -
  335. $important = ' !important';
  336. -
  337. $this->sub_value = csstidy::gvw_important($this->sub_value);
  338. -
  339. }
  340. -
  341. if($this->sub_value == 'bold')
  342. -
  343. {
  344. -
  345. $this->sub_value = '700'.$important;
  346. -
  347. $this->parser->log('Optimised font-weight: Changed "bold" to "700"','Information');
  348. -
  349. }
  350. -
  351. else if($this->sub_value == 'normal')
  352. -
  353. {
  354. -
  355. $this->sub_value = '400'.$important;
  356. -
  357. $this->parser->log('Optimised font-weight: Changed "normal" to "400"','Information');
  358. -
  359. }
  360. -
  361. }
  362. -
  363. -
  364. $temp = $this->compress_numbers($this->sub_value);
  365. -
  366. if($temp != $this->sub_value)
  367. -
  368. {
  369. -
  370. if(strlen($temp) > strlen($this->sub_value)) {
  371. -
  372. $this->parser->log('Fixed invalid number: Changed "'.$this->sub_value.'" to "'.$temp.'"','Warning');
  373. -
  374. } else {
  375. -
  376. $this->parser->log('Optimised number: Changed "'.$this->sub_value.'" to "'.$temp.'"','Information');
  377. -
  378. }
  379. -
  380. $this->sub_value = $temp;
  381. -
  382. }
  383. -
  384. if($this->parser->get_cfg('compress_colors'))
  385. -
  386. {
  387. -
  388. $temp = $this->cut_color($this->sub_value);
  389. -
  390. if($temp !== $this->sub_value)
  391. -
  392. {
  393. -
  394. if(isset($replace_colors[$this->sub_value])) {
  395. -
  396. $this->parser->log('Fixed invalid color name: Changed "'.$this->sub_value.'" to "'.$temp.'"','Warning');
  397. -
  398. } else {
  399. -
  400. $this->parser->log('Optimised color: Changed "'.$this->sub_value.'" to "'.$temp.'"','Information');
  401. -
  402. }
  403. -
  404. $this->sub_value = $temp;
  405. -
  406. }
  407. -
  408. }
  409. -
  410. }
  411. -
  412. -
  413. /**
  414. -
  415. * Compresses shorthand values. Example: margin:1px 1px 1px 1px -> margin:1px
  416. -
  417. * @param string $value
  418. -
  419. * @access public
  420. -
  421. * @return string
  422. -
  423. * @version 1.0
  424. -
  425. */
  426. -
  427. function shorthand($value)
  428. -
  429. {
  430. -
  431. $important = '';
  432. -
  433. if(csstidy::is_important($value))
  434. -
  435. {
  436. -
  437. $values = csstidy::gvw_important($value);
  438. -
  439. $important = ' !important';
  440. -
  441. }
  442. -
  443. else $values = $value;
  444. -
  445. -
  446. $values = explode(' ',$values);
  447. -
  448. switch(count($values))
  449. -
  450. {
  451. -
  452. case 4:
  453. -
  454. if($values[0] == $values[1] && $values[0] == $values[2] && $values[0] == $values[3])
  455. -
  456. {
  457. -
  458. return $values[0].$important;
  459. -
  460. }
  461. -
  462. elseif($values[1] == $values[3] && $values[0] == $values[2])
  463. -
  464. {
  465. -
  466. return $values[0].' '.$values[1].$important;
  467. -
  468. }
  469. -
  470. elseif($values[1] == $values[3])
  471. -
  472. {
  473. -
  474. return $values[0].' '.$values[1].' '.$values[2].$important;
  475. -
  476. }
  477. -
  478. break;
  479. -
  480. -
  481. case 3:
  482. -
  483. if($values[0] == $values[1] && $values[0] == $values[2])
  484. -
  485. {
  486. -
  487. return $values[0].$important;
  488. -
  489. }
  490. -
  491. elseif($values[0] == $values[2])
  492. -
  493. {
  494. -
  495. return $values[0].' '.$values[1].$important;
  496. -
  497. }
  498. -
  499. break;
  500. -
  501. -
  502. case 2:
  503. -
  504. if($values[0] == $values[1])
  505. -
  506. {
  507. -
  508. return $values[0].$important;
  509. -
  510. }
  511. -
  512. break;
  513. -
  514. }
  515. -
  516. -
  517. return $value;
  518. -
  519. }
  520. -
  521.  
  522. -
  523. /**
  524. -
  525. * Removes unnecessary whitespace in ! important
  526. -
  527. * @param string $string
  528. -
  529. * @return string
  530. -
  531. * @access public
  532. -
  533. * @version 1.1
  534. -
  535. */
  536. -
  537. function compress_important(&$string)
  538. -
  539. {
  540. -
  541. if(csstidy::is_important($string))
  542. -
  543. {
  544. -
  545. $string = csstidy::gvw_important($string) . ' !important';
  546. -
  547. }
  548. -
  549. return $string;
  550. -
  551. }
  552. -
  553.  
  554. -
  555. /**
  556. -
  557. * Color compression function. Converts all rgb() values to #-values and uses the short-form if possible. Also replaces 4 color names by #-values.
  558. -
  559. * @param string $color
  560. -
  561. * @return string
  562. -
  563. * @version 1.1
  564. -
  565. */
  566. -
  567. function cut_color($color)
  568. -
  569. {
  570. -
  571. $replace_colors =& $GLOBALS['csstidy']['replace_colors'];
  572. -
  573. -
  574. // rgb(0,0,0) -> #000000 (or #000 in this case later)
  575. -
  576. if(strtolower(substr($color,0,4)) == 'rgb(')
  577. -
  578. {
  579. -
  580. $color_tmp = substr($color,4,strlen($color)-5);
  581. -
  582. $color_tmp = explode(',',$color_tmp);
  583. -
  584. for ( $i = 0; $i < count($color_tmp); $i++ )
  585. -
  586. {
  587. -
  588. $color_tmp[$i] = trim ($color_tmp[$i]);
  589. -
  590. if(substr($color_tmp[$i],-1) == '%')
  591. -
  592. {
  593. -
  594. $color_tmp[$i] = round((255*$color_tmp[$i])/100);
  595. -
  596. }
  597. -
  598. if($color_tmp[$i]>255) $color_tmp[$i] = 255;
  599. -
  600. }
  601. -
  602. $color = '#';
  603. -
  604. for ($i = 0; $i < 3; $i++ )
  605. -
  606. {
  607. -
  608. if($color_tmp[$i]<16) {
  609. -
  610. $color .= '0' . dechex($color_tmp[$i]);
  611. -
  612. } else {
  613. -
  614. $color .= dechex($color_tmp[$i]);
  615. -
  616. }
  617. -
  618. }
  619. -
  620. }
  621. -
  622. -
  623. // Fix bad color names
  624. -
  625. if(isset($replace_colors[strtolower($color)]))
  626. -
  627. {
  628. -
  629. $color = $replace_colors[strtolower($color)];
  630. -
  631. }
  632. -
  633. -
  634. // #aabbcc -> #abc
  635. -
  636. if(strlen($color) == 7)
  637. -
  638. {
  639. -
  640. $color_temp = strtolower($color);
  641. -
  642. if($color_temp{0} == '#' && $color_temp{1} == $color_temp{2} && $color_temp{3} == $color_temp{4} && $color_temp{5} == $color_temp{6})
  643. -
  644. {
  645. -
  646. $color = '#'.$color{1}.$color{3}.$color{5};
  647. -
  648. }
  649. -
  650. }
  651. -
  652. -
  653. switch(strtolower($color))
  654. -
  655. {
  656. -
  657. /* color name -> hex code */
  658. -
  659. case 'black': return '#000';
  660. -
  661. case 'fuchsia': return '#F0F';
  662. -
  663. case 'white': return '#FFF';
  664. -
  665. case 'yellow': return '#FF0';
  666. -
  667. -
  668. /* hex code -> color name */
  669. -
  670. case '#800000': return 'maroon';
  671. -
  672. case '#ffa500': return 'orange';
  673. -
  674. case '#808000': return 'olive';
  675. -
  676. case '#800080': return 'purple';
  677. -
  678. case '#008000': return 'green';
  679. -
  680. case '#000080': return 'navy';
  681. -
  682. case '#008080': return 'teal';
  683. -
  684. case '#c0c0c0': return 'silver';
  685. -
  686. case '#808080': return 'gray';
  687. -
  688. case '#f00': return 'red';
  689. -
  690. }
  691. -
  692.  
  693. -
  694. return $color;
  695. -
  696. }
  697. -
  698. -
  699. /**
  700. -
  701. * Compresses numbers (ie. 1.0 becomes 1 or 1.100 becomes 1.1 )
  702. -
  703. * @param string $subvalue
  704. -
  705. * @return string
  706. -
  707. * @version 1.2
  708. -
  709. */
  710. -
  711. function compress_numbers($subvalue)
  712. -
  713. {
  714. -
  715. $units =& $GLOBALS['csstidy']['units'];
  716. -
  717. $number_values =& $GLOBALS['csstidy']['number_values'];
  718. -
  719. $color_values =& $GLOBALS['csstidy']['color_values'];
  720. -
  721.  
  722. -
  723. // for font:1em/1em sans-serif...;
  724. -
  725. if($this->property == 'font')
  726. -
  727. {
  728. -
  729. $temp = explode('/',$subvalue);
  730. -
  731. }
  732. -
  733. else
  734. -
  735. {
  736. -
  737. $temp = array($subvalue);
  738. -
  739. }
  740. -
  741. for ($l = 0; $l < count($temp); $l++)
  742. -
  743. {
  744. -
  745. // continue if no numeric value
  746. -
  747. if (!(strlen($temp[$l]) > 0 && ( is_numeric($temp[$l]{0}) || $temp[$l]{0} == '+' || $temp[$l]{0} == '-' ) ))
  748. -
  749. {
  750. -
  751. continue;
  752. -
  753. }
  754. -
  755.  
  756. -
  757. // Fix bad colors
  758. -
  759. if (in_array($this->property, $color_values))
  760. -
  761. {
  762. -
  763. $temp[$l] = '#'.$temp[$l];
  764. -
  765. }
  766. -
  767. -
  768. if (floatval($temp[$l]) == 0)
  769. -
  770. {
  771. -
  772. $temp[$l] = '0';
  773. -
  774. }
  775. -
  776. else
  777. -
  778. {
  779. -
  780. $unit_found = FALSE;
  781. -
  782. for ($m = 0, $size_4 = count($units); $m < $size_4; $m++)
  783. -
  784. {
  785. -
  786. if (strpos(strtolower($temp[$l]),$units[$m]) !== FALSE)
  787. -
  788. {
  789. -
  790. $temp[$l] = floatval($temp[$l]).$units[$m];
  791. -
  792. $unit_found = TRUE;
  793. -
  794. break;
  795. -
  796. }
  797. -
  798. }
  799. -
  800. if (!$unit_found && !in_array($this->property,$number_values,TRUE))
  801. -
  802. {
  803. -
  804. $temp[$l] = floatval($temp[$l]).'px';
  805. -
  806. }
  807. -
  808. else if (!$unit_found)
  809. -
  810. {
  811. -
  812. $temp[$l] = floatval($temp[$l]);
  813. -
  814. }
  815. -
  816. }
  817. -
  818. }
  819. -
  820.  
  821. -
  822. return ((count($temp) > 1) ? $temp[0].'/'.$temp[1] : $temp[0]);
  823. -
  824. }
  825. -
  826.  
  827. -
  828. /**
  829. -
  830. * Merges selectors with same properties. Example: a{color:red} b{color:red} -> a,b{color:red}
  831. -
  832. * Very basic and has at least one bug. Hopefully there is a replacement soon.
  833. -
  834. * @param array $array
  835. -
  836. * @return array
  837. -
  838. * @access public
  839. -
  840. * @version 1.2
  841. -
  842. */
  843. -
  844. function merge_selectors(&$array)
  845. -
  846. {
  847. -
  848. $css = $array;
  849. -
  850. foreach($css as $key => $value)
  851. -
  852. {
  853. -
  854. if(!isset($css[$key]))
  855. -
  856. {
  857. -
  858. continue;
  859. -
  860. }
  861. -
  862. $newsel = '';
  863. -
  864. -
  865. // Check if properties also exist in another selector
  866. -
  867. $keys = array();
  868. -
  869. // PHP bug (?) without $css = $array; here
  870. -
  871. foreach($css as $selector => $vali)
  872. -
  873. {
  874. -
  875. if($selector == $key)
  876. -
  877. {
  878. -
  879. continue;
  880. -
  881. }
  882. -
  883. -
  884. if($css[$key] === $vali)
  885. -
  886. {
  887. -
  888. $keys[] = $selector;
  889. -
  890. }
  891. -
  892. }
  893. -
  894.  
  895. -
  896. if(!empty($keys))
  897. -
  898. {
  899. -
  900. $newsel = $key;
  901. -
  902. unset($css[$key]);
  903. -
  904. foreach($keys as $selector)
  905. -
  906. {
  907. -
  908. unset($css[$selector]);
  909. -
  910. $newsel .= ','.$selector;
  911. -
  912. }
  913. -
  914. $css[$newsel] = $value;
  915. -
  916. }
  917. -
  918. }
  919. -
  920. $array = $css;
  921. -
  922. }
  923. -
  924.  
  925. -
  926. /**
  927. -
  928. * Dissolves properties like padding:10px 10px 10px to padding-top:10px;padding-bottom:10px;...
  929. -
  930. * @param string $property
  931. -
  932. * @param string $value
  933. -
  934. * @return array
  935. -
  936. * @version 1.0
  937. -
  938. * @see merge_4value_shorthands()
  939. -
  940. */
  941. -
  942. function dissolve_4value_shorthands($property,$value)
  943. -
  944. {
  945. -
  946. $shorthands =& $GLOBALS['csstidy']['shorthands'];
  947. -
  948. if(!is_array($shorthands[$property]))
  949. -
  950. {
  951. -
  952. $return[$property] = $value;
  953. -
  954. return $return;
  955. -
  956. }
  957. -
  958. -
  959. $important = '';
  960. -
  961. if(csstidy::is_important($value))
  962. -
  963. {
  964. -
  965. $value = csstidy::gvw_important($value);
  966. -
  967. $important = ' !important';
  968. -
  969. }
  970. -
  971. $values = explode(' ',$value);
  972. -
  973.  
  974. -
  975.  
  976. -
  977. $return = array();
  978. -
  979. if(count($values) == 4)
  980. -
  981. {
  982. -
  983. for($i=0;$i<4;$i++)
  984. -
  985. {
  986. -
  987. $return[$shorthands[$property][$i]] = $values[$i].$important;
  988. -
  989. }
  990. -
  991. }
  992. -
  993. elseif(count($values) == 3)
  994. -
  995. {
  996. -
  997. $return[$shorthands[$property][0]] = $values[0].$important;
  998. -
  999. $return[$shorthands[$property][1]] = $values[1].$important;
  1000. -
  1001. $return[$shorthands[$property][3]] = $values[1].$important;
  1002. -
  1003. $return[$shorthands[$property][2]] = $values[2].$important;
  1004. -
  1005. }
  1006. -
  1007. elseif(count($values) == 2)
  1008. -
  1009. {
  1010. -
  1011. for($i=0;$i<4;$i++)
  1012. -
  1013. {
  1014. -
  1015. $return[$shorthands[$property][$i]] = (($i % 2 != 0)) ? $values[1].$important : $values[0].$important;
  1016. -
  1017. }
  1018. -
  1019. }
  1020. -
  1021. else
  1022. -
  1023. {
  1024. -
  1025. for($i=0;$i<4;$i++)
  1026. -
  1027. {
  1028. -
  1029. $return[$shorthands[$property][$i]] = $values[0].$important;
  1030. -
  1031. }
  1032. -
  1033. }
  1034. -
  1035. -
  1036. return $return;
  1037. -
  1038. }
  1039. -
  1040.  
  1041. -
  1042. /**
  1043. -
  1044. * Explodes a string as explode() does, however, not if $sep is escaped or within a string.
  1045. -
  1046. * @param string $sep seperator
  1047. -
  1048. * @param string $string
  1049. -
  1050. * @return array
  1051. -
  1052. * @version 1.0
  1053. -
  1054. */
  1055. -
  1056. function explode_ws($sep,$string)
  1057. -
  1058. {
  1059. -
  1060. $status = 'st';
  1061. -
  1062. $to = '';
  1063. -
  1064. -
  1065. $output = array();
  1066. -
  1067. $num = 0;
  1068. -
  1069. for($i = 0, $len = strlen($string);$i < $len; $i++)
  1070. -
  1071. {
  1072. -
  1073. switch($status)
  1074. -
  1075. {
  1076. -
  1077. case 'st':
  1078. -
  1079. if($string{$i} == $sep && !csstidy::escaped($string,$i))
  1080. -
  1081. {
  1082. -
  1083. ++$num;
  1084. -
  1085. }
  1086. -
  1087. elseif($string{$i} == '"' || $string{$i} == '\'' || $string{$i} == '(' && !csstidy::escaped($string,$i))
  1088. -
  1089. {
  1090. -
  1091. $status = 'str';
  1092. -
  1093. $to = ($string{$i} == '(') ? ')' : $string{$i};
  1094. -
  1095. (isset($output[$num])) ? $output[$num] .= $string{$i} : $output[$num] = $string{$i};
  1096. -
  1097. }
  1098. -
  1099. else
  1100. -
  1101. {
  1102. -
  1103. (isset($output[$num])) ? $output[$num] .= $string{$i} : $output[$num] = $string{$i};
  1104. -
  1105. }
  1106. -
  1107. break;
  1108. -
  1109. -
  1110. case 'str':
  1111. -
  1112. if($string{$i} == $to && !csstidy::escaped($string,$i))
  1113. -
  1114. {
  1115. -
  1116. $status = 'st';
  1117. -
  1118. }
  1119. -
  1120. (isset($output[$num])) ? $output[$num] .= $string{$i} : $output[$num] = $string{$i};
  1121. -
  1122. break;
  1123. -
  1124. }
  1125. -
  1126. }
  1127. -
  1128. -
  1129. if(isset($output[0]))
  1130. -
  1131. {
  1132. -
  1133. return $output;
  1134. -
  1135. }
  1136. -
  1137. else
  1138. -
  1139. {
  1140. -
  1141. return array($output);
  1142. -
  1143. }
  1144. -
  1145. }
  1146. -
  1147.  
  1148. -
  1149. /**
  1150. -
  1151. * Merges Shorthand properties again, the opposite of dissolve_4value_shorthands()
  1152. -
  1153. * @param array $array
  1154. -
  1155. * @return array
  1156. -
  1157. * @version 1.2
  1158. -
  1159. * @see dissolve_4value_shorthands()
  1160. -
  1161. */
  1162. -
  1163. function merge_4value_shorthands($array)
  1164. -
  1165. {
  1166. -
  1167. $return = $array;
  1168. -
  1169. $shorthands =& $GLOBALS['csstidy']['shorthands'];
  1170. -
  1171. -
  1172. foreach($shorthands as $key => $value)
  1173. -
  1174. {
  1175. -
  1176. if(isset($array[$value[0]]) && isset($array[$value[1]])
  1177. -
  1178. && isset($array[$value[2]]) && isset($array[$value[3]]) && $value !== 0)
  1179. -
  1180. {
  1181. -
  1182. $return[$key] = '';
  1183. -
  1184. -
  1185. $important = '';
  1186. -
  1187. for($i = 0; $i < 4; $i++)
  1188. -
  1189. {
  1190. -
  1191. $val = $array[$value[$i]];
  1192. -
  1193. if(csstidy::is_important($val))
  1194. -
  1195. {
  1196. -
  1197. $important = '!important';
  1198. -
  1199. $return[$key] .= csstidy::gvw_important($val).' ';
  1200. -
  1201. }
  1202. -
  1203. else
  1204. -
  1205. {
  1206. -
  1207. $return[$key] .= $val.' ';
  1208. -
  1209. }
  1210. -
  1211. unset($return[$value[$i]]);
  1212. -
  1213. }
  1214. -
  1215. $return[$key] = csstidy_optimise::shorthand(trim($return[$key].$important));
  1216. -
  1217. }
  1218. -
  1219. }
  1220. -
  1221. return $return;
  1222. -
  1223. }
  1224. -
  1225.  
  1226. -
  1227. /**
  1228. -
  1229. * Dissolve background property
  1230. -
  1231. * @param string $str_value
  1232. -
  1233. * @return array
  1234. -
  1235. * @version 1.0
  1236. -
  1237. * @see merge_bg()
  1238. -
  1239. * @todo full CSS 3 compliance
  1240. -
  1241. */
  1242. -
  1243. function dissolve_short_bg($str_value)
  1244. -
  1245. {
  1246. -
  1247. $background_prop_default =& $GLOBALS['csstidy']['background_prop_default'];
  1248. -
  1249. $repeat = array('repeat','repeat-x','repeat-y','no-repeat','space');
  1250. -
  1251. $attachment = array('scroll','fixed','local');
  1252. -
  1253. $clip = array('border','padding');
  1254. -
  1255. $origin = array('border','padding','content');
  1256. -
  1257. $pos = array('top','center','bottom','left','right');
  1258. -
  1259. $important = '';
  1260. -
  1261. $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);
  1262. -
  1263. -
  1264. if(csstidy::is_important($str_value))
  1265. -
  1266. {
  1267. -
  1268. $important = ' !important';
  1269. -
  1270. $str_value = csstidy::gvw_important($str_value);
  1271. -
  1272. }
  1273. -
  1274. -
  1275. $str_value = csstidy_optimise::explode_ws(',',$str_value);
  1276. -
  1277. for($i = 0; $i < count($str_value); $i++)
  1278. -
  1279. {
  1280. -
  1281. $have['clip'] = FALSE; $have['pos'] = FALSE;
  1282. -
  1283. $have['color'] = FALSE; $have['bg'] = FALSE;
  1284. -
  1285. -
  1286. $str_value[$i] = csstidy_optimise::explode_ws(' ',trim($str_value[$i]));
  1287. -
  1288. -
  1289. for($j = 0; $j < count($str_value[$i]); $j++)
  1290. -
  1291. {
  1292. -
  1293. if($have['bg'] === FALSE && (substr($str_value[$i][$j],0,4) == 'url(' || $str_value[$i][$j] === 'none'))
  1294. -
  1295. {
  1296. -
  1297. $return['background-image'] .= $str_value[$i][$j].',';
  1298. -
  1299. $have['bg'] = TRUE;
  1300. -
  1301. }
  1302. -
  1303. elseif(in_array($str_value[$i][$j],$repeat,TRUE))
  1304. -
  1305. {
  1306. -
  1307. $return['background-repeat'] .= $str_value[$i][$j].',';
  1308. -
  1309. }
  1310. -
  1311. elseif(in_array($str_value[$i][$j],$attachment,TRUE))
  1312. -
  1313. {
  1314. -
  1315. $return['background-attachment'] .= $str_value[$i][$j].',';
  1316. -
  1317. }
  1318. -
  1319. elseif(in_array($str_value[$i][$j],$clip,TRUE) && !$have['clip'])
  1320. -
  1321. {
  1322. -
  1323. $return['background-clip'] .= $str_value[$i][$j].',';
  1324. -
  1325. $have['clip'] = TRUE;
  1326. -
  1327. }
  1328. -
  1329. elseif(in_array($str_value[$i][$j],$origin,TRUE))
  1330. -
  1331. {
  1332. -
  1333. $return['background-origin'] .= $str_value[$i][$j].',';
  1334. -
  1335. }
  1336. -
  1337. elseif($str_value[$i][$j]{0} == '(')
  1338. -
  1339. {
  1340. -
  1341. $return['background-size'] .= substr($str_value[$i][$j],1,-1).',';
  1342. -
  1343. }
  1344. -
  1345. elseif(in_array($str_value[$i][$j],$pos,TRUE) || is_numeric($str_value[$i][$j]{0}) || $str_value[$i][$j]{0} === NULL)
  1346. -
  1347. {
  1348. -
  1349. $return['background-position'] .= $str_value[$i][$j];
  1350. -
  1351. if(!$have['pos']) $return['background-position'] .= ' '; else $return['background-position'].= ',';
  1352. -
  1353. $have['pos'] = TRUE;
  1354. -
  1355. }
  1356. -
  1357. elseif(!$have['color'])
  1358. -
  1359. {
  1360. -
  1361. $return['background-color'] .= $str_value[$i][$j].',';
  1362. -
  1363. $have['color'] = TRUE;
  1364. -
  1365. }
  1366. -
  1367. }
  1368. -
  1369. }
  1370. -
  1371. -
  1372. foreach($background_prop_default as $bg_prop => $default_value)
  1373. -
  1374. {
  1375. -
  1376. if($return[$bg_prop] !== NULL)
  1377. -
  1378. {
  1379. -
  1380. $return[$bg_prop] = substr($return[$bg_prop],0,-1).$important;
  1381. -
  1382. }
  1383. -
  1384. else $return[$bg_prop] = $default_value.$important;
  1385. -
  1386. }
  1387. -
  1388. return $return;
  1389. -
  1390. }
  1391. -
  1392.  
  1393. -
  1394. /**
  1395. -
  1396. * Merges all background properties
  1397. -
  1398. * @param array $input_css
  1399. -
  1400. * @return array
  1401. -
  1402. * @version 1.0
  1403. -
  1404. * @see dissolve_short_bg()
  1405. -
  1406. * @todo full CSS 3 compliance
  1407. -
  1408. */
  1409. -
  1410. function merge_bg($input_css)
  1411. -
  1412. {
  1413. -
  1414. $background_prop_default =& $GLOBALS['csstidy']['background_prop_default'];
  1415. -
  1416. // Max number of background images. CSS3 not yet fully implemented
  1417. -
  1418. $number_of_values = @max(count(csstidy_optimise::explode_ws(',',$input_css['background-image'])),count(csstidy_optimise::explode_ws(',',$input_css['background-color'])),1);
  1419. -
  1420. // Array with background images to check if BG image exists
  1421. -
  1422. $bg_img_array = @csstidy_optimise::explode_ws(',',csstidy::gvw_important($input_css['background-image']));
  1423. -
  1424. $new_bg_value = '';
  1425. -
  1426. $important = '';
  1427. -
  1428. -
  1429. for($i = 0; $i < $number_of_values; $i++)
  1430. -
  1431. {
  1432. -
  1433. foreach($background_prop_default as $bg_property => $default_value)
  1434. -
  1435. {
  1436. -
  1437. // Skip if property does not exist
  1438. -
  1439. if(!isset($input_css[$bg_property]))
  1440. -
  1441. {
  1442. -
  1443. continue;
  1444. -
  1445. }
  1446. -
  1447. -
  1448. $cur_value = $input_css[$bg_property];
  1449. -
  1450. -
  1451. // Skip some properties if there is no background image
  1452. -
  1453. if((!isset($bg_img_array[$i]) || $bg_img_array[$i] === 'none')
  1454. -
  1455. && ($bg_property === 'background-size' || $bg_property === 'background-position'
  1456. -
  1457. || $bg_property === 'background-attachment' || $bg_property === 'background-repeat'))
  1458. -
  1459. {
  1460. -
  1461. continue;
  1462. -
  1463. }
  1464. -
  1465. -
  1466. // Remove !important
  1467. -
  1468. if(csstidy::is_important($cur_value))
  1469. -
  1470. {
  1471. -
  1472. $important = ' !important';
  1473. -
  1474. $cur_value = csstidy::gvw_important($cur_value);
  1475. -
  1476. }
  1477. -
  1478. -
  1479. // Do not add default values
  1480. -
  1481. if($cur_value === $default_value)
  1482. -
  1483. {
  1484. -
  1485. continue;
  1486. -
  1487. }
  1488. -
  1489. -
  1490. $temp = csstidy_optimise::explode_ws(',',$cur_value);
  1491. -
  1492.  
  1493. -
  1494. if(isset($temp[$i]))
  1495. -
  1496. {
  1497. -
  1498. if($bg_property == 'background-size')
  1499. -
  1500. {
  1501. -
  1502. $new_bg_value .= '('.$temp[$i].') ';
  1503. -
  1504. }
  1505. -
  1506. else
  1507. -
  1508. {
  1509. -
  1510. $new_bg_value .= $temp[$i].' ';
  1511. -
  1512. }
  1513. -
  1514. }
  1515. -
  1516. }
  1517. -
  1518. -
  1519. $new_bg_value = trim($new_bg_value);
  1520. -
  1521. if($i != $number_of_values-1) $new_bg_value .= ',';
  1522. -
  1523. }
  1524. -
  1525. -
  1526. // Delete all background-properties
  1527. -
  1528. foreach($background_prop_default as $bg_property => $default_value)
  1529. -
  1530. {
  1531. -
  1532. unset($input_css[$bg_property]);
  1533. -
  1534. }
  1535. -
  1536. -
  1537. // Add new background property
  1538. -
  1539. if($new_bg_value !== '') $input_css['background'] = $new_bg_value.$important;
  1540. -
  1541. -
  1542. return $input_css;
  1543. -
  1544. }
  1545. -
  1546. }
  1547. -
  1548. ?>
  1549. -
-
-
-
- Documentation generated on Mon, 15 May 2006 22:55:34 +0200 by phpDocumentor 1.3.0RC3 -
-
-
- - - \ No newline at end of file