| 148 |
lars |
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 TokenizerPatterns $patterns;
|
|
|
35 |
private TokenizerEscaping $escaping;
|
|
|
36 |
|
|
|
37 |
public function __construct(TokenizerPatterns $patterns, TokenizerEscaping $escaping)
|
|
|
38 |
{
|
|
|
39 |
$this->patterns = $patterns;
|
|
|
40 |
$this->escaping = $escaping;
|
|
|
41 |
}
|
|
|
42 |
|
|
|
43 |
public function handle(Reader $reader, TokenStream $stream): bool
|
|
|
44 |
{
|
|
|
45 |
$quote = $reader->getSubstring(1);
|
|
|
46 |
|
|
|
47 |
if (!\in_array($quote, ["'", '"'])) {
|
|
|
48 |
return false;
|
|
|
49 |
}
|
|
|
50 |
|
|
|
51 |
$reader->moveForward(1);
|
|
|
52 |
$match = $reader->findPattern($this->patterns->getQuotedStringPattern($quote));
|
|
|
53 |
|
|
|
54 |
if (!$match) {
|
|
|
55 |
throw new InternalErrorException(sprintf('Should have found at least an empty match at %d.', $reader->getPosition()));
|
|
|
56 |
}
|
|
|
57 |
|
|
|
58 |
// check unclosed strings
|
|
|
59 |
if (\strlen($match[0]) === $reader->getRemainingLength()) {
|
|
|
60 |
throw SyntaxErrorException::unclosedString($reader->getPosition() - 1);
|
|
|
61 |
}
|
|
|
62 |
|
|
|
63 |
// check quotes pairs validity
|
|
|
64 |
if ($quote !== $reader->getSubstring(1, \strlen($match[0]))) {
|
|
|
65 |
throw SyntaxErrorException::unclosedString($reader->getPosition() - 1);
|
|
|
66 |
}
|
|
|
67 |
|
|
|
68 |
$string = $this->escaping->escapeUnicodeAndNewLine($match[0]);
|
|
|
69 |
$stream->push(new Token(Token::TYPE_STRING, $string, $reader->getPosition()));
|
|
|
70 |
$reader->moveForward(\strlen($match[0]) + 1);
|
|
|
71 |
|
|
|
72 |
return true;
|
|
|
73 |
}
|
|
|
74 |
}
|