Skip to content
Browse files

Fixed bug #19361 : CSS tokenzier generates errors when PHP embedded i…

…n CSS file. Also added debug output during CSS tokenizing when using -vv.
  • Loading branch information...
1 parent 1f9242c commit 3d66c81cc1768258099bf5d294d96cd1376623cd @gsherwood gsherwood committed
View
9 CodeSniffer/Standards/Squiz/Tests/CSS/ColonSpacingUnitTest.css
@@ -21,3 +21,12 @@ margin-bottom:
text-decoration: none;
color: #444;
}
+
+/* checking embedded PHP */
+li {
+ background:url(<?php print $staticserver; ?>/images/<?php print $staticdir; ?>/bullet.gif) left <?php print $left; ?>px no-repeat;
+ margin:0px;
+ padding-left:10px;
+ margin-bottom:<?php echo $marginBottom; ?>px;
+ line-height:13px;
+}
View
15 CodeSniffer/Standards/Squiz/Tests/CSS/ColonSpacingUnitTest.php
@@ -40,11 +40,16 @@ class Squiz_Tests_CSS_ColonSpacingUnitTest extends AbstractSniffUnitTest
public function getErrorList()
{
return array(
- 3 => 1,
- 4 => 2,
- 5 => 1,
- 6 => 1,
- 8 => 1,
+ 3 => 1,
+ 4 => 2,
+ 5 => 1,
+ 6 => 1,
+ 8 => 1,
+ 27 => 1,
+ 28 => 1,
+ 29 => 1,
+ 30 => 1,
+ 31 => 1,
);
}//end getErrorList()
View
5 CodeSniffer/Standards/Squiz/Tests/CSS/DisallowMultipleStyleDefinitionsUnitTest.css
@@ -4,3 +4,8 @@
line-height: -25px;
cursor: pointer; margin: 10px; float: right;
}
+
+/* testing embedded PHP */
+li {
+ background:url(<?php print $staticserver; ?>/images/<?php print $staticdir; ?>/bullet.gif) left <?php print $left; ?>px no-repeat; margin:0px; padding-left:10px; margin-bottom:<?php echo $marginBottom; ?>px; line-height:13px;
+}
View
5 CodeSniffer/Standards/Squiz/Tests/CSS/DisallowMultipleStyleDefinitionsUnitTest.php
@@ -40,8 +40,9 @@ class Squiz_Tests_CSS_DisallowMultipleStyleDefinitionsUnitTest extends AbstractS
public function getErrorList()
{
return array(
- 3 => 1,
- 5 => 2,
+ 3 => 1,
+ 5 => 2,
+ 10 => 4,
);
}//end getErrorList()
View
60 CodeSniffer/Tokenizers/CSS.php
@@ -43,6 +43,10 @@ class PHP_CodeSniffer_Tokenizers_CSS extends PHP_CodeSniffer_Tokenizers_PHP
*/
public function tokenizeString($string, $eolChar='\n')
{
+ if (PHP_CODESNIFFER_VERBOSITY > 1) {
+ echo "\t*** START CSS TOKENIZING ***".PHP_EOL;
+ }
+
$tokens = parent::tokenizeString('<?php '.$string.' ?>', $eolChar);
$finalTokens = array();
@@ -52,6 +56,58 @@ public function tokenizeString($string, $eolChar='\n')
for ($stackPtr = 0; $stackPtr < $numTokens; $stackPtr++) {
$token = $tokens[$stackPtr];
+ if (PHP_CODESNIFFER_VERBOSITY > 1) {
+ $type = $token['type'];
+ $content = str_replace($eolChar, '\n', $token['content']);
+ echo "\tProcess token $stackPtr: $type => $content".PHP_EOL;
+ }
+
+ // Sometimes, there are PHP tags embedded in the code, which causes issues
+ // with how PHP tokenizeses the string. After the first closing tag is found,
+ // everything outside PHP tags is set as inline HTML tokens (1 for each line).
+ // So we need to go through and find these tokens so we can re-tokenize them.
+ if ($token['code'] === T_CLOSE_TAG && $stackPtr !== ($numTokens - 1)) {
+ $content = '<?php ';
+ for ($x = ($stackPtr + 1); $x < $numTokens; $x++) {
+ if ($tokens[$x]['code'] === T_INLINE_HTML) {
+ $content .= $tokens[$x]['content'];
+ } else {
+ $x--;
+ break;
+ }
+ }
+
+ if ($x < ($numTokens - 1)) {
+ // This is not the last closing tag in the file, so we
+ // have to add another closing tag here. If it is the last closing
+ // tag, this additional one would have been added during the
+ // original tokenize call.
+ $content .= ' ?>';
+ }
+
+ if (PHP_CODESNIFFER_VERBOSITY > 1) {
+ echo "\t\t=> Found premature closing tag at $stackPtr".PHP_EOL;
+ $cleanContent = str_replace($eolChar, '\n', $content);
+ echo "\t\tcontent: $cleanContent".PHP_EOL;
+ $oldNumTokens = $numTokens;
+ }
+
+ // Tokenize the string and remove the extra PHP tags we dont need.
+ $moreTokens = parent::tokenizeString($content, $eolChar);
+ array_shift($moreTokens);
+ array_pop($moreTokens);
+ array_pop($moreTokens);
+
+ // Rebuild the tokens array.
+ array_splice($tokens, ($stackPtr + 1), ($x - $stackPtr), $moreTokens);
+ $numTokens = count($tokens);
+ if (PHP_CODESNIFFER_VERBOSITY > 1) {
+ $count = count($moreTokens);
+ $diff = ($x - $stackPtr);
+ echo "\t\t* added $count tokens, replaced $diff; size changed from $oldNumTokens to $numTokens *".PHP_EOL;
+ }
+ }//end if
+
if ($token['code'] === T_FUNCTION) {
// There are no functions in CSS, so convert this to a string.
$finalTokens[$newStackPtr] = array(
@@ -279,6 +335,10 @@ public function tokenizeString($string, $eolChar='\n')
}//end switch
}//end for
+ if (PHP_CODESNIFFER_VERBOSITY > 1) {
+ echo "\t*** END CSS TOKENIZING ***".PHP_EOL;
+ }
+
return $finalTokens;
}//end tokenizeString()
View
2 package.xml
@@ -35,12 +35,14 @@ http://pear.php.net/dtd/package-2.0.xsd">
-- In particular, allows the Drupal CS to work without needing to symlink it into the PHPCS install
-- Thanks to Peter Philipp for the patch
- Squiz ObjectInstantiationSniff now allows objects to be returned without assinging them to a variable
+ - Debug option -vv now shows tokenizer status for CSS files
- Fixed an issue in AbstractPatternSniff where comments were not being ignored in some cases
- Fixed an issue in Zend ClosingTagSniff where the closing tag was not always being detected correctly
-- Thanks to Jonathan Robson for the patch
- Fixed bug #19290 : Generic indent sniffer fails for anonymous functions
- Fixed bug #19324 : Setting show_warnings configuration option does not work
- Fixed bug #19354 : Not recognizing references passed to method
+ - Fixed bug #19361 : CSS tokenzier generates errors when PHP embedded in CSS file
</notes>
<contents>
<dir name="/">

0 comments on commit 3d66c81

Please sign in to comment.
Something went wrong with that request. Please try again.