WP_HTML_Processor::normalize( string $html ): string|null

In this article

Normalizes an HTML fragment by serializing it.

Description

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 &lt; &lt;&gt; &quot;oddities&quot;

Parameters

$htmlstringrequired
Input HTML to normalize.

Return

string|null Normalized output, or null if unable to normalize.

Source

public static function normalize( string $html ): ?string {
	return static::create_fragment( $html )->serialize();
}

Changelog

VersionDescription
6.7.0Introduced.

User Contributed Notes

You must log in before being able to contribute a note or feedback.