<?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 alpha' => array(
'color' => '#abcff',
'expected' => null,
'maybe_alpha' => true,
),
'$maybe_alpha = true, 6 mixed with 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" : '' ) . ( $testdox ? PHP_EOL : '' );
}
}
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 7.4.0 - 7.4.33, 8.0.1 - 8.0.30, 8.1.0 - 8.1.30, 8.2.0 - 8.2.25, 8.3.0 - 8.3.14, 8.4.1
- 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 alpha
✅ $maybe_alpha = true, 6 mixed with alpha
preferences:
44.34 ms | 410 KiB | 5 Q