wppaste
WordPress

WP_HTML_Processor::serialize_token(): string

Since
6.7.0, 6.9.0
Source
wp-includes/html-api/class-wp-html-processor.php:1357
Serializes the currently-matched token.

Description

This method produces a fully-normative HTML string for the currently-matched token, if able. If not matched at any token or if the token doesn't correspond to any HTML it will return an empty string (for example, presumptuous end tags are ignored).

Compatibility

WordPress
since 6.9.0
PHP
7.4–8.6-dev
  • 6.7.7
  • 6.8.8
  • 6.9.7
  • 7.0.4
  • 7.1.0

Present in every tracked release (6.7.7 to 7.1.0), and compiles on PHP 7.4 through 8.6-dev.

Return value

string
Serialization of token, or empty string if no serialization exists.

Performance profile

How much work a call to WP_HTML_Processor::serialize_token() does, and what it touches: the algorithmic scaling, the Zend instruction count per call across PHP versions, the hooks it hands control to, and the core code that calls it. Measured from the compiled opcodes, not a stopwatch, so every number is identical on any machine running the same PHP version, and every function in core is ranked by cost.

Cost class
Light

Touches nothing outside its own arguments.

Scaling
Scales with input

The body loops, so the work grows with how much data it finds.

Instructions
13–111

Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 229.

Plugin surface
None

Nothing here hands control to plugin code.

Called by
1

1 place in core call this, so the cost is paid more often than your own code shows.

What one call costs · 15 distinct outcomes

One number would be a lie: the work depends on which branch runs. These are every distinct cost WP_HTML_Processor::serialize_token() can have, taken from its control-flow graph on PHP 8.5.

WhenInstructionsCalls it makes
$token_type !== "#tag"13–39->get_token_type(), ->get_doctype_info()
$token_type === "#tag" && ->is_tag_closer()31–57->get_token_type(), ->get_doctype_info(), ->get_tag(), ->get_namespace(), ->get_qualified_tag_name(), ->is_tag_closer()
$token_type === "#tag" && ->is_tag_closer()33–59->get_token_type(), ->get_doctype_info(), ->get_tag(), ->get_namespace(), strtolower(), ->is_tag_closer()
$token_type === "#tag" && !->is_tag_closer()37–79->get_token_type(), ->get_doctype_info(), ->get_tag(), ->get_namespace(), ->get_qualified_tag_name(), ->is_tag_closer(), ->get_attribute_names_with_prefix()
$token_type === "#tag" && !->is_tag_closer()39–81->get_token_type(), ->get_doctype_info(), ->get_tag(), ->get_namespace(), strtolower(), ->is_tag_closer(), ->get_attribute_names_with_prefix()
$token_type === "#tag" && !->is_tag_closer() && isset($attribute_names)45–83->get_token_type(), ->get_doctype_info(), ->get_tag(), ->get_namespace(), ->get_qualified_tag_name(), ->is_tag_closer(), ->get_attribute_names_with_prefix(), ->has_self_closing_flag()
$token_type === "#tag" && !->is_tag_closer() && isset($attribute_names)47–85->get_token_type(), ->get_doctype_info(), ->get_tag(), ->get_namespace(), strtolower(), ->is_tag_closer(), ->get_attribute_names_with_prefix(), ->has_self_closing_flag()
$token_type === "#tag" && !->is_tag_closer() && isset($attribute_names) && $tag_name58–100->get_token_type(), ->get_doctype_info(), ->get_tag(), ->get_namespace(), ->get_qualified_tag_name(), ->is_tag_closer(), ->get_attribute_names_with_prefix(), ->get_modifiable_text()
$token_type === "#tag" && !->is_tag_closer() && isset($attribute_names) && $tag_name60–102->get_token_type(), ->get_doctype_info(), ->get_tag(), ->get_namespace(), strtolower(), ->is_tag_closer(), ->get_attribute_names_with_prefix(), ->get_modifiable_text()
$token_type === "#tag" && !->is_tag_closer() && isset($attribute_names) && $tag_name61–105->get_token_type(), ->get_doctype_info(), ->get_tag(), ->get_namespace(), ->get_qualified_tag_name(), ->is_tag_closer(), ->get_attribute_names_with_prefix(), ->get_modifiable_text(), ::escape_text_for_serialization()
$token_type === "#tag" && !->is_tag_closer() && isset($attribute_names) && $tag_name61–104->get_token_type(), ->get_doctype_info(), ->get_tag(), ->get_namespace(), ->get_qualified_tag_name(), ->is_tag_closer(), ->get_attribute_names_with_prefix(), ->has_self_closing_flag(), ->get_modifiable_text()
$token_type === "#tag" && !->is_tag_closer() && isset($attribute_names) && $tag_name63–107->get_token_type(), ->get_doctype_info(), ->get_tag(), ->get_namespace(), strtolower(), ->is_tag_closer(), ->get_attribute_names_with_prefix(), ->get_modifiable_text(), ::escape_text_for_serialization()
3 further outcomes, up to 111 instructions
$token_type === "#tag" && !->is_tag_closer() && isset($attribute_names) && $tag_name63–106->get_token_type(), ->get_doctype_info(), ->get_tag(), ->get_namespace(), strtolower(), ->is_tag_closer(), ->get_attribute_names_with_prefix(), ->has_self_closing_flag(), ->get_modifiable_text()
$token_type === "#tag" && !->is_tag_closer() && isset($attribute_names) && $tag_name64–109->get_token_type(), ->get_doctype_info(), ->get_tag(), ->get_namespace(), ->get_qualified_tag_name(), ->is_tag_closer(), ->get_attribute_names_with_prefix(), ->has_self_closing_flag(), ->get_modifiable_text(), ::escape_text_for_serialization()
$token_type === "#tag" && !->is_tag_closer() && isset($attribute_names) && $tag_name66–111->get_token_type(), ->get_doctype_info(), ->get_tag(), ->get_namespace(), strtolower(), ->is_tag_closer(), ->get_attribute_names_with_prefix(), ->has_self_closing_flag(), ->get_modifiable_text(), ::escape_text_for_serialization()

