Chris@0: Chris@0: * Chris@0: * For the full copyright and license information, please view the LICENSE Chris@0: * file that was distributed with this source code. Chris@0: */ Chris@0: Chris@0: namespace Symfony\Component\CssSelector\Parser\Handler; Chris@0: Chris@0: use Symfony\Component\CssSelector\Exception\InternalErrorException; Chris@0: use Symfony\Component\CssSelector\Exception\SyntaxErrorException; Chris@0: use Symfony\Component\CssSelector\Parser\Reader; Chris@0: use Symfony\Component\CssSelector\Parser\Token; Chris@0: use Symfony\Component\CssSelector\Parser\Tokenizer\TokenizerEscaping; Chris@0: use Symfony\Component\CssSelector\Parser\Tokenizer\TokenizerPatterns; Chris@17: use Symfony\Component\CssSelector\Parser\TokenStream; Chris@0: Chris@0: /** Chris@0: * CSS selector comment handler. Chris@0: * Chris@0: * This component is a port of the Python cssselect library, Chris@0: * which is copyright Ian Bicking, @see https://github.com/SimonSapin/cssselect. Chris@0: * Chris@0: * @author Jean-François Simon Chris@0: * Chris@0: * @internal Chris@0: */ Chris@0: class StringHandler implements HandlerInterface Chris@0: { Chris@0: private $patterns; Chris@0: private $escaping; Chris@0: Chris@0: public function __construct(TokenizerPatterns $patterns, TokenizerEscaping $escaping) Chris@0: { Chris@0: $this->patterns = $patterns; Chris@0: $this->escaping = $escaping; Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function handle(Reader $reader, TokenStream $stream) Chris@0: { Chris@0: $quote = $reader->getSubstring(1); Chris@0: Chris@17: if (!\in_array($quote, ["'", '"'])) { Chris@0: return false; Chris@0: } Chris@0: Chris@0: $reader->moveForward(1); Chris@0: $match = $reader->findPattern($this->patterns->getQuotedStringPattern($quote)); Chris@0: Chris@0: if (!$match) { Chris@0: throw new InternalErrorException(sprintf('Should have found at least an empty match at %s.', $reader->getPosition())); Chris@0: } Chris@0: Chris@0: // check unclosed strings Chris@17: if (\strlen($match[0]) === $reader->getRemainingLength()) { Chris@0: throw SyntaxErrorException::unclosedString($reader->getPosition() - 1); Chris@0: } Chris@0: Chris@0: // check quotes pairs validity Chris@17: if ($quote !== $reader->getSubstring(1, \strlen($match[0]))) { Chris@0: throw SyntaxErrorException::unclosedString($reader->getPosition() - 1); Chris@0: } Chris@0: Chris@0: $string = $this->escaping->escapeUnicodeAndNewLine($match[0]); Chris@0: $stream->push(new Token(Token::TYPE_STRING, $string, $reader->getPosition())); Chris@17: $reader->moveForward(\strlen($match[0]) + 1); Chris@0: Chris@0: return true; Chris@0: } Chris@0: }