Skip to content

Commit f59de42

Browse files
committed
KSES: Clarify that valid_unicode() checks XML Char ranges.
`valid_unicode()` follows the XML `Char` production rather than checking whether a code point is valid Unicode. It rejects C0 controls other than tab, line feed, and carriage return, along with surrogates and U+FFFE and U+FFFF. Update its docblock to say so, and point callers to the newer Unicode and UTF-8 helpers, such as `wp_is_valid_utf8()`. Add rejected test cases for U+000B and U+001F, which guard the boundary below U+0020, and rename the test and its data provider to describe XML `Char` acceptance. Follow-up to [64770]. Developed in: #15105 Props shailu25, dmsnell. Fixes #66483. See #65819. git-svn-id: https://develop.svn.wordpress.org/trunk@65268 602fd350-edb4-49c9-b593-d223f7449a82
1 parent 135a187 commit f59de42

2 files changed

Lines changed: 21 additions & 10 deletions

File tree

‎src/wp-includes/kses.php‎

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3403,9 +3403,15 @@ function wp_kses_normalize_entities3( $matches ) {
34033403
}
34043404

34053405
/**
3406-
* Determines if a Unicode codepoint is valid.
3406+
* Determines whether a code point is allowed by the XML Char production.
34073407
*
3408-
* The definition of a valid Unicode codepoint is taken from the XML definition:
3408+
* Despite its name, this helper does not report whether a code point is valid
3409+
* Unicode. It follows the XML Char production: C0 controls other than
3410+
* HT/LF/CR are rejected even though they are valid Unicode (for example
3411+
* U+0008 BACKSPACE), as are surrogates and U+FFFE/U+FFFF.
3412+
*
3413+
* Prefer the newer Unicode/UTF-8 helpers (for example {@see wp_is_valid_utf8()})
3414+
* rather than extending call sites here.
34093415
*
34103416
* > Characters
34113417
* >
@@ -3419,8 +3425,8 @@ function wp_kses_normalize_entities3( $matches ) {
34193425
*
34203426
* @link https://www.w3.org/TR/xml/#charsets
34213427
*
3422-
* @param int $i Unicode codepoint.
3423-
* @return bool Whether or not the codepoint is a valid Unicode codepoint.
3428+
* @param int $i Code point to check.
3429+
* @return bool Whether the code point is allowed by the XML Char production.
34243430
*/
34253431
function valid_unicode( $i ) {
34263432
$i = (int) $i;

‎tests/phpunit/tests/kses/validUnicode.php‎

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33
/**
44
* Tests for valid_unicode().
55
*
6+
* These tests document the helper's historical accept/reject ranges for the
7+
* XML Char-style check. See the function docblock for caller guidance.
8+
*
69
* @group kses
710
*
811
* @covers ::valid_unicode
@@ -12,21 +15,21 @@ class Tests_Kses_ValidUnicode extends WP_UnitTestCase {
1215
/**
1316
* @ticket 65819
1417
*
15-
* @dataProvider data_valid_unicode
18+
* @dataProvider data_indicates_if_codepoint_is_valid_xml_char
1619
*
17-
* @param int|string $codepoint Unicode codepoint.
18-
* @param bool $expected Whether the codepoint is valid.
20+
* @param int|string $codepoint Code point under the XML Char-style check.
21+
* @param bool $is_xml_char Whether valid_unicode() accepts it as an XML Char.
1922
*/
20-
public function test_valid_unicode( $codepoint, $expected ) {
21-
$this->assertSame( $expected, valid_unicode( $codepoint ) );
23+
public function test_indicates_if_codepoint_is_valid_xml_char( $codepoint, $is_xml_char ) {
24+
$this->assertSame( $is_xml_char, valid_unicode( $codepoint ) );
2225
}
2326

2427
/**
2528
* Data provider.
2629
*
2730
* @return array[]
2831
*/
29-
public static function data_valid_unicode() {
32+
public static function data_indicates_if_codepoint_is_valid_xml_char() {
3033
return array(
3134
'horizontal tab' => array( 0x9, true ),
3235
'line feed' => array( 0xA, true ),
@@ -39,6 +42,8 @@ public static function data_valid_unicode() {
3942
'supplementary plane start' => array( 0x10000, true ),
4043
'max unicode' => array( 0x10FFFF, true ),
4144
'backspace control' => array( 0x8, false ),
45+
'vertical tab' => array( 0xB, false ),
46+
'unit separator control' => array( 0x1F, false ),
4247
'null' => array( 0x0, false ),
4348
'surrogate start' => array( 0xD800, false ),
4449
'surrogate end' => array( 0xDFFF, false ),

0 commit comments

Comments
 (0)