HashHandler.php 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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\TokenStream;
  14. use Symfony\Component\CssSelector\Parser\Tokenizer\TokenizerEscaping;
  15. use Symfony\Component\CssSelector\Parser\Tokenizer\TokenizerPatterns;
  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 HashHandler implements HandlerInterface
  27. {
  28. /**
  29. * @var TokenizerPatterns
  30. */
  31. private $patterns;
  32. /**
  33. * @var TokenizerEscaping
  34. */
  35. private $escaping;
  36. /**
  37. * @param TokenizerPatterns $patterns
  38. * @param TokenizerEscaping $escaping
  39. */
  40. public function __construct(TokenizerPatterns $patterns, TokenizerEscaping $escaping)
  41. {
  42. $this->patterns = $patterns;
  43. $this->escaping = $escaping;
  44. }
  45. /**
  46. * {@inheritdoc}
  47. */
  48. public function handle(Reader $reader, TokenStream $stream)
  49. {
  50. $match = $reader->findPattern($this->patterns->getHashPattern());
  51. if (!$match) {
  52. return false;
  53. }
  54. $value = $this->escaping->escapeUnicode($match[1]);
  55. $stream->push(new Token(Token::TYPE_HASH, $value, $reader->getPosition()));
  56. $reader->moveForward(strlen($match[0]));
  57. return true;
  58. }
  59. }