PageRenderTime 54ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 0ms

/mod/chat/pfc/lib/csstidy-1.2/class.csstidy_print.php

https://github.com/masuman/elgg-1
PHP | 350 lines | 189 code | 41 blank | 120 comment | 44 complexity | b8dda68989a41554e4d39402d42b2b10 MD5 | raw file
  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. * Saves the formatted CSS string
  47. * @var string
  48. * @access public
  49. */
  50. var $output_css = '';
  51. /**
  52. * Saves the formatted CSS string (plain text)
  53. * @var string
  54. * @access public
  55. */
  56. var $output_css_plain = '';
  57. /**
  58. * Constructor
  59. * @param array $css contains the class csstidy
  60. * @access private
  61. * @version 1.0
  62. */
  63. function csstidy_print(&$css)
  64. {
  65. $this->parser =& $css;
  66. $this->css =& $css->css;
  67. $this->template =& $css->template;
  68. $this->tokens =& $css->tokens;
  69. $this->charset =& $css->charset;
  70. $this->import =& $css->import;
  71. $this->namespace =& $css->namespace;
  72. }
  73. /**
  74. * Resets output_css and output_css_plain (new css code)
  75. * @access private
  76. * @version 1.0
  77. */
  78. function _reset()
  79. {
  80. $this->output_css = '';
  81. $this->output_css_plain = '';
  82. }
  83. /**
  84. * Returns the CSS code as plain text
  85. * @return string
  86. * @access public
  87. * @version 1.0
  88. */
  89. function plain()
  90. {
  91. $this->_print(true);
  92. return $this->output_css_plain;
  93. }
  94. /**
  95. * Returns the formatted CSS code
  96. * @return string
  97. * @access public
  98. * @version 1.0
  99. */
  100. function formatted()
  101. {
  102. $this->_print(false);
  103. return $this->output_css;
  104. }
  105. /**
  106. * Returns the formatted CSS Code and saves it into $this->output_css and $this->output_css_plain
  107. * @param bool $plain plain text or not
  108. * @access private
  109. * @version 2.0
  110. */
  111. function _print($plain = false)
  112. {
  113. if ($this->output_css && $this->output_css_plain) {
  114. return;
  115. }
  116. $output = '';
  117. if (!$this->parser->get_cfg('preserve_css')) {
  118. $this->_convert_raw_css();
  119. }
  120. $template =& $this->template;
  121. if ($plain) {
  122. $template = array_map('strip_tags', $template);
  123. }
  124. if ($this->parser->get_cfg('timestamp')) {
  125. array_unshift($this->tokens, array(COMMENT, ' CSSTidy ' . $this->parser->version . ': ' . date('r') . ' '));
  126. }
  127. if (!empty($this->charset)) {
  128. $output .= $template[0].'@charset '.$template[5].$this->charset.$template[6];
  129. }
  130. if (!empty($this->import)) {
  131. for ($i = 0, $size = count($this->import); $i < $size; $i ++) {
  132. $output .= $template[0].'@import '.$template[5].$this->import[$i].$template[6];
  133. }
  134. }
  135. if (!empty($this->namespace)) {
  136. $output .= $template[0].'@namespace '.$template[5].$this->namespace.$template[6];
  137. }
  138. $output .= $template[13];
  139. $in_at_out = '';
  140. $out =& $output;
  141. foreach ($this->tokens as $key => $token)
  142. {
  143. switch ($token[0])
  144. {
  145. case AT_START:
  146. $out .= $template[0].$this->_htmlsp($token[1], $plain).$template[1];
  147. $out =& $in_at_out;
  148. break;
  149. case SEL_START:
  150. if($this->parser->get_cfg('lowercase_s')) $token[1] = strtolower($token[1]);
  151. $out .= ($token[1]{0} !== '@') ? $template[2].$this->_htmlsp($token[1], $plain) : $template[0].$this->_htmlsp($token[1], $plain);
  152. $out .= $template[3];
  153. break;
  154. case PROPERTY:
  155. if($this->parser->get_cfg('case_properties') == 2) $token[1] = strtoupper($token[1]);
  156. if($this->parser->get_cfg('case_properties') == 1) $token[1] = strtolower($token[1]);
  157. $out .= $template[4] . $this->_htmlsp($token[1], $plain) . ':' . $template[5];
  158. break;
  159. case VALUE:
  160. $out .= $this->_htmlsp($token[1], $plain);
  161. if($this->_seeknocomment($key, 1) == SEL_END && $this->parser->get_cfg('remove_last_;')) {
  162. $out .= str_replace(';', '', $template[6]);
  163. } else {
  164. $out .= $template[6];
  165. }
  166. break;
  167. case SEL_END:
  168. $out .= $template[7];
  169. if($this->_seeknocomment($key, 1) != AT_END) $out .= $template[8];
  170. break;
  171. case AT_END:
  172. $out =& $output;
  173. $out .= $template[10] . str_replace("\n", "\n" . $template[10], $in_at_out);
  174. $in_at_out = '';
  175. $out .= $template[9];
  176. break;
  177. case COMMENT:
  178. $out .= $template[11] . '/*' . $this->_htmlsp($token[1], $plain) . '*/' . $template[12];
  179. break;
  180. }
  181. }
  182. $output = trim($output);
  183. if (!$plain) {
  184. $this->output_css = $output;
  185. $this->_print(true);
  186. } else {
  187. $this->output_css_plain = $output;
  188. }
  189. }
  190. /**
  191. * Gets the next token type which is $move away from $key, excluding comments
  192. * @param integer $key current position
  193. * @param integer $move move this far
  194. * @return mixed a token type
  195. * @access private
  196. * @version 1.0
  197. */
  198. function _seeknocomment($key, $move) {
  199. $go = ($move > 0) ? 1 : -1;
  200. for ($i = $key + 1; abs($key-$i)-1 < abs($move); $i += $go) {
  201. if (!isset($this->tokens[$i])) {
  202. return;
  203. }
  204. if ($this->tokens[$i][0] == COMMENT) {
  205. $move += 1;
  206. continue;
  207. }
  208. return $this->tokens[$i][0];
  209. }
  210. }
  211. /**
  212. * Converts $this->css array to a raw array ($this->tokens)
  213. * @access private
  214. * @version 1.0
  215. */
  216. function _convert_raw_css()
  217. {
  218. $this->tokens = array();
  219. ksort($this->css);
  220. foreach ($this->css as $medium => $val)
  221. {
  222. if ($this->parser->get_cfg('sort_selectors')) ksort($val);
  223. if ($medium != DEFAULT_AT) {
  224. $this->parser->_add_token(AT_START, $medium, true);
  225. }
  226. foreach ($val as $selector => $vali)
  227. {
  228. if ($this->parser->get_cfg('sort_properties')) ksort($vali);
  229. $this->parser->_add_token(SEL_START, $selector, true);
  230. foreach ($vali as $property => $valj)
  231. {
  232. $this->parser->_add_token(PROPERTY, $property, true);
  233. $this->parser->_add_token(VALUE, $valj, true);
  234. }
  235. $this->parser->_add_token(SEL_END, $selector, true);
  236. }
  237. if ($medium != DEFAULT_AT) {
  238. $this->parser->_add_token(AT_END, $medium, true);
  239. }
  240. }
  241. }
  242. /**
  243. * Same as htmlspecialchars, only that chars are not replaced if $plain !== true. This makes print_code() cleaner.
  244. * @param string $string
  245. * @param bool $plain
  246. * @return string
  247. * @see csstidy_print::_print()
  248. * @access private
  249. * @version 1.0
  250. */
  251. function _htmlsp($string, $plain)
  252. {
  253. if (!$plain) {
  254. return htmlspecialchars($string);
  255. }
  256. return $string;
  257. }
  258. /**
  259. * Get compression ratio
  260. * @access public
  261. * @return float
  262. * @version 1.2
  263. */
  264. function get_ratio()
  265. {
  266. if (!$this->output_css_plain) {
  267. $this->formatted();
  268. }
  269. return round((strlen($this->input_css) - strlen($this->output_css_plain)) / strlen($this->input_css), 3) * 100;
  270. }
  271. /**
  272. * Get difference between the old and new code in bytes and prints the code if necessary.
  273. * @access public
  274. * @return string
  275. * @version 1.1
  276. */
  277. function get_diff()
  278. {
  279. if (!$this->output_css_plain) {
  280. $this->formatted();
  281. }
  282. $diff = strlen($this->output_css_plain) - strlen($this->input_css);
  283. if ($diff > 0) {
  284. return '+' . $diff;
  285. } elseif ($diff == 0) {
  286. return '+-' . $diff;
  287. }
  288. return $diff;
  289. }
  290. /**
  291. * Get the size of either input or output CSS in KB
  292. * @param string $loc default is "output"
  293. * @access public
  294. * @return integer
  295. * @version 1.0
  296. */
  297. function size($loc = 'output')
  298. {
  299. if ($loc == 'output' && !$this->output_css) {
  300. $this->formatted();
  301. }
  302. if ($loc == 'input') {
  303. return (strlen($this->input_css) / 1000);
  304. } else {
  305. return (strlen($this->output_css_plain) / 1000);
  306. }
  307. }
  308. }
  309. ?>