Skip to content

Fix/css4 rgb parsing #202

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

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 5 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
24 changes: 19 additions & 5 deletions src/Value/Color.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,14 +66,21 @@ public static function parse(ParserState $oParserState)
$oParserState->consume('(');

$bContainsVar = false;
$iLength = $oParserState->strlen($sColorMode);
if (strpos($sColorMode, 'rgb') !== false) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

rgb has to be at the start, right?

Suggested change
if (strpos($sColorMode, 'rgb') !== false) {
if (str_starts_with($sColorMode, 'rgb')) {

Copy link
Collaborator

Choose a reason for hiding this comment

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

Though I prefer being explicit:

Suggested change
if (strpos($sColorMode, 'rgb') !== false) {
if ($sColorMode === 'rgb') {

(the rgba case will be handled in the else branch anyway).

$sColorTarget = 'rgba';
} elseif (strpos($sColorMode, 'hsl') !== false) {
$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 @@ -83,7 +90,14 @@ public static function parse(ParserState $oParserState)

$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 @@ -162,7 +162,9 @@ public function colorParsing()
. '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 @@ -26,3 +26,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
Collaborator

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
Collaborator

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);
}