Skip to content

Commit 695011b

Browse files
committed
Consider color functions with var() as CSSFunction #196
1 parent b256b0f commit 695011b

1 file changed

Lines changed: 13 additions & 23 deletions

File tree

lib/Sabberworm/CSS/Value/Color.php

Lines changed: 13 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -52,15 +52,7 @@ public static function parse(ParserState $oParserState) {
5252
}
5353

5454
if ($bContainsVar && $oParserState->comes(')')) {
55-
// With a var argument the function can have fewer arguments and the assignment to the color
56-
// channels does not make sense
57-
if ($i < $iLength - 1) {
58-
// Add remaining characters to last item
59-
$remainingChars = substr($sColorMode, $i + 1);
60-
$aColor[$sColorMode[$i] . $remainingChars] = $aColor[$sColorMode[$i]];
61-
unset($aColor[$sColorMode[$i]]);
62-
}
63-
55+
// With a var argument the function can have fewer arguments
6456
break;
6557
}
6658

@@ -70,6 +62,10 @@ public static function parse(ParserState $oParserState) {
7062
}
7163
}
7264
$oParserState->consume(')');
65+
66+
if ($bContainsVar) {
67+
return new CSSFunction($sColorMode, array_values($aColor), ',', $oParserState->currentLine());
68+
}
7369
}
7470
return new Color($aColor, $oParserState->currentLine());
7571
}
@@ -102,20 +98,14 @@ public function __toString() {
10298

10399
public function render(\Sabberworm\CSS\OutputFormat $oOutputFormat) {
104100
// Shorthand RGB color values
105-
if($oOutputFormat->getRGBHashNotation() && implode('', array_keys($this->aComponents)) === 'rgb' && count($this->aComponents) == 3) {
106-
$bAllAreSizes = count(array_filter($this->aComponents, function ($component) {
107-
return $component instanceof Size;
108-
})) === 3;
109-
110-
if ($bAllAreSizes) {
111-
$sResult = sprintf(
112-
'%02x%02x%02x',
113-
$this->aComponents['r']->getSize(),
114-
$this->aComponents['g']->getSize(),
115-
$this->aComponents['b']->getSize()
116-
);
117-
return '#'.(($sResult[0] == $sResult[1]) && ($sResult[2] == $sResult[3]) && ($sResult[4] == $sResult[5]) ? "$sResult[0]$sResult[2]$sResult[4]" : $sResult);
118-
}
101+
if($oOutputFormat->getRGBHashNotation() && implode('', array_keys($this->aComponents)) === 'rgb') {
102+
$sResult = sprintf(
103+
'%02x%02x%02x',
104+
$this->aComponents['r']->getSize(),
105+
$this->aComponents['g']->getSize(),
106+
$this->aComponents['b']->getSize()
107+
);
108+
return '#'.(($sResult[0] == $sResult[1]) && ($sResult[2] == $sResult[3]) && ($sResult[4] == $sResult[5]) ? "$sResult[0]$sResult[2]$sResult[4]" : $sResult);
119109
}
120110
return parent::render($oOutputFormat);
121111
}

0 commit comments

Comments
 (0)