summaryrefslogtreecommitdiff
blob: 080a368fb388bbb07a7136ee87295a31a4ad03d3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
<?php

/**
 * CSSTidy - CSS Parser and Optimiser
 *
 * CSS Printing class
 * This class prints CSS data generated by csstidy.
 *
 * Copyright 2005, 2006, 2007 Florian Schmitz
 *
 * This file is part of CSSTidy.
 *
 *   CSSTidy is free software; you can redistribute it and/or modify
 *   it under the terms of the GNU Lesser General Public License as published by
 *   the Free Software Foundation; either version 2.1 of the License, or
 *   (at your option) any later version.
 *
 *   CSSTidy is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU Lesser General Public License for more details.
 *
 *   You should have received a copy of the GNU Lesser General Public License
 *   along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * @license http://opensource.org/licenses/lgpl-license.php GNU Lesser General Public License
 * @package csstidy
 * @author Florian Schmitz (floele at gmail dot com) 2005-2007
 * @author Brett Zamir (brettz9 at yahoo dot com) 2007
 * @author Cedric Morin (cedric at yterium dot com) 2010
 */

/**
 * CSS Printing class
 *
 * This class prints CSS data generated by csstidy.
 *
 * @package csstidy
 * @author Florian Schmitz (floele at gmail dot com) 2005-2006
 * @version 1.0.1
 */
class csstidy_print {

	/**
	 * Saves the input CSS string
	 * @var string
	 * @access private
	 */
	public $input_css = '';
	/**
	 * Saves the formatted CSS string
	 * @var string
	 * @access public
	 */
	public $output_css = '';
	/**
	 * Saves the formatted CSS string (plain text)
	 * @var string
	 * @access public
	 */
	public $output_css_plain = '';

	/**
	 * Constructor
	 * @param array $css contains the class csstidy
	 * @access private
	 * @version 1.0
	 */
	function csstidy_print(&$css) {
		$this->parser = & $css;
		$this->css = & $css->css;
		$this->template = & $css->template;
		$this->tokens = & $css->tokens;
		$this->charset = & $css->charset;
		$this->import = & $css->import;
		$this->namespace = & $css->namespace;
	}

	/**
	 * Resets output_css and output_css_plain (new css code)
	 * @access private
	 * @version 1.0
	 */
	function _reset() {
		$this->output_css = '';
		$this->output_css_plain = '';
	}

	/**
	 * Returns the CSS code as plain text
	 * @param string $default_media default @media to add to selectors without any @media
	 * @return string
	 * @access public
	 * @version 1.0
	 */
	function plain($default_media='') {
		$this->_print(true, $default_media);
		return $this->output_css_plain;
	}

	/**
	 * Returns the formatted CSS code
	 * @param string $default_media default @media to add to selectors without any @media
	 * @return string
	 * @access public
	 * @version 1.0
	 */
	function formatted($default_media='') {
		$this->_print(false, $default_media);
		return $this->output_css;
	}

	/**
	 * Returns the formatted CSS code to make a complete webpage
	 * @param string $doctype shorthand for the document type
	 * @param bool $externalcss indicates whether styles to be attached internally or as an external stylesheet
	 * @param string $title title to be added in the head of the document
	 * @param string $lang two-letter language code to be added to the output
	 * @return string
	 * @access public
	 * @version 1.4
	 */
	function formatted_page($doctype='xhtml1.1', $externalcss=true, $title='', $lang='en') {
		switch ($doctype) {
			case 'xhtml1.0strict':
				$doctype_output = '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
			"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">';
				break;
			case 'xhtml1.1':
			default:
				$doctype_output = '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
				"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">';
				break;
		}

		$output = $cssparsed = '';
		$this->output_css_plain = & $output;

		$output .= $doctype_output . "\n" . '<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="' . $lang . '"';
		$output .= ( $doctype === 'xhtml1.1') ? '>' : ' lang="' . $lang . '">';
		$output .= "\n<head>\n    <title>$title</title>";

		if ($externalcss) {
			$output .= "\n    <style type=\"text/css\">\n";
			$cssparsed = file_get_contents('cssparsed.css');
			$output .= $cssparsed; // Adds an invisible BOM or something, but not in css_optimised.php
			$output .= "\n</style>";
		} else {
			$output .= "\n" . '    <link rel="stylesheet" type="text/css" href="cssparsed.css" />';
//			}
		}
		$output .= "\n</head>\n<body><code id=\"copytext\">";
		$output .= $this->formatted();
		$output .= '</code>' . "\n" . '</body></html>';
		return $this->output_css_plain;
	}

