friendship ended with social-app. php is my new best friend
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
12namespace Symfony\Component\CssSelector\Parser\Handler;
13
14use Symfony\Component\CssSelector\Exception\InternalErrorException;
15use Symfony\Component\CssSelector\Exception\SyntaxErrorException;
16use Symfony\Component\CssSelector\Parser\Reader;
17use Symfony\Component\CssSelector\Parser\Token;
18use Symfony\Component\CssSelector\Parser\Tokenizer\TokenizerEscaping;
19use Symfony\Component\CssSelector\Parser\Tokenizer\TokenizerPatterns;
20use 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 */
32class StringHandler implements HandlerInterface
33{
34 public function __construct(
35 private TokenizerPatterns $patterns,
36 private TokenizerEscaping $escaping,
37 ) {
38 }
39
40 public function handle(Reader $reader, TokenStream $stream): bool
41 {
42 $quote = $reader->getSubstring(1);
43
44 if (!\in_array($quote, ["'", '"'])) {
45 return false;
46 }
47
48 $reader->moveForward(1);
49 $match = $reader->findPattern($this->patterns->getQuotedStringPattern($quote));
50
51 if (!$match) {
52 throw new InternalErrorException(\sprintf('Should have found at least an empty match at %d.', $reader->getPosition()));
53 }
54
55 // check unclosed strings
56 if (\strlen($match[0]) === $reader->getRemainingLength()) {
57 throw SyntaxErrorException::unclosedString($reader->getPosition() - 1);
58 }
59
60 // check quotes pairs validity
61 if ($quote !== $reader->getSubstring(1, \strlen($match[0]))) {
62 throw SyntaxErrorException::unclosedString($reader->getPosition() - 1);
63 }
64
65 $string = $this->escaping->escapeUnicodeAndNewLine($match[0]);
66 $stream->push(new Token(Token::TYPE_STRING, $string, $reader->getPosition()));
67 $reader->moveForward(\strlen($match[0]) + 1);
68
69 return true;
70 }
71}