: str_replace(): Passing null to parameter #2 ($replace) of type array|string is deprecated in
* This file is part of the Symfony package.
* (c) Fabien Potencier <fabien@symfony.com>
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
namespace WPForms\Vendor\Symfony\Component\CssSelector\Parser;
use WPForms\Vendor\Symfony\Component\CssSelector\Exception\SyntaxErrorException;
use WPForms\Vendor\Symfony\Component\CssSelector\Node;
use WPForms\Vendor\Symfony\Component\CssSelector\Parser\Tokenizer\Tokenizer;
* This component is a port of the Python cssselect library,
* which is copyright Ian Bicking, @see https://github.com/SimonSapin/cssselect.
* @author Jean-François Simon <jeanfrancois.simon@sensiolabs.com>
class Parser implements ParserInterface
* @param null|Tokenizer $tokenizer
public function __construct(Tokenizer $tokenizer = null)
$this->tokenizer = $tokenizer ?: new Tokenizer();
public function parse($source)
$reader = new Reader($source);
$stream = $this->tokenizer->tokenize($reader);
return $this->parseSelectorList($stream);
* Parses the arguments for ":nth-child()" and friends.
* @throws SyntaxErrorException
public static function parseSeries(array $tokens)
foreach ($tokens as $token) {
if ($token->isString()) {
throw SyntaxErrorException::stringAsFunctionArgument();
$joined = \trim(\implode('', \array_map(function (Token $token) {
return $token->getValue();
$int = function ($string) {
if (!\is_numeric($string)) {
throw SyntaxErrorException::stringAsFunctionArgument();
case \false === \strpos($joined, 'n'):
return array(0, $int($joined));
$split = \explode('n', $joined);
$first = isset($split[0]) ? $split[0] : null;
return array($first ? '-' === $first || '+' === $first ? $int($first . '1') : $int($first) : 1, isset($split[1]) && $split[1] ? $int($split[1]) : 0);
* @param TokenStream $stream
private function parseSelectorList(TokenStream $stream)
$stream->skipWhitespace();
$selectors[] = $this->parserSelectorNode($stream);
if ($stream->getPeek()->isDelimiter(array(','))) {
$stream->skipWhitespace();
* Parses next selector or combined node.
* @param TokenStream $stream
* @return Node\SelectorNode
* @throws SyntaxErrorException
private function parserSelectorNode(TokenStream $stream)
list($result, $pseudoElement) = $this->parseSimpleSelector($stream);
$stream->skipWhitespace();
$peek = $stream->getPeek();
if ($peek->isFileEnd() || $peek->isDelimiter(array(','))) {
if (null !== $pseudoElement) {
throw SyntaxErrorException::pseudoElementFound($pseudoElement, 'not at the end of a selector');
if ($peek->isDelimiter(array('+', '>', '~'))) {
$combinator = $stream->getNext()->getValue();
$stream->skipWhitespace();
list($nextSelector, $pseudoElement) = $this->parseSimpleSelector($stream);
$result = new Node\CombinedSelectorNode($result, $combinator, $nextSelector);
return new Node\SelectorNode($result, $pseudoElement);
* Parses next simple node (hash, class, pseudo, negation).
* @param TokenStream $stream
* @param bool $insideNegation
* @throws SyntaxErrorException
private function parseSimpleSelector(TokenStream $stream, $insideNegation = \false)
$stream->skipWhitespace();
$selectorStart = \count($stream->getUsed());
$result = $this->parseElementNode($stream);
$peek = $stream->getPeek();
if ($peek->isWhitespace() || $peek->isFileEnd() || $peek->isDelimiter(array(',', '+', '>', '~')) || $insideNegation && $peek->isDelimiter(array(')'))) {
if (null !== $pseudoElement) {
throw SyntaxErrorException::pseudoElementFound($pseudoElement, 'not at the end of a selector');
$result = new Node\HashNode($result, $stream->getNext()->getValue());
} elseif ($peek->isDelimiter(array('.'))) {
$result = new Node\ClassNode($result, $stream->getNextIdentifier());
} elseif ($peek->isDelimiter(array('['))) {
$result = $this->parseAttributeNode($result, $stream);
} elseif ($peek->isDelimiter(array(':'))) {
if ($stream->getPeek()->isDelimiter(array(':'))) {
$pseudoElement = $stream->getNextIdentifier();
$identifier = $stream->getNextIdentifier();
if (\in_array(\strtolower($identifier), array('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;
if (!$stream->getPeek()->isDelimiter(array('('))) {
$result = new Node\PseudoNode($result, $identifier);
$stream->skipWhitespace();
if ('not' === \strtolower($identifier)) {
throw SyntaxErrorException::nestedNot();
list($argument, $argumentPseudoElement) = $this->parseSimpleSelector($stream, \true);
$next = $stream->getNext();
if (null !== $argumentPseudoElement) {
throw SyntaxErrorException::pseudoElementFound($argumentPseudoElement, 'inside ::not()');
if (!$next->isDelimiter(array(')'))) {
throw SyntaxErrorException::unexpectedToken('")"', $next);
$result = new Node\NegationNode($result, $argument);
$stream->skipWhitespace();
$next = $stream->getNext();
if ($next->isIdentifier() || $next->isString() || $next->isNumber() || $next->isDelimiter(array('+', '-'))) {
} elseif ($next->isDelimiter(array(')'))) {
throw SyntaxErrorException::unexpectedToken('an argument', $next);
throw SyntaxErrorException::unexpectedToken('at least one argument', $next);
$result = new Node\FunctionNode($result, $identifier, $arguments);
throw SyntaxErrorException::unexpectedToken('selector', $peek);
if (\count($stream->getUsed()) === $selectorStart) {
throw SyntaxErrorException::unexpectedToken('selector', $stream->getPeek());
return array($result, $pseudoElement);
* Parses next element node.
* @param TokenStream $stream
* @return Node\ElementNode
private function parseElementNode(TokenStream $stream)
$peek = $stream->getPeek();
if ($peek->isIdentifier() || $peek->isDelimiter(array('*'))) {
if ($peek->isIdentifier()) {
$namespace = $stream->getNext()->getValue();
if ($stream->getPeek()->isDelimiter(array('|'))) {
$element = $stream->getNextIdentifierOrStar();
$element = $namespace = null;
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)
$stream->skipWhitespace();
$attribute = $stream->getNextIdentifierOrStar();
if (null === $attribute && !$stream->getPeek()->isDelimiter(array('|'))) {
throw SyntaxErrorException::unexpectedToken('"|"', $stream->getPeek());
if ($stream->getPeek()->isDelimiter(array('|'))) {
if ($stream->getPeek()->isDelimiter(array('='))) {
$attribute = $stream->getNextIdentifier();
$namespace = $operator = null;
if (null === $operator) {
$stream->skipWhitespace();
$next = $stream->getNext();
if ($next->isDelimiter(array(']'))) {
return new Node\AttributeNode($selector, $namespace, $attribute, 'exists', null);
} elseif ($next->isDelimiter(array('='))) {
} elseif ($next->isDelimiter(array('^', '$', '*', '~', '|', '!')) && $stream->getPeek()->isDelimiter(array('='))) {
$operator = $next->getValue() . '=';
throw SyntaxErrorException::unexpectedToken('operator', $next);
$stream->skipWhitespace();
$value = $stream->getNext();
if ($value->isNumber()) {
// if the value is a number, it's casted into a string
$value = new Token(Token::TYPE_STRING, (string) $value->getValue(), $value->getPosition());
if (!($value->isIdentifier() || $value->isString())) {
throw SyntaxErrorException::unexpectedToken('string or identifier', $value);
$stream->skipWhitespace();
$next = $stream->getNext();
if (!$next->isDelimiter(array(']'))) {
throw SyntaxErrorException::unexpectedToken('"]"', $next);
return new Node\AttributeNode($selector, $namespace, $attribute, $operator, $value->getValue());