laravel-6 support

This commit is contained in:
RafficMohammed
2023-01-08 01:17:22 +05:30
parent 1a5c16ae4b
commit 774eed8b0e
4962 changed files with 279380 additions and 297961 deletions

View File

@@ -29,7 +29,7 @@ class CommentHandler implements HandlerInterface
/**
* {@inheritdoc}
*/
public function handle(Reader $reader, TokenStream $stream)
public function handle(Reader $reader, TokenStream $stream): bool
{
if ('/*' !== $reader->getSubstring(2)) {
return false;

View File

@@ -26,11 +26,5 @@ use Symfony\Component\CssSelector\Parser\TokenStream;
*/
interface HandlerInterface
{
/**
* @param Reader $reader
* @param TokenStream $stream
*
* @return bool
*/
public function handle(Reader $reader, TokenStream $stream);
public function handle(Reader $reader, TokenStream $stream): bool;
}

View File

@@ -13,9 +13,9 @@ namespace Symfony\Component\CssSelector\Parser\Handler;
use Symfony\Component\CssSelector\Parser\Reader;
use Symfony\Component\CssSelector\Parser\Token;
use Symfony\Component\CssSelector\Parser\TokenStream;
use Symfony\Component\CssSelector\Parser\Tokenizer\TokenizerEscaping;
use Symfony\Component\CssSelector\Parser\Tokenizer\TokenizerPatterns;
use Symfony\Component\CssSelector\Parser\TokenStream;
/**
* CSS selector comment handler.
@@ -29,20 +29,9 @@ use Symfony\Component\CssSelector\Parser\Tokenizer\TokenizerPatterns;
*/
class HashHandler implements HandlerInterface
{
/**
* @var TokenizerPatterns
*/
private $patterns;
/**
* @var TokenizerEscaping
*/
private $escaping;
/**
* @param TokenizerPatterns $patterns
* @param TokenizerEscaping $escaping
*/
public function __construct(TokenizerPatterns $patterns, TokenizerEscaping $escaping)
{
$this->patterns = $patterns;
@@ -52,7 +41,7 @@ class HashHandler implements HandlerInterface
/**
* {@inheritdoc}
*/
public function handle(Reader $reader, TokenStream $stream)
public function handle(Reader $reader, TokenStream $stream): bool
{
$match = $reader->findPattern($this->patterns->getHashPattern());
@@ -62,7 +51,7 @@ class HashHandler implements HandlerInterface
$value = $this->escaping->escapeUnicode($match[1]);
$stream->push(new Token(Token::TYPE_HASH, $value, $reader->getPosition()));
$reader->moveForward(strlen($match[0]));
$reader->moveForward(\strlen($match[0]));
return true;
}

View File

@@ -13,9 +13,9 @@ namespace Symfony\Component\CssSelector\Parser\Handler;
use Symfony\Component\CssSelector\Parser\Reader;
use Symfony\Component\CssSelector\Parser\Token;
use Symfony\Component\CssSelector\Parser\TokenStream;
use Symfony\Component\CssSelector\Parser\Tokenizer\TokenizerEscaping;
use Symfony\Component\CssSelector\Parser\Tokenizer\TokenizerPatterns;
use Symfony\Component\CssSelector\Parser\TokenStream;
/**
* CSS selector comment handler.
@@ -29,20 +29,9 @@ use Symfony\Component\CssSelector\Parser\Tokenizer\TokenizerPatterns;
*/
class IdentifierHandler implements HandlerInterface
{
/**
* @var TokenizerPatterns
*/
private $patterns;
/**
* @var TokenizerEscaping
*/
private $escaping;
/**
* @param TokenizerPatterns $patterns
* @param TokenizerEscaping $escaping
*/
public function __construct(TokenizerPatterns $patterns, TokenizerEscaping $escaping)
{
$this->patterns = $patterns;
@@ -52,7 +41,7 @@ class IdentifierHandler implements HandlerInterface
/**
* {@inheritdoc}
*/
public function handle(Reader $reader, TokenStream $stream)
public function handle(Reader $reader, TokenStream $stream): bool
{
$match = $reader->findPattern($this->patterns->getIdentifierPattern());
@@ -62,7 +51,7 @@ class IdentifierHandler implements HandlerInterface
$value = $this->escaping->escapeUnicode($match[0]);
$stream->push(new Token(Token::TYPE_IDENTIFIER, $value, $reader->getPosition()));
$reader->moveForward(strlen($match[0]));
$reader->moveForward(\strlen($match[0]));
return true;
}

View File

@@ -13,8 +13,8 @@ namespace Symfony\Component\CssSelector\Parser\Handler;
use Symfony\Component\CssSelector\Parser\Reader;
use Symfony\Component\CssSelector\Parser\Token;
use Symfony\Component\CssSelector\Parser\TokenStream;
use Symfony\Component\CssSelector\Parser\Tokenizer\TokenizerPatterns;
use Symfony\Component\CssSelector\Parser\TokenStream;
/**
* CSS selector comment handler.
@@ -28,14 +28,8 @@ use Symfony\Component\CssSelector\Parser\Tokenizer\TokenizerPatterns;
*/
class NumberHandler implements HandlerInterface
{
/**
* @var TokenizerPatterns
*/
private $patterns;
/**
* @param TokenizerPatterns $patterns
*/
public function __construct(TokenizerPatterns $patterns)
{
$this->patterns = $patterns;
@@ -44,7 +38,7 @@ class NumberHandler implements HandlerInterface
/**
* {@inheritdoc}
*/
public function handle(Reader $reader, TokenStream $stream)
public function handle(Reader $reader, TokenStream $stream): bool
{
$match = $reader->findPattern($this->patterns->getNumberPattern());
@@ -53,7 +47,7 @@ class NumberHandler implements HandlerInterface
}
$stream->push(new Token(Token::TYPE_NUMBER, $match[0], $reader->getPosition()));
$reader->moveForward(strlen($match[0]));
$reader->moveForward(\strlen($match[0]));
return true;
}

View File

@@ -15,9 +15,9 @@ use Symfony\Component\CssSelector\Exception\InternalErrorException;
use Symfony\Component\CssSelector\Exception\SyntaxErrorException;
use Symfony\Component\CssSelector\Parser\Reader;
use Symfony\Component\CssSelector\Parser\Token;
use Symfony\Component\CssSelector\Parser\TokenStream;
use Symfony\Component\CssSelector\Parser\Tokenizer\TokenizerEscaping;
use Symfony\Component\CssSelector\Parser\Tokenizer\TokenizerPatterns;
use Symfony\Component\CssSelector\Parser\TokenStream;
/**
* CSS selector comment handler.
@@ -31,20 +31,9 @@ use Symfony\Component\CssSelector\Parser\Tokenizer\TokenizerPatterns;
*/
class StringHandler implements HandlerInterface
{
/**
* @var TokenizerPatterns
*/
private $patterns;
/**
* @var TokenizerEscaping
*/
private $escaping;
/**
* @param TokenizerPatterns $patterns
* @param TokenizerEscaping $escaping
*/
public function __construct(TokenizerPatterns $patterns, TokenizerEscaping $escaping)
{
$this->patterns = $patterns;
@@ -54,11 +43,11 @@ class StringHandler implements HandlerInterface
/**
* {@inheritdoc}
*/
public function handle(Reader $reader, TokenStream $stream)
public function handle(Reader $reader, TokenStream $stream): bool
{
$quote = $reader->getSubstring(1);
if (!in_array($quote, array("'", '"'))) {
if (!\in_array($quote, ["'", '"'])) {
return false;
}
@@ -66,22 +55,22 @@ class StringHandler implements HandlerInterface
$match = $reader->findPattern($this->patterns->getQuotedStringPattern($quote));
if (!$match) {
throw new InternalErrorException(sprintf('Should have found at least an empty match at %s.', $reader->getPosition()));
throw new InternalErrorException(sprintf('Should have found at least an empty match at %d.', $reader->getPosition()));
}
// check unclosed strings
if (strlen($match[0]) === $reader->getRemainingLength()) {
if (\strlen($match[0]) === $reader->getRemainingLength()) {
throw SyntaxErrorException::unclosedString($reader->getPosition() - 1);
}
// check quotes pairs validity
if ($quote !== $reader->getSubstring(1, strlen($match[0]))) {
if ($quote !== $reader->getSubstring(1, \strlen($match[0]))) {
throw SyntaxErrorException::unclosedString($reader->getPosition() - 1);
}
$string = $this->escaping->escapeUnicodeAndNewLine($match[0]);
$stream->push(new Token(Token::TYPE_STRING, $string, $reader->getPosition()));
$reader->moveForward(strlen($match[0]) + 1);
$reader->moveForward(\strlen($match[0]) + 1);
return true;
}

View File

@@ -30,7 +30,7 @@ class WhitespaceHandler implements HandlerInterface
/**
* {@inheritdoc}
*/
public function handle(Reader $reader, TokenStream $stream)
public function handle(Reader $reader, TokenStream $stream): bool
{
$match = $reader->findPattern('~^[ \t\r\n\f]+~');
@@ -39,7 +39,7 @@ class WhitespaceHandler implements HandlerInterface
}
$stream->push(new Token(Token::TYPE_WHITESPACE, $match[0], $reader->getPosition()));
$reader->moveForward(strlen($match[0]));
$reader->moveForward(\strlen($match[0]));
return true;
}

View File

@@ -27,25 +27,17 @@ use Symfony\Component\CssSelector\Parser\Tokenizer\Tokenizer;
*/
class Parser implements ParserInterface
{
/**
* @var Tokenizer
*/
private $tokenizer;
/**
* Constructor.
*
* @param null|Tokenizer $tokenizer
*/
public function __construct(Tokenizer $tokenizer = null)
{
$this->tokenizer = $tokenizer ?: new Tokenizer();
$this->tokenizer = $tokenizer ?? new Tokenizer();
}
/**
* {@inheritdoc}
*/
public function parse($source)
public function parse(string $source): array
{
$reader = new Reader($source);
$stream = $this->tokenizer->tokenize($reader);
@@ -58,11 +50,9 @@ class Parser implements ParserInterface
*
* @param Token[] $tokens
*
* @return array
*
* @throws SyntaxErrorException
*/
public static function parseSeries(array $tokens)
public static function parseSeries(array $tokens): array
{
foreach ($tokens as $token) {
if ($token->isString()) {
@@ -84,40 +74,33 @@ class Parser implements ParserInterface
switch (true) {
case 'odd' === $joined:
return array(2, 1);
return [2, 1];
case 'even' === $joined:
return array(2, 0);
return [2, 0];
case 'n' === $joined:
return array(1, 0);
case false === strpos($joined, 'n'):
return array(0, $int($joined));
return [1, 0];
case !str_contains($joined, 'n'):
return [0, $int($joined)];
}
$split = explode('n', $joined);
$first = isset($split[0]) ? $split[0] : null;
$first = $split[0] ?? null;
return array(
return [
$first ? ('-' === $first || '+' === $first ? $int($first.'1') : $int($first)) : 1,
isset($split[1]) && $split[1] ? $int($split[1]) : 0,
);
];
}
/**
* Parses selector nodes.
*
* @param TokenStream $stream
*
* @return array
*/
private function parseSelectorList(TokenStream $stream)
private function parseSelectorList(TokenStream $stream): array
{
$stream->skipWhitespace();
$selectors = array();
$selectors = [];
while (true) {
$selectors[] = $this->parserSelectorNode($stream);
if ($stream->getPeek()->isDelimiter(array(','))) {
if ($stream->getPeek()->isDelimiter([','])) {
$stream->getNext();
$stream->skipWhitespace();
} else {
@@ -128,24 +111,15 @@ class Parser implements ParserInterface
return $selectors;
}
/**
* Parses next selector or combined node.
*
* @param TokenStream $stream
*
* @return Node\SelectorNode
*
* @throws SyntaxErrorException
*/
private function parserSelectorNode(TokenStream $stream)
private function parserSelectorNode(TokenStream $stream): Node\SelectorNode
{
list($result, $pseudoElement) = $this->parseSimpleSelector($stream);
[$result, $pseudoElement] = $this->parseSimpleSelector($stream);
while (true) {
$stream->skipWhitespace();
$peek = $stream->getPeek();
if ($peek->isFileEnd() || $peek->isDelimiter(array(','))) {
if ($peek->isFileEnd() || $peek->isDelimiter([','])) {
break;
}
@@ -153,14 +127,14 @@ class Parser implements ParserInterface
throw SyntaxErrorException::pseudoElementFound($pseudoElement, 'not at the end of a selector');
}
if ($peek->isDelimiter(array('+', '>', '~'))) {
if ($peek->isDelimiter(['+', '>', '~'])) {
$combinator = $stream->getNext()->getValue();
$stream->skipWhitespace();
} else {
$combinator = ' ';
}
list($nextSelector, $pseudoElement) = $this->parseSimpleSelector($stream);
[$nextSelector, $pseudoElement] = $this->parseSimpleSelector($stream);
$result = new Node\CombinedSelectorNode($result, $combinator, $nextSelector);
}
@@ -170,18 +144,13 @@ class Parser implements ParserInterface
/**
* Parses next simple node (hash, class, pseudo, negation).
*
* @param TokenStream $stream
* @param bool $insideNegation
*
* @return array
*
* @throws SyntaxErrorException
*/
private function parseSimpleSelector(TokenStream $stream, $insideNegation = false)
private function parseSimpleSelector(TokenStream $stream, bool $insideNegation = false): array
{
$stream->skipWhitespace();
$selectorStart = count($stream->getUsed());
$selectorStart = \count($stream->getUsed());
$result = $this->parseElementNode($stream);
$pseudoElement = null;
@@ -189,8 +158,8 @@ class Parser implements ParserInterface
$peek = $stream->getPeek();
if ($peek->isWhitespace()
|| $peek->isFileEnd()
|| $peek->isDelimiter(array(',', '+', '>', '~'))
|| ($insideNegation && $peek->isDelimiter(array(')')))
|| $peek->isDelimiter([',', '+', '>', '~'])
|| ($insideNegation && $peek->isDelimiter([')']))
) {
break;
}
@@ -201,16 +170,16 @@ class Parser implements ParserInterface
if ($peek->isHash()) {
$result = new Node\HashNode($result, $stream->getNext()->getValue());
} elseif ($peek->isDelimiter(array('.'))) {
} elseif ($peek->isDelimiter(['.'])) {
$stream->getNext();
$result = new Node\ClassNode($result, $stream->getNextIdentifier());
} elseif ($peek->isDelimiter(array('['))) {
} elseif ($peek->isDelimiter(['['])) {
$stream->getNext();
$result = $this->parseAttributeNode($result, $stream);
} elseif ($peek->isDelimiter(array(':'))) {
} elseif ($peek->isDelimiter([':'])) {
$stream->getNext();
if ($stream->getPeek()->isDelimiter(array(':'))) {
if ($stream->getPeek()->isDelimiter([':'])) {
$stream->getNext();
$pseudoElement = $stream->getNextIdentifier();
@@ -218,7 +187,7 @@ class Parser implements ParserInterface
}
$identifier = $stream->getNextIdentifier();
if (in_array(strtolower($identifier), array('first-line', 'first-letter', 'before', 'after'))) {
if (\in_array(strtolower($identifier), ['first-line', 'first-letter', 'before', 'after'])) {
// Special case: CSS 2.1 pseudo-elements can have a single ':'.
// Any new pseudo-element must have two.
$pseudoElement = $identifier;
@@ -226,7 +195,7 @@ class Parser implements ParserInterface
continue;
}
if (!$stream->getPeek()->isDelimiter(array('('))) {
if (!$stream->getPeek()->isDelimiter(['('])) {
$result = new Node\PseudoNode($result, $identifier);
continue;
@@ -240,20 +209,20 @@ class Parser implements ParserInterface
throw SyntaxErrorException::nestedNot();
}
list($argument, $argumentPseudoElement) = $this->parseSimpleSelector($stream, true);
[$argument, $argumentPseudoElement] = $this->parseSimpleSelector($stream, true);
$next = $stream->getNext();
if (null !== $argumentPseudoElement) {
throw SyntaxErrorException::pseudoElementFound($argumentPseudoElement, 'inside ::not()');
}
if (!$next->isDelimiter(array(')'))) {
if (!$next->isDelimiter([')'])) {
throw SyntaxErrorException::unexpectedToken('")"', $next);
}
$result = new Node\NegationNode($result, $argument);
} else {
$arguments = array();
$arguments = [];
$next = null;
while (true) {
@@ -263,10 +232,10 @@ class Parser implements ParserInterface
if ($next->isIdentifier()
|| $next->isString()
|| $next->isNumber()
|| $next->isDelimiter(array('+', '-'))
|| $next->isDelimiter(['+', '-'])
) {
$arguments[] = $next;
} elseif ($next->isDelimiter(array(')'))) {
} elseif ($next->isDelimiter([')'])) {
break;
} else {
throw SyntaxErrorException::unexpectedToken('an argument', $next);
@@ -284,25 +253,18 @@ class Parser implements ParserInterface
}
}
if (count($stream->getUsed()) === $selectorStart) {
if (\count($stream->getUsed()) === $selectorStart) {
throw SyntaxErrorException::unexpectedToken('selector', $stream->getPeek());
}
return array($result, $pseudoElement);
return [$result, $pseudoElement];
}
/**
* Parses next element node.
*
* @param TokenStream $stream
*
* @return Node\ElementNode
*/
private function parseElementNode(TokenStream $stream)
private function parseElementNode(TokenStream $stream): Node\ElementNode
{
$peek = $stream->getPeek();
if ($peek->isIdentifier() || $peek->isDelimiter(array('*'))) {
if ($peek->isIdentifier() || $peek->isDelimiter(['*'])) {
if ($peek->isIdentifier()) {
$namespace = $stream->getNext()->getValue();
} else {
@@ -310,7 +272,7 @@ class Parser implements ParserInterface
$namespace = null;
}
if ($stream->getPeek()->isDelimiter(array('|'))) {
if ($stream->getPeek()->isDelimiter(['|'])) {
$stream->getNext();
$element = $stream->getNextIdentifierOrStar();
} else {
@@ -324,29 +286,19 @@ class Parser implements ParserInterface
return new Node\ElementNode($namespace, $element);
}
/**
* Parses next attribute node.
*
* @param Node\NodeInterface $selector
* @param TokenStream $stream
*
* @return Node\AttributeNode
*
* @throws SyntaxErrorException
*/
private function parseAttributeNode(Node\NodeInterface $selector, TokenStream $stream)
private function parseAttributeNode(Node\NodeInterface $selector, TokenStream $stream): Node\AttributeNode
{
$stream->skipWhitespace();
$attribute = $stream->getNextIdentifierOrStar();
if (null === $attribute && !$stream->getPeek()->isDelimiter(array('|'))) {
if (null === $attribute && !$stream->getPeek()->isDelimiter(['|'])) {
throw SyntaxErrorException::unexpectedToken('"|"', $stream->getPeek());
}
if ($stream->getPeek()->isDelimiter(array('|'))) {
if ($stream->getPeek()->isDelimiter(['|'])) {
$stream->getNext();
if ($stream->getPeek()->isDelimiter(array('='))) {
if ($stream->getPeek()->isDelimiter(['='])) {
$namespace = null;
$stream->getNext();
$operator = '|=';
@@ -363,12 +315,12 @@ class Parser implements ParserInterface
$stream->skipWhitespace();
$next = $stream->getNext();
if ($next->isDelimiter(array(']'))) {
if ($next->isDelimiter([']'])) {
return new Node\AttributeNode($selector, $namespace, $attribute, 'exists', null);
} elseif ($next->isDelimiter(array('='))) {
} elseif ($next->isDelimiter(['='])) {
$operator = '=';
} elseif ($next->isDelimiter(array('^', '$', '*', '~', '|', '!'))
&& $stream->getPeek()->isDelimiter(array('='))
} elseif ($next->isDelimiter(['^', '$', '*', '~', '|', '!'])
&& $stream->getPeek()->isDelimiter(['='])
) {
$operator = $next->getValue().'=';
$stream->getNext();
@@ -392,7 +344,7 @@ class Parser implements ParserInterface
$stream->skipWhitespace();
$next = $stream->getNext();
if (!$next->isDelimiter(array(']'))) {
if (!$next->isDelimiter([']'])) {
throw SyntaxErrorException::unexpectedToken('"]"', $next);
}

View File

@@ -28,9 +28,7 @@ interface ParserInterface
/**
* Parses given selector source into an array of tokens.
*
* @param string $source
*
* @return SelectorNode[]
*/
public function parse($source);
public function parse(string $source): array;
}

View File

@@ -23,71 +23,37 @@ namespace Symfony\Component\CssSelector\Parser;
*/
class Reader
{
/**
* @var string
*/
private $source;
/**
* @var int
*/
private $length;
/**
* @var int
*/
private $position = 0;
/**
* @param string $source
*/
public function __construct($source)
public function __construct(string $source)
{
$this->source = $source;
$this->length = strlen($source);
$this->length = \strlen($source);
}
/**
* @return bool
*/
public function isEOF()
public function isEOF(): bool
{
return $this->position >= $this->length;
}
/**
* @return int
*/
public function getPosition()
public function getPosition(): int
{
return $this->position;
}
/**
* @return int
*/
public function getRemainingLength()
public function getRemainingLength(): int
{
return $this->length - $this->position;
}
/**
* @param int $length
* @param int $offset
*
* @return string
*/
public function getSubstring($length, $offset = 0)
public function getSubstring(int $length, int $offset = 0): string
{
return substr($this->source, $this->position + $offset, $length);
}
/**
* @param string $string
*
* @return int
*/
public function getOffset($string)
public function getOffset(string $string)
{
$position = strpos($this->source, $string, $this->position);
@@ -95,11 +61,9 @@ class Reader
}
/**
* @param string $pattern
*
* @return bool
* @return array|false
*/
public function findPattern($pattern)
public function findPattern(string $pattern)
{
$source = substr($this->source, $this->position);
@@ -110,10 +74,7 @@ class Reader
return false;
}
/**
* @param int $length
*/
public function moveForward($length)
public function moveForward(int $length)
{
$this->position += $length;
}

View File

@@ -31,7 +31,7 @@ class ClassParser implements ParserInterface
/**
* {@inheritdoc}
*/
public function parse($source)
public function parse(string $source): array
{
// Matches an optional namespace, optional element, and required class
// $source = 'test|input.ab6bd_field';
@@ -41,11 +41,11 @@ class ClassParser implements ParserInterface
// 2 => string 'input' (length=5)
// 3 => string 'ab6bd_field' (length=11)
if (preg_match('/^(?:([a-z]++)\|)?+([\w-]++|\*)?+\.([\w-]++)$/i', trim($source), $matches)) {
return array(
return [
new SelectorNode(new ClassNode(new ElementNode($matches[1] ?: null, $matches[2] ?: null), $matches[3])),
);
];
}
return array();
return [];
}
}

View File

@@ -30,7 +30,7 @@ class ElementParser implements ParserInterface
/**
* {@inheritdoc}
*/
public function parse($source)
public function parse(string $source): array
{
// Matches an optional namespace, required element or `*`
// $source = 'testns|testel';
@@ -39,9 +39,9 @@ class ElementParser implements ParserInterface
// 1 => string 'testns' (length=6)
// 2 => string 'testel' (length=6)
if (preg_match('/^(?:([a-z]++)\|)?([\w-]++|\*)$/i', trim($source), $matches)) {
return array(new SelectorNode(new ElementNode($matches[1] ?: null, $matches[2])));
return [new SelectorNode(new ElementNode($matches[1] ?: null, $matches[2]))];
}
return array();
return [];
}
}

View File

@@ -34,13 +34,13 @@ class EmptyStringParser implements ParserInterface
/**
* {@inheritdoc}
*/
public function parse($source)
public function parse(string $source): array
{
// Matches an empty string
if ($source == '') {
return array(new SelectorNode(new ElementNode(null, '*')));
if ('' == $source) {
return [new SelectorNode(new ElementNode(null, '*'))];
}
return array();
return [];
}
}

View File

@@ -31,7 +31,7 @@ class HashParser implements ParserInterface
/**
* {@inheritdoc}
*/
public function parse($source)
public function parse(string $source): array
{
// Matches an optional namespace, optional element, and required id
// $source = 'test|input#ab6bd_field';
@@ -41,11 +41,11 @@ class HashParser implements ParserInterface
// 2 => string 'input' (length=5)
// 3 => string 'ab6bd_field' (length=11)
if (preg_match('/^(?:([a-z]++)\|)?+([\w-]++|\*)?+#([\w-]++)$/i', trim($source), $matches)) {
return array(
return [
new SelectorNode(new HashNode(new ElementNode($matches[1] ?: null, $matches[2] ?: null), $matches[3])),
);
];
}
return array();
return [];
}
}

View File

@@ -23,79 +23,46 @@ namespace Symfony\Component\CssSelector\Parser;
*/
class Token
{
const TYPE_FILE_END = 'eof';
const TYPE_DELIMITER = 'delimiter';
const TYPE_WHITESPACE = 'whitespace';
const TYPE_IDENTIFIER = 'identifier';
const TYPE_HASH = 'hash';
const TYPE_NUMBER = 'number';
const TYPE_STRING = 'string';
public const TYPE_FILE_END = 'eof';
public const TYPE_DELIMITER = 'delimiter';
public const TYPE_WHITESPACE = 'whitespace';
public const TYPE_IDENTIFIER = 'identifier';
public const TYPE_HASH = 'hash';
public const TYPE_NUMBER = 'number';
public const TYPE_STRING = 'string';
/**
* @var int
*/
private $type;
/**
* @var string
*/
private $value;
/**
* @var int
*/
private $position;
/**
* @param int $type
* @param string $value
* @param int $position
*/
public function __construct($type, $value, $position)
public function __construct(?string $type, ?string $value, ?int $position)
{
$this->type = $type;
$this->value = $value;
$this->position = $position;
}
/**
* @return int
*/
public function getType()
public function getType(): ?int
{
return $this->type;
}
/**
* @return string
*/
public function getValue()
public function getValue(): ?string
{
return $this->value;
}
/**
* @return int
*/
public function getPosition()
public function getPosition(): ?int
{
return $this->position;
}
/**
* @return bool
*/
public function isFileEnd()
public function isFileEnd(): bool
{
return self::TYPE_FILE_END === $this->type;
}
/**
* @param array $values
*
* @return bool
*/
public function isDelimiter(array $values = array())
public function isDelimiter(array $values = []): bool
{
if (self::TYPE_DELIMITER !== $this->type) {
return false;
@@ -105,53 +72,35 @@ class Token
return true;
}
return in_array($this->value, $values);
return \in_array($this->value, $values);
}
/**
* @return bool
*/
public function isWhitespace()
public function isWhitespace(): bool
{
return self::TYPE_WHITESPACE === $this->type;
}
/**
* @return bool
*/
public function isIdentifier()
public function isIdentifier(): bool
{
return self::TYPE_IDENTIFIER === $this->type;
}
/**
* @return bool
*/
public function isHash()
public function isHash(): bool
{
return self::TYPE_HASH === $this->type;
}
/**
* @return bool
*/
public function isNumber()
public function isNumber(): bool
{
return self::TYPE_NUMBER === $this->type;
}
/**
* @return bool
*/
public function isString()
public function isString(): bool
{
return self::TYPE_STRING === $this->type;
}
/**
* @return string
*/
public function __toString()
public function __toString(): string
{
if ($this->value) {
return sprintf('<%s "%s" at %s>', $this->type, $this->value, $this->position);

View File

@@ -29,17 +29,12 @@ class TokenStream
/**
* @var Token[]
*/
private $tokens = array();
/**
* @var bool
*/
private $frozen = false;
private $tokens = [];
/**
* @var Token[]
*/
private $used = array();
private $used = [];
/**
* @var int
@@ -49,7 +44,7 @@ class TokenStream
/**
* @var Token|null
*/
private $peeked = null;
private $peeked;
/**
* @var bool
@@ -59,11 +54,9 @@ class TokenStream
/**
* Pushes a token.
*
* @param Token $token
*
* @return $this
*/
public function push(Token $token)
public function push(Token $token): self
{
$this->tokens[] = $token;
@@ -75,21 +68,17 @@ class TokenStream
*
* @return $this
*/
public function freeze()
public function freeze(): self
{
$this->frozen = true;
return $this;
}
/**
* Returns next token.
*
* @return Token
*
* @throws InternalErrorException If there is no more token
*/
public function getNext()
public function getNext(): Token
{
if ($this->peeking) {
$this->peeking = false;
@@ -107,10 +96,8 @@ class TokenStream
/**
* Returns peeked token.
*
* @return Token
*/
public function getPeek()
public function getPeek(): Token
{
if (!$this->peeking) {
$this->peeked = $this->getNext();
@@ -125,7 +112,7 @@ class TokenStream
*
* @return Token[]
*/
public function getUsed()
public function getUsed(): array
{
return $this->used;
}
@@ -137,7 +124,7 @@ class TokenStream
*
* @throws SyntaxErrorException If next token is not an identifier
*/
public function getNextIdentifier()
public function getNextIdentifier(): string
{
$next = $this->getNext();
@@ -151,11 +138,11 @@ class TokenStream
/**
* Returns nex identifier or star delimiter token.
*
* @return null|string The identifier token value or null if star found
* @return string|null The identifier token value or null if star found
*
* @throws SyntaxErrorException If next token is not an identifier or a star delimiter
*/
public function getNextIdentifierOrStar()
public function getNextIdentifierOrStar(): ?string
{
$next = $this->getNext();
@@ -163,8 +150,8 @@ class TokenStream
return $next->getValue();
}
if ($next->isDelimiter(array('*'))) {
return;
if ($next->isDelimiter(['*'])) {
return null;
}
throw SyntaxErrorException::unexpectedToken('identifier or "*"', $next);

View File

@@ -33,32 +33,25 @@ class Tokenizer
*/
private $handlers;
/**
* Constructor.
*/
public function __construct()
{
$patterns = new TokenizerPatterns();
$escaping = new TokenizerEscaping($patterns);
$this->handlers = array(
$this->handlers = [
new Handler\WhitespaceHandler(),
new Handler\IdentifierHandler($patterns, $escaping),
new Handler\HashHandler($patterns, $escaping),
new Handler\StringHandler($patterns, $escaping),
new Handler\NumberHandler($patterns),
new Handler\CommentHandler(),
);
];
}
/**
* Tokenize selector source code.
*
* @param Reader $reader
*
* @return TokenStream
*/
public function tokenize(Reader $reader)
public function tokenize(Reader $reader): TokenStream
{
$stream = new TokenStream();

View File

@@ -23,62 +23,43 @@ namespace Symfony\Component\CssSelector\Parser\Tokenizer;
*/
class TokenizerEscaping
{
/**
* @var TokenizerPatterns
*/
private $patterns;
/**
* @param TokenizerPatterns $patterns
*/
public function __construct(TokenizerPatterns $patterns)
{
$this->patterns = $patterns;
}
/**
* @param string $value
*
* @return string
*/
public function escapeUnicode($value)
public function escapeUnicode(string $value): string
{
$value = $this->replaceUnicodeSequences($value);
return preg_replace($this->patterns->getSimpleEscapePattern(), '$1', $value);
}
/**
* @param string $value
*
* @return string
*/
public function escapeUnicodeAndNewLine($value)
public function escapeUnicodeAndNewLine(string $value): string
{
$value = preg_replace($this->patterns->getNewLineEscapePattern(), '', $value);
return $this->escapeUnicode($value);
}
/**
* @param string $value
*
* @return string
*/
private function replaceUnicodeSequences($value)
private function replaceUnicodeSequences(string $value): string
{
return preg_replace_callback($this->patterns->getUnicodeEscapePattern(), function ($match) {
$c = hexdec($match[1]);
if (0x80 > $c %= 0x200000) {
return chr($c);
return \chr($c);
}
if (0x800 > $c) {
return chr(0xC0 | $c >> 6).chr(0x80 | $c & 0x3F);
return \chr(0xC0 | $c >> 6).\chr(0x80 | $c & 0x3F);
}
if (0x10000 > $c) {
return chr(0xE0 | $c >> 12).chr(0x80 | $c >> 6 & 0x3F).chr(0x80 | $c & 0x3F);
return \chr(0xE0 | $c >> 12).\chr(0x80 | $c >> 6 & 0x3F).\chr(0x80 | $c & 0x3F);
}
return '';
}, $value);
}
}

View File

@@ -23,69 +23,19 @@ namespace Symfony\Component\CssSelector\Parser\Tokenizer;
*/
class TokenizerPatterns
{
/**
* @var string
*/
private $unicodeEscapePattern;
/**
* @var string
*/
private $simpleEscapePattern;
/**
* @var string
*/
private $newLineEscapePattern;
/**
* @var string
*/
private $escapePattern;
/**
* @var string
*/
private $stringEscapePattern;
/**
* @var string
*/
private $nonAsciiPattern;
/**
* @var string
*/
private $nmCharPattern;
/**
* @var string
*/
private $nmStartPattern;
/**
* @var string
*/
private $identifierPattern;
/**
* @var string
*/
private $hashPattern;
/**
* @var string
*/
private $numberPattern;
/**
* @var string
*/
private $quotedStringPattern;
/**
* Constructor.
*/
public function __construct()
{
$this->unicodeEscapePattern = '\\\\([0-9a-f]{1,6})(?:\r\n|[ \n\r\t\f])?';
@@ -96,66 +46,43 @@ class TokenizerPatterns
$this->nonAsciiPattern = '[^\x00-\x7F]';
$this->nmCharPattern = '[_a-z0-9-]|'.$this->escapePattern.'|'.$this->nonAsciiPattern;
$this->nmStartPattern = '[_a-z]|'.$this->escapePattern.'|'.$this->nonAsciiPattern;
$this->identifierPattern = '(?:'.$this->nmStartPattern.')(?:'.$this->nmCharPattern.')*';
$this->identifierPattern = '-?(?:'.$this->nmStartPattern.')(?:'.$this->nmCharPattern.')*';
$this->hashPattern = '#((?:'.$this->nmCharPattern.')+)';
$this->numberPattern = '[+-]?(?:[0-9]*\.[0-9]+|[0-9]+)';
$this->quotedStringPattern = '([^\n\r\f%s]|'.$this->stringEscapePattern.')*';
}
/**
* @return string
*/
public function getNewLineEscapePattern()
public function getNewLineEscapePattern(): string
{
return '~^'.$this->newLineEscapePattern.'~';
}
/**
* @return string
*/
public function getSimpleEscapePattern()
public function getSimpleEscapePattern(): string
{
return '~^'.$this->simpleEscapePattern.'~';
}
/**
* @return string
*/
public function getUnicodeEscapePattern()
public function getUnicodeEscapePattern(): string
{
return '~^'.$this->unicodeEscapePattern.'~i';
}
/**
* @return string
*/
public function getIdentifierPattern()
public function getIdentifierPattern(): string
{
return '~^'.$this->identifierPattern.'~i';
}
/**
* @return string
*/
public function getHashPattern()
public function getHashPattern(): string
{
return '~^'.$this->hashPattern.'~i';
}
/**
* @return string
*/
public function getNumberPattern()
public function getNumberPattern(): string
{
return '~^'.$this->numberPattern.'~';
}
/**
* @param string $quote
*
* @return string
*/
public function getQuotedStringPattern($quote)
public function getQuotedStringPattern(string $quote): string
{
return '~^'.sprintf($this->quotedStringPattern, $quote).'~i';
}