function Html::load
Same name in other branches
- 8.9.x core/lib/Drupal/Component/Utility/Html.php \Drupal\Component\Utility\Html::load()
- 10 core/lib/Drupal/Component/Utility/Html.php \Drupal\Component\Utility\Html::load()
- 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 (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.
Parameters
string $html: The partial (X)HTML snippet to load. Invalid markup will be corrected on import.
Return value
\DOMDocument A \DOMDocument that represents the loaded (X)HTML snippet.
20 calls to Html::load()
- CKEditor5TestTrait::getEditorDataAsDom in core/
modules/ ckeditor5/ tests/ src/ Traits/ CKEditor5TestTrait.php - Gets CKEditor 5 instance data as a PHP DOMDocument.
- EditorFileReference::process in core/
modules/ editor/ src/ Plugin/ Filter/ EditorFileReference.php - Performs the filter processing.
- FilterAlign::process in core/
modules/ filter/ src/ Plugin/ Filter/ FilterAlign.php - Performs the filter processing.
- FilterCaption::process in core/
modules/ filter/ src/ Plugin/ Filter/ FilterCaption.php - Performs the filter processing.
- FilterHtml::filterAttributes in core/
modules/ filter/ src/ Plugin/ Filter/ FilterHtml.php - Provides filtering of tag attributes into accepted HTML.
File
-
core/
lib/ Drupal/ Component/ Utility/ Html.php, line 274
Class
- Html
- Provides DOMDocument helpers for parsing and serializing HTML strings.
Namespace
Drupal\Component\UtilityCode
public static function load($html) {
$document = <<<EOD
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /></head>
<body>!html</body>
</html>
EOD;
// PHP's \DOMDocument serialization adds extra whitespace when the markup
// of the wrapping document contains newlines, so ensure we remove all
// newlines before injecting the actual HTML body to be processed.
$document = strtr($document, [
"\n" => '',
'!html' => $html,
]);
$dom = new \DOMDocument();
// Ignore warnings during HTML soup loading.
@$dom->loadHTML($document, LIBXML_NOBLANKS);
return $dom;
}
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.