Ticket #27583: 27583.3.diff
File 27583.3.diff, 7.1 KB (added by , 9 years ago) |
---|
-
trunk/wp-includes/formatting.php
4468 4468 } 4469 4469 4470 4470 /** 4471 * Validates a hex color. 4472 * 4473 * @since 4.4.0 4474 * 4475 * @param string $color The color to validate. 4476 * @return bool True if the value is a valid hex color; false if it is not. 4477 */ 4478 function is_hex_color( $color ) { 4479 $clean_color = sanitize_hex_color( $color ); 4480 return ! empty( $clean_color ); 4481 } 4482 4483 /** 4484 * Sanitizes a hex color. 4485 * 4486 * Returns either '', a 3 or 6 digit hex color (with #). 4487 * 4488 * @since 4.4.0 4489 * 4490 * @param string $color The color to "sanitize". 4491 * @param string $fallback Optional. The value to return if the sanitization ends up as an empty string. 4492 * Defaults to an empty string. 4493 * @return string The sanitized value. 4494 */ 4495 function sanitize_hex_color( $color, $fallback = '' ) { 4496 // 3 or 6 hex digits, or the empty string. 4497 if ( preg_match( '|^#([A-Fa-f0-9]{3}){1,2}$|', $color ) ) { 4498 $sanitized = $color; 4499 } else { 4500 $sanitized = $fallback; 4501 } 4502 4503 /** 4504 * Filter a sanitized hex color string. 4505 * 4506 * @since 2.8.0 4507 * 4508 * @param string $sanitized The sanitized hex color. 4509 * @param string $color Hex color before sanitization. 4510 * @param string $fallback The fallback string. 4511 */ 4512 4513 return apply_filters( 'sanitize_hex_color', $sanitized, $color, $fallback ); 4514 } 4515 4516 /** 4471 4517 * Print the important emoji-related styles. 4472 4518 * 4473 4519 * @since 4.2.0 4520 No newline at end of file -
tests/phpunit/tests/formatting/color.php
1 <?php 2 /** 3 * @group formatting 4 * @group color 5 */ 6 class Tests_Formatting_Color extends WP_UnitTestCase { 7 /** 8 * @ticket 27583 9 */ 10 function test_valid_colors_pass_validation() { 11 // Numeric 3 digit 12 $color = '#333'; 13 $this->assertTrue( is_hex_color( $color ) ); 14 15 // Alphanumeric 3 character 16 $color = '#f4A'; 17 $this->assertTrue( is_hex_color( $color ) ); 18 19 // Alpha 3 character 20 $color = '#fff'; 21 $this->assertTrue( is_hex_color( $color ) ); 22 23 // Numeric 6 digit 24 $color = '#333333'; 25 $this->assertTrue( is_hex_color( $color ) ); 26 27 // Alphanumeric 6 character 28 $color = '#ff44AA'; 29 $this->assertTrue( is_hex_color( $color ) ); 30 31 // Alpha 6 character 32 $color = '#ffffff'; 33 $this->assertTrue( is_hex_color( $color ) ); 34 } 35 36 /** 37 * @ticket 27583 38 */ 39 function test_invalid_colors_do_not_pass() { 40 // Too many characters 41 $color = '#1234567'; 42 $this->assertFalse( is_hex_color( $color ) ); 43 44 // Too few characters 45 $color = '#12'; 46 $this->assertFalse( is_hex_color( $color ) ); 47 48 // Missing # 49 $color = 'ffffff'; 50 $this->assertFalse( is_hex_color( $color ) ); 51 52 // Invalid character 53 $color = '#f9a%ad'; 54 $this->assertFalse( is_hex_color( $color ) ); 55 56 // Empty string 57 $color = ''; 58 $this->assertFalse( is_hex_color( $color ) ); 59 60 // Whitespace 61 $color = '#ff fff'; 62 $this->assertFalse( is_hex_color( $color ) ); 63 } 64 65 function test_removal_of_hash_from_colors() { 66 require_once ABSPATH . WPINC . '/class-wp-customize-manager.php'; 67 68 // Numeric 3 digit 69 $color = '#333'; 70 $color_no_hash = '333'; 71 $this->assertEquals( $color_no_hash, sanitize_hex_color_no_hash( $color ) ); 72 73 // Alphanumeric 3 character 74 $color = '#f4A'; 75 $color_no_hash = 'f4A'; 76 $this->assertEquals( $color_no_hash, sanitize_hex_color_no_hash( $color ) ); 77 78 // Alpha 3 character 79 $color = '#fff'; 80 $color_no_hash = 'fff'; 81 $this->assertEquals( $color_no_hash, sanitize_hex_color_no_hash( $color ) ); 82 83 // Numeric 6 digit 84 $color = '#333333'; 85 $color_no_hash = '333333'; 86 $this->assertEquals( $color_no_hash, sanitize_hex_color_no_hash( $color ) ); 87 88 // Alphanumeric 6 character 89 $color = '#ff44AA'; 90 $color_no_hash = 'ff44AA'; 91 $this->assertEquals( $color_no_hash, sanitize_hex_color_no_hash( $color ) ); 92 93 // Alpha 6 character 94 $color = '#ffffff'; 95 $color_no_hash = 'ffffff'; 96 $this->assertEquals( $color_no_hash, sanitize_hex_color_no_hash( $color ) ); 97 } 98 99 function test_prepend_hash_on_color_without_hash() { 100 require_once ABSPATH . WPINC . '/class-wp-customize-manager.php'; 101 102 // Numeric 3 digit 103 $color = '#333'; 104 $color_no_hash = '333'; 105 $this->assertEquals( $color, maybe_hash_hex_color( $color_no_hash ) ); 106 107 // Alphanumeric 3 character 108 $color = '#f4A'; 109 $color_no_hash = 'f4A'; 110 $this->assertEquals( $color, maybe_hash_hex_color( $color_no_hash ) ); 111 112 // Alpha 3 character 113 $color = '#fff'; 114 $color_no_hash = 'fff'; 115 $this->assertEquals( $color, maybe_hash_hex_color( $color_no_hash ) ); 116 117 // Numeric 6 digit 118 $color = '#333333'; 119 $color_no_hash = '333333'; 120 $this->assertEquals( $color, maybe_hash_hex_color( $color_no_hash ) ); 121 122 // Alphanumeric 6 character 123 $color = '#ff44AA'; 124 $color_no_hash = 'ff44AA'; 125 $this->assertEquals( $color, maybe_hash_hex_color( $color_no_hash ) ); 126 127 // Alpha 6 character 128 $color = '#ffffff'; 129 $color_no_hash = 'ffffff'; 130 $this->assertEquals( $color, maybe_hash_hex_color( $color_no_hash ) ); 131 } 132 } -
trunk/wp-includes/class-wp-customize-manager.php
2004 2004 } 2005 2005 2006 2006 /** 2007 * Sanitizes a hex color.2008 *2009 * Returns either '', a 3 or 6 digit hex color (with #), or nothing.2010 * For sanitizing values without a #, see sanitize_hex_color_no_hash().2011 *2012 * @since 3.4.02013 *2014 * @param string $color2015 * @return string|void2016 */2017 function sanitize_hex_color( $color ) {2018 if ( '' === $color )2019 return '';2020 2021 // 3 or 6 hex digits, or the empty string.2022 if ( preg_match('|^#([A-Fa-f0-9]{3}){1,2}$|', $color ) )2023 return $color;2024 }2025 2026 /**2027 2007 * Sanitizes a hex color without a hash. Use sanitize_hex_color() when possible. 2028 2008 * 2029 2009 * Saving hex colors without a hash puts the burden of adding the hash on the … … 2034 2014 * 2035 2015 * @since 3.4.0 2036 2016 * 2037 * @param string $color 2038 * @return string|null 2017 * @param string $color The hex color to remove the hash from. 2018 * @return string|null The color with the removed hash. 2039 2019 */ 2040 2020 function sanitize_hex_color_no_hash( $color ) { 2041 2021 $color = ltrim( $color, '#' ); 2042 2022 2043 if ( '' === $color ) 2023 if ( '' === $color ) { 2044 2024 return ''; 2025 } 2045 2026 2046 return sanitize_hex_color( '#' . $color ) ? $color : null; 2047 } 2027 if ( is_hex_color( '#' . $color ) ) { 2028 return $color; 2029 } else { 2030 return null; 2031 } 2032 } 2048 2033 2049 2034 /** 2050 2035 * Ensures that any hex color is properly hashed. … … 2054 2039 * 2055 2040 * @since 3.4.0 2056 2041 * 2057 * @param string $color 2058 * @return string 2042 * @param string $color The color to "sanitize" to prepend a hash to. 2043 * @return string The color with the prepended hash. 2059 2044 */ 2060 2045 function maybe_hash_hex_color( $color ) { 2061 if ( $unhashed = sanitize_hex_color_no_hash( $color ) ) 2046 if ( $unhashed = sanitize_hex_color_no_hash( $color ) ) { 2062 2047 return '#' . $unhashed; 2048 } 2063 2049 2064 2050 return $color; 2065 2051 }