IdentifierHandler.php 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  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\Parser\Reader;
  12. use Symfony\Component\CssSelector\Parser\Token;
  13. use Symfony\Component\CssSelector\Parser\Tokenizer\TokenizerEscaping;
  14. use Symfony\Component\CssSelector\Parser\Tokenizer\TokenizerPatterns;
  15. use Symfony\Component\CssSelector\Parser\TokenStream;
  16. /**
  17. * CSS selector comment handler.
  18. *
  19. * This component is a port of the Python cssselect library,
  20. * which is copyright Ian Bicking, @see https://github.com/SimonSapin/cssselect.
  21. *
  22. * @author Jean-François Simon <jeanfrancois.simon@sensiolabs.com>
  23. *
  24. * @internal
  25. */
  26. class IdentifierHandler implements HandlerInterface
  27. {
  28. private $patterns;
  29. private $escaping;
  30. public function __construct(TokenizerPatterns $patterns, TokenizerEscaping $escaping)
  31. {
  32. $this->patterns = $patterns;
  33. $this->escaping = $escaping;
  34. }
  35. /**
  36. * {@inheritdoc}
  37. */
  38. public function handle(Reader $reader, TokenStream $stream): bool
  39. {
  40. $match = $reader->findPattern($this->patterns->getIdentifierPattern());
  41. if (!$match) {
  42. return false;
  43. }
  44. $value = $this->escaping->escapeUnicode($match[0]);
  45. $stream->push(new Token(Token::TYPE_IDENTIFIER, $value, $reader->getPosition()));
  46. $reader->moveForward(\strlen($match[0]));
  47. return true;
  48. }
  49. }