<?php
function sanitize_hex_color( $color, $maybe_alpha = false ) {
if ( '' === $color ) {
return '';
}
$allowed_lengths = $maybe_alpha ? array( 4, 5, 7, 9 ) : array( 4, 7 );
$correct_length = in_array( strlen( $color ), $allowed_lengths, true );
if ( $correct_length && preg_replace( '|^#([^A-Fa-f0-9]+)$|', '', $color ) === $color ) {
return $color;
}
}
/**
* Test Suite
*/
$happy_bc_tests = array(
'$maybe_alpha = false, 3 digit' => array(
'color' => '#123',
'expected' => '#123',
'maybe_alpha' => false,
),
'$maybe_alpha = false, 3 letter' => array(
'color' => '#abc',
'expected' => '#abc',
'maybe_alpha' => false,
),
'$maybe_alpha = false, 3 mixed' => array(
'color' => '#0ab',
'expected' => '#0ab',
'maybe_alpha' => false,
),
'$maybe_alpha = false, 6 digit' => array(
'color' => '#123456',
'expected' => '#123456',
'maybe_alpha' => false,
),
'$maybe_alpha = false, 6 letter' => array(
'color' => '#abcdef',
'expected' => '#abcdef',
'maybe_alpha' => false,
),
'$maybe_alpha = false, 6 mixed' => array(
'color' => '#abc123',
'expected' => '#abc123',
'maybe_alpha' => false,
),
);
$unhappy_bc_tests = array(
'empty string' => array(
'color' => '',
'expected' => '',
'maybe_alpha' => false,
),
'no hash' => array(
'color' => '123',
'expected' => null,
'maybe_alpha' => false,
),
'not a-f' => array(
'color' => '#hjg',
'expected' => null,
'maybe_alpha' => false,
),
'not A-F' => array(
'color' => '#HJG',
'expected' => null,
'maybe_alpha' => false,
),
'$maybe_alpha = false, 3 digit with 1 alpha' => array(
'color' => '#123f',
'expected' => null,
'maybe_alpha' => false,
),
'$maybe_alpha = false, 3 letter with 1 alpha' => array(
'color' => '#abcf',
'expected' => null,
'maybe_alpha' => false,
),
'$maybe_alpha = false, 3 mixed with 1 alpha' => array(
'color' => '#0abf',
'expected' => null,
'maybe_alpha' => false,
),
'$maybe_alpha = false, 6 digit with 2 alpha' => array(
'color' => '#123456ff',
'expected' => null,
'maybe_alpha' => false,
),
'$maybe_alpha = false, 6 letter with 2 alpha' => array(
'color' => '#abcdefff',
'expected' => null,
'maybe_alpha' => false,
),
'$maybe_alpha = false, 6 mixed with 2 alpha' => array(
'color' => '#abc123ff',
'expected' => null,
'maybe_alpha' => false,
),
);
$happy_alpha_tests = array(
// Happy.
'$maybe_alpha = true, 3 digit' => array(
'color' => '#123',
'expected' => '#123',
'maybe_alpha' => true,
),
'$maybe_alpha = true, 3 letter' => array(
'color' => '#abc',
'expected' => '#abc',
'maybe_alpha' => true,
),
'$maybe_alpha = true, 3 mixed' => array(
'color' => '#0ab',
'expected' => '#0ab',
'maybe_alpha' => true,
),
'$maybe_alpha = true, 6 digit' => array(
'color' => '#123456',
'expected' => '#123456',
'maybe_alpha' => true,
),
'$maybe_alpha = true, 6 letter' => array(
'color' => '#abcdef',
'expected' => '#abcdef',
'maybe_alpha' => true,
),
'$maybe_alpha = true, 6 mixed' => array(
'color' => '#abc123',
'expected' => '#abc123',
'maybe_alpha' => true,
),
'$maybe_alpha = true, 3 digit with 1 alpha' => array(
'color' => '#123f',
'expected' => '#123f',
'maybe_alpha' => true,
),
'$maybe_alpha = true, 3 letter with 1 alpha' => array(
'color' => '#abcf',
'expected' => '#abcf',
'maybe_alpha' => true,
),
'$maybe_alpha = true, 3 mixed with 1 alpha' => array(
'color' => '#0abf',
'expected' => '#0abf',
'maybe_alpha' => true,
),
'$maybe_alpha = true, 6 digit with 2 alpha' => array(
'color' => '#123456ff',
'expected' => '#123456ff',
'maybe_alpha' => true,
),
'$maybe_alpha = true, 6 letter with 2 alpha' => array(
'color' => '#abcdefff',
'expected' => '#abcdefff',
'maybe_alpha' => true,
),
'$maybe_alpha = true, 6 mixed with 2 alpha' => array(
'color' => '#abc123ff',
'expected' => '#abc123ff',
'maybe_alpha' => true,
),
);
$unhappy_alpha_tests = array(
'empty string' => array(
'color' => '',
'expected' => '',
'maybe_alpha' => false,
),
'no hash' => array(
'color' => '123',
'expected' => null,
'maybe_alpha' => false,
),
'not a-f' => array(
'color' => '#hjg',
'expected' => null,
'maybe_alpha' => false,
),
'not A-F' => array(
'color' => '#HJG',
'expected' => null,
'maybe_alpha' => false,
),
'$maybe_alpha = true, 3 digit with 2 alpha' => array(
'color' => '#123ff',
'expected' => null,
'maybe_alpha' => true,
),
'$maybe_alpha = true, 3 letter with 2 alpha' => array(
'color' => '#abcff',
'expected' => null,
'maybe_alpha' => true,
),
'$maybe_alpha = true, 3 mixed with 2 alpha' => array(
'color' => '#0abff',
'expected' => null,
'maybe_alpha' => true,
),
'$maybe_alpha = true, 6 digit with 1 alpha' => array(
'color' => '#123456f',
'expected' => null,
'maybe_alpha' => true,
),
'$maybe_alpha = true, 6 letter with 1 alpha' => array(
'color' => '#abcff',
'expected' => null,
'maybe_alpha' => true,
),
'$maybe_alpha = true, 6 mixed with 1 alpha' => array(
'color' => '#0abff',
'expected' => null,
'maybe_alpha' => true,
),
);
function run_tests( $tests = array(), $testdox = false ) {
$markers = array(
$testdox ? '❌' : 'F',
$testdox ? '✅' : '.',
);
foreach ( $tests as $test => $data ) {
list( $color, $expected, $maybe_alpha ) = array_values( $data );
$actual = sanitize_hex_color( $color, $maybe_alpha );
echo $markers[ (int) ( $actual === $expected ) ] . ( $testdox ? " $test\n" : '' );
}
}
echo "Happy BC tests\n";
run_tests( $happy_bc_tests, true );
echo "\nUnhappy BC tests\n";
run_tests( $unhappy_bc_tests, true );
echo "\nHappy alpha tests\n";
run_tests( $happy_alpha_tests, true );
echo "\nUnhappy alpha tests\n";
run_tests( $unhappy_alpha_tests, true );
- Output for 4.3.0 - 4.3.11, 4.4.0 - 4.4.9, 5.0.0 - 5.0.5, 5.1.0 - 5.1.6, 5.2.0 - 5.2.17, 5.3.0 - 5.3.29, 5.4.0 - 5.4.45, 5.5.0 - 5.5.38, 5.6.0 - 5.6.40, 7.0.0 - 7.0.33, 7.1.0 - 7.1.33, 7.2.0 - 7.2.34, 7.3.0 - 7.3.33, 7.4.0 - 7.4.33, 8.0.0 - 8.0.30, 8.1.0 - 8.1.26, 8.2.0 - 8.2.13, 8.3.0
- Happy BC tests
✅ $maybe_alpha = false, 3 digit
✅ $maybe_alpha = false, 3 letter
✅ $maybe_alpha = false, 3 mixed
✅ $maybe_alpha = false, 6 digit
✅ $maybe_alpha = false, 6 letter
✅ $maybe_alpha = false, 6 mixed
Unhappy BC tests
✅ empty string
✅ no hash
✅ not a-f
✅ not A-F
✅ $maybe_alpha = false, 3 digit with 1 alpha
✅ $maybe_alpha = false, 3 letter with 1 alpha
✅ $maybe_alpha = false, 3 mixed with 1 alpha
✅ $maybe_alpha = false, 6 digit with 2 alpha
✅ $maybe_alpha = false, 6 letter with 2 alpha
✅ $maybe_alpha = false, 6 mixed with 2 alpha
Happy alpha tests
✅ $maybe_alpha = true, 3 digit
✅ $maybe_alpha = true, 3 letter
✅ $maybe_alpha = true, 3 mixed
✅ $maybe_alpha = true, 6 digit
✅ $maybe_alpha = true, 6 letter
✅ $maybe_alpha = true, 6 mixed
✅ $maybe_alpha = true, 3 digit with 1 alpha
✅ $maybe_alpha = true, 3 letter with 1 alpha
✅ $maybe_alpha = true, 3 mixed with 1 alpha
✅ $maybe_alpha = true, 6 digit with 2 alpha
✅ $maybe_alpha = true, 6 letter with 2 alpha
✅ $maybe_alpha = true, 6 mixed with 2 alpha
Unhappy alpha tests
✅ empty string
✅ no hash
✅ not a-f
✅ not A-F
✅ $maybe_alpha = true, 3 digit with 2 alpha
✅ $maybe_alpha = true, 3 letter with 2 alpha
✅ $maybe_alpha = true, 3 mixed with 2 alpha
✅ $maybe_alpha = true, 6 digit with 1 alpha
✅ $maybe_alpha = true, 6 letter with 1 alpha
✅ $maybe_alpha = true, 6 mixed with 1 alpha
preferences:
181.42 ms | 409 KiB | 455 Q