Skip to content

[FEATURE] Support rgba and rgb (etc.) being aliases #797

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

Merged
merged 3 commits into from
Jan 22, 2025
Merged
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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ Please also have a look at our

### Added

- Partial support for CSS Color Module Level 4 syntax:
- `rgb` and `rgba`, and `hsl` and `hsla` are now aliases (#797}
- Add official support for PHP 8.4 (#657)
- Support arithmetic operators in CSS function arguments (#607)
- Add support for inserting an item in a CSS list (#545)
Expand Down
40 changes: 34 additions & 6 deletions src/Value/Color.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,23 +72,51 @@ public static function parse(ParserState $oParserState, bool $bIgnoreCase = fals
$oParserState->consumeWhiteSpace();
$oParserState->consume('(');

// CSS Color Module Level 4 says that `rgb` and `rgba` are now aliases; likewise `hsl` and `hsla`.
// So, attempt to parse with the `a`, and allow for it not being there.
switch ($sColorMode) {
case 'rgb':
$colorModeForParsing = 'rgba';
$mayHaveOptionalAlpha = true;
break;
case 'hsl':
$colorModeForParsing = 'hsla';
$mayHaveOptionalAlpha = true;
break;
case 'rgba':
// This is handled identically to the following case.
case 'hsla':
$colorModeForParsing = $sColorMode;
$mayHaveOptionalAlpha = true;
break;
default:
$colorModeForParsing = $sColorMode;
$mayHaveOptionalAlpha = false;
}

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

if ($bContainsVar && $oParserState->comes(')')) {
// With a var argument the function can have fewer arguments
// This must be done first, to consume comments as well, so that the `comes` test will work.
$oParserState->consumeWhiteSpace();

// With a `var` argument, the function can have fewer arguments.
// And as of CSS Color Module Level 4, the alpha argument is optional.
$canCloseNow =
$bContainsVar ||
($mayHaveOptionalAlpha && $i >= $iLength - 2);
if ($canCloseNow && $oParserState->comes(')')) {
break;
}

$oParserState->consumeWhiteSpace();
if ($i < ($iLength - 1)) {
$oParserState->consume(',');
}
Expand Down
8 changes: 3 additions & 5 deletions tests/Unit/Value/ColorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,15 +62,15 @@ public static function provideValidColorAndExpectedRendering(): array
'rgba(0, 119, 0, 50%)',
'rgba(0,119,0,50%)',
],
/*
'legacy rgb as rgba' => [
'rgba(0, 119, 0)',
'rgb(0,119,0)',
'#070',
],
'legacy rgba as rgb' => [
'rgb(0, 119, 0, 0.5)',
'rgba(0,119,0,.5)',
],
/*
'modern rgb' => [
'rgb(0 119 0)',
'rgb(0,119,0)',
Expand Down Expand Up @@ -112,16 +112,14 @@ public static function provideValidColorAndExpectedRendering(): array
'hsla(120, 100%, 25%, 50%)',
'hsla(120,100%,25%,50%)',
],
/*
'legacy hsl as hsla' => [
'hsla(120, 100%, 25%)',
'hsl(120,100%,25%)',
],
'legacy hsla as hsl' => [
'hsl(120, 100%, 25%, 0.5)',
'hsla(120,100%,25%,0.5)',
'hsla(120,100%,25%,.5)',
],
//*/
];
}

Expand Down