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"
Метод класса: WP_HTML_Processor{}
Хуков нет.
Возвращает
Строку|null
. Normalized output, or null if unable to normalize.
Использование
$result = WP_HTML_Processor::normalize( $html ): ?string;
- $html(строка) (обязательный)
- Input HTML to normalize.
Список изменений
С версии 6.7.0 | Введена. |
Код WP_HTML_Processor::normalize() WP HTML Processor::normalize WP 6.8.1
public static function normalize( string $html ): ?string { return static::create_fragment( $html )->serialize(); }