00155b04650d0a6dc7c99a570acd3821b45ca315
[yaffs-website] / vendor / symfony / css-selector / Parser / Handler / StringHandler.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\Handler;
13
14 use Symfony\Component\CssSelector\Exception\InternalErrorException;
15 use Symfony\Component\CssSelector\Exception\SyntaxErrorException;
16 use Symfony\Component\CssSelector\Parser\Reader;
17 use Symfony\Component\CssSelector\Parser\Token;
18 use Symfony\Component\CssSelector\Parser\Tokenizer\TokenizerEscaping;
19 use Symfony\Component\CssSelector\Parser\Tokenizer\TokenizerPatterns;
20 use Symfony\Component\CssSelector\Parser\TokenStream;
21
22 /**
23  * CSS selector comment handler.
24  *
25  * This component is a port of the Python cssselect library,
26  * which is copyright Ian Bicking, @see https://github.com/SimonSapin/cssselect.
27  *
28  * @author Jean-François Simon <jeanfrancois.simon@sensiolabs.com>
29  *
30  * @internal
31  */
32 class StringHandler implements HandlerInterface
33 {
34     private $patterns;
35     private $escaping;
36
37     public function __construct(TokenizerPatterns $patterns, TokenizerEscaping $escaping)
38     {
39         $this->patterns = $patterns;
40         $this->escaping = $escaping;
41     }
42
43     /**
44      * {@inheritdoc}
45      */
46     public function handle(Reader $reader, TokenStream $stream)
47     {
48         $quote = $reader->getSubstring(1);
49
50         if (!\in_array($quote, array("'", '"'))) {
51             return false;
52         }
53
54         $reader->moveForward(1);
55         $match = $reader->findPattern($this->patterns->getQuotedStringPattern($quote));
56
57         if (!$match) {
58             throw new InternalErrorException(sprintf('Should have found at least an empty match at %s.', $reader->getPosition()));
59         }
60
61         // check unclosed strings
62         if (\strlen($match[0]) === $reader->getRemainingLength()) {
63             throw SyntaxErrorException::unclosedString($reader->getPosition() - 1);
64         }
65
66         // check quotes pairs validity
67         if ($quote !== $reader->getSubstring(1, \strlen($match[0]))) {
68             throw SyntaxErrorException::unclosedString($reader->getPosition() - 1);
69         }
70
71         $string = $this->escaping->escapeUnicodeAndNewLine($match[0]);
72         $stream->push(new Token(Token::TYPE_STRING, $string, $reader->getPosition()));
73         $reader->moveForward(\strlen($match[0]) + 1);
74
75         return true;
76     }
77 }