	/**
	 * Returns the formatted CSS Code and saves it into $this->output_css and $this->output_css_plain
	 * @param bool $plain plain text or not
	 * @param string $default_media default @media to add to selectors without any @media
	 * @access private
	 * @version 2.0
	 */
	function _print($plain = false, $default_media='') {
		if ($this->output_css && $this->output_css_plain) {
			return;
		}

		$output = '';
		if (!$this->parser->get_cfg('preserve_css')) {
			$this->_convert_raw_css($default_media);
		}

		$template = & $this->template;

		if ($plain) {
			$template = array_map('strip_tags', $template);
		}

		if ($this->parser->get_cfg('timestamp')) {
			array_unshift($this->tokens, array(COMMENT, ' CSSTidy ' . $this->parser->version . ': ' . date('r') . ' '));
		}

		if (!empty($this->charset)) {
			$output .= $template[0] . '@charset ' . $template[5] . $this->charset . $template[6];
		}

		if (!empty($this->import)) {
			for ($i = 0, $size = count($this->import); $i < $size; $i++) {
				$import_components = explode(' ', $this->import[$i]);
				if (substr($import_components[0], 0, 4) === 'url(' && substr($import_components[0], -1, 1) === ')') {
					$import_components[0] = '\'' . trim(substr($import_components[0], 4, -1), "'\"") . '\'';
					$this->import[$i] = implode(' ', $import_components);
					$this->parser->log('Optimised @import : Removed "url("', 'Information');
				}
				$output .= $template[0] . '@import ' . $template[5] . $this->import[$i] . $template[6];
			}
		}

		if (!empty($this->namespace)) {
			if (substr($this->namespace, 0, 4) === 'url(' && substr($this->namespace, -1, 1) === ')') {
				$this->namespace = '\'' . substr($this->namespace, 4, -1) . '\'';
				$this->parser->log('Optimised @namespace : Removed "url("', 'Information');
			}
			$output .= $template[0] . '@namespace ' . $template[5] . $this->namespace . $template[6];
		}

		$output .= $template[13];
		$in_at_out = '';
		$out = & $output;

		foreach ($this->tokens as $key => $token) {
			switch ($token[0]) {
				case AT_START:
					$out .= $template[0] . $this->_htmlsp($token[1], $plain) . $template[1];
					$out = & $in_at_out;
					break;

				case SEL_START:
					if ($this->parser->get_cfg('lowercase_s'))
						$token[1] = strtolower($token[1]);
					$out .= ( $token[1]{0} !== '@') ? $template[2] . $this->_htmlsp($token[1], $plain) : $template[0] . $this->_htmlsp($token[1], $plain);
					$out .= $template[3];
					break;

				case PROPERTY:
					if ($this->parser->get_cfg('case_properties') === 2) {
						$token[1] = strtoupper($token[1]);
					} elseif ($this->parser->get_cfg('case_properties') === 1) {
						$token[1] = strtolower($token[1]);
					}
					$out .= $template[4] . $this->_htmlsp($token[1], $plain) . ':' . $template[5];
					break;

				case VALUE:
					$out .= $this->_htmlsp($token[1], $plain);
					if ($this->_seeknocomment($key, 1) == SEL_END && $this->parser->get_cfg('remove_last_;')) {
						$out .= str_replace(';', '', $template[6]);
					} else {
						$out .= $template[6];
					}
					break;

				case SEL_END:
					$out .= $template[7];
					if ($this->_seeknocomment($key, 1) != AT_END)
						$out .= $template[8];
					break;

				case AT_END:
					$out = & $output;
					$out .= $template[10] . str_replace("\n", "\n" . $template[10], $in_at_out);
					$in_at_out = '';
					$out .= $template[9];
					break;

				case COMMENT:
					$out .= $template[11] . '/*' . $this->_htmlsp($token[1], $plain) . '*/' . $template[12];
					break;
			}
		}

		$output = trim($output);

		if (!$plain) {
			$this->output_css = $output;
			$this->_print(true);
		} else {
			// If using spaces in the template, don't want these to appear in the plain output
			$this->output_css_plain = str_replace('&#160;', '', $output);
		}
	}

