Returns normalized HTML for a fragment by serializing it.
Description
This differs from WP_HTML_Processor::normalize in that it starts with a specific HTML Processor, which <em>must</em> not have already started scanning; it must be in the initial ready state and will be in the completed state once serialization is complete. Many aspects of an input HTML fragment may be changed during normalization. <ul> <li>Attribute values will be double-quoted.</li> <li>Duplicate attributes will be removed.</li> <li>Omitted tags will be added.</li> <li>Tag and attribute name casing will be lower-cased, except for specific SVG and MathML tags or attributes.</li> <li>Text will be re-encoded, null bytes handled, and invalid UTF-8 replaced with U+FFFD.</li> <li>Any incomplete syntax trailing at the end will be omitted, for example, an unclosed comment opener will be removed.</li> </ul> Example: $processor = WP_HTML_Processor::create_fragment( '<a href=#anchor v=5 href="/" enabled>One</a another v=5><!--' );
echo $processor->serialize();
// <a href="#anchor" v="5" enabled>One</a>
$processor = WP_HTML_Processor::create_fragment( '<div></p>fun<table><td>cell</div>' );
echo $processor->serialize();
// <div><p></p>fun<table><tbody><tr><td>cell</td></tr></tbody></table></div>
$processor = WP_HTML_Processor::create_fragment( '<![CDATA[invalid comment]]> syntax < <> "oddities"' );
echo $processor->serialize();
// <!--[CDATA[invalid comment]]--> syntax &lt; &lt;&gt; &quot;oddities&quot;
Return
string|null
Normalized HTML markup represented by processor, or null if unable to generate serialization.
Uses · 4
wp_trigger_error()Generates a user-level error/warning/notice/deprecation message.
1306publicfunctionserialize():?string{1307if(WP_HTML_Tag_Processor::STATE_READY!==$this->parser_state){1308wp_trigger_error(1309__METHOD__,1310'An HTML Processor which has already started processing cannot serialize its contents. Serialize immediately after creating the instance.',1311E_USER_WARNING1312);1313returnnull;1314}13151316$html='';1317while($this->next_token()){1318$html.=$this->serialize_token();1319}13201321if(null!==$this->get_last_error()){1322wp_trigger_error(1323__METHOD__,1324"Cannot serialize HTML Processor with parsing error: {$this->get_last_error()}.",1325E_USER_WARNING1326);1327returnnull;1328}13291330return$html;1331}
History
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.0.4 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.