Skip to content

Commit c01aee2

Browse files
committed
The CSS tokenizer is now more reliable when encountering 'list' and 'break' strings
1 parent b8fa536 commit c01aee2

File tree

2 files changed

+11
-146
lines changed

2 files changed

+11
-146
lines changed

CodeSniffer/Tokenizers/CSS.php

Lines changed: 10 additions & 146 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,14 @@ public function tokenizeString($string, $eolChar='\n')
6262
for ($stackPtr = 0; $stackPtr < $numTokens; $stackPtr++) {
6363
$token = $tokens[$stackPtr];
6464

65+
// CSS files don't have lists or break tags, so convert these to
66+
// standard strings early so they can be converted into T_STYLE
67+
// tokens and joined with other strings if needed.
68+
if ($token['code'] === T_BREAK || $token['code'] === T_LIST) {
69+
$token['type'] = 'T_STRING';
70+
$token['code'] = T_STRING;
71+
}
72+
6573
if (PHP_CODESNIFFER_VERBOSITY > 1) {
6674
$type = $token['type'];
6775
$content = str_replace($eolChar, '\n', $token['content']);
@@ -355,159 +363,15 @@ public function tokenizeString($string, $eolChar='\n')
355363
/**
356364
* Performs additional processing after main tokenizing.
357365
*
358-
* This additional processing converts T_LIST tokens to T_STRING
359-
* because there are no list constructs in CSS and list-* styles
360-
* look like lists to the PHP tokenizer.
361-
*
362366
* @param array &$tokens The array of tokens to process.
363367
* @param string $eolChar The EOL character to use for splitting strings.
364368
*
365369
* @return void
366370
*/
367371
public function processAdditional(&$tokens, $eolChar)
368372
{
369-
if (PHP_CODESNIFFER_VERBOSITY > 1) {
370-
echo "\t*** START ADDITIONAL CSS PROCESSING ***".PHP_EOL;
371-
}
372-
373-
$numTokens = (count($tokens) - 1);
374-
$changeMade = false;
375-
376-
for ($i = 0; $i < $numTokens; $i++) {
377-
if ($tokens[($i + 1)]['code'] !== T_STYLE) {
378-
continue;
379-
}
380-
381-
$style = ($i + 1);
382-
383-
if ($tokens[$i]['code'] === T_LIST) {
384-
$tokens[$style]['content'] = $tokens[$i]['content'].$tokens[$style]['content'];
385-
$tokens[$style]['column'] = $tokens[$i]['column'];
386-
387-
if (PHP_CODESNIFFER_VERBOSITY > 1) {
388-
$line = $tokens[$i]['line'];
389-
echo "\t* T_LIST token $i on line $line merged into T_STYLE token $style *".PHP_EOL;
390-
}
391-
392-
// Now fix the brackets that surround this token as they will
393-
// be pointing too far ahead now that we have removed a token.
394-
for ($t = $i; $t >= 0; $t--) {
395-
if (isset($tokens[$t]['bracket_closer']) === true) {
396-
$old = $tokens[$t]['bracket_closer'];
397-
$tokens[$t]['bracket_closer']--;
398-
if (PHP_CODESNIFFER_VERBOSITY > 1) {
399-
$new = $tokens[$t]['bracket_closer'];
400-
$type = $tokens[$t]['type'];
401-
$line = $tokens[$t]['line'];
402-
echo "\t\t* $type token $t on line $line closer changed from $old to $new *".PHP_EOL;
403-
}
404-
405-
// Only need to fix one set of brackets.
406-
break;
407-
}
408-
}
409-
410-
// Now fix all future brackets as they are no longer pointing
411-
// to the correct tokens either.
412-
for ($t = $i; $t <= $numTokens; $t++) {
413-
if (isset($tokens[$t]) === false) {
414-
break;
415-
}
416-
417-
if ($tokens[$t]['code'] === T_OPEN_CURLY_BRACKET) {
418-
$old = $tokens[$t]['bracket_closer'];
419-
$tokens[$t]['bracket_closer']--;
420-
if (PHP_CODESNIFFER_VERBOSITY > 1) {
421-
$new = $tokens[$t]['bracket_closer'];
422-
$type = $tokens[$t]['type'];
423-
$line = $tokens[$t]['line'];
424-
echo "\t\t* $type token $t on line $line closer changed from $old to $new *".PHP_EOL;
425-
}
426-
427-
$t = $old;
428-
}
429-
}
430-
431-
unset($tokens[$i]);
432-
$changeMade = true;
433-
$i++;
434-
} else if ($tokens[$i]['code'] === T_BREAK) {
435-
// Break is sometimes used in style definitions, like page-break-inside
436-
// so we need merge the elements around it into the next T_STYLE.
437-
$newStyle = 'break'.$tokens[$style]['content'];
438-
for ($x = ($i - 1); $x >= 0; $x--) {
439-
if ($tokens[$x]['code'] !== T_STRING && $tokens[$x]['code'] !== T_MINUS) {
440-
break;
441-
}
442-
443-
$newStyle = $tokens[$x]['content'].$newStyle;
444-
}
445-
446-
$x++;
447-
$tokens[$style]['content'] = $newStyle;
448-
$tokens[$style]['column'] = $tokens[$x]['column'];
449-
450-
if (PHP_CODESNIFFER_VERBOSITY > 1) {
451-
$line = $tokens[$i]['line'];
452-
echo "\t* tokens $x - $i on line $line merged into T_STYLE token $style due to T_BREAK at token $i *".PHP_EOL;
453-
}
454-
455-
// Now fix the brackets that surround this token as they will
456-
// be pointing too far ahead now that we have removed tokens.
457-
$diff = ($style - $x);
458-
for ($t = $style; $t >= 0; $t--) {
459-
if (isset($tokens[$t]['bracket_closer']) === true) {
460-
$old = $tokens[$t]['bracket_closer'];
461-
$tokens[$t]['bracket_closer'] -= $diff;
462-
if (PHP_CODESNIFFER_VERBOSITY > 1) {
463-
$new = $tokens[$t]['bracket_closer'];
464-
$type = $tokens[$t]['type'];
465-
$line = $tokens[$t]['line'];
466-
echo "\t\t* $type token $t on line $line closer changed from $old to $new *".PHP_EOL;
467-
}
468-
469-
// Only need to fix one set of brackets.
470-
break;
471-
}
472-
}
473-
474-
// Now fix all future brackets as they are no longer pointing
475-
// to the correct tokens either.
476-
for ($t = $style; $t <= $numTokens; $t++) {
477-
if (isset($tokens[$t]) === false) {
478-
break;
479-
}
480-
481-
if ($tokens[$t]['code'] === T_OPEN_CURLY_BRACKET) {
482-
$old = $tokens[$t]['bracket_closer'];
483-
$tokens[$t]['bracket_closer'] -= $diff;
484-
if (PHP_CODESNIFFER_VERBOSITY > 1) {
485-
$new = $tokens[$t]['bracket_closer'];
486-
$type = $tokens[$t]['type'];
487-
$line = $tokens[$t]['line'];
488-
echo "\t\t* $type token $t on line $line closer changed from $old to $new *".PHP_EOL;
489-
}
490-
491-
$t = $old;
492-
}
493-
}
494-
495-
for ($x; $x <= $i; $x++) {
496-
unset($tokens[$x]);
497-
}
498-
499-
$changeMade = true;
500-
$i++;
501-
}//end if
502-
}//end for
503-
504-
if ($changeMade === true) {
505-
$tokens = array_values($tokens);
506-
}
507-
508-
if (PHP_CODESNIFFER_VERBOSITY > 1) {
509-
echo "\t*** END ADDITIONAL CSS PROCESSING ***".PHP_EOL;
510-
}
373+
// We override this method because we don't want the PHP version to
374+
// run during CSS processing because it is wasted processing time.
511375

512376
}//end processAdditional()
513377

package.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ http://pear.php.net/dtd/package-2.0.xsd">
3131
- Added support for the PHP 5.4 callable type hint
3232
- Fixed problem where some file content could be ignored when checking STDIN
3333
- Version information is now printed when installed via composer or run from a Git clone (request #20050)
34+
- The CSS tokenizer is now more reliable when encountering 'list' and 'break' strings
3435
- Coding standard ignore comments can now appear instead doc blocks as well as inline comments
3536
-- Thanks to Stuart Langley for the patch
3637
- Generic LineLengthSniff now ignores SVN URL and Head URL comments

0 commit comments

Comments
 (0)