Speedup \MailSo\Base\HtmlUtils::BuildHtml()

This commit is contained in:
the-djmaze 2022-03-02 11:33:04 +01:00
parent 059b9eb322
commit 75766573e9

View file

@ -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);