Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix/arithmetic in functions #390

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 31 additions & 2 deletions src/Value/CSSFunction.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,35 @@ public function __construct($sName, $aArguments, $sSeparator = ',', $iLineNo = 0
parent::__construct($aArguments, $sSeparator, $iLineNo);
}

/**
* @param ParserState $oParserState
* @param bool $bIgnoreCase
*
* @return string
*
* @throws SourceException
* @throws UnexpectedEOFException
* @throws UnexpectedTokenException
*/
public static function parseName(ParserState $oParserState, $bIgnoreCase = false)
{
return $oParserState->parseIdentifier($bIgnoreCase);
}

/**
* @param ParserState $oParserState
*
* @return array
*
* @throws SourceException
* @throws UnexpectedEOFException
* @throws UnexpectedTokenException
*/
public static function parseArgs(ParserState $oParserState)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps parseArguments is a better name (though I know 'args' is a very common shorthand).

{
return Value::parseValue($oParserState, ['=', ' ', ',']);
}

Comment on lines +36 to +64
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like the refactoring. But can the new methods be made private - I presume they are (currently) only intended to be used internally by the class?

Also, since we now have a minimum PHP version of 7.2, the return types can be included in the function declaration (and removed from the DocBlock, since they would then become redundant there). (I know we didn't have PHP>=7.2 at the time this PR was created.)

/**
* @param ParserState $oParserState
* @param bool $bIgnoreCase
Expand All @@ -45,9 +74,9 @@ public function __construct($sName, $aArguments, $sSeparator = ',', $iLineNo = 0
*/
public static function parse(ParserState $oParserState, $bIgnoreCase = false)
{
$mResult = $oParserState->parseIdentifier($bIgnoreCase);
$mResult = self::parseName($oParserState, $bIgnoreCase);
$oParserState->consume('(');
$aArguments = Value::parseValue($oParserState, ['=', ' ', ',']);
$aArguments = self::parseArgs($oParserState);
$mResult = new CSSFunction($mResult, $aArguments, ',', $oParserState->currentLine());
$oParserState->consume(')');
return $mResult;
Expand Down
14 changes: 13 additions & 1 deletion src/Value/Value.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,9 @@ public static function parseValue(ParserState $oParserState, array $aListDelimit
}
$iStartPosition = null;
while (($iStartPosition = array_search($sDelimiter, $aStack, true)) !== false) {
if ($iStartPosition === 0) {
break;
}
$iLength = 2; //Number of elements to be joined
for ($i = $iStartPosition + 2; $i < count($aStack); $i += 2, ++$iLength) {
if ($sDelimiter !== $aStack[$i]) {
Expand Down Expand Up @@ -156,7 +159,16 @@ public static function parsePrimitiveValue(ParserState $oParserState)
} elseif ($oParserState->comes("U+")) {
$oValue = self::parseUnicodeRangeValue($oParserState);
} else {
$oValue = self::parseIdentifierOrFunction($oParserState);
$sNextChar = $oParserState->peek(1);
try {
$oValue = self::parseIdentifierOrFunction($oParserState);
} catch (UnexpectedTokenException $e) {
if (in_array($sNextChar, ['+', '-', '*', '/'], true)) {
$oValue = $oParserState->consume(1);
} else {
throw $e;
}
}
}
$oParserState->consumeWhiteSpace();
return $oValue;
Expand Down
23 changes: 23 additions & 0 deletions tests/ParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1240,6 +1240,29 @@ public function lonelyImport()
self::assertSame($sExpected, $oDoc->render());
}

/**
* @test
*/
public function functionArithmeticInFile()
{
$oDoc = self::parsedStructureForFile('function-arithmetic', Settings::create()->withMultibyteSupport(true));
$sExpected = 'div {height: max(300,vh + 10);}
div {height: max(300,vh - 10);}
div {height: max(300,vh * 10);}
div {height: max(300,vh / 10);}';
Comment on lines +1249 to +1252
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For readability, can this be a series of concatenations with "\n", so that indentation is retained?

self::assertSame($sExpected, $oDoc->render());
}

/**
* @test
*/
public function infiniteLoopInFile()
{
$oDoc = self::parsedStructureForFile('infinite-loop', Settings::create()->withMultibyteSupport(true));
$sExpected = 'div {}';
self::assertSame($sExpected, $oDoc->render());
}

public function escapedSpecialCaseTokens()
{
$oDoc = $this->parsedStructureForFile('escaped-tokens');
Expand Down
12 changes: 12 additions & 0 deletions tests/fixtures/function-arithmetic.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
div {
height: max(300, vh + 10);
}
div {
height: max(300, vh - 10);
}
div {
height: max(300, vh * 10);
}
div {
height: max(300, vh / 10);
}
Comment on lines +1 to +12
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd prefer valid examples here. vh is a unit not a value. 100vh is the viewport height. Though an extra test where the number is omitted wouldn't go amiss.

3 changes: 3 additions & 0 deletions tests/fixtures/infinite-loop.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
div {
height: ///!;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice.

}