function Html::load

Same name and namespace in other branches
  1. 9 core/lib/Drupal/Component/Utility/Html.php \Drupal\Component\Utility\Html::load()
  2. 8.9.x core/lib/Drupal/Component/Utility/Html.php \Drupal\Component\Utility\Html::load()
  3. 11.x core/lib/Drupal/Component/Utility/Html.php \Drupal\Component\Utility\Html::load()

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

This function loads the body part of a partial 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 HTML snippet to load. Invalid markup will be corrected on import.

Return value

\DOMDocument A \DOMDocument that represents the loaded HTML snippet.

9 calls to Html::load()
AssertContentTrait::parse in core/tests/Drupal/KernelTests/AssertContentTrait.php
Parse content returned from curlExec using DOM and SimpleXML.
AttributeTest::getXPathResultCount in core/tests/Drupal/Tests/Core/Template/AttributeTest.php
Counts the occurrences of the given XPath query in a given HTML snippet.
FilterImageLazyLoad::transformImages in core/modules/filter/src/Plugin/Filter/FilterImageLazyLoad.php
Transform markup of images to include loading="lazy".
Html::normalize in core/lib/Drupal/Component/Utility/Html.php
Normalizes an HTML snippet.
Html::transformRootRelativeUrlsToAbsolute in core/lib/Drupal/Component/Utility/Html.php
Converts all root-relative URLs to absolute URLs.

... See full list

File

core/lib/Drupal/Component/Utility/Html.php, line 280

Class

Html
Provides DOMDocument helpers for parsing and serializing HTML strings.

Namespace

Drupal\Component\Utility

Code

public static function load($html) {
  // Instantiate the HTML5 parser, but without the HTML5 namespace being
  // added to the DOM document.
  $html5 = new HTML5([
    'disable_html_ns' => TRUE,
    'encoding' => 'UTF-8',
  ]);
  // Attach the provided HTML inside the body. Rely on the HTML5 parser to
  // close the body tag.
  return $html5->loadHTML('<body>' . $html);
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.