	/**
	 * Gets the next token type which is $move away from $key, excluding comments
	 * @param integer $key current position
	 * @param integer $move move this far
	 * @return mixed a token type
	 * @access private
	 * @version 1.0
	 */
	function _seeknocomment($key, $move) {
		$go = ($move > 0) ? 1 : -1;
		for ($i = $key + 1; abs($key - $i) - 1 < abs($move); $i += $go) {
			if (!isset($this->tokens[$i])) {
				return;
			}
			if ($this->tokens[$i][0] == COMMENT) {
				$move += 1;
				continue;
			}
			return $this->tokens[$i][0];
		}
	}

	/**
	 * Converts $this->css array to a raw array ($this->tokens)
	 * @param string $default_media default @media to add to selectors without any @media
	 * @access private
	 * @version 1.0
	 */
	function _convert_raw_css($default_media='') {
		$this->tokens = array();

		foreach ($this->css as $medium => $val) {
			if ($this->parser->get_cfg('sort_selectors'))
				ksort($val);
			if (intval($medium) < DEFAULT_AT) {
				$this->parser->_add_token(AT_START, $medium, true);
			}
			elseif ($default_media) {
				$this->parser->_add_token(AT_START, $default_media, true);
			}

			foreach ($val as $selector => $vali) {
				if ($this->parser->get_cfg('sort_properties'))
					ksort($vali);
				$this->parser->_add_token(SEL_START, $selector, true);

				foreach ($vali as $property => $valj) {
					$this->parser->_add_token(PROPERTY, $property, true);
					$this->parser->_add_token(VALUE, $valj, true);
				}

				$this->parser->_add_token(SEL_END, $selector, true);
			}

			if (intval($medium) < DEFAULT_AT) {
				$this->parser->_add_token(AT_END, $medium, true);
			}
			elseif ($default_media) {
				$this->parser->_add_token(AT_END, $default_media, true);
			}
		}
	}

	/**
	 * Same as htmlspecialchars, only that chars are not replaced if $plain !== true. This makes  print_code() cleaner.
	 * @param string $string
	 * @param bool $plain
	 * @return string
	 * @see csstidy_print::_print()
	 * @access private
	 * @version 1.0
	 */
	function _htmlsp($string, $plain) {
		if (!$plain) {
			return htmlspecialchars($string, ENT_QUOTES, 'utf-8');
		}
		return $string;
	}

	/**
	 * Get compression ratio
	 * @access public
	 * @return float
	 * @version 1.2
	 */
	function get_ratio() {
		if (!$this->output_css_plain) {
			$this->formatted();
		}
		return round((strlen($this->input_css) - strlen($this->output_css_plain)) / strlen($this->input_css), 3) * 100;
	}

	/**
	 * Get difference between the old and new code in bytes and prints the code if necessary.
	 * @access public
	 * @return string
	 * @version 1.1
	 */
	function get_diff() {
		if (!$this->output_css_plain) {
			$this->formatted();
		}

		$diff = strlen($this->output_css_plain) - strlen($this->input_css);

		if ($diff > 0) {
			return '+' . $diff;
		} elseif ($diff == 0) {
			return '+-' . $diff;
		}

		return $diff;
	}

	/**
	 * Get the size of either input or output CSS in KB
	 * @param string $loc default is "output"
	 * @access public
	 * @return integer
	 * @version 1.0
	 */
	function size($loc = 'output') {
		if ($loc === 'output' && !$this->output_css) {
			$this->formatted();
		}

		if ($loc === 'input') {
			return (strlen($this->input_css) / 1000);
		} else {
			return (strlen($this->output_css_plain) / 1000);
		}
	}

}