From fd1547c9a510b93990da1ab3591aa8134f34f173 Mon Sep 17 00:00:00 2001 From: Thomas Higginbotham Date: Tue, 8 Jun 2021 15:12:44 -0400 Subject: [PATCH] Allow calc() function inside rgba() and hsla() This fix allows the alpha value of rgba() and hsla() functions to accept a calc() function. --- src/Value/Color.php | 11 +++++++---- tests/ParserTest.php | 5 ++++- tests/fixtures/colortest.css | 5 +++++ 3 files changed, 16 insertions(+), 5 deletions(-) diff --git a/src/Value/Color.php b/src/Value/Color.php index 6e9e8b74..d617f188 100644 --- a/src/Value/Color.php +++ b/src/Value/Color.php @@ -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; } @@ -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()); } } diff --git a/tests/ParserTest.php b/tests/ParserTest.php index a4cc97dc..7d16222b 100644 --- a/tests/ParserTest.php +++ b/tests/ParserTest.php @@ -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);}' + . "\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() ); } diff --git a/tests/fixtures/colortest.css b/tests/fixtures/colortest.css index 1c89cf41..3651dd50 100644 --- a/tests/fixtures/colortest.css +++ b/tests/fixtures/colortest.css @@ -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)); +}