Same name and namespace in other branches
  1. 8.9.x core/lib/Drupal/Component/Utility/Html.php \Drupal\Component\Utility\Html::load()
  2. 9 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.

17 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.
CKEditor5TestTrait::getEditorDataAsDom in core/modules/ckeditor5/tests/src/Traits/CKEditor5TestTrait.php
Gets CKEditor 5 instance data as a PHP DOMDocument.
FieldWebTest::parseContent in core/modules/views/tests/src/Functional/Handler/FieldWebTest.php
Parse a content and return the html element.
FunctionsTest::testDrupalPreRenderLinks in core/modules/system/tests/src/Kernel/Theme/FunctionsTest.php
Tests the use of Link::preRenderLinks() on a nested array of links.

... See full list

File

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

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);
}