WP_HTML_Processor::normalize()
Normalizes an HTML fragment by serializing it.
This method assumes that the given HTML snippet is found in BODY context. For normalizing full documents or fragments found in other contexts, create a new processor using WP_HTML_Processor::create_fragment() or WP_HTML_Processor::create_full_parser() and call WP_HTML_Processor::serialize() on the created instances.
Many aspects of an input HTML fragment may be changed during normalization.
- Attribute values will be double-quoted.
- Duplicate attributes will be removed.
- Omitted tags will be added.
- Tag and attribute name casing will be lower-cased,
except for specific SVG and MathML tags or attributes.
- Text will be re-encoded, null bytes handled,
and invalid UTF-8 replaced with U+FFFD.
- Any incomplete syntax trailing at the end will be omitted,
for example, an unclosed comment opener will be removed.
Example:
echo WP_HTML_Processor::normalize( '<a href=#anchor v=5 href="/" enabled>One</a another v=5><!--' ); // <a href="#anchor" v="5" enabled>One</a>
echo WP_HTML_Processor::normalize( '<div></p>fun<table><td>cell</div>' ); // <div><p></p>fun<table><tbody><tr><td>cell</td></tr></tbody></table></div>
echo WP_HTML_Processor::normalize( '<![CDATA[invalid comment]]> syntax < <> "oddities"' ); // <!--[CDATA[invalid comment]]--> syntax < <> "oddities"
Method of the class: WP_HTML_Processor{}
No Hooks.
Return
String|null
. Normalized output, or null if unable to normalize.
Usage
$result = WP_HTML_Processor::normalize( $html ): ?string;
- $html(string) (required)
- Input HTML to normalize.
Changelog
Since 6.7.0 | Introduced. |
WP_HTML_Processor::normalize() WP HTML Processor::normalize code WP 6.7.1
public static function normalize( string $html ): ?string { return static::create_fragment( $html )->serialize(); }