function Parser::parseSeries
Parses the arguments for ":nth-child()" and friends.
Parameters
Token[] $tokens:
Throws
1 call to Parser::parseSeries()
- FunctionExtension::translateNthChild in vendor/
symfony/ css-selector/ XPath/ Extension/ FunctionExtension.php
File
-
vendor/
symfony/ css-selector/ Parser/ Parser.php, line 52
Class
- Parser
- CSS selector parser.
Namespace
Symfony\Component\CssSelector\ParserCode
public static function parseSeries(array $tokens) : array {
foreach ($tokens as $token) {
if ($token->isString()) {
throw SyntaxErrorException::stringAsFunctionArgument();
}
}
$joined = trim(implode('', array_map(fn(Token $token) => $token->getValue(), $tokens)));
$int = function ($string) {
if (!is_numeric($string)) {
throw SyntaxErrorException::stringAsFunctionArgument();
}
return (int) $string;
};
switch (true) {
case 'odd' === $joined:
return [
2,
1,
];
case 'even' === $joined:
return [
2,
0,
];
case 'n' === $joined:
return [
1,
0,
];
case !str_contains($joined, 'n'):
return [
0,
$int($joined),
];
}
$split = explode('n', $joined);
$first = $split[0] ?? null;
return [
$first ? '-' === $first || '+' === $first ? $int($first . '1') : $int($first) : 1,
isset($split[1]) && $split[1] ? $int($split[1]) : 0,
];
}