Html.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483
  1. <?php
  2. namespace Drupal\Component\Utility;
  3. /**
  4. * Provides DOMDocument helpers for parsing and serializing HTML strings.
  5. *
  6. * @ingroup utility
  7. */
  8. class Html {
  9. /**
  10. * An array of previously cleaned HTML classes.
  11. *
  12. * @var array
  13. */
  14. protected static $classes = [];
  15. /**
  16. * An array of the initial IDs used in one request.
  17. *
  18. * @var array
  19. */
  20. protected static $seenIdsInit;
  21. /**
  22. * An array of IDs, including incremented versions when an ID is duplicated.
  23. * @var array
  24. */
  25. protected static $seenIds;
  26. /**
  27. * Stores whether the current request was sent via AJAX.
  28. *
  29. * @var bool
  30. */
  31. protected static $isAjax = FALSE;
  32. /**
  33. * All attributes that may contain URIs.
  34. *
  35. * - The attributes 'code' and 'codebase' are omitted, because they only exist
  36. * for the <applet> tag. The time of Java applets has passed.
  37. * - The attribute 'icon' is omitted, because no browser implements the
  38. * <command> tag anymore.
  39. * See https://developer.mozilla.org/en-US/docs/Web/HTML/Element/command.
  40. * - The 'manifest' attribute is omitted because it only exists for the <html>
  41. * tag. That tag only makes sense in a HTML-served-as-HTML context, in which
  42. * case relative URLs are guaranteed to work.
  43. *
  44. * @see https://developer.mozilla.org/en-US/docs/Web/HTML/Attributes
  45. * @see https://stackoverflow.com/questions/2725156/complete-list-of-html-tag-attributes-which-have-a-url-value
  46. *
  47. * @var string[]
  48. */
  49. protected static $uriAttributes = ['href', 'poster', 'src', 'cite', 'data', 'action', 'formaction', 'srcset', 'about'];
  50. /**
  51. * Prepares a string for use as a valid class name.
  52. *
  53. * Do not pass one string containing multiple classes as they will be
  54. * incorrectly concatenated with dashes, i.e. "one two" will become "one-two".
  55. *
  56. * @param mixed $class
  57. * The class name to clean. It can be a string or anything that can be cast
  58. * to string.
  59. *
  60. * @return string
  61. * The cleaned class name.
  62. */
  63. public static function getClass($class) {
  64. $class = (string) $class;
  65. if (!isset(static::$classes[$class])) {
  66. static::$classes[$class] = static::cleanCssIdentifier(Unicode::strtolower($class));
  67. }
  68. return static::$classes[$class];
  69. }
  70. /**
  71. * Prepares a string for use as a CSS identifier (element, class, or ID name).
  72. *
  73. * http://www.w3.org/TR/CSS21/syndata.html#characters shows the syntax for
  74. * valid CSS identifiers (including element names, classes, and IDs in
  75. * selectors.)
  76. *
  77. * @param string $identifier
  78. * The identifier to clean.
  79. * @param array $filter
  80. * An array of string replacements to use on the identifier.
  81. *
  82. * @return string
  83. * The cleaned identifier.
  84. */
  85. public static function cleanCssIdentifier($identifier, array $filter = [
  86. ' ' => '-',
  87. '_' => '-',
  88. '/' => '-',
  89. '[' => '-',
  90. ']' => '',
  91. ]) {
  92. // We could also use strtr() here but its much slower than str_replace(). In
  93. // order to keep '__' to stay '__' we first replace it with a different
  94. // placeholder after checking that it is not defined as a filter.
  95. $double_underscore_replacements = 0;
  96. if (!isset($filter['__'])) {
  97. $identifier = str_replace('__', '##', $identifier, $double_underscore_replacements);
  98. }
  99. $identifier = str_replace(array_keys($filter), array_values($filter), $identifier);
  100. // Replace temporary placeholder '##' with '__' only if the original
  101. // $identifier contained '__'.
  102. if ($double_underscore_replacements > 0) {
  103. $identifier = str_replace('##', '__', $identifier);
  104. }
  105. // Valid characters in a CSS identifier are:
  106. // - the hyphen (U+002D)
  107. // - a-z (U+0030 - U+0039)
  108. // - A-Z (U+0041 - U+005A)
  109. // - the underscore (U+005F)
  110. // - 0-9 (U+0061 - U+007A)
  111. // - ISO 10646 characters U+00A1 and higher
  112. // We strip out any character not in the above list.
  113. $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);
  114. // Identifiers cannot start with a digit, two hyphens, or a hyphen followed by a digit.
  115. $identifier = preg_replace([
  116. '/^[0-9]/',
  117. '/^(-[0-9])|^(--)/'
  118. ], ['_', '__'], $identifier);
  119. return $identifier;
  120. }
  121. /**
  122. * Sets if this request is an Ajax request.
  123. *
  124. * @param bool $is_ajax
  125. * TRUE if this request is an Ajax request, FALSE otherwise.
  126. */
  127. public static function setIsAjax($is_ajax) {
  128. static::$isAjax = $is_ajax;
  129. }
  130. /**
  131. * Prepares a string for use as a valid HTML ID and guarantees uniqueness.
  132. *
  133. * This function ensures that each passed HTML ID value only exists once on
  134. * the page. By tracking the already returned ids, this function enables
  135. * forms, blocks, and other content to be output multiple times on the same
  136. * page, without breaking (X)HTML validation.
  137. *
  138. * For already existing IDs, a counter is appended to the ID string.
  139. * Therefore, JavaScript and CSS code should not rely on any value that was
  140. * generated by this function and instead should rely on manually added CSS
  141. * classes or similarly reliable constructs.
  142. *
  143. * Two consecutive hyphens separate the counter from the original ID. To
  144. * manage uniqueness across multiple Ajax requests on the same page, Ajax
  145. * requests POST an array of all IDs currently present on the page, which are
  146. * used to prime this function's cache upon first invocation.
  147. *
  148. * To allow reverse-parsing of IDs submitted via Ajax, any multiple
  149. * consecutive hyphens in the originally passed $id are replaced with a
  150. * single hyphen.
  151. *
  152. * @param string $id
  153. * The ID to clean.
  154. *
  155. * @return string
  156. * The cleaned ID.
  157. */
  158. public static function getUniqueId($id) {
  159. // If this is an Ajax request, then content returned by this page request
  160. // will be merged with content already on the base page. The HTML IDs must
  161. // be unique for the fully merged content. Therefore use unique IDs.
  162. if (static::$isAjax) {
  163. return static::getId($id) . '--' . Crypt::randomBytesBase64(8);
  164. }
  165. // @todo Remove all that code once we switch over to random IDs only,
  166. // see https://www.drupal.org/node/1090592.
  167. if (!isset(static::$seenIdsInit)) {
  168. static::$seenIdsInit = [];
  169. }
  170. if (!isset(static::$seenIds)) {
  171. static::$seenIds = static::$seenIdsInit;
  172. }
  173. $id = static::getId($id);
  174. // Ensure IDs are unique by appending a counter after the first occurrence.
  175. // The counter needs to be appended with a delimiter that does not exist in
  176. // the base ID. Requiring a unique delimiter helps ensure that we really do
  177. // return unique IDs and also helps us re-create the $seen_ids array during
  178. // Ajax requests.
  179. if (isset(static::$seenIds[$id])) {
  180. $id = $id . '--' . ++static::$seenIds[$id];
  181. }
  182. else {
  183. static::$seenIds[$id] = 1;
  184. }
  185. return $id;
  186. }
  187. /**
  188. * Prepares a string for use as a valid HTML ID.
  189. *
  190. * Only use this function when you want to intentionally skip the uniqueness
  191. * guarantee of self::getUniqueId().
  192. *
  193. * @param string $id
  194. * The ID to clean.
  195. *
  196. * @return string
  197. * The cleaned ID.
  198. *
  199. * @see self::getUniqueId()
  200. */
  201. public static function getId($id) {
  202. $id = str_replace([' ', '_', '[', ']'], ['-', '-', '-', ''], Unicode::strtolower($id));
  203. // As defined in http://www.w3.org/TR/html4/types.html#type-name, HTML IDs can
  204. // only contain letters, digits ([0-9]), hyphens ("-"), underscores ("_"),
  205. // colons (":"), and periods ("."). We strip out any character not in that
  206. // list. Note that the CSS spec doesn't allow colons or periods in identifiers
  207. // (http://www.w3.org/TR/CSS21/syndata.html#characters), so we strip those two
  208. // characters as well.
  209. $id = preg_replace('/[^A-Za-z0-9\-_]/', '', $id);
  210. // Removing multiple consecutive hyphens.
  211. $id = preg_replace('/\-+/', '-', $id);
  212. return $id;
  213. }
  214. /**
  215. * Resets the list of seen IDs.
  216. */
  217. public static function resetSeenIds() {
  218. static::$seenIds = NULL;
  219. }
  220. /**
  221. * Normalizes an HTML snippet.
  222. *
  223. * This function is essentially \DOMDocument::normalizeDocument(), but
  224. * operates on an HTML string instead of a \DOMDocument.
  225. *
  226. * @param string $html
  227. * The HTML string to normalize.
  228. *
  229. * @return string
  230. * The normalized HTML string.
  231. */
  232. public static function normalize($html) {
  233. $document = static::load($html);
  234. return static::serialize($document);
  235. }
  236. /**
  237. * Parses an HTML snippet and returns it as a DOM object.
  238. *
  239. * This function loads the body part of a partial (X)HTML document and returns
  240. * a full \DOMDocument object that represents this document.
  241. *
  242. * Use \Drupal\Component\Utility\Html::serialize() to serialize this
  243. * \DOMDocument back to a string.
  244. *
  245. * @param string $html
  246. * The partial (X)HTML snippet to load. Invalid markup will be corrected on
  247. * import.
  248. *
  249. * @return \DOMDocument
  250. * A \DOMDocument that represents the loaded (X)HTML snippet.
  251. */
  252. public static function load($html) {
  253. $document = <<<EOD
  254. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
  255. <html xmlns="http://www.w3.org/1999/xhtml">
  256. <head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /></head>
  257. <body>!html</body>
  258. </html>
  259. EOD;
  260. // PHP's \DOMDocument serialization adds extra whitespace when the markup
  261. // of the wrapping document contains newlines, so ensure we remove all
  262. // newlines before injecting the actual HTML body to be processed.
  263. $document = strtr($document, ["\n" => '', '!html' => $html]);
  264. $dom = new \DOMDocument();
  265. // Ignore warnings during HTML soup loading.
  266. @$dom->loadHTML($document);
  267. return $dom;
  268. }
  269. /**
  270. * Converts the body of a \DOMDocument back to an HTML snippet.
  271. *
  272. * The function serializes the body part of a \DOMDocument back to an (X)HTML
  273. * snippet. The resulting (X)HTML snippet will be properly formatted to be
  274. * compatible with HTML user agents.
  275. *
  276. * @param \DOMDocument $document
  277. * A \DOMDocument object to serialize, only the tags below the first <body>
  278. * node will be converted.
  279. *
  280. * @return string
  281. * A valid (X)HTML snippet, as a string.
  282. */
  283. public static function serialize(\DOMDocument $document) {
  284. $body_node = $document->getElementsByTagName('body')->item(0);
  285. $html = '';
  286. if ($body_node !== NULL) {
  287. foreach ($body_node->getElementsByTagName('script') as $node) {
  288. static::escapeCdataElement($node);
  289. }
  290. foreach ($body_node->getElementsByTagName('style') as $node) {
  291. static::escapeCdataElement($node, '/*', '*/');
  292. }
  293. foreach ($body_node->childNodes as $node) {
  294. $html .= $document->saveXML($node);
  295. }
  296. }
  297. return $html;
  298. }
  299. /**
  300. * Adds comments around a <!CDATA section in a \DOMNode.
  301. *
  302. * \DOMDocument::loadHTML() in \Drupal\Component\Utility\Html::load() makes
  303. * CDATA sections from the contents of inline script and style tags. This can
  304. * cause HTML4 browsers to throw exceptions.
  305. *
  306. * This function attempts to solve the problem by creating a
  307. * \DOMDocumentFragment to comment the CDATA tag.
  308. *
  309. * @param \DOMNode $node
  310. * The element potentially containing a CDATA node.
  311. * @param string $comment_start
  312. * (optional) A string to use as a comment start marker to escape the CDATA
  313. * declaration. Defaults to '//'.
  314. * @param string $comment_end
  315. * (optional) A string to use as a comment end marker to escape the CDATA
  316. * declaration. Defaults to an empty string.
  317. */
  318. public static function escapeCdataElement(\DOMNode $node, $comment_start = '//', $comment_end = '') {
  319. foreach ($node->childNodes as $child_node) {
  320. if ($child_node instanceof \DOMCdataSection) {
  321. $embed_prefix = "\n<!--{$comment_start}--><![CDATA[{$comment_start} ><!--{$comment_end}\n";
  322. $embed_suffix = "\n{$comment_start}--><!]]>{$comment_end}\n";
  323. // Prevent invalid cdata escaping as this would throw a DOM error.
  324. // This is the same behavior as found in libxml2.
  325. // Related W3C standard: http://www.w3.org/TR/REC-xml/#dt-cdsection
  326. // Fix explanation: http://wikipedia.org/wiki/CDATA#Nesting
  327. $data = str_replace(']]>', ']]]]><![CDATA[>', $child_node->data);
  328. $fragment = $node->ownerDocument->createDocumentFragment();
  329. $fragment->appendXML($embed_prefix . $data . $embed_suffix);
  330. $node->appendChild($fragment);
  331. $node->removeChild($child_node);
  332. }
  333. }
  334. }
  335. /**
  336. * Decodes all HTML entities including numerical ones to regular UTF-8 bytes.
  337. *
  338. * Double-escaped entities will only be decoded once ("&amp;lt;" becomes
  339. * "&lt;", not "<"). Be careful when using this function, as it will revert
  340. * previous sanitization efforts (&lt;script&gt; will become <script>).
  341. *
  342. * This method is not the opposite of Html::escape(). For example, this method
  343. * will convert "&eacute;" to "é", whereas Html::escape() will not convert "é"
  344. * to "&eacute;".
  345. *
  346. * @param string $text
  347. * The text to decode entities in.
  348. *
  349. * @return string
  350. * The input $text, with all HTML entities decoded once.
  351. *
  352. * @see html_entity_decode()
  353. * @see \Drupal\Component\Utility\Html::escape()
  354. */
  355. public static function decodeEntities($text) {
  356. return html_entity_decode($text, ENT_QUOTES, 'UTF-8');
  357. }
  358. /**
  359. * Escapes text by converting special characters to HTML entities.
  360. *
  361. * This method escapes HTML for sanitization purposes by replacing the
  362. * following special characters with their HTML entity equivalents:
  363. * - & (ampersand) becomes &amp;
  364. * - " (double quote) becomes &quot;
  365. * - ' (single quote) becomes &#039;
  366. * - < (less than) becomes &lt;
  367. * - > (greater than) becomes &gt;
  368. * Special characters that have already been escaped will be double-escaped
  369. * (for example, "&lt;" becomes "&amp;lt;"), and invalid UTF-8 encoding
  370. * will be converted to the Unicode replacement character ("�").
  371. *
  372. * This method is not the opposite of Html::decodeEntities(). For example,
  373. * this method will not encode "é" to "&eacute;", whereas
  374. * Html::decodeEntities() will convert all HTML entities to UTF-8 bytes,
  375. * including "&eacute;" and "&lt;" to "é" and "<".
  376. *
  377. * When constructing @link theme_render render arrays @endlink passing the output of Html::escape() to
  378. * '#markup' is not recommended. Use the '#plain_text' key instead and the
  379. * renderer will autoescape the text.
  380. *
  381. * @param string $text
  382. * The input text.
  383. *
  384. * @return string
  385. * The text with all HTML special characters converted.
  386. *
  387. * @see htmlspecialchars()
  388. * @see \Drupal\Component\Utility\Html::decodeEntities()
  389. *
  390. * @ingroup sanitization
  391. */
  392. public static function escape($text) {
  393. return htmlspecialchars($text, ENT_QUOTES | ENT_SUBSTITUTE, 'UTF-8');
  394. }
  395. /**
  396. * Converts all root-relative URLs to absolute URLs.
  397. *
  398. * Does not change any existing protocol-relative or absolute URLs. Does not
  399. * change other relative URLs because they would result in different absolute
  400. * URLs depending on the current path. For example: when the same content
  401. * containing such a relative URL (for example 'image.png'), is served from
  402. * its canonical URL (for example 'http://example.com/some-article') or from
  403. * a listing or feed (for example 'http://example.com/all-articles') their
  404. * "current path" differs, resulting in different absolute URLs:
  405. * 'http://example.com/some-article/image.png' versus
  406. * 'http://example.com/all-articles/image.png'. Only one can be correct.
  407. * Therefore relative URLs that are not root-relative cannot be safely
  408. * transformed and should generally be avoided.
  409. *
  410. * Necessary for HTML that is served outside of a website, for example, RSS
  411. * and e-mail.
  412. *
  413. * @param string $html
  414. * The partial (X)HTML snippet to load. Invalid markup will be corrected on
  415. * import.
  416. * @param string $scheme_and_host
  417. * The root URL, which has a URI scheme, host and optional port.
  418. *
  419. * @return string
  420. * The updated (X)HTML snippet.
  421. */
  422. public static function transformRootRelativeUrlsToAbsolute($html, $scheme_and_host) {
  423. assert(empty(array_diff(array_keys(parse_url($scheme_and_host)), ["scheme", "host", "port"])), '$scheme_and_host contains scheme, host and port at most.');
  424. assert(isset(parse_url($scheme_and_host)["scheme"]), '$scheme_and_host is absolute and hence has a scheme.');
  425. assert(isset(parse_url($scheme_and_host)["host"]), '$base_url is absolute and hence has a host.');
  426. $html_dom = Html::load($html);
  427. $xpath = new \DOMXpath($html_dom);
  428. // Update all root-relative URLs to absolute URLs in the given HTML.
  429. foreach (static::$uriAttributes as $attr) {
  430. foreach ($xpath->query("//*[starts-with(@$attr, '/') and not(starts-with(@$attr, '//'))]") as $node) {
  431. $node->setAttribute($attr, $scheme_and_host . $node->getAttribute($attr));
  432. }
  433. foreach ($xpath->query("//*[@srcset]") as $node) {
  434. // @see https://html.spec.whatwg.org/multipage/embedded-content.html#attr-img-srcset
  435. // @see https://html.spec.whatwg.org/multipage/embedded-content.html#image-candidate-string
  436. $image_candidate_strings = explode(',', $node->getAttribute('srcset'));
  437. $image_candidate_strings = array_map('trim', $image_candidate_strings);
  438. for ($i = 0; $i < count($image_candidate_strings); $i++) {
  439. $image_candidate_string = $image_candidate_strings[$i];
  440. if ($image_candidate_string[0] === '/' && $image_candidate_string[1] !== '/') {
  441. $image_candidate_strings[$i] = $scheme_and_host . $image_candidate_string;
  442. }
  443. }
  444. $node->setAttribute('srcset', implode(', ', $image_candidate_strings));
  445. }
  446. }
  447. return Html::serialize($html_dom);
  448. }
  449. }