From 54d644ff4c6a907d0d33e2cc26fe9ba195edf324 Mon Sep 17 00:00:00 2001 From: Dennis Snell Date: Mon, 22 Jan 2024 22:10:29 -0700 Subject: [PATCH] Normalize PI-Node lookalike behavior --- .../html-api/class-wp-html-tag-processor.php | 17 +++++++++--- .../wpHtmlTagProcessor-token-scanning.php | 27 ++++++++++++------- 2 files changed, 30 insertions(+), 14 deletions(-) diff --git a/src/wp-includes/html-api/class-wp-html-tag-processor.php b/src/wp-includes/html-api/class-wp-html-tag-processor.php index bb1f8dff7115d..e7c71bada4e9f 100644 --- a/src/wp-includes/html-api/class-wp-html-tag-processor.php +++ b/src/wp-includes/html-api/class-wp-html-tag-processor.php @@ -1818,7 +1818,6 @@ private function parse_next_tag() { if ( 0 < $pi_target_length ) { $pi_target_length += strspn( $comment_text, 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789:_-.', $pi_target_length ); - $this->parser_state = self::STATE_COMMENT; $this->comment_type = self::COMMENT_AS_PI_NODE_LOOKALIKE; $this->tag_name_starts_at = $this->token_starts_at + 2; $this->tag_name_length = $pi_target_length; @@ -2545,13 +2544,24 @@ public function get_attribute_names_with_prefix( $prefix ) { * @return string|null Name of currently matched tag in input HTML, or `null` if none found. */ public function get_tag() { - if ( self::STATE_MATCHED_TAG !== $this->parser_state ) { + if ( null === $this->tag_name_starts_at ) { return null; } $tag_name = substr( $this->html, $this->tag_name_starts_at, $this->tag_name_length ); - return strtoupper( $tag_name ); + if ( self::STATE_MATCHED_TAG === $this->parser_state ) { + return strtoupper( $tag_name ); + } + + if ( + self::STATE_COMMENT === $this->parser_state && + self::COMMENT_AS_PI_NODE_LOOKALIKE === $this->get_comment_type() + ) { + return $tag_name; + } + + return null; } /** @@ -3397,7 +3407,6 @@ private function matches() { * * * - * * * @since 6.5.0 */ diff --git a/tests/phpunit/tests/html-api/wpHtmlTagProcessor-token-scanning.php b/tests/phpunit/tests/html-api/wpHtmlTagProcessor-token-scanning.php index 2f59b430f21c3..538144910550f 100644 --- a/tests/phpunit/tests/html-api/wpHtmlTagProcessor-token-scanning.php +++ b/tests/phpunit/tests/html-api/wpHtmlTagProcessor-token-scanning.php @@ -319,9 +319,15 @@ public function test_basic_assertion_cdata_section() { $processor->next_token(); $this->assertSame( - '#cdata-section', + '#comment', $processor->get_token_name(), - "Should have found CDATA section name but found {$processor->get_token_name()} instead." + "Should have found comment token but found {$processor->get_token_name()} instead." + ); + + $this->assertSame( + WP_HTML_Processor::COMMENT_AS_CDATA_LOOKALIKE, + $processor->get_comment_type(), + 'Should have detected a CDATA-like invalid comment.' ); $this->assertNull( @@ -405,20 +411,21 @@ public function test_basic_assertion_processing_instruction() { $processor->next_token(); $this->assertSame( - '#processing-instruction', - $processor->get_token_type(), - "Should have found PI node but found {$processor->get_token_type()} instead." + '#comment', + $processor->get_token_name(), + "Should have found comment token but found {$processor->get_token_name()} instead." ); $this->assertSame( - 'wp-bit', - $processor->get_token_name(), - "Should have found PI target as name but found {$processor->get_token_name()} instead." + WP_HTML_Processor::COMMENT_AS_PI_NODE_LOOKALIKE, + $processor->get_comment_type(), + 'Should have detected a Processing Instruction-like invalid comment.' ); - $this->assertNull( + $this->assertSame( + 'wp-bit', $processor->get_tag(), - 'Should not have been able to query tag name on non-element token.' + "Should have found PI target as tag name but found {$processor->get_tag()} instead." ); $this->assertNull(