class Html (View source)

Provides DOMDocument helpers for parsing and serializing HTML strings.

Properties

static protected array $classes

An array of previously cleaned HTML classes.

static protected array $seenIdsInit

An array of the initial IDs used in one request.

static protected array $seenIds

An array of IDs, including incremented versions when an ID is duplicated.

static protected bool $isAjax

Stores whether the current request was sent via AJAX.

static protected string[] $uriAttributes

All attributes that may contain URIs.

Methods

static string
getClass(mixed $class)

Prepares a string for use as a valid class name.

static string
cleanCssIdentifier(string $identifier, array $filter = [' ' => '-', '_' => '-', '/' => '-', '[' => '-', ']' => ''])

Prepares a string for use as a CSS identifier (element, class, or ID name).

static 
setIsAjax(bool $is_ajax)

Sets if this request is an Ajax request.

static string
getUniqueId(string $id)

Prepares a string for use as a valid HTML ID and guarantees uniqueness.

static string
getId(string $id)

Prepares a string for use as a valid HTML ID.

static 
resetSeenIds()

Resets the list of seen IDs.

static string
normalize(string $html)

Normalizes an HTML snippet.

static DOMDocument
load(string $html)

Parses an HTML snippet and returns it as a DOM object.

static string
serialize(DOMDocument $document)

Converts the body of a \DOMDocument back to an HTML snippet.

static 
escapeCdataElement(DOMNode $node, string $comment_start = '//', string $comment_end = '')

Adds comments around a <!CDATA section in a \DOMNode.

static string
decodeEntities(string $text)

Decodes all HTML entities including numerical ones to regular UTF-8 bytes.

static string
escape(string $text)

Escapes text by converting special characters to HTML entities.

static string
transformRootRelativeUrlsToAbsolute(string $html, string $scheme_and_host)

Converts all root-relative URLs to absolute URLs.

Details

static string getClass(mixed $class)

Prepares a string for use as a valid class name.

Do not pass one string containing multiple classes as they will be incorrectly concatenated with dashes, i.e. "one two" will become "one-two".

Parameters

mixed $class

The class name to clean. It can be a string or anything that can be cast to string.

Return Value

string

The cleaned class name.

static string cleanCssIdentifier(string $identifier, array $filter = [' ' => '-', '_' => '-', '/' => '-', '[' => '-', ']' => ''])

Prepares a string for use as a CSS identifier (element, class, or ID name).

Link below shows the syntax for valid CSS identifiers (including element names, classes, and IDs in selectors).

Parameters

string $identifier

The identifier to clean.

array $filter

An array of string replacements to use on the identifier.

Return Value

string

The cleaned identifier.

See also

http://www.w3.org/TR/CSS21/syndata.html#characters

static setIsAjax(bool $is_ajax)

Sets if this request is an Ajax request.

Parameters

bool $is_ajax

TRUE if this request is an Ajax request, FALSE otherwise.

static string getUniqueId(string $id)

Prepares a string for use as a valid HTML ID and guarantees uniqueness.

This function ensures that each passed HTML ID value only exists once on the page. By tracking the already returned ids, this function enables forms, blocks, and other content to be output multiple times on the same page, without breaking (X)HTML validation.

For already existing IDs, a counter is appended to the ID string. Therefore, JavaScript and CSS code should not rely on any value that was generated by this function and instead should rely on manually added CSS classes or similarly reliable constructs.

Two consecutive hyphens separate the counter from the original ID. To manage uniqueness across multiple Ajax requests on the same page, Ajax requests POST an array of all IDs currently present on the page, which are used to prime this function's cache upon first invocation.

To allow reverse-parsing of IDs submitted via Ajax, any multiple consecutive hyphens in the originally passed $id are replaced with a single hyphen.

Parameters

string $id

The ID to clean.

Return Value

string

The cleaned ID.

static string getId(string $id)

Prepares a string for use as a valid HTML ID.

Only use this function when you want to intentionally skip the uniqueness guarantee of self::getUniqueId().

Parameters

string $id

The ID to clean.

Return Value

string

The cleaned ID.

See also

\Drupal\Component\Utility\self::getUniqueId()

static resetSeenIds()

Resets the list of seen IDs.

static string normalize(string $html)

Normalizes an HTML snippet.

This function is essentially \DOMDocument::normalizeDocument(), but operates on an HTML string instead of a \DOMDocument.

Parameters

string $html

The HTML string to normalize.

Return Value

string

The normalized HTML string.

static DOMDocument load(string $html)

Parses an HTML snippet and returns it as a DOM object.

This function loads the body part of a partial (X)HTML document and returns a full \DOMDocument object that represents this document.

Use \Drupal\Component\Utility\Html::serialize() to serialize this \DOMDocument back to a string.

Parameters

string $html

The partial (X)HTML snippet to load. Invalid markup will be corrected on import.

Return Value

DOMDocument

A \DOMDocument that represents the loaded (X)HTML snippet.

static string serialize(DOMDocument $document)

Converts the body of a \DOMDocument back to an HTML snippet.

The function serializes the body part of a \DOMDocument back to an (X)HTML snippet. The resulting (X)HTML snippet will be properly formatted to be compatible with HTML user agents.

Parameters

DOMDocument $document

A \DOMDocument object to serialize, only the tags below the first node will be converted.

Return Value

string

A valid (X)HTML snippet, as a string.

static escapeCdataElement(DOMNode $node, string $comment_start = '//', string $comment_end = '')

Adds comments around a <!CDATA section in a \DOMNode.

\DOMDocument::loadHTML() in \Drupal\Component\Utility\Html::load() makes CDATA sections from the contents of inline script and style tags. This can cause HTML4 browsers to throw exceptions.

This function attempts to solve the problem by creating a \DOMDocumentFragment to comment the CDATA tag.

Parameters

DOMNode $node

The element potentially containing a CDATA node.

string $comment_start

(optional) A string to use as a comment start marker to escape the CDATA declaration. Defaults to '//'.

string $comment_end

(optional) A string to use as a comment end marker to escape the CDATA declaration. Defaults to an empty string.

static string decodeEntities(string $text)

Decodes all HTML entities including numerical ones to regular UTF-8 bytes.

Double-escaped entities will only be decoded once ("&lt;" becomes "<", not "<"). Be careful when using this function, as it will revert previous sanitization efforts (<script> will become