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

Allow calc() function inside rgba() and hsla() #259

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
11 changes: 7 additions & 4 deletions src/Value/Color.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,18 +49,21 @@ public static function parse(ParserState $oParserState)
$oParserState->consumeWhiteSpace();
$oParserState->consume('(');

$bContainsVar = false;
$bContainsVarOrCalc = false;
$iLength = $oParserState->strlen($sColorMode);
for ($i = 0; $i < $iLength; ++$i) {
$oParserState->consumeWhiteSpace();
if ($oParserState->comes('var')) {
$aColor[$sColorMode[$i]] = CSSFunction::parseIdentifierOrFunction($oParserState);
$bContainsVar = true;
$bContainsVarOrCalc = true;
} elseif ($oParserState->comes('calc')) {
$aColor[$sColorMode[$i]] = CalcFunction::parse($oParserState);
$bContainsVarOrCalc = true;
} else {
$aColor[$sColorMode[$i]] = Size::parse($oParserState, true);
}

if ($bContainsVar && $oParserState->comes(')')) {
if ($bContainsVarOrCalc && $oParserState->comes(')')) {
// With a var argument the function can have fewer arguments
break;
}
Expand All @@ -72,7 +75,7 @@ public static function parse(ParserState $oParserState)
}
$oParserState->consume(')');

if ($bContainsVar) {
if ($bContainsVarOrCalc) {
return new CSSFunction($sColorMode, array_values($aColor), ',', $oParserState->currentLine());
}
}
Expand Down
5 changes: 4 additions & 1 deletion tests/ParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,10 @@ public function testColorParsing()
. 'background-color: rgb(255,var(--rg));background-color: hsl(var(--some-hsl));}'
. "\n"
. '#variables-alpha {background-color: rgba(var(--some-rgb),.1);'
. 'background-color: rgba(var(--some-rg),255,.1);background-color: hsla(var(--some-hsl),.1);}',
. 'background-color: rgba(var(--some-rg),255,.1);background-color: hsla(var(--some-hsl),.1);}'
Copy link
Contributor

Choose a reason for hiding this comment

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

Soon, I'd like to split up the tests so that each test method is only about one piece of behavior, not multiple things mashed together. You can help my work by moving the test for the new behavior into a new test. Thanks!

Copy link
Author

Choose a reason for hiding this comment

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

Not sure if I'll have time for awhile. I needed a quick fix for the calc() issue, and I saw the necessary changes were simple enough. I didn't dig too far into the unit testing, but I agree that it would be better to break them up into separate tests.

Copy link
Contributor

Choose a reason for hiding this comment

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

@thomashigginbotham If you don't have time now, then let's keep this PR as it is, and then I will split up the test later. Kudos to you for covering your change with a test, by the way! ❤️

. "\n"
. '#calc {background-color: rgba(var(--some-rgb),calc(var(--some-alpha) * .1));'
. 'background-color: hsla(var(--some-hsl),calc(var(--some-alpha) * .1));}',
$oDoc->render()
);
}
Expand Down
5 changes: 5 additions & 0 deletions tests/fixtures/colortest.css
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,8 @@
background-color: rgba(var(--some-rg), 255, 0.1);
background-color: hsla(var(--some-hsl), 0.1);
}

#calc {
background-color: rgba(var(--some-rgb), calc(var(--some-alpha) * 0.1));
background-color: hsla(var(--some-hsl), calc(var(--some-alpha) * 0.1));
}