Skip to content

Commit d536cc4

Browse files
committed
Merge branch 'master' of github.com:raxbg/PHP-CSS-Parser into suppress_eof_in_lenient
2 parents 5c733e2 + 917949b commit d536cc4

File tree

3 files changed

+26
-1
lines changed

3 files changed

+26
-1
lines changed

lib/Sabberworm/CSS/Parser.php

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -512,8 +512,15 @@ private function parseColorValue() {
512512
$sValue = $this->parseIdentifier(false);
513513
if ($this->strlen($sValue) === 3) {
514514
$sValue = $sValue[0] . $sValue[0] . $sValue[1] . $sValue[1] . $sValue[2] . $sValue[2];
515+
} else if ($this->strlen($sValue) === 4) {
516+
$sValue = $sValue[0] . $sValue[0] . $sValue[1] . $sValue[1] . $sValue[2] . $sValue[2] . $sValue[3] . $sValue[3];
517+
}
518+
519+
if ($this->strlen($sValue) === 8) {
520+
$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));
521+
} else {
522+
$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));
515523
}
516-
$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));
517524
} else {
518525
$sColorMode = $this->parseIdentifier(false);
519526
$this->consumeWhiteSpace();
@@ -800,4 +807,13 @@ private function strpos($sString, $sNeedle, $iOffset) {
800807
}
801808
}
802809

810+
private function mapRange($fVal, $fFromMin, $fFromMax, $fToMin, $fToMax) {
811+
$fFromRange = $fFromMax - $fFromMin;
812+
$fToRange = $fToMax - $fToMin;
813+
$fMultiplier = $fToRange / $fFromRange;
814+
$fNewVal = $fVal - $fFromMin;
815+
$fNewVal *= $fMultiplier;
816+
return $fNewVal + $fToMin;
817+
}
818+
803819
}

tests/Sabberworm/CSS/ParserTest.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -387,6 +387,13 @@ function testUrlInFile() {
387387
$this->assertSame($sExpected, $oDoc->render());
388388
}
389389

390+
function testHexAlphaInFile() {
391+
$oDoc = $this->parsedStructureForFile('hex-alpha', Settings::create()->withMultibyteSupport(true));
392+
$sExpected = 'div {background: rgba(17,34,51,.27);}
393+
div {background: rgba(17,34,51,.27);}';
394+
$this->assertSame($sExpected, $oDoc->render());
395+
}
396+
390397
function testCalcInFile() {
391398
$oDoc = $this->parsedStructureForFile('calc', Settings::create()->withMultibyteSupport(true));
392399
$sExpected = 'div {width: calc(100% / 4);}

tests/files/hex-alpha.css

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
div { background: #1234; }
2+
div { background: #11223344; }

0 commit comments

Comments
 (0)