WP_Block_Processor::find_html_comment_endprivateWP 6.9.0

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

Method of the class: WP_Block_Processor{}

No Hooks.

Returns

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

Usage

// private - for code of main (parent) class only
$result = $this->find_html_comment_end( $comment_starting_at, $search_end ): int;
$comment_starting_at(int) (required)
Where the HTML comment started, the leading <.
$search_end(int) (required)
Last offset in which to search, for limiting search span.

Changelog

Since 6.9.0 Introduced.

WP_Block_Processor::find_html_comment_end() code WP 6.9.1

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;
}