CSSFunction.php
8 months ago
CSSString.php
8 months ago
CalcFunction.php
8 months ago
CalcRuleValueList.php
8 months ago
Color.php
8 months ago
LineName.php
8 months ago
PrimitiveValue.php
8 months ago
RuleValueList.php
8 months ago
Size.php
8 months ago
URL.php
8 months ago
Value.php
8 months ago
ValueList.php
8 months ago
CalcFunction.php
116 lines
| 1 | <?php |
| 2 | |
| 3 | namespace Sabberworm\CSS\Value; |
| 4 | |
| 5 | use Sabberworm\CSS\Parsing\ParserState; |
| 6 | use Sabberworm\CSS\Parsing\UnexpectedEOFException; |
| 7 | use Sabberworm\CSS\Parsing\UnexpectedTokenException; |
| 8 | |
| 9 | /** |
| 10 | * Support for `-webkit-calc` and `-moz-calc` is deprecated in version 8.8.0, and will be removed in version 9.0.0. |
| 11 | */ |
| 12 | class CalcFunction extends CSSFunction |
| 13 | { |
| 14 | /** |
| 15 | * @var int |
| 16 | * |
| 17 | * @internal |
| 18 | */ |
| 19 | const T_OPERAND = 1; |
| 20 | |
| 21 | /** |
| 22 | * @var int |
| 23 | * |
| 24 | * @internal |
| 25 | */ |
| 26 | const T_OPERATOR = 2; |
| 27 | |
| 28 | /** |
| 29 | * @param ParserState $oParserState |
| 30 | * @param bool $bIgnoreCase |
| 31 | * |
| 32 | * @return CalcFunction |
| 33 | * |
| 34 | * @throws UnexpectedTokenException |
| 35 | * @throws UnexpectedEOFException |
| 36 | * |
| 37 | * @internal since V8.8.0 |
| 38 | */ |
| 39 | public static function parse(ParserState $oParserState, $bIgnoreCase = false) |
| 40 | { |
| 41 | $aOperators = ['+', '-', '*', '/']; |
| 42 | $sFunction = $oParserState->parseIdentifier(); |
| 43 | if ($oParserState->peek() != '(') { |
| 44 | // Found ; or end of line before an opening bracket |
| 45 | throw new UnexpectedTokenException('(', $oParserState->peek(), 'literal', $oParserState->currentLine()); |
| 46 | } elseif (!in_array($sFunction, ['calc', '-moz-calc', '-webkit-calc'])) { |
| 47 | // Found invalid calc definition. Example calc (... |
| 48 | throw new UnexpectedTokenException('calc', $sFunction, 'literal', $oParserState->currentLine()); |
| 49 | } |
| 50 | $oParserState->consume('('); |
| 51 | $oCalcList = new CalcRuleValueList($oParserState->currentLine()); |
| 52 | $oList = new RuleValueList(',', $oParserState->currentLine()); |
| 53 | $iNestingLevel = 0; |
| 54 | $iLastComponentType = null; |
| 55 | while (!$oParserState->comes(')') || $iNestingLevel > 0) { |
| 56 | if ($oParserState->isEnd() && $iNestingLevel === 0) { |
| 57 | break; |
| 58 | } |
| 59 | |
| 60 | $oParserState->consumeWhiteSpace(); |
| 61 | if ($oParserState->comes('(')) { |
| 62 | $iNestingLevel++; |
| 63 | $oCalcList->addListComponent($oParserState->consume(1)); |
| 64 | $oParserState->consumeWhiteSpace(); |
| 65 | continue; |
| 66 | } elseif ($oParserState->comes(')')) { |
| 67 | $iNestingLevel--; |
| 68 | $oCalcList->addListComponent($oParserState->consume(1)); |
| 69 | $oParserState->consumeWhiteSpace(); |
| 70 | continue; |
| 71 | } |
| 72 | if ($iLastComponentType != CalcFunction::T_OPERAND) { |
| 73 | $oVal = Value::parsePrimitiveValue($oParserState); |
| 74 | $oCalcList->addListComponent($oVal); |
| 75 | $iLastComponentType = CalcFunction::T_OPERAND; |
| 76 | } else { |
| 77 | if (in_array($oParserState->peek(), $aOperators)) { |
| 78 | if (($oParserState->comes('-') || $oParserState->comes('+'))) { |
| 79 | if ( |
| 80 | $oParserState->peek(1, -1) != ' ' |
| 81 | || !($oParserState->comes('- ') |
| 82 | || $oParserState->comes('+ ')) |
| 83 | ) { |
| 84 | throw new UnexpectedTokenException( |
| 85 | " {$oParserState->peek()} ", |
| 86 | $oParserState->peek(1, -1) . $oParserState->peek(2), |
| 87 | 'literal', |
| 88 | $oParserState->currentLine() |
| 89 | ); |
| 90 | } |
| 91 | } |
| 92 | $oCalcList->addListComponent($oParserState->consume(1)); |
| 93 | $iLastComponentType = CalcFunction::T_OPERATOR; |
| 94 | } else { |
| 95 | throw new UnexpectedTokenException( |
| 96 | sprintf( |
| 97 | 'Next token was expected to be an operand of type %s. Instead "%s" was found.', |
| 98 | implode(', ', $aOperators), |
| 99 | $oParserState->peek() |
| 100 | ), |
| 101 | '', |
| 102 | 'custom', |
| 103 | $oParserState->currentLine() |
| 104 | ); |
| 105 | } |
| 106 | } |
| 107 | $oParserState->consumeWhiteSpace(); |
| 108 | } |
| 109 | $oList->addListComponent($oCalcList); |
| 110 | if (!$oParserState->isEnd()) { |
| 111 | $oParserState->consume(')'); |
| 112 | } |
| 113 | return new CalcFunction($sFunction, $oList, ',', $oParserState->currentLine()); |
| 114 | } |
| 115 | } |
| 116 |