Drupal investigation

StringHandler.php 2.6KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\CssSelector\Parser\Handler;
  11. use Symfony\Component\CssSelector\Exception\InternalErrorException;
  12. use Symfony\Component\CssSelector\Exception\SyntaxErrorException;
  13. use Symfony\Component\CssSelector\Parser\Reader;
  14. use Symfony\Component\CssSelector\Parser\Token;
  15. use Symfony\Component\CssSelector\Parser\TokenStream;
  16. use Symfony\Component\CssSelector\Parser\Tokenizer\TokenizerEscaping;
  17. use Symfony\Component\CssSelector\Parser\Tokenizer\TokenizerPatterns;
  18. /**
  19. * CSS selector comment handler.
  20. *
  21. * This component is a port of the Python cssselect library,
  22. * which is copyright Ian Bicking, @see https://github.com/SimonSapin/cssselect.
  23. *
  24. * @author Jean-François Simon <jeanfrancois.simon@sensiolabs.com>
  25. *
  26. * @internal
  27. */
  28. class StringHandler implements HandlerInterface
  29. {
  30. /**
  31. * @var TokenizerPatterns
  32. */
  33. private $patterns;
  34. /**
  35. * @var TokenizerEscaping
  36. */
  37. private $escaping;
  38. /**
  39. * @param TokenizerPatterns $patterns
  40. * @param TokenizerEscaping $escaping
  41. */
  42. public function __construct(TokenizerPatterns $patterns, TokenizerEscaping $escaping)
  43. {
  44. $this->patterns = $patterns;
  45. $this->escaping = $escaping;
  46. }
  47. /**
  48. * {@inheritdoc}
  49. */
  50. public function handle(Reader $reader, TokenStream $stream)
  51. {
  52. $quote = $reader->getSubstring(1);
  53. if (!in_array($quote, array("'", '"'))) {
  54. return false;
  55. }
  56. $reader->moveForward(1);
  57. $match = $reader->findPattern($this->patterns->getQuotedStringPattern($quote));
  58. if (!$match) {
  59. throw new InternalErrorException(sprintf('Should have found at least an empty match at %s.', $reader->getPosition()));
  60. }
  61. // check unclosed strings
  62. if (strlen($match[0]) === $reader->getRemainingLength()) {
  63. throw SyntaxErrorException::unclosedString($reader->getPosition() - 1);
  64. }
  65. // check quotes pairs validity
  66. if ($quote !== $reader->getSubstring(1, strlen($match[0]))) {
  67. throw SyntaxErrorException::unclosedString($reader->getPosition() - 1);
  68. }
  69. $string = $this->escaping->escapeUnicodeAndNewLine($match[0]);
  70. $stream->push(new Token(Token::TYPE_STRING, $string, $reader->getPosition()));
  71. $reader->moveForward(strlen($match[0]) + 1);
  72. return true;
  73. }
  74. }