WP_HTML_Tag_Processor::get_full_comment_text()publicWP 6.7.0

Returns the text of a matched comment or null if not on a comment type node.

This method returns the entire text content of a comment node as it would appear in the browser.

This differs from {@see ::get_modifiable_text()} in that certain comment types in the HTML API cannot allow their entire comment text content to be modified. Namely, "bogus comments" of the form <?not allowed in html> will create a comment whose text content starts with ?. Note that if that character were modified, it would be possible to change the node type.

Method of the class: WP_HTML_Tag_Processor{}

No Hooks.

Return

String|null. The comment text as it would appear in the browser or null if not on a comment type node.

Usage

$WP_HTML_Tag_Processor = new WP_HTML_Tag_Processor();
$WP_HTML_Tag_Processor->get_full_comment_text(): ?string;

Changelog

Since 6.7.0 Introduced.

WP_HTML_Tag_Processor::get_full_comment_text() code WP 6.7.1

public function get_full_comment_text(): ?string {
	if ( self::STATE_FUNKY_COMMENT === $this->parser_state ) {
		return $this->get_modifiable_text();
	}

	if ( self::STATE_COMMENT !== $this->parser_state ) {
		return null;
	}

	switch ( $this->get_comment_type() ) {
		case self::COMMENT_AS_HTML_COMMENT:
		case self::COMMENT_AS_ABRUPTLY_CLOSED_COMMENT:
			return $this->get_modifiable_text();

		case self::COMMENT_AS_CDATA_LOOKALIKE:
			return "[CDATA[{$this->get_modifiable_text()}]]";

		case self::COMMENT_AS_PI_NODE_LOOKALIKE:
			return "?{$this->get_tag()}{$this->get_modifiable_text()}?";

		/*
		 * This represents "bogus comments state" from HTML tokenization.
		 * This can be entered by `<?` or `<!`, where `?` is included in
		 * the comment text but `!` is not.
		 */
		case self::COMMENT_AS_INVALID_HTML:
			$preceding_character = $this->html[ $this->text_starts_at - 1 ];
			$comment_start       = '?' === $preceding_character ? '?' : '';
			return "{$comment_start}{$this->get_modifiable_text()}";
	}

	return null;
}