diff --git a/src/HTML5DOMDocument.php b/src/HTML5DOMDocument.php index d3243a3..e46470f 100644 --- a/src/HTML5DOMDocument.php +++ b/src/HTML5DOMDocument.php @@ -237,15 +237,20 @@ public function saveHTML(\DOMNode $node = NULL) if ($bodyElement !== null) { // This preserves the whitespace between the HTML tags $bodyElements = $bodyElement->getElementsByTagName('*'); $bodyElementsCount = $bodyElements->length; + $tempNodes = []; $tempTextNode = $this->createTextNode('html5-dom-document-internal-content'); for ($i = 0; $i < $bodyElementsCount; $i++) { - $bodyElement = $bodyElements->item($i); - $bodyElement->parentNode->insertBefore(clone($tempTextNode), $bodyElement); - if ($bodyElement->nextSibling !== null) { - $bodyElement->parentNode->insertBefore(clone($tempTextNode), $bodyElement->nextSibling); + $element = $bodyElements->item($i); + $newTextNode1 = clone($tempTextNode); + $newTextNode2 = clone($tempTextNode); + $parentNode = $element->parentNode; + $parentNode->insertBefore($newTextNode1, $element); + if (($nextSibling = $element->nextSibling) !== null) { + $parentNode->insertBefore($newTextNode2, $nextSibling); } else { - $bodyElement->parentNode->appendChild(clone($tempTextNode)); + $parentNode->appendChild($newTextNode2); } + $tempNodes[] = [$parentNode, $newTextNode1, $newTextNode2]; } } @@ -270,11 +275,11 @@ public function saveHTML(\DOMNode $node = NULL) $html = parent::saveHTML($node); if ($bodyElement !== null) { - for ($i = 0; $i < $bodyElementsCount; $i++) { - $bodyElement = $bodyElements->item($i); - $bodyElement->parentNode->removeChild($bodyElement->previousSibling); - $bodyElement->parentNode->removeChild($bodyElement->nextSibling); + foreach ($tempNodes as $tempNodesData) { + $tempNodesData[0]->removeChild($tempNodesData[1]); + $tempNodesData[0]->removeChild($tempNodesData[2]); } + unset($tempNodes); } if ($removeHeadElement) { @@ -283,20 +288,24 @@ public function saveHTML(\DOMNode $node = NULL) $meta->parentNode->removeChild($meta); } - $html = str_replace('', '', $html); - if ($removeHeadElement) { - $html = str_replace('', '', $html); - } - $html = str_replace('html5-dom-document-internal-content', '', $html); if (strpos($html, 'html5-dom-document-internal-entity') !== false) { $html = preg_replace('/html5-dom-document-internal-entity1-(.*?)-end/', '&$1;', $html); $html = preg_replace('/html5-dom-document-internal-entity2-(.*?)-end/', '&#$1;', $html); } + + $codeToRemove = [ + 'html5-dom-document-internal-content', + '', + '', '', '
', '', '', '', '', '', '', '', '', '', '', '', '', '' + ]; + if ($removeHeadElement) { + $codeToRemove[] = ''; + } if ($removeHtmlElement) { - $html = str_replace('', '', $html); + $codeToRemove[] = ''; } + $html = str_replace($codeToRemove, '', $html); - $html = str_replace(['', '', '
', '', '', '', '', '', '', '', '', '', '', '', '', ''], '', $html); // Remove the whitespace between the doctype and html tag $html = preg_replace('/\>\s\