Skip to content

Commit

Permalink
Performance optimizations.
Browse files Browse the repository at this point in the history
  • Loading branch information
ivopetkov committed Jan 23, 2017
1 parent 7c24b7a commit 1cabbca
Showing 1 changed file with 25 additions and 16 deletions.
41 changes: 25 additions & 16 deletions src/HTML5DOMDocument.php
Original file line number Diff line number Diff line change
Expand Up @@ -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];
}
}

Expand All @@ -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) {
Expand All @@ -283,20 +288,24 @@ public function saveHTML(\DOMNode $node = NULL)
$meta->parentNode->removeChild($meta);
}

$html = str_replace('<meta data-html5-dom-document-internal-attribute="charset-meta" http-equiv="content-type" content="text/html; charset=utf-8">', '', $html);
if ($removeHeadElement) {
$html = str_replace('<head></head>', '', $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',
'<meta data-html5-dom-document-internal-attribute="charset-meta" http-equiv="content-type" content="text/html; charset=utf-8">',
'</area>', '</base>', '</br>', '</col>', '</command>', '</embed>', '</hr>', '</img>', '</input>', '</keygen>', '</link>', '</meta>', '</param>', '</source>', '</track>', '</wbr>'
];
if ($removeHeadElement) {
$codeToRemove[] = '<head></head>';
}
if ($removeHtmlElement) {
$html = str_replace('<html></html>', '', $html);
$codeToRemove[] = '<html></html>';
}
$html = str_replace($codeToRemove, '', $html);

$html = str_replace(['</area>', '</base>', '</br>', '</col>', '</command>', '</embed>', '</hr>', '</img>', '</input>', '</keygen>', '</link>', '</meta>', '</param>', '</source>', '</track>', '</wbr>'], '', $html);
// Remove the whitespace between the doctype and html tag
$html = preg_replace('/\>\s\<html/', '><html', $html, 1);
return trim($html);
Expand Down

0 comments on commit 1cabbca

Please sign in to comment.