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