From 75766573e9e9b03f7b7743be5bf7b3cf4786b9a6 Mon Sep 17 00:00:00 2001 From: the-djmaze <> Date: Wed, 2 Mar 2022 11:33:04 +0100 Subject: [PATCH] Speedup \MailSo\Base\HtmlUtils::BuildHtml() --- .../app/libraries/MailSo/Base/HtmlUtils.php | 63 +++++++------------ 1 file changed, 22 insertions(+), 41 deletions(-) diff --git a/snappymail/v/0.0.0/app/libraries/MailSo/Base/HtmlUtils.php b/snappymail/v/0.0.0/app/libraries/MailSo/Base/HtmlUtils.php index 5a9082501..09532574b 100644 --- a/snappymail/v/0.0.0/app/libraries/MailSo/Base/HtmlUtils.php +++ b/snappymail/v/0.0.0/app/libraries/MailSo/Base/HtmlUtils.php @@ -191,60 +191,33 @@ abstract class HtmlUtils return $sResult; } - private static function ClearTags(\DOMDocument $oDom, bool $bClearStyleAndHead = true) : void - { - $aRemoveTags = array( - 'svg', 'link', 'base', 'meta', 'title', 'x-script', 'script', 'bgsound', 'keygen', 'source', - 'object', 'embed', 'applet', 'mocha', 'iframe', 'frame', 'frameset', 'video', 'audio', 'area', 'map' - ); - - if ($bClearStyleAndHead) - { - $aRemoveTags[] = 'head'; - $aRemoveTags[] = 'style'; - } - - $aRemove = array(); - $aNodes = $oDom->getElementsByTagName('*'); - foreach ($aNodes as /* @var $oElement \DOMElement */ $oElement) - { - if ($oElement) - { - $sTagNameLower = \trim(\strtolower($oElement->tagName)); - if ('' !== $sTagNameLower) - { - if (\in_array($sTagNameLower, $aRemoveTags)) - { - $aRemove[] = @$oElement; - } - } - } - } - - foreach ($aRemove as /* @var $oElement \DOMElement */ $oElement) - { - if (isset($oElement->parentNode)) - { - @$oElement->parentNode->removeChild($oElement); - } - } - } - /** * Used by DoSaveMessage() and DoSendMessage() */ public static function BuildHtml(string $sHtml, array &$aFoundCids, array &$aFoundDataURL, array &$aFoundContentLocationUrls) : string { $oDom = static::GetDomFromText($sHtml); - - static::ClearTags($oDom); unset($sHtml); + $aRemoveTags = array( + 'svg', 'link', 'base', 'meta', 'title', 'x-script', 'script', 'bgsound', 'keygen', 'source', + 'object', 'embed', 'applet', 'mocha', 'iframe', 'frame', 'frameset', 'video', 'audio', 'area', 'map', + 'head', 'style' + ); + + $aRemove = array(); + $aNodes = $oDom->getElementsByTagName('*'); foreach ($aNodes as /* @var $oElement \DOMElement */ $oElement) { $sTagNameLower = \strtolower($oElement->tagName); + if (\in_array($sTagNameLower, $aRemoveTags)) + { + $aRemove[] = @$oElement; + continue; + } + if ($oElement->hasAttribute('data-x-src-cid')) { $sCid = $oElement->getAttribute('data-x-src-cid'); @@ -361,6 +334,14 @@ abstract class HtmlUtils } } + foreach ($aRemove as /* @var $oElement \DOMElement */ $oElement) + { + if (isset($oElement->parentNode)) + { + @$oElement->parentNode->removeChild($oElement); + } + } + $sResult = static::GetTextFromDom($oDom, false); unset($oDom);