← All changes
|
back/Libraries/Symfony/CssSelector/Parser/Handler/StringHandler.php
+77
-0
10.10.1
→
11.1.0
View file →
| @@ -1,0 +1,77 @@ | ||
| 1 | +<?php | |
| 2 | + | |
| 3 | +/* | |
| 4 | + * This file is part of the Symfony package. | |
| 5 | + * | |
| 6 | + * (c) Fabien Potencier <[email protected]> | |
| 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 AcyMailing\Libraries\Symfony\CssSelector\Parser\Handler; | |
| 13 | + | |
| 14 | +use AcyMailing\Libraries\Symfony\CssSelector\Exception\InternalErrorException; | |
| 15 | +use AcyMailing\Libraries\Symfony\CssSelector\Exception\SyntaxErrorException; | |
| 16 | +use AcyMailing\Libraries\Symfony\CssSelector\Parser\Reader; | |
| 17 | +use AcyMailing\Libraries\Symfony\CssSelector\Parser\Token; | |
| 18 | +use AcyMailing\Libraries\Symfony\CssSelector\Parser\Tokenizer\TokenizerEscaping; | |
| 19 | +use AcyMailing\Libraries\Symfony\CssSelector\Parser\Tokenizer\TokenizerPatterns; | |
| 20 | +use AcyMailing\Libraries\Symfony\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 <[email protected]> | |
| 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): bool | |
| 47 | + { | |
| 48 | + $quote = $reader->getSubstring(1); | |
| 49 | + | |
| 50 | + if (!\in_array($quote, ["'", '"'])) { | |
| 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 %d.', $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 | +} | |