Skip to content

Commit f225822

Browse files
authored
Merge pull request MyIntervals#197 from digilist/fix_var_inside_color_functions
2 parents 940279c + 695011b commit f225822

File tree

3 files changed

+37
-2
lines changed

3 files changed

+37
-2
lines changed

lib/Sabberworm/CSS/Value/Color.php

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,16 +39,33 @@ public static function parse(ParserState $oParserState) {
3939
$sColorMode = $oParserState->parseIdentifier(true);
4040
$oParserState->consumeWhiteSpace();
4141
$oParserState->consume('(');
42+
43+
$bContainsVar = false;
4244
$iLength = $oParserState->strlen($sColorMode);
4345
for ($i = 0; $i < $iLength; ++$i) {
4446
$oParserState->consumeWhiteSpace();
45-
$aColor[$sColorMode[$i]] = Size::parse($oParserState, true);
47+
if ($oParserState->comes('var')) {
48+
$aColor[$sColorMode[$i]] = CSSFunction::parseIdentifierOrFunction($oParserState);
49+
$bContainsVar = true;
50+
} else {
51+
$aColor[$sColorMode[$i]] = Size::parse($oParserState, true);
52+
}
53+
54+
if ($bContainsVar && $oParserState->comes(')')) {
55+
// With a var argument the function can have fewer arguments
56+
break;
57+
}
58+
4659
$oParserState->consumeWhiteSpace();
4760
if ($i < ($iLength - 1)) {
4861
$oParserState->consume(',');
4962
}
5063
}
5164
$oParserState->consume(')');
65+
66+
if ($bContainsVar) {
67+
return new CSSFunction($sColorMode, array_values($aColor), ',', $oParserState->currentLine());
68+
}
5269
}
5370
return new Color($aColor, $oParserState->currentLine());
5471
}

tests/Sabberworm/CSS/ParserTest.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,9 @@ function testColorParsing() {
7676
$this->assertSame('red', $sColor);
7777
}
7878
$this->assertSame('#mine {color: red;border-color: #0a64e6;border-color: rgba(10,100,231,.3);outline-color: #222;background-color: #232323;}
79-
#yours {background-color: hsl(220,10%,220%);background-color: hsla(220,10%,220%,.3);}', $oDoc->render());
79+
#yours {background-color: hsl(220,10%,220%);background-color: hsla(220,10%,220%,.3);}
80+
#variables {background-color: rgb(var(--some-rgb));background-color: rgb(var(--r),var(--g),var(--b));background-color: rgb(255,var(--g),var(--b));background-color: rgb(255,255,var(--b));background-color: rgb(255,var(--rg));background-color: hsl(var(--some-hsl));}
81+
#variables-alpha {background-color: rgba(var(--some-rgb),.1);background-color: rgba(var(--some-rg),255,.1);background-color: hsla(var(--some-hsl),.1);}', $oDoc->render());
8082
}
8183

8284
function testUnicodeParsing() {

tests/files/colortest.css

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,19 @@
1010
background-color: hsl(220, 10%, 220%);
1111
background-color: hsla(220, 10%, 220%, 0.3);
1212
}
13+
14+
#variables {
15+
background-color: rgb(var(--some-rgb));
16+
background-color: rgb(var(--r), var(--g), var(--b));
17+
background-color: rgb(255, var(--g), var(--b));
18+
background-color: rgb(255, 255, var(--b));
19+
background-color: rgb(255, var(--rg));
20+
21+
background-color: hsl(var(--some-hsl));
22+
}
23+
24+
#variables-alpha {
25+
background-color: rgba(var(--some-rgb), 0.1);
26+
background-color: rgba(var(--some-rg), 255, 0.1);
27+
background-color: hsla(var(--some-hsl), 0.1);
28+
}

0 commit comments

Comments
 (0)