WP_Block_Processor::find_html_comment_end( int $comment_starting_at, int $search_end ): int

In this article

This function’s access is marked private. This means it is not intended for use by plugin or theme developers, only in other core functions. It is listed here for completeness.

Returns the byte-offset after the ending character of an HTML comment, assuming the proper starting byte offset.

Parameters

$comment_starting_atintrequired
Where the HTML comment started, the leading <.
$search_endintrequired
Last offset in which to search, for limiting search span.

Return

int Offset after the current HTML comment ends, or $search_end if no end was found.

Source

private function find_html_comment_end( int $comment_starting_at, int $search_end ): int {
	$text = $this->source_text;

	// Find span-of-dashes comments which look like `<!----->`.
	$span_of_dashes = strspn( $text, '-', $comment_starting_at + 2 );
	if (
		$comment_starting_at + 2 + $span_of_dashes < $search_end &&
		'>' === $text[ $comment_starting_at + 2 + $span_of_dashes ]
	) {
		return $comment_starting_at + $span_of_dashes + 1;
	}

	// Otherwise, there are other characters inside the comment, find the first `-->` or `--!>`.
	$now_at = $comment_starting_at + 4;
	while ( $now_at < $search_end ) {
		$dashes_at = strpos( $text, '--', $now_at );
		if ( false === $dashes_at ) {
			return $search_end;
		}

		$closer_must_be_at = $dashes_at + 2 + strspn( $text, '-', $dashes_at + 2 );
		if ( $closer_must_be_at < $search_end && '!' === $text[ $closer_must_be_at ] ) {
			++$closer_must_be_at;
		}

		if ( $closer_must_be_at < $search_end && '>' === $text[ $closer_must_be_at ] ) {
			return $closer_must_be_at + 1;
		}

		++$now_at;
	}

	return $search_end;
}

Changelog

VersionDescription
6.9.0Introduced.

User Contributed Notes

You must log in before being able to contribute a note or feedback.