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

Source for file class.csstidy_print.php

Documentation is available at class.csstidy_print.php

  1. <?php
  2. /**
  3. * CSSTidy - CSS Parser and Optimiser
  4. *
  5. * CSS Printing class
  6. * This class prints CSS data generated by csstidy.
  7. *
  8. * This file is part of CSSTidy.
  9. *
  10. * CSSTidy is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License as published by
  12. * the Free Software Foundation; either version 2 of the License, or
  13. * (at your option) any later version.
  14. *
  15. * CSSTidy is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License
  21. * along with CSSTidy; if not, write to the Free Software
  22. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  23. *
  24. * @license http://opensource.org/licenses/gpl-license.php GNU Public License
  25. * @package csstidy
  26. * @author Florian Schmitz (floele at gmail dot com) 2005-2006
  27. */
  28. /**
  29. * CSS Printing class
  30. *
  31. * This class prints CSS data generated by csstidy.
  32. *
  33. * @package csstidy
  34. * @author Florian Schmitz (floele at gmail dot com) 2005-2006
  35. * @version 1.0
  36. */
  37. class csstidy_print
  38. {
  39. /**
  40. * Saves the input CSS string
  41. * @var string
  42. * @access private
  43. */
  44. var $input_css = '';
  45.  
  46. /**
  47. * Saves the formatted CSS string
  48. * @var string
  49. * @access public
  50. */
  51. var $output_css = '';
  52.  
  53. /**
  54. * Saves the formatted CSS string (plain text)
  55. * @var string
  56. * @access public
  57. */
  58. var $output_css_plain = '';
  59.  
  60. /**
  61. * Constructor
  62. * @param array $css contains the class csstidy
  63. * @access private
  64. * @version 1.0
  65. */
  66. function csstidy_print(&$css)
  67. {
  68. $this->parser =& $css;
  69. $this->css =& $css->css;
  70. $this->template =& $css->template;
  71. $this->tokens =& $css->tokens;
  72. $this->charset =& $css->charset;
  73. $this->import =& $css->import;
  74. $this->namespace =& $css->namespace;
  75. }
  76.  
  77. /**
  78. * Resets output_css and output_css_plain (new css code)
  79. * @access private
  80. * @version 1.0
  81. */
  82. function _reset()
  83. {
  84. $this->output_css = '';
  85. $this->output_css_plain = '';
  86. }
  87.  
  88. /**
  89. * Returns the CSS code as plain text
  90. * @return string
  91. * @access public
  92. * @version 1.0
  93. */
  94. function plain()
  95. {
  96. $this->_print(true);
  97. return $this->output_css_plain;
  98. }
  99.  
  100. /**
  101. * Returns the formatted CSS code
  102. * @return string
  103. * @access public
  104. * @version 1.0
  105. */
  106. function formatted()
  107. {
  108. $this->_print(false);
  109. return $this->output_css;
  110. }
  111. /**
  112. * Returns the formatted CSS Code and saves it into $this->output_css and $this->output_css_plain
  113. * @param bool $plain plain text or not
  114. * @access private
  115. * @version 2.0
  116. */
  117. function _print($plain = false)
  118. {
  119. if ($this->output_css && $this->output_css_plain) {
  120. return;
  121. }
  122. $output = '';
  123. if (!$this->parser->get_cfg('preserve_css')) {
  124. $this->_convert_raw_css();
  125. }
  126.  
  127. $template =& $this->template;
  128.  
  129. if ($plain) {
  130. $template = array_map('strip_tags', $template);
  131. }
  132. if ($this->parser->get_cfg('timestamp')) {
  133. array_unshift($this->tokens, array(COMMENT, ' CSSTidy ' . $this->parser->version . ': ' . date('r') . ' '));
  134. }
  135. if (!empty($this->charset)) {
  136. $output .= $template[0].'@charset '.$template[5].$this->charset.$template[6];
  137. }
  138. if (!empty($this->import)) {
  139. for ($i = 0, $size = count($this->import); $i < $size; $i ++) {
  140. $output .= $template[0].'@import '.$template[5].$this->import[$i].$template[6];
  141. }
  142. }
  143. if (!empty($this->namespace)) {
  144. $output .= $template[0].'@namespace '.$template[5].$this->namespace.$template[6];
  145. }
  146. $output .= $template[13];
  147. $in_at_out = '';
  148. $out =& $output;
  149. foreach ($this->tokens as $key => $token)
  150. {
  151. switch ($token[0])
  152. {
  153. case AT_START:
  154. $out .= $template[0].$this->_htmlsp($token[1], $plain).$template[1];
  155. $out =& $in_at_out;
  156. break;
  157. case SEL_START:
  158. if($this->parser->get_cfg('lowercase_s')) $token[1] = strtolower($token[1]);
  159. $out .= ($token[1]{0} !== '@') ? $template[2].$this->_htmlsp($token[1], $plain) : $template[0].$this->_htmlsp($token[1], $plain);
  160. $out .= $template[3];
  161. break;
  162. case PROPERTY:
  163. if($this->parser->get_cfg('case_properties') == 2) $token[1] = strtoupper($token[1]);
  164. if($this->parser->get_cfg('case_properties') == 1) $token[1] = strtolower($token[1]);
  165. $out .= $template[4] . $this->_htmlsp($token[1], $plain) . ':' . $template[5];
  166. break;
  167. case VALUE:
  168. $out .= $this->_htmlsp($token[1], $plain);
  169. if($this->_seeknocomment($key, 1) == SEL_END && $this->parser->get_cfg('remove_last_;')) {
  170. $out .= str_replace(';', '', $template[6]);
  171. } else {
  172. $out .= $template[6];
  173. }
  174. break;
  175. case SEL_END:
  176. $out .= $template[7];
  177. if($this->_seeknocomment($key, 1) != AT_END) $out .= $template[8];
  178. break;
  179. case AT_END:
  180. $out =& $output;
  181. $out .= $template[10] . str_replace("\n", "\n" . $template[10], $in_at_out);
  182. $in_at_out = '';
  183. $out .= $template[9];
  184. break;
  185.  
  186. case COMMENT:
  187. $out .= $template[11] . '/*' . $this->_htmlsp($token[1], $plain) . '*/' . $template[12];
  188. break;
  189. }
  190. }
  191.  
  192. $output = trim($output);
  193. if (!$plain) {
  194. $this->output_css = $output;
  195. $this->_print(true);
  196. } else {
  197. $this->output_css_plain = $output;
  198. }
  199. }
  200. /**
  201. * Gets the next token type which is $move away from $key, excluding comments
  202. * @param integer $key current position
  203. * @param integer $move move this far
  204. * @return mixed a token type
  205. * @access private
  206. * @version 1.0
  207. */
  208. function _seeknocomment($key, $move) {
  209. $go = ($move > 0) ? 1 : -1;
  210. for ($i = $key + 1; abs($key-$i)-1 < abs($move); $i += $go) {
  211. if (!isset($this->tokens[$i])) {
  212. return;
  213. }
  214. if ($this->tokens[$i][0] == COMMENT) {
  215. $move += 1;
  216. continue;
  217. }
  218. return $this->tokens[$i][0];
  219. }
  220. }
  221. /**
  222. * Converts $this->css array to a raw array ($this->tokens)
  223. * @access private
  224. * @version 1.0
  225. */
  226. function _convert_raw_css()
  227. {
  228. $this->tokens = array();
  229. ksort($this->css);
  230. foreach ($this->css as $medium => $val)
  231. {
  232. if ($this->parser->get_cfg('sort_selectors')) ksort($val);
  233. if ($medium != DEFAULT_AT) {
  234. $this->parser->_add_token(AT_START, $medium, true);
  235. }
  236. foreach ($val as $selector => $vali)
  237. {
  238. if ($this->parser->get_cfg('sort_properties')) ksort($vali);
  239. $this->parser->_add_token(SEL_START, $selector, true);
  240. foreach ($vali as $property => $valj)
  241. {
  242. $this->parser->_add_token(PROPERTY, $property, true);
  243. $this->parser->_add_token(VALUE, $valj, true);
  244. }
  245. $this->parser->_add_token(SEL_END, $selector, true);
  246. }
  247. if ($medium != DEFAULT_AT) {
  248. $this->parser->_add_token(AT_END, $medium, true);
  249. }
  250. }
  251. }
  252. /**
  253. * Same as htmlspecialchars, only that chars are not replaced if $plain !== true. This makes print_code() cleaner.
  254. * @param string $string
  255. * @param bool $plain
  256. * @return string
  257. * @see csstidy_print::_print()
  258. * @access private
  259. * @version 1.0
  260. */
  261. function _htmlsp($string, $plain)
  262. {
  263. if (!$plain) {
  264. return htmlspecialchars($string);
  265. }
  266. return $string;
  267. }
  268. /**
  269. * Get compression ratio
  270. * @access public
  271. * @return float
  272. * @version 1.2
  273. */
  274. function get_ratio()
  275. {
  276. if (!$this->output_css_plain) {
  277. $this->formatted();
  278. }
  279. return round((strlen($this->input_css) - strlen($this->output_css_plain)) / strlen($this->input_css), 3) * 100;
  280. }
  281.  
  282. /**
  283. * Get difference between the old and new code in bytes and prints the code if necessary.
  284. * @access public
  285. * @return string
  286. * @version 1.1
  287. */
  288. function get_diff()
  289. {
  290. if (!$this->output_css_plain) {
  291. $this->formatted();
  292. }
  293. $diff = strlen($this->output_css_plain) - strlen($this->input_css);
  294. if ($diff > 0) {
  295. return '+' . $diff;
  296. } elseif ($diff == 0) {
  297. return '+-' . $diff;
  298. }
  299. return $diff;
  300. }
  301.  
  302. /**
  303. * Get the size of either input or output CSS in KB
  304. * @param string $loc default is "output"
  305. * @access public
  306. * @return integer
  307. * @version 1.0
  308. */
  309. function size($loc = 'output')
  310. {
  311. if ($loc == 'output' && !$this->output_css) {
  312. $this->formatted();
  313. }
  314. if ($loc == 'input') {
  315. return (strlen($this->input_css) / 1000);
  316. } else {
  317. return (strlen($this->output_css_plain) / 1000);
  318. }
  319. }
  320. }
  321. ?>

Documentation generated on Mon, 15 May 2006 22:55:42 +0200 by phpDocumentor 1.3.0RC3