Нет описания

class.csstidy_print.php 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411
  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. * 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 Cedric Morin (cedric at yterium dot com) 2010
  30. */
  31. /**
  32. * CSS Printing class
  33. *
  34. * This class prints CSS data generated by csstidy.
  35. *
  36. * @package csstidy
  37. * @author Florian Schmitz (floele at gmail dot com) 2005-2006
  38. * @version 1.0.1
  39. */
  40. class csstidy_print {
  41. /**
  42. * Saves the input CSS string
  43. * @var string
  44. * @access private
  45. */
  46. public $input_css = '';
  47. /**
  48. * Saves the formatted CSS string
  49. * @var string
  50. * @access public
  51. */
  52. public $output_css = '';
  53. /**
  54. * Saves the formatted CSS string (plain text)
  55. * @var string
  56. * @access public
  57. */
  58. public $output_css_plain = '';
  59. /**
  60. * Constructor
  61. * @param array $css contains the class csstidy
  62. * @access private
  63. * @version 1.0
  64. */
  65. function __construct(&$css) {
  66. $this->parser = & $css;
  67. $this->css = & $css->css;
  68. $this->template = & $css->template;
  69. $this->tokens = & $css->tokens;
  70. $this->charset = & $css->charset;
  71. $this->import = & $css->import;
  72. $this->namespace = & $css->namespace;
  73. }
  74. function csstidy_print(&$css) {
  75. $this->__construct($css);
  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. $this->output_css = '';
  84. $this->output_css_plain = '';
  85. }
  86. /**
  87. * Returns the CSS code as plain text
  88. * @param string $default_media default @media to add to selectors without any @media
  89. * @return string
  90. * @access public
  91. * @version 1.0
  92. */
  93. function plain($default_media='') {
  94. $this->_print(true, $default_media);
  95. return $this->output_css_plain;
  96. }
  97. /**
  98. * Returns the formatted CSS code
  99. * @param string $default_media default @media to add to selectors without any @media
  100. * @return string
  101. * @access public
  102. * @version 1.0
  103. */
  104. function formatted($default_media='') {
  105. $this->_print(false, $default_media);
  106. return $this->output_css;
  107. }
  108. /**
  109. * Returns the formatted CSS code to make a complete webpage
  110. * @param string $doctype shorthand for the document type
  111. * @param bool $externalcss indicates whether styles to be attached internally or as an external stylesheet
  112. * @param string $title title to be added in the head of the document
  113. * @param string $lang two-letter language code to be added to the output
  114. * @return string
  115. * @access public
  116. * @version 1.4
  117. */
  118. function formatted_page($doctype='xhtml1.1', $externalcss=true, $title='', $lang='en') {
  119. switch ($doctype) {
  120. case 'xhtml1.0strict':
  121. $doctype_output = '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
  122. "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">';
  123. break;
  124. case 'xhtml1.1':
  125. default:
  126. $doctype_output = '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
  127. "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">';
  128. break;
  129. }
  130. $output = $cssparsed = '';
  131. $this->output_css_plain = & $output;
  132. $output .= $doctype_output . "\n" . '<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="' . $lang . '"';
  133. $output .= ( $doctype === 'xhtml1.1') ? '>' : ' lang="' . $lang . '">';
  134. $output .= "\n<head>\n <title>$title</title>";
  135. if ($externalcss) {
  136. $output .= "\n <style type=\"text/css\">\n";
  137. $cssparsed = file_get_contents('cssparsed.css');
  138. $output .= $cssparsed; // Adds an invisible BOM or something, but not in css_optimised.php
  139. $output .= "\n</style>";
  140. } else {
  141. $output .= "\n" . ' <link rel="stylesheet" type="text/css" href="cssparsed.css" />';
  142. // }
  143. }
  144. $output .= "\n</head>\n<body><code id=\"copytext\">";
  145. $output .= $this->formatted();
  146. $output .= '</code>' . "\n" . '</body></html>';
  147. return $this->output_css_plain;
  148. }
  149. /**
  150. * Returns the formatted CSS Code and saves it into $this->output_css and $this->output_css_plain
  151. * @param bool $plain plain text or not
  152. * @param string $default_media default @media to add to selectors without any @media
  153. * @access private
  154. * @version 2.0
  155. */
  156. function _print($plain = false, $default_media='') {
  157. if ($this->output_css && $this->output_css_plain) {
  158. return;
  159. }
  160. $output = '';
  161. if (!$this->parser->get_cfg('preserve_css')) {
  162. $this->_convert_raw_css($default_media);
  163. }
  164. $template = & $this->template;
  165. if ($plain) {
  166. $template = array_map('strip_tags', $template);
  167. }
  168. if ($this->parser->get_cfg('timestamp')) {
  169. array_unshift($this->tokens, array(COMMENT, ' CSSTidy ' . $this->parser->version . ': ' . date('r') . ' '));
  170. }
  171. if (!empty($this->charset)) {
  172. $output .= $template[0] . '@charset ' . $template[5] . $this->charset . $template[6];
  173. }
  174. if (!empty($this->import)) {
  175. for ($i = 0, $size = count($this->import); $i < $size; $i++) {
  176. $import_components = explode(' ', $this->import[$i]);
  177. if (substr($import_components[0], 0, 4) === 'url(' && substr($import_components[0], -1, 1) === ')') {
  178. $import_components[0] = '\'' . trim(substr($import_components[0], 4, -1), "'\"") . '\'';
  179. $this->import[$i] = implode(' ', $import_components);
  180. $this->parser->log('Optimised @import : Removed "url("', 'Information');
  181. }
  182. $output .= $template[0] . '@import ' . $template[5] . $this->import[$i] . $template[6];
  183. }
  184. }
  185. if (!empty($this->namespace)) {
  186. if (substr($this->namespace, 0, 4) === 'url(' && substr($this->namespace, -1, 1) === ')') {
  187. $this->namespace = '\'' . substr($this->namespace, 4, -1) . '\'';
  188. $this->parser->log('Optimised @namespace : Removed "url("', 'Information');
  189. }
  190. $output .= $template[0] . '@namespace ' . $template[5] . $this->namespace . $template[6];
  191. }
  192. $output .= $template[13];
  193. $in_at_out = '';
  194. $out = & $output;
  195. foreach ($this->tokens as $key => $token) {
  196. switch ($token[0]) {
  197. case AT_START:
  198. $out .= $template[0] . $this->_htmlsp($token[1], $plain) . $template[1];
  199. $out = & $in_at_out;
  200. break;
  201. case SEL_START:
  202. if ($this->parser->get_cfg('lowercase_s'))
  203. $token[1] = strtolower($token[1]);
  204. $out .= ( $token[1][0] !== '@') ? $template[2] . $this->_htmlsp($token[1], $plain) : $template[0] . $this->_htmlsp($token[1], $plain);
  205. $out .= $template[3];
  206. break;
  207. case PROPERTY:
  208. if ($this->parser->get_cfg('case_properties') === 2) {
  209. $token[1] = strtoupper($token[1]);
  210. } elseif ($this->parser->get_cfg('case_properties') === 1) {
  211. $token[1] = strtolower($token[1]);
  212. }
  213. $out .= $template[4] . $this->_htmlsp($token[1], $plain) . ':' . $template[5];
  214. break;
  215. case VALUE:
  216. $out .= $this->_htmlsp($token[1], $plain);
  217. if ($this->_seeknocomment($key, 1) == SEL_END && $this->parser->get_cfg('remove_last_;')) {
  218. $out .= str_replace(';', '', $template[6]);
  219. } else {
  220. $out .= $template[6];
  221. }
  222. break;
  223. case SEL_END:
  224. $out .= $template[7];
  225. if ($this->_seeknocomment($key, 1) != AT_END)
  226. $out .= $template[8];
  227. break;
  228. case AT_END:
  229. $out = & $output;
  230. $out .= $template[10] . str_replace("\n", "\n" . $template[10], $in_at_out);
  231. $in_at_out = '';
  232. $out .= $template[9];
  233. break;
  234. case COMMENT:
  235. $out .= $template[11] . '/*' . $this->_htmlsp($token[1], $plain) . '*/' . $template[12];
  236. break;
  237. }
  238. }
  239. $output = trim($output);
  240. if (!$plain) {
  241. $this->output_css = $output;
  242. $this->_print(true);
  243. } else {
  244. // If using spaces in the template, don't want these to appear in the plain output
  245. $this->output_css_plain = str_replace('&#160;', '', $output);
  246. }
  247. }
  248. /**
  249. * Gets the next token type which is $move away from $key, excluding comments
  250. * @param integer $key current position
  251. * @param integer $move move this far
  252. * @return mixed a token type
  253. * @access private
  254. * @version 1.0
  255. */
  256. function _seeknocomment($key, $move) {
  257. $go = ($move > 0) ? 1 : -1;
  258. for ($i = $key + 1; abs($key - $i) - 1 < abs($move); $i += $go) {
  259. if (!isset($this->tokens[$i])) {
  260. return;
  261. }
  262. if ($this->tokens[$i][0] == COMMENT) {
  263. $move += 1;
  264. continue;
  265. }
  266. return $this->tokens[$i][0];
  267. }
  268. }
  269. /**
  270. * Converts $this->css array to a raw array ($this->tokens)
  271. * @param string $default_media default @media to add to selectors without any @media
  272. * @access private
  273. * @version 1.0
  274. */
  275. function _convert_raw_css($default_media='') {
  276. $this->tokens = array();
  277. foreach ($this->css as $medium => $val) {
  278. if ($this->parser->get_cfg('sort_selectors'))
  279. ksort($val);
  280. if ( (int) $medium < DEFAULT_AT ) {
  281. $this->parser->_add_token(AT_START, $medium, true);
  282. }
  283. elseif ($default_media) {
  284. $this->parser->_add_token(AT_START, $default_media, true);
  285. }
  286. foreach ($val as $selector => $vali) {
  287. if ($this->parser->get_cfg('sort_properties'))
  288. ksort($vali);
  289. $this->parser->_add_token(SEL_START, $selector, true);
  290. foreach ($vali as $property => $valj) {
  291. $this->parser->_add_token(PROPERTY, $property, true);
  292. $this->parser->_add_token(VALUE, $valj, true);
  293. }
  294. $this->parser->_add_token(SEL_END, $selector, true);
  295. }
  296. if ( (int) $medium < DEFAULT_AT ) {
  297. $this->parser->_add_token(AT_END, $medium, true);
  298. }
  299. elseif ($default_media) {
  300. $this->parser->_add_token(AT_END, $default_media, true);
  301. }
  302. }
  303. }
  304. /**
  305. * Same as htmlspecialchars, only that chars are not replaced if $plain !== true. This makes print_code() cleaner.
  306. * @param string $string
  307. * @param bool $plain
  308. * @return string
  309. * @see csstidy_print::_print()
  310. * @access private
  311. * @version 1.0
  312. */
  313. function _htmlsp($string, $plain) {
  314. if (!$plain) {
  315. return htmlspecialchars($string, ENT_QUOTES, 'utf-8');
  316. }
  317. return $string;
  318. }
  319. /**
  320. * Get compression ratio
  321. * @access public
  322. * @return float
  323. * @version 1.2
  324. */
  325. function get_ratio() {
  326. if (!$this->output_css_plain) {
  327. $this->formatted();
  328. }
  329. return round((strlen($this->input_css) - strlen($this->output_css_plain)) / strlen($this->input_css), 3) * 100;
  330. }
  331. /**
  332. * Get difference between the old and new code in bytes and prints the code if necessary.
  333. * @access public
  334. * @return string
  335. * @version 1.1
  336. */
  337. function get_diff() {
  338. if (!$this->output_css_plain) {
  339. $this->formatted();
  340. }
  341. $diff = strlen($this->output_css_plain) - strlen($this->input_css);
  342. if ($diff > 0) {
  343. return '+' . $diff;
  344. } elseif ($diff == 0) {
  345. return '+-' . $diff;
  346. }
  347. return $diff;
  348. }
  349. /**
  350. * Get the size of either input or output CSS in KB
  351. * @param string $loc default is "output"
  352. * @access public
  353. * @return integer
  354. * @version 1.0
  355. */
  356. function size($loc = 'output') {
  357. if ($loc === 'output' && !$this->output_css) {
  358. $this->formatted();
  359. }
  360. if ($loc === 'input') {
  361. return (strlen($this->input_css) / 1000);
  362. } else {
  363. return (strlen($this->output_css_plain) / 1000);
  364. }
  365. }
  366. }