Chris@17
|
1 <?php
|
Chris@17
|
2 /**
|
Chris@17
|
3 * Tokenizes CSS code.
|
Chris@17
|
4 *
|
Chris@17
|
5 * @author Greg Sherwood <gsherwood@squiz.net>
|
Chris@17
|
6 * @copyright 2006-2015 Squiz Pty Ltd (ABN 77 084 670 600)
|
Chris@17
|
7 * @license https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
|
Chris@17
|
8 */
|
Chris@17
|
9
|
Chris@17
|
10 namespace PHP_CodeSniffer\Tokenizers;
|
Chris@17
|
11
|
Chris@17
|
12 use PHP_CodeSniffer\Util;
|
Chris@17
|
13 use PHP_CodeSniffer\Config;
|
Chris@17
|
14 use PHP_CodeSniffer\Exceptions\TokenizerException;
|
Chris@17
|
15
|
Chris@17
|
16 class CSS extends PHP
|
Chris@17
|
17 {
|
Chris@17
|
18
|
Chris@17
|
19
|
Chris@17
|
20 /**
|
Chris@17
|
21 * Initialise the tokenizer.
|
Chris@17
|
22 *
|
Chris@17
|
23 * Pre-checks the content to see if it looks minified.
|
Chris@17
|
24 *
|
Chris@17
|
25 * @param string $content The content to tokenize,
|
Chris@17
|
26 * @param \PHP_CodeSniffer\Config $config The config data for the run.
|
Chris@17
|
27 * @param string $eolChar The EOL char used in the content.
|
Chris@17
|
28 *
|
Chris@17
|
29 * @return void
|
Chris@17
|
30 * @throws TokenizerException If the file appears to be minified.
|
Chris@17
|
31 */
|
Chris@17
|
32 public function __construct($content, Config $config, $eolChar='\n')
|
Chris@17
|
33 {
|
Chris@17
|
34 if ($this->isMinifiedContent($content, $eolChar) === true) {
|
Chris@17
|
35 throw new TokenizerException('File appears to be minified and cannot be processed');
|
Chris@17
|
36 }
|
Chris@17
|
37
|
Chris@17
|
38 return parent::__construct($content, $config, $eolChar);
|
Chris@17
|
39
|
Chris@17
|
40 }//end __construct()
|
Chris@17
|
41
|
Chris@17
|
42
|
Chris@17
|
43 /**
|
Chris@17
|
44 * Creates an array of tokens when given some CSS code.
|
Chris@17
|
45 *
|
Chris@17
|
46 * Uses the PHP tokenizer to do all the tricky work
|
Chris@17
|
47 *
|
Chris@17
|
48 * @param string $string The string to tokenize.
|
Chris@17
|
49 *
|
Chris@17
|
50 * @return array
|
Chris@17
|
51 */
|
Chris@17
|
52 public function tokenize($string)
|
Chris@17
|
53 {
|
Chris@17
|
54 if (PHP_CODESNIFFER_VERBOSITY > 1) {
|
Chris@17
|
55 echo "\t*** START CSS TOKENIZING 1ST PASS ***".PHP_EOL;
|
Chris@17
|
56 }
|
Chris@17
|
57
|
Chris@17
|
58 // If the content doesn't have an EOL char on the end, add one so
|
Chris@17
|
59 // the open and close tags we add are parsed correctly.
|
Chris@17
|
60 $eolAdded = false;
|
Chris@17
|
61 if (substr($string, (strlen($this->eolChar) * -1)) !== $this->eolChar) {
|
Chris@17
|
62 $string .= $this->eolChar;
|
Chris@17
|
63 $eolAdded = true;
|
Chris@17
|
64 }
|
Chris@17
|
65
|
Chris@17
|
66 $string = str_replace('<?php', '^PHPCS_CSS_T_OPEN_TAG^', $string);
|
Chris@17
|
67 $string = str_replace('?>', '^PHPCS_CSS_T_CLOSE_TAG^', $string);
|
Chris@17
|
68 $tokens = parent::tokenize('<?php '.$string.'?>');
|
Chris@17
|
69
|
Chris@17
|
70 $finalTokens = [];
|
Chris@17
|
71 $finalTokens[0] = [
|
Chris@17
|
72 'code' => T_OPEN_TAG,
|
Chris@17
|
73 'type' => 'T_OPEN_TAG',
|
Chris@17
|
74 'content' => '',
|
Chris@17
|
75 ];
|
Chris@17
|
76
|
Chris@17
|
77 $newStackPtr = 1;
|
Chris@17
|
78 $numTokens = count($tokens);
|
Chris@17
|
79 $multiLineComment = false;
|
Chris@17
|
80 for ($stackPtr = 1; $stackPtr < $numTokens; $stackPtr++) {
|
Chris@17
|
81 $token = $tokens[$stackPtr];
|
Chris@17
|
82
|
Chris@17
|
83 // CSS files don't have lists, breaks etc, so convert these to
|
Chris@17
|
84 // standard strings early so they can be converted into T_STYLE
|
Chris@17
|
85 // tokens and joined with other strings if needed.
|
Chris@17
|
86 if ($token['code'] === T_BREAK
|
Chris@17
|
87 || $token['code'] === T_LIST
|
Chris@17
|
88 || $token['code'] === T_DEFAULT
|
Chris@17
|
89 || $token['code'] === T_SWITCH
|
Chris@17
|
90 || $token['code'] === T_FOR
|
Chris@17
|
91 || $token['code'] === T_FOREACH
|
Chris@17
|
92 || $token['code'] === T_WHILE
|
Chris@17
|
93 || $token['code'] === T_DEC
|
Chris@17
|
94 || $token['code'] === T_NEW
|
Chris@17
|
95 ) {
|
Chris@17
|
96 $token['type'] = 'T_STRING';
|
Chris@17
|
97 $token['code'] = T_STRING;
|
Chris@17
|
98 }
|
Chris@17
|
99
|
Chris@17
|
100 if (PHP_CODESNIFFER_VERBOSITY > 1) {
|
Chris@17
|
101 $type = $token['type'];
|
Chris@17
|
102 $content = Util\Common::prepareForOutput($token['content']);
|
Chris@17
|
103 echo "\tProcess token $stackPtr: $type => $content".PHP_EOL;
|
Chris@17
|
104 }
|
Chris@17
|
105
|
Chris@17
|
106 if ($token['code'] === T_BITWISE_XOR
|
Chris@17
|
107 && $tokens[($stackPtr + 1)]['content'] === 'PHPCS_CSS_T_OPEN_TAG'
|
Chris@17
|
108 ) {
|
Chris@17
|
109 $content = '<?php';
|
Chris@17
|
110 for ($stackPtr += 3; $stackPtr < $numTokens; $stackPtr++) {
|
Chris@17
|
111 if ($tokens[$stackPtr]['code'] === T_BITWISE_XOR
|
Chris@17
|
112 && $tokens[($stackPtr + 1)]['content'] === 'PHPCS_CSS_T_CLOSE_TAG'
|
Chris@17
|
113 ) {
|
Chris@17
|
114 // Add the end tag and ignore the * we put at the end.
|
Chris@17
|
115 $content .= '?>';
|
Chris@17
|
116 $stackPtr += 2;
|
Chris@17
|
117 break;
|
Chris@17
|
118 } else {
|
Chris@17
|
119 $content .= $tokens[$stackPtr]['content'];
|
Chris@17
|
120 }
|
Chris@17
|
121 }
|
Chris@17
|
122
|
Chris@17
|
123 if (PHP_CODESNIFFER_VERBOSITY > 1) {
|
Chris@17
|
124 echo "\t\t=> Found embedded PHP code: ";
|
Chris@17
|
125 $cleanContent = Util\Common::prepareForOutput($content);
|
Chris@17
|
126 echo $cleanContent.PHP_EOL;
|
Chris@17
|
127 }
|
Chris@17
|
128
|
Chris@17
|
129 $finalTokens[$newStackPtr] = [
|
Chris@17
|
130 'type' => 'T_EMBEDDED_PHP',
|
Chris@17
|
131 'code' => T_EMBEDDED_PHP,
|
Chris@17
|
132 'content' => $content,
|
Chris@17
|
133 ];
|
Chris@17
|
134
|
Chris@17
|
135 $newStackPtr++;
|
Chris@17
|
136 continue;
|
Chris@17
|
137 }//end if
|
Chris@17
|
138
|
Chris@17
|
139 if ($token['code'] === T_GOTO_LABEL) {
|
Chris@17
|
140 // Convert these back to T_STRING followed by T_COLON so we can
|
Chris@17
|
141 // more easily process style definitions.
|
Chris@17
|
142 $finalTokens[$newStackPtr] = [
|
Chris@17
|
143 'type' => 'T_STRING',
|
Chris@17
|
144 'code' => T_STRING,
|
Chris@17
|
145 'content' => substr($token['content'], 0, -1),
|
Chris@17
|
146 ];
|
Chris@17
|
147 $newStackPtr++;
|
Chris@17
|
148 $finalTokens[$newStackPtr] = [
|
Chris@17
|
149 'type' => 'T_COLON',
|
Chris@17
|
150 'code' => T_COLON,
|
Chris@17
|
151 'content' => ':',
|
Chris@17
|
152 ];
|
Chris@17
|
153 $newStackPtr++;
|
Chris@17
|
154 continue;
|
Chris@17
|
155 }
|
Chris@17
|
156
|
Chris@17
|
157 if ($token['code'] === T_FUNCTION) {
|
Chris@17
|
158 // There are no functions in CSS, so convert this to a string.
|
Chris@17
|
159 $finalTokens[$newStackPtr] = [
|
Chris@17
|
160 'type' => 'T_STRING',
|
Chris@17
|
161 'code' => T_STRING,
|
Chris@17
|
162 'content' => $token['content'],
|
Chris@17
|
163 ];
|
Chris@17
|
164
|
Chris@17
|
165 $newStackPtr++;
|
Chris@17
|
166 continue;
|
Chris@17
|
167 }
|
Chris@17
|
168
|
Chris@17
|
169 if ($token['code'] === T_COMMENT
|
Chris@17
|
170 && substr($token['content'], 0, 2) === '/*'
|
Chris@17
|
171 ) {
|
Chris@17
|
172 // Multi-line comment. Record it so we can ignore other
|
Chris@17
|
173 // comment tags until we get out of this one.
|
Chris@17
|
174 $multiLineComment = true;
|
Chris@17
|
175 }
|
Chris@17
|
176
|
Chris@17
|
177 if ($token['code'] === T_COMMENT
|
Chris@17
|
178 && $multiLineComment === false
|
Chris@17
|
179 && (substr($token['content'], 0, 2) === '//'
|
Chris@17
|
180 || $token['content']{0} === '#')
|
Chris@17
|
181 ) {
|
Chris@17
|
182 $content = ltrim($token['content'], '#/');
|
Chris@17
|
183
|
Chris@17
|
184 // Guard against PHP7+ syntax errors by stripping
|
Chris@17
|
185 // leading zeros so the content doesn't look like an invalid int.
|
Chris@17
|
186 $leadingZero = false;
|
Chris@17
|
187 if ($content{0} === '0') {
|
Chris@17
|
188 $content = '1'.$content;
|
Chris@17
|
189 $leadingZero = true;
|
Chris@17
|
190 }
|
Chris@17
|
191
|
Chris@17
|
192 $commentTokens = parent::tokenize('<?php '.$content.'?>');
|
Chris@17
|
193
|
Chris@17
|
194 // The first and last tokens are the open/close tags.
|
Chris@17
|
195 array_shift($commentTokens);
|
Chris@17
|
196 array_pop($commentTokens);
|
Chris@17
|
197
|
Chris@17
|
198 if ($leadingZero === true) {
|
Chris@17
|
199 $commentTokens[0]['content'] = substr($commentTokens[0]['content'], 1);
|
Chris@17
|
200 $content = substr($content, 1);
|
Chris@17
|
201 }
|
Chris@17
|
202
|
Chris@17
|
203 if ($token['content']{0} === '#') {
|
Chris@17
|
204 // The # character is not a comment in CSS files, so
|
Chris@17
|
205 // determine what it means in this context.
|
Chris@17
|
206 $firstContent = $commentTokens[0]['content'];
|
Chris@17
|
207
|
Chris@17
|
208 // If the first content is just a number, it is probably a
|
Chris@17
|
209 // colour like 8FB7DB, which PHP splits into 8 and FB7DB.
|
Chris@17
|
210 if (($commentTokens[0]['code'] === T_LNUMBER
|
Chris@17
|
211 || $commentTokens[0]['code'] === T_DNUMBER)
|
Chris@17
|
212 && $commentTokens[1]['code'] === T_STRING
|
Chris@17
|
213 ) {
|
Chris@17
|
214 $firstContent .= $commentTokens[1]['content'];
|
Chris@17
|
215 array_shift($commentTokens);
|
Chris@17
|
216 }
|
Chris@17
|
217
|
Chris@17
|
218 // If the first content looks like a colour and not a class
|
Chris@17
|
219 // definition, join the tokens together.
|
Chris@17
|
220 if (preg_match('/^[ABCDEF0-9]+$/i', $firstContent) === 1
|
Chris@17
|
221 && $commentTokens[1]['content'] !== '-'
|
Chris@17
|
222 ) {
|
Chris@17
|
223 array_shift($commentTokens);
|
Chris@17
|
224 // Work out what we trimmed off above and remember to re-add it.
|
Chris@17
|
225 $trimmed = substr($token['content'], 0, (strlen($token['content']) - strlen($content)));
|
Chris@17
|
226 $finalTokens[$newStackPtr] = [
|
Chris@17
|
227 'type' => 'T_COLOUR',
|
Chris@17
|
228 'code' => T_COLOUR,
|
Chris@17
|
229 'content' => $trimmed.$firstContent,
|
Chris@17
|
230 ];
|
Chris@17
|
231 } else {
|
Chris@17
|
232 $finalTokens[$newStackPtr] = [
|
Chris@17
|
233 'type' => 'T_HASH',
|
Chris@17
|
234 'code' => T_HASH,
|
Chris@17
|
235 'content' => '#',
|
Chris@17
|
236 ];
|
Chris@17
|
237 }
|
Chris@17
|
238 } else {
|
Chris@17
|
239 $finalTokens[$newStackPtr] = [
|
Chris@17
|
240 'type' => 'T_STRING',
|
Chris@17
|
241 'code' => T_STRING,
|
Chris@17
|
242 'content' => '//',
|
Chris@17
|
243 ];
|
Chris@17
|
244 }//end if
|
Chris@17
|
245
|
Chris@17
|
246 $newStackPtr++;
|
Chris@17
|
247
|
Chris@17
|
248 array_splice($tokens, $stackPtr, 1, $commentTokens);
|
Chris@17
|
249 $numTokens = count($tokens);
|
Chris@17
|
250 $stackPtr--;
|
Chris@17
|
251 continue;
|
Chris@17
|
252 }//end if
|
Chris@17
|
253
|
Chris@17
|
254 if ($token['code'] === T_COMMENT
|
Chris@17
|
255 && substr($token['content'], -2) === '*/'
|
Chris@17
|
256 ) {
|
Chris@17
|
257 // Multi-line comment is done.
|
Chris@17
|
258 $multiLineComment = false;
|
Chris@17
|
259 }
|
Chris@17
|
260
|
Chris@17
|
261 $finalTokens[$newStackPtr] = $token;
|
Chris@17
|
262 $newStackPtr++;
|
Chris@17
|
263 }//end for
|
Chris@17
|
264
|
Chris@17
|
265 if (PHP_CODESNIFFER_VERBOSITY > 1) {
|
Chris@17
|
266 echo "\t*** END CSS TOKENIZING 1ST PASS ***".PHP_EOL;
|
Chris@17
|
267 echo "\t*** START CSS TOKENIZING 2ND PASS ***".PHP_EOL;
|
Chris@17
|
268 }
|
Chris@17
|
269
|
Chris@17
|
270 // A flag to indicate if we are inside a style definition,
|
Chris@17
|
271 // which is defined using curly braces.
|
Chris@17
|
272 $inStyleDef = false;
|
Chris@17
|
273
|
Chris@17
|
274 // A flag to indicate if an At-rule like "@media" is used, which will result
|
Chris@17
|
275 // in nested curly brackets.
|
Chris@17
|
276 $asperandStart = false;
|
Chris@17
|
277
|
Chris@17
|
278 $numTokens = count($finalTokens);
|
Chris@17
|
279 for ($stackPtr = 0; $stackPtr < $numTokens; $stackPtr++) {
|
Chris@17
|
280 $token = $finalTokens[$stackPtr];
|
Chris@17
|
281
|
Chris@17
|
282 if (PHP_CODESNIFFER_VERBOSITY > 1) {
|
Chris@17
|
283 $type = $token['type'];
|
Chris@17
|
284 $content = Util\Common::prepareForOutput($token['content']);
|
Chris@17
|
285 echo "\tProcess token $stackPtr: $type => $content".PHP_EOL;
|
Chris@17
|
286 }
|
Chris@17
|
287
|
Chris@17
|
288 switch ($token['code']) {
|
Chris@17
|
289 case T_OPEN_CURLY_BRACKET:
|
Chris@17
|
290 // Opening curly brackets for an At-rule do not start a style
|
Chris@17
|
291 // definition. We also reset the asperand flag here because the next
|
Chris@17
|
292 // opening curly bracket could be indeed the start of a style
|
Chris@17
|
293 // definition.
|
Chris@17
|
294 if ($asperandStart === true) {
|
Chris@17
|
295 if (PHP_CODESNIFFER_VERBOSITY > 1) {
|
Chris@17
|
296 if ($inStyleDef === true) {
|
Chris@17
|
297 echo "\t\t* style definition closed *".PHP_EOL;
|
Chris@17
|
298 }
|
Chris@17
|
299
|
Chris@17
|
300 if ($asperandStart === true) {
|
Chris@17
|
301 echo "\t\t* at-rule definition closed *".PHP_EOL;
|
Chris@17
|
302 }
|
Chris@17
|
303 }
|
Chris@17
|
304
|
Chris@17
|
305 $inStyleDef = false;
|
Chris@17
|
306 $asperandStart = false;
|
Chris@17
|
307 } else {
|
Chris@17
|
308 $inStyleDef = true;
|
Chris@17
|
309 if (PHP_CODESNIFFER_VERBOSITY > 1) {
|
Chris@17
|
310 echo "\t\t* style definition opened *".PHP_EOL;
|
Chris@17
|
311 }
|
Chris@17
|
312 }
|
Chris@17
|
313 break;
|
Chris@17
|
314 case T_CLOSE_CURLY_BRACKET:
|
Chris@17
|
315 if (PHP_CODESNIFFER_VERBOSITY > 1) {
|
Chris@17
|
316 if ($inStyleDef === true) {
|
Chris@17
|
317 echo "\t\t* style definition closed *".PHP_EOL;
|
Chris@17
|
318 }
|
Chris@17
|
319
|
Chris@17
|
320 if ($asperandStart === true) {
|
Chris@17
|
321 echo "\t\t* at-rule definition closed *".PHP_EOL;
|
Chris@17
|
322 }
|
Chris@17
|
323 }
|
Chris@17
|
324
|
Chris@17
|
325 $inStyleDef = false;
|
Chris@17
|
326 $asperandStart = false;
|
Chris@17
|
327 break;
|
Chris@17
|
328 case T_MINUS:
|
Chris@17
|
329 // Minus signs are often used instead of spaces inside
|
Chris@17
|
330 // class names, IDs and styles.
|
Chris@17
|
331 if ($finalTokens[($stackPtr + 1)]['code'] === T_STRING) {
|
Chris@17
|
332 if ($finalTokens[($stackPtr - 1)]['code'] === T_STRING) {
|
Chris@17
|
333 $newContent = $finalTokens[($stackPtr - 1)]['content'].'-'.$finalTokens[($stackPtr + 1)]['content'];
|
Chris@17
|
334
|
Chris@17
|
335 if (PHP_CODESNIFFER_VERBOSITY > 1) {
|
Chris@17
|
336 echo "\t\t* token is a string joiner; ignoring this and previous token".PHP_EOL;
|
Chris@17
|
337 $old = Util\Common::prepareForOutput($finalTokens[($stackPtr + 1)]['content']);
|
Chris@17
|
338 $new = Util\Common::prepareForOutput($newContent);
|
Chris@17
|
339 echo "\t\t=> token ".($stackPtr + 1)." content changed from \"$old\" to \"$new\"".PHP_EOL;
|
Chris@17
|
340 }
|
Chris@17
|
341
|
Chris@17
|
342 $finalTokens[($stackPtr + 1)]['content'] = $newContent;
|
Chris@17
|
343 unset($finalTokens[$stackPtr]);
|
Chris@17
|
344 unset($finalTokens[($stackPtr - 1)]);
|
Chris@17
|
345 } else {
|
Chris@17
|
346 $newContent = '-'.$finalTokens[($stackPtr + 1)]['content'];
|
Chris@17
|
347
|
Chris@17
|
348 $finalTokens[($stackPtr + 1)]['content'] = $newContent;
|
Chris@17
|
349 unset($finalTokens[$stackPtr]);
|
Chris@17
|
350 }
|
Chris@17
|
351 } else if ($finalTokens[($stackPtr + 1)]['code'] === T_LNUMBER) {
|
Chris@17
|
352 // They can also be used to provide negative numbers.
|
Chris@17
|
353 if (PHP_CODESNIFFER_VERBOSITY > 1) {
|
Chris@17
|
354 echo "\t\t* token is part of a negative number; adding content to next token and ignoring *".PHP_EOL;
|
Chris@17
|
355 $content = Util\Common::prepareForOutput($finalTokens[($stackPtr + 1)]['content']);
|
Chris@17
|
356 echo "\t\t=> token ".($stackPtr + 1)." content changed from \"$content\" to \"-$content\"".PHP_EOL;
|
Chris@17
|
357 }
|
Chris@17
|
358
|
Chris@17
|
359 $finalTokens[($stackPtr + 1)]['content'] = '-'.$finalTokens[($stackPtr + 1)]['content'];
|
Chris@17
|
360 unset($finalTokens[$stackPtr]);
|
Chris@17
|
361 }//end if
|
Chris@17
|
362 break;
|
Chris@17
|
363 case T_COLON:
|
Chris@17
|
364 // Only interested in colons that are defining styles.
|
Chris@17
|
365 if ($inStyleDef === false) {
|
Chris@17
|
366 break;
|
Chris@17
|
367 }
|
Chris@17
|
368
|
Chris@17
|
369 for ($x = ($stackPtr - 1); $x >= 0; $x--) {
|
Chris@17
|
370 if (isset(Util\Tokens::$emptyTokens[$finalTokens[$x]['code']]) === false) {
|
Chris@17
|
371 break;
|
Chris@17
|
372 }
|
Chris@17
|
373 }
|
Chris@17
|
374
|
Chris@17
|
375 if (PHP_CODESNIFFER_VERBOSITY > 1) {
|
Chris@17
|
376 $type = $finalTokens[$x]['type'];
|
Chris@17
|
377 echo "\t\t=> token $x changed from $type to T_STYLE".PHP_EOL;
|
Chris@17
|
378 }
|
Chris@17
|
379
|
Chris@17
|
380 $finalTokens[$x]['type'] = 'T_STYLE';
|
Chris@17
|
381 $finalTokens[$x]['code'] = T_STYLE;
|
Chris@17
|
382 break;
|
Chris@17
|
383 case T_STRING:
|
Chris@17
|
384 if (strtolower($token['content']) === 'url') {
|
Chris@17
|
385 // Find the next content.
|
Chris@17
|
386 for ($x = ($stackPtr + 1); $x < $numTokens; $x++) {
|
Chris@17
|
387 if (isset(Util\Tokens::$emptyTokens[$finalTokens[$x]['code']]) === false) {
|
Chris@17
|
388 break;
|
Chris@17
|
389 }
|
Chris@17
|
390 }
|
Chris@17
|
391
|
Chris@17
|
392 // Needs to be in the format "url(" for it to be a URL.
|
Chris@17
|
393 if ($finalTokens[$x]['code'] !== T_OPEN_PARENTHESIS) {
|
Chris@17
|
394 continue 2;
|
Chris@17
|
395 }
|
Chris@17
|
396
|
Chris@17
|
397 // Make sure the content isn't empty.
|
Chris@17
|
398 for ($y = ($x + 1); $y < $numTokens; $y++) {
|
Chris@17
|
399 if (isset(Util\Tokens::$emptyTokens[$finalTokens[$y]['code']]) === false) {
|
Chris@17
|
400 break;
|
Chris@17
|
401 }
|
Chris@17
|
402 }
|
Chris@17
|
403
|
Chris@17
|
404 if ($finalTokens[$y]['code'] === T_CLOSE_PARENTHESIS) {
|
Chris@17
|
405 continue 2;
|
Chris@17
|
406 }
|
Chris@17
|
407
|
Chris@17
|
408 if (PHP_CODESNIFFER_VERBOSITY > 1) {
|
Chris@17
|
409 for ($i = ($stackPtr + 1); $i <= $y; $i++) {
|
Chris@17
|
410 $type = $finalTokens[$i]['type'];
|
Chris@17
|
411 $content = Util\Common::prepareForOutput($finalTokens[$i]['content']);
|
Chris@17
|
412 echo "\tProcess token $i: $type => $content".PHP_EOL;
|
Chris@17
|
413 }
|
Chris@17
|
414
|
Chris@17
|
415 echo "\t\t* token starts a URL *".PHP_EOL;
|
Chris@17
|
416 }
|
Chris@17
|
417
|
Chris@17
|
418 // Join all the content together inside the url() statement.
|
Chris@17
|
419 $newContent = '';
|
Chris@17
|
420 for ($i = ($x + 2); $i < $numTokens; $i++) {
|
Chris@17
|
421 if ($finalTokens[$i]['code'] === T_CLOSE_PARENTHESIS) {
|
Chris@17
|
422 break;
|
Chris@17
|
423 }
|
Chris@17
|
424
|
Chris@17
|
425 $newContent .= $finalTokens[$i]['content'];
|
Chris@17
|
426 if (PHP_CODESNIFFER_VERBOSITY > 1) {
|
Chris@17
|
427 $content = Util\Common::prepareForOutput($finalTokens[$i]['content']);
|
Chris@17
|
428 echo "\t\t=> token $i added to URL string and ignored: $content".PHP_EOL;
|
Chris@17
|
429 }
|
Chris@17
|
430
|
Chris@17
|
431 unset($finalTokens[$i]);
|
Chris@17
|
432 }
|
Chris@17
|
433
|
Chris@17
|
434 $stackPtr = $i;
|
Chris@17
|
435
|
Chris@17
|
436 // If the content inside the "url()" is in double quotes
|
Chris@17
|
437 // there will only be one token and so we don't have to do
|
Chris@17
|
438 // anything except change its type. If it is not empty,
|
Chris@17
|
439 // we need to do some token merging.
|
Chris@17
|
440 $finalTokens[($x + 1)]['type'] = 'T_URL';
|
Chris@17
|
441 $finalTokens[($x + 1)]['code'] = T_URL;
|
Chris@17
|
442
|
Chris@17
|
443 if ($newContent !== '') {
|
Chris@17
|
444 $finalTokens[($x + 1)]['content'] .= $newContent;
|
Chris@17
|
445 if (PHP_CODESNIFFER_VERBOSITY > 1) {
|
Chris@17
|
446 $content = Util\Common::prepareForOutput($finalTokens[($x + 1)]['content']);
|
Chris@17
|
447 echo "\t\t=> token content changed to: $content".PHP_EOL;
|
Chris@17
|
448 }
|
Chris@17
|
449 }
|
Chris@17
|
450 } else if ($finalTokens[$stackPtr]['content'][0] === '-'
|
Chris@17
|
451 && $finalTokens[($stackPtr + 1)]['code'] === T_STRING
|
Chris@17
|
452 ) {
|
Chris@17
|
453 if (isset($finalTokens[($stackPtr - 1)]) === true
|
Chris@17
|
454 && $finalTokens[($stackPtr - 1)]['code'] === T_STRING
|
Chris@17
|
455 ) {
|
Chris@17
|
456 $newContent = $finalTokens[($stackPtr - 1)]['content'].$finalTokens[$stackPtr]['content'].$finalTokens[($stackPtr + 1)]['content'];
|
Chris@17
|
457
|
Chris@17
|
458 if (PHP_CODESNIFFER_VERBOSITY > 1) {
|
Chris@17
|
459 echo "\t\t* token is a string joiner; ignoring this and previous token".PHP_EOL;
|
Chris@17
|
460 $old = Util\Common::prepareForOutput($finalTokens[($stackPtr + 1)]['content']);
|
Chris@17
|
461 $new = Util\Common::prepareForOutput($newContent);
|
Chris@17
|
462 echo "\t\t=> token ".($stackPtr + 1)." content changed from \"$old\" to \"$new\"".PHP_EOL;
|
Chris@17
|
463 }
|
Chris@17
|
464
|
Chris@17
|
465 $finalTokens[($stackPtr + 1)]['content'] = $newContent;
|
Chris@17
|
466 unset($finalTokens[$stackPtr]);
|
Chris@17
|
467 unset($finalTokens[($stackPtr - 1)]);
|
Chris@17
|
468 } else {
|
Chris@17
|
469 $newContent = $finalTokens[$stackPtr]['content'].$finalTokens[($stackPtr + 1)]['content'];
|
Chris@17
|
470
|
Chris@17
|
471 $finalTokens[($stackPtr + 1)]['content'] = $newContent;
|
Chris@17
|
472 unset($finalTokens[$stackPtr]);
|
Chris@17
|
473 }
|
Chris@17
|
474 }//end if
|
Chris@17
|
475 break;
|
Chris@17
|
476 case T_ASPERAND:
|
Chris@17
|
477 $asperandStart = true;
|
Chris@17
|
478 if (PHP_CODESNIFFER_VERBOSITY > 1) {
|
Chris@17
|
479 echo "\t\t* at-rule definition opened *".PHP_EOL;
|
Chris@17
|
480 }
|
Chris@17
|
481 break;
|
Chris@17
|
482 default:
|
Chris@17
|
483 // Nothing special to be done with this token.
|
Chris@17
|
484 break;
|
Chris@17
|
485 }//end switch
|
Chris@17
|
486 }//end for
|
Chris@17
|
487
|
Chris@17
|
488 // Reset the array keys to avoid gaps.
|
Chris@17
|
489 $finalTokens = array_values($finalTokens);
|
Chris@17
|
490 $numTokens = count($finalTokens);
|
Chris@17
|
491
|
Chris@17
|
492 // Blank out the content of the end tag.
|
Chris@17
|
493 $finalTokens[($numTokens - 1)]['content'] = '';
|
Chris@17
|
494
|
Chris@17
|
495 if ($eolAdded === true) {
|
Chris@17
|
496 // Strip off the extra EOL char we added for tokenizing.
|
Chris@17
|
497 $finalTokens[($numTokens - 2)]['content'] = substr(
|
Chris@17
|
498 $finalTokens[($numTokens - 2)]['content'],
|
Chris@17
|
499 0,
|
Chris@17
|
500 (strlen($this->eolChar) * -1)
|
Chris@17
|
501 );
|
Chris@17
|
502
|
Chris@17
|
503 if ($finalTokens[($numTokens - 2)]['content'] === '') {
|
Chris@17
|
504 unset($finalTokens[($numTokens - 2)]);
|
Chris@17
|
505 $finalTokens = array_values($finalTokens);
|
Chris@17
|
506 $numTokens = count($finalTokens);
|
Chris@17
|
507 }
|
Chris@17
|
508 }
|
Chris@17
|
509
|
Chris@17
|
510 if (PHP_CODESNIFFER_VERBOSITY > 1) {
|
Chris@17
|
511 echo "\t*** END CSS TOKENIZING 2ND PASS ***".PHP_EOL;
|
Chris@17
|
512 }
|
Chris@17
|
513
|
Chris@17
|
514 return $finalTokens;
|
Chris@17
|
515
|
Chris@17
|
516 }//end tokenize()
|
Chris@17
|
517
|
Chris@17
|
518
|
Chris@17
|
519 /**
|
Chris@17
|
520 * Performs additional processing after main tokenizing.
|
Chris@17
|
521 *
|
Chris@17
|
522 * @return void
|
Chris@17
|
523 */
|
Chris@17
|
524 public function processAdditional()
|
Chris@17
|
525 {
|
Chris@17
|
526 /*
|
Chris@17
|
527 We override this method because we don't want the PHP version to
|
Chris@17
|
528 run during CSS processing because it is wasted processing time.
|
Chris@17
|
529 */
|
Chris@17
|
530
|
Chris@17
|
531 }//end processAdditional()
|
Chris@17
|
532
|
Chris@17
|
533
|
Chris@17
|
534 }//end class
|