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