3 namespace Drupal\Component\Utility;
6 * Provides DOMDocument helpers for parsing and serializing HTML strings.
13 * An array of previously cleaned HTML classes.
17 protected static $classes = [];
20 * An array of the initial IDs used in one request.
24 protected static $seenIdsInit;
27 * An array of IDs, including incremented versions when an ID is duplicated.
30 protected static $seenIds;
33 * Stores whether the current request was sent via AJAX.
37 protected static $isAjax = FALSE;
40 * All attributes that may contain URIs.
42 * - The attributes 'code' and 'codebase' are omitted, because they only exist
43 * for the <applet> tag. The time of Java applets has passed.
44 * - The attribute 'icon' is omitted, because no browser implements the
45 * <command> tag anymore.
46 * See https://developer.mozilla.org/en-US/docs/Web/HTML/Element/command.
47 * - The 'manifest' attribute is omitted because it only exists for the <html>
48 * tag. That tag only makes sense in a HTML-served-as-HTML context, in which
49 * case relative URLs are guaranteed to work.
51 * @see https://developer.mozilla.org/en-US/docs/Web/HTML/Attributes
52 * @see https://stackoverflow.com/questions/2725156/complete-list-of-html-tag-attributes-which-have-a-url-value
56 protected static $uriAttributes = ['href', 'poster', 'src', 'cite', 'data', 'action', 'formaction', 'srcset', 'about'];
59 * Prepares a string for use as a valid class name.
61 * Do not pass one string containing multiple classes as they will be
62 * incorrectly concatenated with dashes, i.e. "one two" will become "one-two".
65 * The class name to clean. It can be a string or anything that can be cast
69 * The cleaned class name.
71 public static function getClass($class) {
72 $class = (string) $class;
73 if (!isset(static::$classes[$class])) {
74 static::$classes[$class] = static::cleanCssIdentifier(mb_strtolower($class));
76 return static::$classes[$class];
80 * Prepares a string for use as a CSS identifier (element, class, or ID name).
82 * Link below shows the syntax for valid CSS identifiers (including element
83 * names, classes, and IDs in selectors).
85 * @see http://www.w3.org/TR/CSS21/syndata.html#characters
87 * @param string $identifier
88 * The identifier to clean.
89 * @param array $filter
90 * An array of string replacements to use on the identifier.
93 * The cleaned identifier.
95 public static function cleanCssIdentifier($identifier, array $filter = [
102 // We could also use strtr() here but its much slower than str_replace(). In
103 // order to keep '__' to stay '__' we first replace it with a different
104 // placeholder after checking that it is not defined as a filter.
105 $double_underscore_replacements = 0;
106 if (!isset($filter['__'])) {
107 $identifier = str_replace('__', '##', $identifier, $double_underscore_replacements);
109 $identifier = str_replace(array_keys($filter), array_values($filter), $identifier);
110 // Replace temporary placeholder '##' with '__' only if the original
111 // $identifier contained '__'.
112 if ($double_underscore_replacements > 0) {
113 $identifier = str_replace('##', '__', $identifier);
116 // Valid characters in a CSS identifier are:
117 // - the hyphen (U+002D)
118 // - a-z (U+0030 - U+0039)
119 // - A-Z (U+0041 - U+005A)
120 // - the underscore (U+005F)
121 // - 0-9 (U+0061 - U+007A)
122 // - ISO 10646 characters U+00A1 and higher
123 // We strip out any character not in the above list.
124 $identifier = preg_replace('/[^\x{002D}\x{0030}-\x{0039}\x{0041}-\x{005A}\x{005F}\x{0061}-\x{007A}\x{00A1}-\x{FFFF}]/u', '', $identifier);
125 // Identifiers cannot start with a digit, two hyphens, or a hyphen followed by a digit.
126 $identifier = preg_replace([
129 ], ['_', '__'], $identifier);
134 * Sets if this request is an Ajax request.
136 * @param bool $is_ajax
137 * TRUE if this request is an Ajax request, FALSE otherwise.
139 public static function setIsAjax($is_ajax) {
140 static::$isAjax = $is_ajax;
144 * Prepares a string for use as a valid HTML ID and guarantees uniqueness.
146 * This function ensures that each passed HTML ID value only exists once on
147 * the page. By tracking the already returned ids, this function enables
148 * forms, blocks, and other content to be output multiple times on the same
149 * page, without breaking (X)HTML validation.
151 * For already existing IDs, a counter is appended to the ID string.
152 * Therefore, JavaScript and CSS code should not rely on any value that was
153 * generated by this function and instead should rely on manually added CSS
154 * classes or similarly reliable constructs.
156 * Two consecutive hyphens separate the counter from the original ID. To
157 * manage uniqueness across multiple Ajax requests on the same page, Ajax
158 * requests POST an array of all IDs currently present on the page, which are
159 * used to prime this function's cache upon first invocation.
161 * To allow reverse-parsing of IDs submitted via Ajax, any multiple
162 * consecutive hyphens in the originally passed $id are replaced with a
171 public static function getUniqueId($id) {
172 // If this is an Ajax request, then content returned by this page request
173 // will be merged with content already on the base page. The HTML IDs must
174 // be unique for the fully merged content. Therefore use unique IDs.
175 if (static::$isAjax) {
176 return static::getId($id) . '--' . Crypt::randomBytesBase64(8);
179 // @todo Remove all that code once we switch over to random IDs only,
180 // see https://www.drupal.org/node/1090592.
181 if (!isset(static::$seenIdsInit)) {
182 static::$seenIdsInit = [];
184 if (!isset(static::$seenIds)) {
185 static::$seenIds = static::$seenIdsInit;
188 $id = static::getId($id);
190 // Ensure IDs are unique by appending a counter after the first occurrence.
191 // The counter needs to be appended with a delimiter that does not exist in
192 // the base ID. Requiring a unique delimiter helps ensure that we really do
193 // return unique IDs and also helps us re-create the $seen_ids array during
195 if (isset(static::$seenIds[$id])) {
196 $id = $id . '--' . ++static::$seenIds[$id];
199 static::$seenIds[$id] = 1;
205 * Prepares a string for use as a valid HTML ID.
207 * Only use this function when you want to intentionally skip the uniqueness
208 * guarantee of self::getUniqueId().
216 * @see self::getUniqueId()
218 public static function getId($id) {
219 $id = str_replace([' ', '_', '[', ']'], ['-', '-', '-', ''], mb_strtolower($id));
221 // As defined in http://www.w3.org/TR/html4/types.html#type-name, HTML IDs can
222 // only contain letters, digits ([0-9]), hyphens ("-"), underscores ("_"),
223 // colons (":"), and periods ("."). We strip out any character not in that
224 // list. Note that the CSS spec doesn't allow colons or periods in identifiers
225 // (http://www.w3.org/TR/CSS21/syndata.html#characters), so we strip those two
226 // characters as well.
227 $id = preg_replace('/[^A-Za-z0-9\-_]/', '', $id);
229 // Removing multiple consecutive hyphens.
230 $id = preg_replace('/\-+/', '-', $id);
235 * Resets the list of seen IDs.
237 public static function resetSeenIds() {
238 static::$seenIds = NULL;
242 * Normalizes an HTML snippet.
244 * This function is essentially \DOMDocument::normalizeDocument(), but
245 * operates on an HTML string instead of a \DOMDocument.
247 * @param string $html
248 * The HTML string to normalize.
251 * The normalized HTML string.
253 public static function normalize($html) {
254 $document = static::load($html);
255 return static::serialize($document);
259 * Parses an HTML snippet and returns it as a DOM object.
261 * This function loads the body part of a partial (X)HTML document and returns
262 * a full \DOMDocument object that represents this document.
264 * Use \Drupal\Component\Utility\Html::serialize() to serialize this
265 * \DOMDocument back to a string.
267 * @param string $html
268 * The partial (X)HTML snippet to load. Invalid markup will be corrected on
271 * @return \DOMDocument
272 * A \DOMDocument that represents the loaded (X)HTML snippet.
274 public static function load($html) {
276 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
277 <html xmlns="http://www.w3.org/1999/xhtml">
278 <head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /></head>
282 // PHP's \DOMDocument serialization adds extra whitespace when the markup
283 // of the wrapping document contains newlines, so ensure we remove all
284 // newlines before injecting the actual HTML body to be processed.
285 $document = strtr($document, ["\n" => '', '!html' => $html]);
287 $dom = new \DOMDocument();
288 // Ignore warnings during HTML soup loading.
289 @$dom->loadHTML($document);
295 * Converts the body of a \DOMDocument back to an HTML snippet.
297 * The function serializes the body part of a \DOMDocument back to an (X)HTML
298 * snippet. The resulting (X)HTML snippet will be properly formatted to be
299 * compatible with HTML user agents.
301 * @param \DOMDocument $document
302 * A \DOMDocument object to serialize, only the tags below the first <body>
303 * node will be converted.
306 * A valid (X)HTML snippet, as a string.
308 public static function serialize(\DOMDocument $document) {
309 $body_node = $document->getElementsByTagName('body')->item(0);
312 if ($body_node !== NULL) {
313 foreach ($body_node->getElementsByTagName('script') as $node) {
314 static::escapeCdataElement($node);
316 foreach ($body_node->getElementsByTagName('style') as $node) {
317 static::escapeCdataElement($node, '/*', '*/');
319 foreach ($body_node->childNodes as $node) {
320 $html .= $document->saveXML($node);
327 * Adds comments around a <!CDATA section in a \DOMNode.
329 * \DOMDocument::loadHTML() in \Drupal\Component\Utility\Html::load() makes
330 * CDATA sections from the contents of inline script and style tags. This can
331 * cause HTML4 browsers to throw exceptions.
333 * This function attempts to solve the problem by creating a
334 * \DOMDocumentFragment to comment the CDATA tag.
336 * @param \DOMNode $node
337 * The element potentially containing a CDATA node.
338 * @param string $comment_start
339 * (optional) A string to use as a comment start marker to escape the CDATA
340 * declaration. Defaults to '//'.
341 * @param string $comment_end
342 * (optional) A string to use as a comment end marker to escape the CDATA
343 * declaration. Defaults to an empty string.
345 public static function escapeCdataElement(\DOMNode $node, $comment_start = '//', $comment_end = '') {
346 foreach ($node->childNodes as $child_node) {
347 if ($child_node instanceof \DOMCdataSection) {
348 $embed_prefix = "\n<!--{$comment_start}--><![CDATA[{$comment_start} ><!--{$comment_end}\n";
349 $embed_suffix = "\n{$comment_start}--><!]]>{$comment_end}\n";
351 // Prevent invalid cdata escaping as this would throw a DOM error.
352 // This is the same behavior as found in libxml2.
353 // Related W3C standard: http://www.w3.org/TR/REC-xml/#dt-cdsection
354 // Fix explanation: http://wikipedia.org/wiki/CDATA#Nesting
355 $data = str_replace(']]>', ']]]]><![CDATA[>', $child_node->data);
357 $fragment = $node->ownerDocument->createDocumentFragment();
358 $fragment->appendXML($embed_prefix . $data . $embed_suffix);
359 $node->appendChild($fragment);
360 $node->removeChild($child_node);
366 * Decodes all HTML entities including numerical ones to regular UTF-8 bytes.
368 * Double-escaped entities will only be decoded once ("&lt;" becomes
369 * "<", not "<"). Be careful when using this function, as it will revert
370 * previous sanitization efforts (<script> will become <script>).
372 * This method is not the opposite of Html::escape(). For example, this method
373 * will convert "é" to "é", whereas Html::escape() will not convert "é"
376 * @param string $text
377 * The text to decode entities in.
380 * The input $text, with all HTML entities decoded once.
382 * @see html_entity_decode()
383 * @see \Drupal\Component\Utility\Html::escape()
385 public static function decodeEntities($text) {
386 return html_entity_decode($text, ENT_QUOTES, 'UTF-8');
390 * Escapes text by converting special characters to HTML entities.
392 * This method escapes HTML for sanitization purposes by replacing the
393 * following special characters with their HTML entity equivalents:
394 * - & (ampersand) becomes &
395 * - " (double quote) becomes "
396 * - ' (single quote) becomes '
397 * - < (less than) becomes <
398 * - > (greater than) becomes >
399 * Special characters that have already been escaped will be double-escaped
400 * (for example, "<" becomes "&lt;"), and invalid UTF-8 encoding
401 * will be converted to the Unicode replacement character ("�").
403 * This method is not the opposite of Html::decodeEntities(). For example,
404 * this method will not encode "é" to "é", whereas
405 * Html::decodeEntities() will convert all HTML entities to UTF-8 bytes,
406 * including "é" and "<" to "é" and "<".
408 * When constructing @link theme_render render arrays @endlink passing the output of Html::escape() to
409 * '#markup' is not recommended. Use the '#plain_text' key instead and the
410 * renderer will autoescape the text.
412 * @param string $text
416 * The text with all HTML special characters converted.
418 * @see htmlspecialchars()
419 * @see \Drupal\Component\Utility\Html::decodeEntities()
421 * @ingroup sanitization
423 public static function escape($text) {
424 return htmlspecialchars($text, ENT_QUOTES | ENT_SUBSTITUTE, 'UTF-8');
428 * Converts all root-relative URLs to absolute URLs.
430 * Does not change any existing protocol-relative or absolute URLs. Does not
431 * change other relative URLs because they would result in different absolute
432 * URLs depending on the current path. For example: when the same content
433 * containing such a relative URL (for example 'image.png'), is served from
434 * its canonical URL (for example 'http://example.com/some-article') or from
435 * a listing or feed (for example 'http://example.com/all-articles') their
436 * "current path" differs, resulting in different absolute URLs:
437 * 'http://example.com/some-article/image.png' versus
438 * 'http://example.com/all-articles/image.png'. Only one can be correct.
439 * Therefore relative URLs that are not root-relative cannot be safely
440 * transformed and should generally be avoided.
442 * Necessary for HTML that is served outside of a website, for example, RSS
445 * @param string $html
446 * The partial (X)HTML snippet to load. Invalid markup will be corrected on
448 * @param string $scheme_and_host
449 * The root URL, which has a URI scheme, host and optional port.
452 * The updated (X)HTML snippet.
454 public static function transformRootRelativeUrlsToAbsolute($html, $scheme_and_host) {
455 assert(empty(array_diff(array_keys(parse_url($scheme_and_host)), ["scheme", "host", "port"])), '$scheme_and_host contains scheme, host and port at most.');
456 assert(isset(parse_url($scheme_and_host)["scheme"]), '$scheme_and_host is absolute and hence has a scheme.');
457 assert(isset(parse_url($scheme_and_host)["host"]), '$base_url is absolute and hence has a host.');
459 $html_dom = Html::load($html);
460 $xpath = new \DOMXpath($html_dom);
462 // Update all root-relative URLs to absolute URLs in the given HTML.
463 foreach (static::$uriAttributes as $attr) {
464 foreach ($xpath->query("//*[starts-with(@$attr, '/') and not(starts-with(@$attr, '//'))]") as $node) {
465 $node->setAttribute($attr, $scheme_and_host . $node->getAttribute($attr));
467 foreach ($xpath->query("//*[@srcset]") as $node) {
468 // @see https://html.spec.whatwg.org/multipage/embedded-content.html#attr-img-srcset
469 // @see https://html.spec.whatwg.org/multipage/embedded-content.html#image-candidate-string
470 $image_candidate_strings = explode(',', $node->getAttribute('srcset'));
471 $image_candidate_strings = array_map('trim', $image_candidate_strings);
472 for ($i = 0; $i < count($image_candidate_strings); $i++) {
473 $image_candidate_string = $image_candidate_strings[$i];
474 if ($image_candidate_string[0] === '/' && $image_candidate_string[1] !== '/') {
475 $image_candidate_strings[$i] = $scheme_and_host . $image_candidate_string;
478 $node->setAttribute('srcset', implode(', ', $image_candidate_strings));
481 return Html::serialize($html_dom);