This body has more branch combinations than are worth enumerating, so the table covers the outcomes found first rather than every one that exists.

Across PHP versions

PHPCompiledExecutedBranchesNotes
8.6-dev22713–110412 fewer instructions than PHP 8.5
8.522913–11141
8.422913–111419 fewer instructions than PHP 8.3
8.323813–11441
8.223813–114411 more instruction than PHP 8.1
8.123714–113412 fewer instructions than PHP 7.4
7.423914–11441

An instruction is not a fixed amount of time, so a matching count is not necessarily the same speed; what it rules out is a difference in the work itself.

Uses · 15

Show all 15

Used by · 1

Source code

	public function serialize_token(): string {		$html       = '';		$token_type = $this->get_token_type(); 		switch ( $token_type ) {			case '#doctype':				$doctype = $this->get_doctype_info();				if ( null === $doctype ) {					break;				} 				$html .= '<!DOCTYPE'; 				if ( $doctype->name ) {					$html .= " {$doctype->name}";				} 				if ( null !== $doctype->public_identifier ) {					$quote = str_contains( $doctype->public_identifier, '"' ) ? "'" : '"';					$html .= " PUBLIC {$quote}{$doctype->public_identifier}{$quote}";				}				if ( null !== $doctype->system_identifier ) {					if ( null === $doctype->public_identifier ) {						$html .= ' SYSTEM';					}					$quote = str_contains( $doctype->system_identifier, '"' ) ? "'" : '"';					$html .= " {$quote}{$doctype->system_identifier}{$quote}";				} 				$html .= '>';				break; 			case '#text':				$html .= self::escape_text_for_serialization( $this->get_modifiable_text() );				break; 			// Unlike the `<>` which is interpreted as plaintext, this is ignored entirely.			case '#presumptuous-tag':				break; 			case '#funky-comment':			case '#comment':				$html .= "<!--{$this->get_full_comment_text()}-->";				break; 			/**			 * Processing instructions are serialized as `"<?" target " " data "?>"`.			 *			 * @link https://html.spec.whatwg.org/multipage/parsing.html#serialising-html-fragments			 */			case '#processing-instruction':				$html .= "<?{$this->get_tag()} {$this->get_modifiable_text()}?>";				break; 			case '#cdata-section':				$html .= "<![CDATA[{$this->get_modifiable_text()}]]>";				break;		} 		if ( '#tag' !== $token_type ) {			return $html;		} 		$tag_name       = $this->get_tag();		$in_html        = 'html' === $this->get_namespace();		$qualified_name = $in_html ? strtolower( $tag_name ) : $this->get_qualified_tag_name(); 		if ( $this->is_tag_closer() ) {			$html .= "</{$qualified_name}>";			return $html;		} 		$attribute_names = $this->get_attribute_names_with_prefix( '' );		if ( ! isset( $attribute_names ) ) {			$html .= "<{$qualified_name}>";			return $html;		} 		$html .= "<{$qualified_name}";

Changelog

Introduced in 6.7.0. Unchanged from 6.7.7 through 7.1.0.

  1. 6.7.7
  2. 6.8.8
  3. 6.9.7
  4. 7.0.4
  5. 7.1.0

Signature, return type and hooks compared across 5 parsed releases.

6.9.0
Converted from protected to public method.from the docblock
6.7.0
Introduced.from the docblock

About this page

Parsed data
Generated from the wordpress-develop 7.1.0 tag, from src/wp-includes/html-api/class-wp-html-processor.php, and regenerated for each WordPress release so it tracks the code rather than a snapshot of it.
Corrections
Something wrong on this page? Report it and it gets fixed in the next regeneration.