Skip to content

Commit 54d644f

Browse files
committed
Normalize PI-Node lookalike behavior
1 parent d62099b commit 54d644f

2 files changed

Lines changed: 30 additions & 14 deletions

File tree

src/wp-includes/html-api/class-wp-html-tag-processor.php

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1818,7 +1818,6 @@ private function parse_next_tag() {
18181818
if ( 0 < $pi_target_length ) {
18191819
$pi_target_length += strspn( $comment_text, 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789:_-.', $pi_target_length );
18201820

1821-
$this->parser_state = self::STATE_COMMENT;
18221821
$this->comment_type = self::COMMENT_AS_PI_NODE_LOOKALIKE;
18231822
$this->tag_name_starts_at = $this->token_starts_at + 2;
18241823
$this->tag_name_length = $pi_target_length;
@@ -2545,13 +2544,24 @@ public function get_attribute_names_with_prefix( $prefix ) {
25452544
* @return string|null Name of currently matched tag in input HTML, or `null` if none found.
25462545
*/
25472546
public function get_tag() {
2548-
if ( self::STATE_MATCHED_TAG !== $this->parser_state ) {
2547+
if ( null === $this->tag_name_starts_at ) {
25492548
return null;
25502549
}
25512550

25522551
$tag_name = substr( $this->html, $this->tag_name_starts_at, $this->tag_name_length );
25532552

2554-
return strtoupper( $tag_name );
2553+
if ( self::STATE_MATCHED_TAG === $this->parser_state ) {
2554+
return strtoupper( $tag_name );
2555+
}
2556+
2557+
if (
2558+
self::STATE_COMMENT === $this->parser_state &&
2559+
self::COMMENT_AS_PI_NODE_LOOKALIKE === $this->get_comment_type()
2560+
) {
2561+
return $tag_name;
2562+
}
2563+
2564+
return null;
25552565
}
25562566

25572567
/**
@@ -3397,7 +3407,6 @@ private function matches() {
33973407
*
33983408
* <!-->
33993409
* <!--->
3400-
* <!---->
34013410
*
34023411
* @since 6.5.0
34033412
*/

tests/phpunit/tests/html-api/wpHtmlTagProcessor-token-scanning.php

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -319,9 +319,15 @@ public function test_basic_assertion_cdata_section() {
319319
$processor->next_token();
320320

321321
$this->assertSame(
322-
'#cdata-section',
322+
'#comment',
323323
$processor->get_token_name(),
324-
"Should have found CDATA section name but found {$processor->get_token_name()} instead."
324+
"Should have found comment token but found {$processor->get_token_name()} instead."
325+
);
326+
327+
$this->assertSame(
328+
WP_HTML_Processor::COMMENT_AS_CDATA_LOOKALIKE,
329+
$processor->get_comment_type(),
330+
'Should have detected a CDATA-like invalid comment.'
325331
);
326332

327333
$this->assertNull(
@@ -405,20 +411,21 @@ public function test_basic_assertion_processing_instruction() {
405411
$processor->next_token();
406412

407413
$this->assertSame(
408-
'#processing-instruction',
409-
$processor->get_token_type(),
410-
"Should have found PI node but found {$processor->get_token_type()} instead."
414+
'#comment',
415+
$processor->get_token_name(),
416+
"Should have found comment token but found {$processor->get_token_name()} instead."
411417
);
412418

413419
$this->assertSame(
414-
'wp-bit',
415-
$processor->get_token_name(),
416-
"Should have found PI target as name but found {$processor->get_token_name()} instead."
420+
WP_HTML_Processor::COMMENT_AS_PI_NODE_LOOKALIKE,
421+
$processor->get_comment_type(),
422+
'Should have detected a Processing Instruction-like invalid comment.'
417423
);
418424

419-
$this->assertNull(
425+
$this->assertSame(
426+
'wp-bit',
420427
$processor->get_tag(),
421-
'Should not have been able to query tag name on non-element token.'
428+
"Should have found PI target as tag name but found {$processor->get_tag()} instead."
422429
);
423430

424431
$this->assertNull(

0 commit comments

Comments
 (0)