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 a487063da2e3b..3a21f2c00b529 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 @@ -619,7 +619,10 @@ public function next_tag( $query = null ) { public function next_token() { $was_at = $this->bytes_already_parsed; - if ( $was_at >= strlen( $this->html ) ) { + if ( + self::STATE_COMPLETE === $this->continuation_state || + $was_at >= strlen( $this->html ) + ) { $this->continuation_state = self::STATE_COMPLETE; return false; } @@ -637,15 +640,20 @@ public function next_token() { // Find the next tag if it exists. if ( false === $this->parse_next_tag() ) { - if ( $this->bytes_already_parsed > $was_at ) { + if ( + $this->bytes_already_parsed <= strlen( $this->html ) && + $this->bytes_already_parsed > $was_at + ) { $this->continuation_state = self::STATE_COMPLETE; $this->last_token_type = self::TEXT_NODE; - $this->tag_name_starts_at = $was_at; - $this->tag_name_length = strlen( $this->html ) - $was_at; + $this->token_starts_at = $was_at; + $this->text_starts_at = $was_at; + $this->text_length = strlen( $this->html ) - $this->text_starts_at; + $this->token_length = strlen( $this->html ) - $was_at; return true; } - $this->continuation_state = self::STATE_INCOMPLETE; + $this->continuation_state = self::STATE_INCOMPLETE; return false; } @@ -666,13 +674,12 @@ public function next_token() { $tag_ends_at = strpos( $this->html, '>', $this->bytes_already_parsed ); if ( false === $tag_ends_at ) { - $this->continuation_state = self::STATE_INCOMPLETE; + $this->continuation_state = self::STATE_INCOMPLETE; return false; } $this->tag_ends_at = $tag_ends_at; $this->bytes_already_parsed = min( strlen( $this->html ) - 1, $tag_ends_at + 1 ); - $this->last_token_type = self::ELEMENT_NODE; /* * For non-DATA sections which might contain text that looks like HTML tags but