public static function domdocumentToHtml($dom, $clone = true)
{
if ($clone) {
$dom = $dom->cloneNode(true);
}
$voids = ['area', 'base', 'br', 'col', 'colgroup', 'command', 'embed', 'hr', 'img', 'input', 'keygen', 'link', 'meta', 'param', 'source', 'track', 'wbr'];
// Every empty node. There is no reason to match nodes with content inside.
$query = '//*[not(node())]';
$nodes = (new \DOMXPath($dom))->query($query);
foreach ($nodes as $n) {
if (!\in_array($n->nodeName, $voids)) {
// If it is not a void/empty tag,
// we need to leave the tag open.
$n->appendChild(new \DOMProcessingInstruction('X-NOT-VOID'));
}
}
$html = static::domdocumentToStringWithoutHeaders($dom);
// Let's remove the placeholder.
$html = \preg_replace('/\\s*<\\?X-NOT-VOID\\?>\\s*/', '', $html);
return $html;
}