annotate vendor/squizlabs/php_codesniffer/CodeSniffer/Tokenizers/CSS.php @ 2:92f882872392

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