Commit d1932299 authored by catch's avatar catch

Issue #2940088 by joelpittet, cilefen: Shorthand hex colors in CSS files break...

Issue #2940088 by joelpittet, cilefen: Shorthand hex colors in CSS files break color module's comparisons
parent 6eee27d8
......@@ -94,4 +94,28 @@ public static function rgbToHex($input) {
return '#' . str_pad(dechex($out), 6, 0, STR_PAD_LEFT);
}
/**
* Normalize the hex color length to 6 characters for comparison.
*
* @param string $hex
* The hex color to normalize.
*
* @return string
* The 6 character hex color.
*/
public static function normalizeHexLength($hex) {
// Ignore '#' prefixes.
$hex = ltrim($hex, '#');
if (strlen($hex) === 3) {
$hex[5] = $hex[2];
$hex[4] = $hex[2];
$hex[3] = $hex[1];
$hex[2] = $hex[1];
$hex[1] = $hex[0];
}
return '#' . $hex;
}
}
......@@ -5,6 +5,7 @@
* Allows users to change the color scheme of themes.
*/
use Drupal\Component\Utility\Color;
use Drupal\Component\Utility\Unicode;
use Drupal\Core\Asset\CssOptimizer;
use Drupal\Component\Utility\Bytes;
......@@ -513,7 +514,8 @@ function _color_rewrite_stylesheet($theme, &$info, &$paths, $palette, $style) {
// Prepare color conversion table.
$conversion = $palette;
foreach ($conversion as $k => $v) {
$conversion[$k] = Unicode::strtolower($v);
$v = Unicode::strtolower($v);
$conversion[$k] = Color::normalizeHexLength($v);
}
$default = color_get_palette($theme, TRUE);
......@@ -533,6 +535,7 @@ function _color_rewrite_stylesheet($theme, &$info, &$paths, $palette, $style) {
foreach ($style as $chunk) {
if ($is_color) {
$chunk = Unicode::strtolower($chunk);
$chunk = Color::normalizeHexLength($chunk);
// Check if this is one of the colors in the default palette.
if ($key = array_search($chunk, $default)) {
$chunk = $conversion[$key];
......
......@@ -123,4 +123,42 @@ public function providerTestRbgToHex() {
return $tests;
}
/**
* Data provider for testNormalizeHexLength().
*
* @see testNormalizeHexLength()
*
* @return array
* An array of arrays containing:
* - The hex color value.
* - The 6 character length hex color value.
*/
public function providerTestNormalizeHexLength() {
$data = [
['#000', '#000000'],
['#FFF', '#FFFFFF'],
['#abc', '#aabbcc'],
['cba', '#ccbbaa'],
['#000000', '#000000'],
['ffffff', '#ffffff'],
['#010203', '#010203'],
];
return $data;
}
/**
* Tests Color::normalizeHexLength().
*
* @param string $value
* The input hex color value.
* @param string $expected
* The expected normalized hex color value.
*
* @dataProvider providerTestNormalizeHexLength
*/
public function testNormalizeHexLength($value, $expected) {
$this->assertSame($expected, Color::normalizeHexLength($value));
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment