protected function getHtmlRootElement($html)
{
if (trim($html) === '') {
return null;
}
$domDocument = new \DOMDocument('1.0', 'UTF-8');
// ignore parsing errors
$useInternalErrorsBackup = libxml_use_internal_errors(true);
$domDocument->loadHTML($html);
$xPath = new \DOMXPath($domDocument);
$rootElement = $xPath->query('//html/body/*');
if ($useInternalErrorsBackup !== true) {
libxml_use_internal_errors($useInternalErrorsBackup);
}
if ($rootElement === false || $rootElement->length !== 1) {
return null;
}
return $rootElement->item(0);
}