WP_HTML_Processor::serialize_token(): string
- Since
- 6.7.0, 6.9.0
- Source
wp-includes/html-api/class-wp-html-processor.php:1357
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
- Scaling
- Scales with input
- Instructions
- 13–111
- Plugin surface
- None
- Called by
- 1
Touches nothing outside its own arguments.
The body loops, so the work grows with how much data it finds.
Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 229.
Nothing here hands control to plugin code.
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.
| When | Instructions | Calls 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_name | 58–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_name | 60–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_name | 61–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_name | 61–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_name | 63–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_name | 63–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_name | 64–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_name | 66–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
| PHP | Compiled | Executed | Branches | Notes |
|---|---|---|---|---|
| 8.6-dev | 227 | 13–110 | 41 | 2 fewer instructions than PHP 8.5 |
| 8.5 | 229 | 13–111 | 41 | |
| 8.4 | 229 | 13–111 | 41 | 9 fewer instructions than PHP 8.3 |
| 8.3 | 238 | 13–114 | 41 | |
| 8.2 | 238 | 13–114 | 41 | 1 more instruction than PHP 8.1 |
| 8.1 | 237 | 14–113 | 41 | 2 fewer instructions than PHP 7.4 |
| 7.4 | 239 | 14–114 | 41 |
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
- str_contains()Polyfill for `str_contains()` function added in PHP 8.0.
- wp_scrub_utf8()Replaces ill-formed UTF-8 byte sequences with the Unicode Replacement Character.
- WP_HTML_Processor::get_token_type()Indicates the kind of matched token, if any.
- WP_HTML_Processor::get_doctype_info()
- WP_HTML_Processor::escape_text_for_serialization()Escapes decoded text for HTML serialization.
- WP_HTML_Processor::get_modifiable_text()Returns the modifiable text for a matched token, or an empty string.
- WP_HTML_Processor::get_full_comment_text()
- WP_HTML_Processor::get_tag()Returns the uppercase name of the matched tag.
- WP_HTML_Processor::get_namespace()Indicates the namespace of the current token, or "html" if there is none.
- WP_HTML_Processor::get_qualified_tag_name()
- WP_HTML_Processor::is_tag_closer()Indicates if the current tag token is a tag closer.
- WP_HTML_Processor::get_attribute_names_with_prefix()Gets lowercase names of all attributes matching a given prefix in the current tag.
Show all 15
- WP_HTML_Processor::get_qualified_attribute_name()
- WP_HTML_Processor::get_attribute()Returns the value of a requested attribute from a matched tag opener if that attribute exists.
- WP_HTML_Processor::has_self_closing_flag()Indicates if the currently matched tag contains the self-closing flag.
Used by · 1
- WP_HTML_Processor::serialize()Returns normalized HTML for a fragment by serializing it.
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.
Signature, return type and hooks compared across 5 parsed releases.
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.