| 1 |
<?php |
| 2 |
|
| 3 |
declare(strict_types=1); |
| 4 |
|
| 5 |
namespace AcyMailing\Libraries\Sabberworm\CSS\Value; |
| 6 |
|
| 7 |
use AcyMailing\Libraries\Sabberworm\CSS\Parsing\ParserState; |
| 8 |
use AcyMailing\Libraries\Sabberworm\CSS\Parsing\UnexpectedEOFException; |
| 9 |
use AcyMailing\Libraries\Sabberworm\CSS\Parsing\UnexpectedTokenException; |
| 10 |
|
| 11 |
class CalcFunction extends CSSFunction |
| 12 |
{ |
| 13 |
/** |
| 14 |
* @var int |
| 15 |
*/ |
| 16 |
private const T_OPERAND = 1; |
| 17 |
|
| 18 |
/** |
| 19 |
* @var int |
| 20 |
*/ |
| 21 |
private const T_OPERATOR = 2; |
| 22 |
|
| 23 |
/** |
| 24 |
* @throws UnexpectedTokenException |
| 25 |
* @throws UnexpectedEOFException |
| 26 |
* |
| 27 |
* @internal since V8.8.0 |
| 28 |
*/ |
| 29 |
public static function parse(ParserState $parserState, bool $ignoreCase = false): CSSFunction |
| 30 |
{ |
| 31 |
$operators = ['+', '-', '*', '/']; |
| 32 |
$function = $parserState->parseIdentifier(); |
| 33 |
if ($parserState->peek() !== '(') { |
| 34 |
// Found ; or end of line before an opening bracket |
| 35 |
throw new UnexpectedTokenException('(', $parserState->peek(), 'literal', $parserState->currentLine()); |
| 36 |
} elseif ($function !== 'calc') { |
| 37 |
// Found invalid calc definition. Example calc (... |
| 38 |
throw new UnexpectedTokenException('calc', $function, 'literal', $parserState->currentLine()); |
| 39 |
} |
| 40 |
$parserState->consume('('); |
| 41 |
$calcRuleValueList = new CalcRuleValueList($parserState->currentLine()); |
| 42 |
$list = new RuleValueList(',', $parserState->currentLine()); |
| 43 |
$nestingLevel = 0; |
| 44 |
$lastComponentType = null; |
| 45 |
while (!$parserState->comes(')') || $nestingLevel > 0) { |
| 46 |
if ($parserState->isEnd() && $nestingLevel === 0) { |
| 47 |
break; |
| 48 |
} |
| 49 |
|
| 50 |
$parserState->consumeWhiteSpace(); |
| 51 |
if ($parserState->comes('(')) { |
| 52 |
$nestingLevel++; |
| 53 |
$calcRuleValueList->addListComponent($parserState->consume(1)); |
| 54 |
$parserState->consumeWhiteSpace(); |
| 55 |
continue; |
| 56 |
} elseif ($parserState->comes(')')) { |
| 57 |
$nestingLevel--; |
| 58 |
$calcRuleValueList->addListComponent($parserState->consume(1)); |
| 59 |
$parserState->consumeWhiteSpace(); |
| 60 |
continue; |
| 61 |
} |
| 62 |
if ($lastComponentType !== CalcFunction::T_OPERAND) { |
| 63 |
$value = Value::parsePrimitiveValue($parserState); |
| 64 |
$calcRuleValueList->addListComponent($value); |
| 65 |
$lastComponentType = CalcFunction::T_OPERAND; |
| 66 |
} else { |
| 67 |
if (\in_array($parserState->peek(), $operators, true)) { |
| 68 |
if (($parserState->comes('-') || $parserState->comes('+'))) { |
| 69 |
if ( |
| 70 |
$parserState->peek(1, -1) !== ' ' |
| 71 |
|| !($parserState->comes('- ') |
| 72 |
|| $parserState->comes('+ ')) |
| 73 |
) { |
| 74 |
throw new UnexpectedTokenException( |
| 75 |
" {$parserState->peek()} ", |
| 76 |
$parserState->peek(1, -1) . $parserState->peek(2), |
| 77 |
'literal', |
| 78 |
$parserState->currentLine() |
| 79 |
); |
| 80 |
} |
| 81 |
} |
| 82 |
$calcRuleValueList->addListComponent($parserState->consume(1)); |
| 83 |
$lastComponentType = CalcFunction::T_OPERATOR; |
| 84 |
} else { |
| 85 |
throw new UnexpectedTokenException( |
| 86 |
\sprintf( |
| 87 |
'Next token was expected to be an operand of type %s. Instead "%s" was found.', |
| 88 |
\implode(', ', $operators), |
| 89 |
$parserState->peek() |
| 90 |
), |
| 91 |
'', |
| 92 |
'custom', |
| 93 |
$parserState->currentLine() |
| 94 |
); |
| 95 |
} |
| 96 |
} |
| 97 |
$parserState->consumeWhiteSpace(); |
| 98 |
} |
| 99 |
$list->addListComponent($calcRuleValueList); |
| 100 |
if (!$parserState->isEnd()) { |
| 101 |
$parserState->consume(')'); |
| 102 |
} |
| 103 |
return new CalcFunction($function, $list, ',', $parserState->currentLine()); |
| 104 |
} |
| 105 |
} |
| 106 |
|