Skip to content

Commit 2161d9b

Browse files
committed
HTML API: Preserve decoder match length on named-reference miss.
The HTML decoder could report a match byte length when there was no match. Ensure the pass-by-reference match byte length is unmodified if there is no match. Developed in WordPress#12379. Follow-up to [58281]. Props jonsurrell, dmsnell, mukesh27. See #65372. git-svn-id: https://develop.svn.wordpress.org/trunk@62626 602fd350-edb4-49c9-b593-d223f7449a82
1 parent 006522b commit 2161d9b

2 files changed

Lines changed: 34 additions & 1 deletion

File tree

src/wp-includes/html-api/class-wp-html-decoder.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -361,7 +361,7 @@ public static function read_character_reference( $context, $text, $at = 0, &$mat
361361

362362
$name_length = 0;
363363
$replacement = $html5_named_character_references->read_token( $text, $name_at, $name_length );
364-
if ( false === $replacement ) {
364+
if ( null === $replacement ) {
365365
return null;
366366
}
367367

tests/phpunit/tests/html-api/wpHtmlDecoder.php

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,39 @@ static function ( int $errno, string $errstr ) use ( &$errors ) {
110110
$this->assertSame( "&\x00b", $decoded, 'Should have decoded the text without changing it.' );
111111
}
112112

113+
/**
114+
* Ensures unmatched named character references leave the by-ref match length unchanged.
115+
*
116+
* @ticket 65372
117+
*
118+
* @dataProvider data_unmatched_named_character_references
119+
*
120+
* @param string $context Decoder context.
121+
* @param string $raw_text_node Raw text containing an unmatched named character reference.
122+
*/
123+
public function test_unmatched_named_character_reference_does_not_set_match_byte_length( $context, $raw_text_node ): void {
124+
$match_byte_length = 'sentinel';
125+
$this->assertNull(
126+
WP_HTML_Decoder::read_character_reference( $context, $raw_text_node, 0, $match_byte_length ),
127+
'Should not have matched an unmatched named character reference.'
128+
);
129+
$this->assertSame( 'sentinel', $match_byte_length );
130+
}
131+
132+
/**
133+
* Data provider.
134+
*
135+
* @return array<string, array{string, string}>.
136+
*/
137+
public static function data_unmatched_named_character_references(): array {
138+
return array(
139+
'text invalid name' => array( 'data', '&bogus;' ),
140+
'text invalid short-name candidate' => array( 'data', '&Fv=q' ),
141+
'attribute invalid name' => array( 'attribute', '&bogus;' ),
142+
'attribute invalid short-name candidate' => array( 'attribute', '&Fv=q' ),
143+
);
144+
}
145+
113146
/**
114147
* Ensures semicolonless legacy references decode before non-ASCII UTF-8 bytes in attributes.
115148
*

0 commit comments

Comments
 (0)