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 54721df406..b9214f2d4c 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 @@ -546,6 +546,10 @@ class WP_HTML_Tag_Processor { } // Ensure that the tag closes before the end of the document. + if ( $this->bytes_already_parsed >= strlen( $this->html ) ) { + return false; + } + $tag_ends_at = strpos( $this->html, '>', $this->bytes_already_parsed ); if ( false === $tag_ends_at ) { return false; diff --git a/tests/phpunit/tests/html-api/wpHtmlTagProcessor.php b/tests/phpunit/tests/html-api/wpHtmlTagProcessor.php index e84fc777de..6cc59289cb 100644 --- a/tests/phpunit/tests/html-api/wpHtmlTagProcessor.php +++ b/tests/phpunit/tests/html-api/wpHtmlTagProcessor.php @@ -2038,6 +2038,47 @@ HTML; ); } + /** + * @ticket 58637 + * + * @covers WP_HTML_Tag_Processor::next_tag + * + * @dataProvider data_incomplete_syntax_elements + * + * @param string $incomplete_html HTML text containing some kind of incomplete syntax. + */ + public function test_returns_false_for_incomplete_syntax_elements( $incomplete_html ) { + $p = new WP_HTML_Tag_Processor( $incomplete_html ); + $this->assertFalse( $p->next_tag() ); + } + + /** + * Data provider. + * + * @return array[] + */ + public function data_incomplete_syntax_elements() { + return array( + 'No tags' => array( 'this is nothing more than a text node' ), + 'Incomplete tag name' => array( ' array( ' array( ' array( ' array( " array( '