class.csstidy_print.php 12 KB

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