Html
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
Prepares a string for use as a valid class name.
Prepares a string for use as a CSS identifier (element, class, or ID name).
Sets if this request is an Ajax request.
Prepares a string for use as a valid HTML ID and guarantees uniqueness.
Prepares a string for use as a valid HTML ID.
Resets the list of seen IDs.
Normalizes an HTML snippet.
Parses an HTML snippet and returns it as a DOM object.
Adds comments around a <!CDATA section in a \DOMNode.
Decodes all HTML entities including numerical ones to regular UTF-8 bytes.
Escapes text by converting special characters to HTML entities.
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".
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).
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.
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.
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().
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.
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.
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.
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.
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 ("<" becomes "<", not "<"). Be careful when using this function, as it will revert previous sanitization efforts (<script> will become