CSSFunction.php
2 years ago
CSSString.php
4 years ago
CalcFunction.php
2 years ago
CalcRuleValueList.php
4 years ago
Color.php
2 years ago
LineName.php
4 years ago
PrimitiveValue.php
4 years ago
RuleValueList.php
4 years ago
Size.php
2 years ago
URL.php
2 years ago
Value.php
2 years ago
ValueList.php
4 years ago
index.php
3 years ago
CalcFunction.php
69 lines
| 1 | <?php |
| 2 | namespace MailPoetVendor\Sabberworm\CSS\Value; |
| 3 | if (!defined('ABSPATH')) exit; |
| 4 | use MailPoetVendor\Sabberworm\CSS\Parsing\ParserState; |
| 5 | use MailPoetVendor\Sabberworm\CSS\Parsing\UnexpectedEOFException; |
| 6 | use MailPoetVendor\Sabberworm\CSS\Parsing\UnexpectedTokenException; |
| 7 | class CalcFunction extends CSSFunction |
| 8 | { |
| 9 | const T_OPERAND = 1; |
| 10 | const T_OPERATOR = 2; |
| 11 | public static function parse(ParserState $oParserState, $bIgnoreCase = \false) |
| 12 | { |
| 13 | $aOperators = ['+', '-', '*', '/']; |
| 14 | $sFunction = $oParserState->parseIdentifier(); |
| 15 | if ($oParserState->peek() != '(') { |
| 16 | // Found ; or end of line before an opening bracket |
| 17 | throw new UnexpectedTokenException('(', $oParserState->peek(), 'literal', $oParserState->currentLine()); |
| 18 | } elseif (!\in_array($sFunction, ['calc', '-moz-calc', '-webkit-calc'])) { |
| 19 | // Found invalid calc definition. Example calc (... |
| 20 | throw new UnexpectedTokenException('calc', $sFunction, 'literal', $oParserState->currentLine()); |
| 21 | } |
| 22 | $oParserState->consume('('); |
| 23 | $oCalcList = new CalcRuleValueList($oParserState->currentLine()); |
| 24 | $oList = new RuleValueList(',', $oParserState->currentLine()); |
| 25 | $iNestingLevel = 0; |
| 26 | $iLastComponentType = null; |
| 27 | while (!$oParserState->comes(')') || $iNestingLevel > 0) { |
| 28 | if ($oParserState->isEnd() && $iNestingLevel === 0) { |
| 29 | break; |
| 30 | } |
| 31 | $oParserState->consumeWhiteSpace(); |
| 32 | if ($oParserState->comes('(')) { |
| 33 | $iNestingLevel++; |
| 34 | $oCalcList->addListComponent($oParserState->consume(1)); |
| 35 | $oParserState->consumeWhiteSpace(); |
| 36 | continue; |
| 37 | } elseif ($oParserState->comes(')')) { |
| 38 | $iNestingLevel--; |
| 39 | $oCalcList->addListComponent($oParserState->consume(1)); |
| 40 | $oParserState->consumeWhiteSpace(); |
| 41 | continue; |
| 42 | } |
| 43 | if ($iLastComponentType != CalcFunction::T_OPERAND) { |
| 44 | $oVal = Value::parsePrimitiveValue($oParserState); |
| 45 | $oCalcList->addListComponent($oVal); |
| 46 | $iLastComponentType = CalcFunction::T_OPERAND; |
| 47 | } else { |
| 48 | if (\in_array($oParserState->peek(), $aOperators)) { |
| 49 | if ($oParserState->comes('-') || $oParserState->comes('+')) { |
| 50 | if ($oParserState->peek(1, -1) != ' ' || !($oParserState->comes('- ') || $oParserState->comes('+ '))) { |
| 51 | throw new UnexpectedTokenException(" {$oParserState->peek()} ", $oParserState->peek(1, -1) . $oParserState->peek(2), 'literal', $oParserState->currentLine()); |
| 52 | } |
| 53 | } |
| 54 | $oCalcList->addListComponent($oParserState->consume(1)); |
| 55 | $iLastComponentType = CalcFunction::T_OPERATOR; |
| 56 | } else { |
| 57 | throw new UnexpectedTokenException(\sprintf('Next token was expected to be an operand of type %s. Instead "%s" was found.', \implode(', ', $aOperators), $oVal), '', 'custom', $oParserState->currentLine()); |
| 58 | } |
| 59 | } |
| 60 | $oParserState->consumeWhiteSpace(); |
| 61 | } |
| 62 | $oList->addListComponent($oCalcList); |
| 63 | if (!$oParserState->isEnd()) { |
| 64 | $oParserState->consume(')'); |
| 65 | } |
| 66 | return new CalcFunction($sFunction, $oList, ',', $oParserState->currentLine()); |
| 67 | } |
| 68 | } |
| 69 |