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/css4 rgb parsing #202

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
24 changes: 19 additions & 5 deletions src/Value/Color.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,14 +71,21 @@ public static function parse(ParserState $oParserState, bool $bIgnoreCase = fals
$oParserState->consume('(');

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

if ($bContainsVar && $oParserState->comes(')')) {
Expand All @@ -88,7 +95,14 @@ public static function parse(ParserState $oParserState, bool $bIgnoreCase = fals

$oParserState->consumeWhiteSpace();
if ($i < ($iLength - 1)) {
$oParserState->consume(',');
if ($oParserState->comes(',')) {
$oParserState->consume(',');
} elseif ($oParserState->comes('/')) {
$oParserState->consume('/');
} elseif ($oParserState->comes(')')) {
// No alpha channel information
break;
}
}
}
$oParserState->consume(')');
Expand Down Expand Up @@ -163,7 +177,7 @@ public function render(OutputFormat $oOutputFormat)
$this->aComponents['b']->getSize()
);
return '#' . (($sResult[0] == $sResult[1]) && ($sResult[2] == $sResult[3]) && ($sResult[4] == $sResult[5])
? "$sResult[0]$sResult[2]$sResult[4]" : $sResult);
? "$sResult[0]$sResult[2]$sResult[4]" : $sResult);
}
return parent::render($oOutputFormat);
}
Expand Down
4 changes: 3 additions & 1 deletion tests/ParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,9 @@ public function colorParsing(): void
. '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"
. '#css4-rgba {background-color: rgba(242,245,249,45%);background-color: #f2f5f9;}',
$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 @@ -27,3 +27,8 @@
background-color: rgba(var(--some-rg), 255, 0.1);
background-color: hsla(var(--some-hsl), 0.1);
}

#css4-rgba {
background-color: rgb(242 245 249 / 45%);
Copy link
Contributor

Choose a reason for hiding this comment

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

It would be cool to have an output format option to prefer this syntax over the other. AFAICT there are three boolean options that are all independent of one another: prefer-rgb-over-rgba / rgb-alpha-separator-use-slash / rgb-alpha-use-percentage.

Copy link
Contributor

Choose a reason for hiding this comment

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

It would, though can (and should) be done as a separate PR.

background-color: rgba(242 245 249);
}