Skip to content

Commit f3ea6a7

Browse files
authored
[FEATURE] Support rgba and rgb (etc.) being aliases (#797)
As of CSS Color Module Level 4, `rgba` is an alias of `rgb`; likewise, `hsla` is an alias of `hsl`. This change allows any of the above color functions to contain either three or four arguments, with alpha assumed as the fourth, and allowed to be absent.
1 parent bc511bb commit f3ea6a7

File tree

3 files changed

+39
-11
lines changed

3 files changed

+39
-11
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ Please also have a look at our
1010

1111
### Added
1212

13+
- Partial support for CSS Color Module Level 4 syntax:
14+
- `rgb` and `rgba`, and `hsl` and `hsla` are now aliases (#797}
1315
- Add official support for PHP 8.4 (#657)
1416
- Support arithmetic operators in CSS function arguments (#607)
1517
- Add support for inserting an item in a CSS list (#545)

src/Value/Color.php

Lines changed: 34 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -72,23 +72,51 @@ public static function parse(ParserState $oParserState, bool $bIgnoreCase = fals
7272
$oParserState->consumeWhiteSpace();
7373
$oParserState->consume('(');
7474

75+
// CSS Color Module Level 4 says that `rgb` and `rgba` are now aliases; likewise `hsl` and `hsla`.
76+
// So, attempt to parse with the `a`, and allow for it not being there.
77+
switch ($sColorMode) {
78+
case 'rgb':
79+
$colorModeForParsing = 'rgba';
80+
$mayHaveOptionalAlpha = true;
81+
break;
82+
case 'hsl':
83+
$colorModeForParsing = 'hsla';
84+
$mayHaveOptionalAlpha = true;
85+
break;
86+
case 'rgba':
87+
// This is handled identically to the following case.
88+
case 'hsla':
89+
$colorModeForParsing = $sColorMode;
90+
$mayHaveOptionalAlpha = true;
91+
break;
92+
default:
93+
$colorModeForParsing = $sColorMode;
94+
$mayHaveOptionalAlpha = false;
95+
}
96+
7597
$bContainsVar = false;
76-
$iLength = $oParserState->strlen($sColorMode);
98+
$iLength = $oParserState->strlen($colorModeForParsing);
7799
for ($i = 0; $i < $iLength; ++$i) {
78100
$oParserState->consumeWhiteSpace();
79101
if ($oParserState->comes('var')) {
80-
$aColor[$sColorMode[$i]] = CSSFunction::parseIdentifierOrFunction($oParserState);
102+
$aColor[$colorModeForParsing[$i]] = CSSFunction::parseIdentifierOrFunction($oParserState);
81103
$bContainsVar = true;
82104
} else {
83-
$aColor[$sColorMode[$i]] = Size::parse($oParserState, true);
105+
$aColor[$colorModeForParsing[$i]] = Size::parse($oParserState, true);
84106
}
85107

86-
if ($bContainsVar && $oParserState->comes(')')) {
87-
// With a var argument the function can have fewer arguments
108+
// This must be done first, to consume comments as well, so that the `comes` test will work.
109+
$oParserState->consumeWhiteSpace();
110+
111+
// With a `var` argument, the function can have fewer arguments.
112+
// And as of CSS Color Module Level 4, the alpha argument is optional.
113+
$canCloseNow =
114+
$bContainsVar ||
115+
($mayHaveOptionalAlpha && $i >= $iLength - 2);
116+
if ($canCloseNow && $oParserState->comes(')')) {
88117
break;
89118
}
90119

91-
$oParserState->consumeWhiteSpace();
92120
if ($i < ($iLength - 1)) {
93121
$oParserState->consume(',');
94122
}

tests/Unit/Value/ColorTest.php

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -62,15 +62,15 @@ public static function provideValidColorAndExpectedRendering(): array
6262
'rgba(0, 119, 0, 50%)',
6363
'rgba(0,119,0,50%)',
6464
],
65-
/*
6665
'legacy rgb as rgba' => [
6766
'rgba(0, 119, 0)',
68-
'rgb(0,119,0)',
67+
'#070',
6968
],
7069
'legacy rgba as rgb' => [
7170
'rgb(0, 119, 0, 0.5)',
7271
'rgba(0,119,0,.5)',
7372
],
73+
/*
7474
'modern rgb' => [
7575
'rgb(0 119 0)',
7676
'rgb(0,119,0)',
@@ -112,16 +112,14 @@ public static function provideValidColorAndExpectedRendering(): array
112112
'hsla(120, 100%, 25%, 50%)',
113113
'hsla(120,100%,25%,50%)',
114114
],
115-
/*
116115
'legacy hsl as hsla' => [
117116
'hsla(120, 100%, 25%)',
118117
'hsl(120,100%,25%)',
119118
],
120119
'legacy hsla as hsl' => [
121120
'hsl(120, 100%, 25%, 0.5)',
122-
'hsla(120,100%,25%,0.5)',
121+
'hsla(120,100%,25%,.5)',
123122
],
124-
//*/
125123
];
126124
}
127125

0 commit comments

Comments
 (0)