From e0d624690501de9f3e07de4c85df42b526185694 Mon Sep 17 00:00:00 2001 From: raxbg Date: Wed, 18 Dec 2019 10:03:05 +0200 Subject: [PATCH 1/2] Handle parsing of invalid HEX values nicely --- lib/Sabberworm/CSS/Value/Color.php | 5 ++++- tests/Sabberworm/CSS/ParserTest.php | 7 +++++++ 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/lib/Sabberworm/CSS/Value/Color.php b/lib/Sabberworm/CSS/Value/Color.php index c6ed9b18..614df257 100644 --- a/lib/Sabberworm/CSS/Value/Color.php +++ b/lib/Sabberworm/CSS/Value/Color.php @@ -3,6 +3,7 @@ namespace Sabberworm\CSS\Value; use Sabberworm\CSS\Parsing\ParserState; +use Sabberworm\CSS\Parsing\UnexpectedTokenException; class Color extends CSSFunction { @@ -28,12 +29,14 @@ public static function parse(ParserState $oParserState) { 'b' => new Size(intval($sValue[4] . $sValue[5], 16), null, true, $oParserState->currentLine()), 'a' => new Size(round(self::mapRange(intval($sValue[6] . $sValue[7], 16), 0, 255, 0, 1), 2), null, true, $oParserState->currentLine()) ); - } else { + } else if ($oParserState->strlen($sValue) === 6) { $aColor = array( 'r' => new Size(intval($sValue[0] . $sValue[1], 16), null, true, $oParserState->currentLine()), 'g' => new Size(intval($sValue[2] . $sValue[3], 16), null, true, $oParserState->currentLine()), 'b' => new Size(intval($sValue[4] . $sValue[5], 16), null, true, $oParserState->currentLine()) ); + } else { + throw new UnexpectedTokenException("RGB(A) HEX value", $sValue, 'literal', $oParserState->currentLine()); } } else { $sColorMode = $oParserState->parseIdentifier(true); diff --git a/tests/Sabberworm/CSS/ParserTest.php b/tests/Sabberworm/CSS/ParserTest.php index ea34f2e7..5bc7b0dd 100644 --- a/tests/Sabberworm/CSS/ParserTest.php +++ b/tests/Sabberworm/CSS/ParserTest.php @@ -326,6 +326,13 @@ function testInnerColors() { $this->assertSame($sExpected, $oDoc->render()); } + function testIncorrectRGBColors() { + $oParser = new Parser(".info-img {border:1px solid #8C0000;background-color:#fffff;}"); + $sExpected = ".info-img {border: 1px solid #8c0000;}"; + $oDoc = $oParser->parse(); + $this->assertSame($sExpected, $oDoc->render()); + } + function testPrefixedGradient() { $oDoc = $this->parsedStructureForFile('webkit'); $sExpected = '.test {background: -webkit-linear-gradient(top right,white,black);}'; From 04aebb43203d0721268a47e063fcced11927f5eb Mon Sep 17 00:00:00 2001 From: Ivailo Hristov Date: Sat, 17 Sep 2022 23:37:21 +0300 Subject: [PATCH 2/2] Fixes for CI --- src/Value/Color.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Value/Color.php b/src/Value/Color.php index e07995fd..aeb63fee 100644 --- a/src/Value/Color.php +++ b/src/Value/Color.php @@ -60,8 +60,8 @@ public static function parse(ParserState $oParserState) 'b' => new Size(intval($sValue[4] . $sValue[5], 16), null, true, $oParserState->currentLine()), ]; } else { - throw new UnexpectedTokenException("RGB(A) HEX value", $sValue, 'literal', $oParserState->currentLine()); - } + throw new UnexpectedTokenException("RGB(A) HEX val", $sValue, 'literal', $oParserState->currentLine()); + } } else { $sColorMode = $oParserState->parseIdentifier(true); $oParserState->consumeWhiteSpace(); @@ -165,7 +165,7 @@ public function render(OutputFormat $oOutputFormat) $this->aComponents['b']->getSize() ); return '#' . (($sResult[0] == $sResult[1]) && ($sResult[2] == $sResult[3]) && ($sResult[4] == $sResult[5]) - ? "$sResult[0]$sResult[2]$sResult[4]" : $sResult); + ? "$sResult[0]$sResult[2]$sResult[4]" : $sResult); } return parent::render($oOutputFormat); }