Skip to content

Commit

Permalink
[BUGFIX] Handle incorrect RGB colors better (#485) (#490)
Browse files Browse the repository at this point in the history
  • Loading branch information
oliverklee authored Feb 22, 2024
1 parent 4a3d572 commit eed92ad
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ This project adheres to [Semantic Versioning](https://semver.org/).

### Fixed

- Fix PHP notice caused by parsing invalid color values having less than 6 characters (#485)
- Fix (regression) failure to parse at-rules with strict parsing (#456)

## 8.5.0
Expand Down
9 changes: 8 additions & 1 deletion src/Value/Color.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,19 @@ public static function parse(ParserState $oParserState, $bIgnoreCase = false)
$oParserState->currentLine()
),
];
} else {
} elseif ($oParserState->strlen($sValue) === 6) {
$aColor = [
'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(
'Invalid hex color value',
$sValue,
'custom',
$oParserState->currentLine()
);
}
} else {
$sColorMode = $oParserState->parseIdentifier(true);
Expand Down
2 changes: 2 additions & 0 deletions tests/ParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,8 @@ public function colorParsing()
'l' => new Size(220.0, '%', true, $oColor->getLineNo()),
'a' => new Size(0000.3, null, true, $oColor->getLineNo()),
], $oColor->getColor());
$aColorRule = $oRuleSet->getRules('outline-color');
self::assertEmpty($aColorRule);
}
}
foreach ($oDoc->getAllValues('color') as $sColor) {
Expand Down
1 change: 1 addition & 0 deletions tests/fixtures/colortest.css
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#yours {
background-color: hsl(220, 10%, 220%);
background-color: hsla(220, 10%, 220%, 0.3);
outline-color: #22;
}

#variables {
Expand Down

0 comments on commit eed92ad

Please sign in to comment.