Drupal investigation

CSS.php 23KB

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