Skip to content

[BUGFIX] Handle incorrect RGB colors better #485

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Feb 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ This project adheres to [Semantic Versioning](https://semver.org/).

### Fixed

- Fix PHP notice caused by parsing invalid color values having less than 6 charachters (#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