Patched to Drupal 8.4.8 level. See https://www.drupal.org/sa-core-2018-004 and patch...
[yaffs-website] / vendor / symfony / css-selector / Parser / Tokenizer / Tokenizer.php
1 <?php
2
3 /*
4  * This file is part of the Symfony package.
5  *
6  * (c) Fabien Potencier <fabien@symfony.com>
7  *
8  * For the full copyright and license information, please view the LICENSE
9  * file that was distributed with this source code.
10  */
11
12 namespace Symfony\Component\CssSelector\Parser\Tokenizer;
13
14 use Symfony\Component\CssSelector\Parser\Handler;
15 use Symfony\Component\CssSelector\Parser\Reader;
16 use Symfony\Component\CssSelector\Parser\Token;
17 use Symfony\Component\CssSelector\Parser\TokenStream;
18
19 /**
20  * CSS selector tokenizer.
21  *
22  * This component is a port of the Python cssselect library,
23  * which is copyright Ian Bicking, @see https://github.com/SimonSapin/cssselect.
24  *
25  * @author Jean-François Simon <jeanfrancois.simon@sensiolabs.com>
26  *
27  * @internal
28  */
29 class Tokenizer
30 {
31     /**
32      * @var Handler\HandlerInterface[]
33      */
34     private $handlers;
35
36     /**
37      * Constructor.
38      */
39     public function __construct()
40     {
41         $patterns = new TokenizerPatterns();
42         $escaping = new TokenizerEscaping($patterns);
43
44         $this->handlers = array(
45             new Handler\WhitespaceHandler(),
46             new Handler\IdentifierHandler($patterns, $escaping),
47             new Handler\HashHandler($patterns, $escaping),
48             new Handler\StringHandler($patterns, $escaping),
49             new Handler\NumberHandler($patterns),
50             new Handler\CommentHandler(),
51         );
52     }
53
54     /**
55      * Tokenize selector source code.
56      *
57      * @param Reader $reader
58      *
59      * @return TokenStream
60      */
61     public function tokenize(Reader $reader)
62     {
63         $stream = new TokenStream();
64
65         while (!$reader->isEOF()) {
66             foreach ($this->handlers as $handler) {
67                 if ($handler->handle($reader, $stream)) {
68                     continue 2;
69                 }
70             }
71
72             $stream->push(new Token(Token::TYPE_DELIMITER, $reader->getSubstring(1), $reader->getPosition()));
73             $reader->moveForward(1);
74         }
75
76         return $stream
77             ->push(new Token(Token::TYPE_FILE_END, null, $reader->getPosition()))
78             ->freeze();
79     }
80 }