diff --git a/lib/Sabberworm/CSS/Parser.php b/lib/Sabberworm/CSS/Parser.php index ff563ddd..eea13853 100644 --- a/lib/Sabberworm/CSS/Parser.php +++ b/lib/Sabberworm/CSS/Parser.php @@ -512,8 +512,15 @@ private function parseColorValue() { $sValue = $this->parseIdentifier(false); if ($this->strlen($sValue) === 3) { $sValue = $sValue[0] . $sValue[0] . $sValue[1] . $sValue[1] . $sValue[2] . $sValue[2]; + } else if ($this->strlen($sValue) === 4) { + $sValue = $sValue[0] . $sValue[0] . $sValue[1] . $sValue[1] . $sValue[2] . $sValue[2] . $sValue[3] . $sValue[3]; + } + + if ($this->strlen($sValue) === 8) { + $aColor = array('r' => new Size(intval($sValue[0] . $sValue[1], 16), null, true, $this->iLineNo), 'g' => new Size(intval($sValue[2] . $sValue[3], 16), null, true, $this->iLineNo), 'b' => new Size(intval($sValue[4] . $sValue[5], 16), null, true, $this->iLineNo), 'a' => new Size(round($this->mapRange(intval($sValue[6] . $sValue[7], 16), 0, 255, 0, 1), 2), null, true, $this->iLineNo)); + } else { + $aColor = array('r' => new Size(intval($sValue[0] . $sValue[1], 16), null, true, $this->iLineNo), 'g' => new Size(intval($sValue[2] . $sValue[3], 16), null, true, $this->iLineNo), 'b' => new Size(intval($sValue[4] . $sValue[5], 16), null, true, $this->iLineNo)); } - $aColor = array('r' => new Size(intval($sValue[0] . $sValue[1], 16), null, true, $this->iLineNo), 'g' => new Size(intval($sValue[2] . $sValue[3], 16), null, true, $this->iLineNo), 'b' => new Size(intval($sValue[4] . $sValue[5], 16), null, true, $this->iLineNo)); } else { $sColorMode = $this->parseIdentifier(false); $this->consumeWhiteSpace(); @@ -800,4 +807,13 @@ private function strpos($sString, $sNeedle, $iOffset) { } } + private function mapRange($fVal, $fFromMin, $fFromMax, $fToMin, $fToMax) { + $fFromRange = $fFromMax - $fFromMin; + $fToRange = $fToMax - $fToMin; + $fMultiplier = $fToRange / $fFromRange; + $fNewVal = $fVal - $fFromMin; + $fNewVal *= $fMultiplier; + return $fNewVal + $fToMin; + } + } diff --git a/tests/Sabberworm/CSS/ParserTest.php b/tests/Sabberworm/CSS/ParserTest.php index 21c1dc1b..ea8bbdf5 100644 --- a/tests/Sabberworm/CSS/ParserTest.php +++ b/tests/Sabberworm/CSS/ParserTest.php @@ -387,6 +387,13 @@ function testUrlInFile() { $this->assertSame($sExpected, $oDoc->render()); } + function testHexAlphaInFile() { + $oDoc = $this->parsedStructureForFile('hex-alpha', Settings::create()->withMultibyteSupport(true)); + $sExpected = 'div {background: rgba(17,34,51,.27);} +div {background: rgba(17,34,51,.27);}'; + $this->assertSame($sExpected, $oDoc->render()); + } + function testCalcInFile() { $oDoc = $this->parsedStructureForFile('calc', Settings::create()->withMultibyteSupport(true)); $sExpected = 'div {width: calc(100% / 4);} diff --git a/tests/files/hex-alpha.css b/tests/files/hex-alpha.css new file mode 100644 index 00000000..dbbfe289 --- /dev/null +++ b/tests/files/hex-alpha.css @@ -0,0 +1,2 @@ +div { background: #1234; } +div { background: #11223344; }