Skip to content

Commit cd29137

Browse files
raxbgJakeQZ
authored andcommitted
[FEATURE] Parse simple expressions
1 parent 3500d42 commit cd29137

4 files changed

Lines changed: 59 additions & 0 deletions

File tree

src/Value/Expression.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
3+
namespace Sabberworm\CSS\Value;
4+
5+
use Sabberworm\CSS\OutputFormat;
6+
use Sabberworm\CSS\Parsing\ParserState;
7+
8+
/**
9+
* An `Expression` represents a special kind of value that is comprised of multiple components wrapped in parenthesis.
10+
* Examle `height: (vh - 10);`.
11+
*/
12+
class Expression extends CSSFunction
13+
{
14+
/**
15+
* @param ParserState $oParserState
16+
* @param bool $bIgnoreCase
17+
*
18+
* @return Expression
19+
*
20+
* @throws SourceException
21+
* @throws UnexpectedEOFException
22+
* @throws UnexpectedTokenException
23+
*/
24+
public static function parse(ParserState $oParserState, $bIgnoreCase = false)
25+
{
26+
$oParserState->consume('(');
27+
$aArguments = Value::parseValue($oParserState, ['=', ' ', ',']);
28+
$mResult = new Expression("", $aArguments, ',', $oParserState->currentLine());
29+
$oParserState->consume(')');
30+
return $mResult;
31+
}
32+
}

src/Value/Value.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,8 @@ public static function parsePrimitiveValue(ParserState $parserState)
164164
$value = LineName::parse($parserState);
165165
} elseif ($parserState->comes('U+')) {
166166
$value = self::parseUnicodeRangeValue($parserState);
167+
} elseif ($parserState->comes("(")) {
168+
$value = Expression::parse($parserState);
167169
} else {
168170
$nextCharacter = $parserState->peek(1);
169171
try {

tests/ParserTest.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -435,6 +435,20 @@ public function functionSyntax(): void
435435
self::assertSame($expected, $document->render());
436436
}
437437

438+
/**
439+
* @test
440+
*/
441+
public function parseExpressions()
442+
{
443+
$oDoc = self::parsedStructureForFile('expressions');
444+
$sExpected = 'div {height: (vh - 10);}'
445+
. "\n"
446+
. 'div {height: (vh - 10)/2;}'
447+
. "\n"
448+
. 'div {height: max(5,(vh - 10));}';
449+
self::assertSame($sExpected, $oDoc->render());
450+
}
451+
438452
/**
439453
* @test
440454
*/

tests/fixtures/expressions.css

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
div {
2+
height: (vh - 10);
3+
}
4+
5+
div {
6+
height: (vh - 10) / 2;
7+
}
8+
9+
div {
10+
height: max(5, (vh - 10));
11+
}

0 commit comments

Comments
 (0)