Improve body attributes parser.

This commit is contained in:
RainLoop Team 2013-11-21 16:28:41 +04:00
parent 726fa6982b
commit 16b375691b

View file

@ -298,54 +298,63 @@ class HtmlUtils
// convert body attributes to styles // convert body attributes to styles
if ('body' === $sTagNameLower) if ('body' === $sTagNameLower)
{ {
$aMargins = array( $aAttrs = array(
$oElement->hasAttribute('topmargin') 'text' => '',
? \trim($oElement->getAttribute('topmargin')) : '', 'topmargin' => '',
$oElement->hasAttribute('leftmargin') 'leftmargin' => '',
? \trim($oElement->getAttribute('leftmargin')) : '', 'bottommargin' => '',
$oElement->hasAttribute('bottommargin') 'rightmargin' => ''
? \trim($oElement->getAttribute('bottommargin')) : '',
$oElement->hasAttribute('rightmargin')
? \trim($oElement->getAttribute('rightmargin')) : '',
); );
$sText = $oElement->hasAttribute('text') ? \trim($oElement->getAttribute('text')) : ''; if (isset($oElement->attributes))
$aStyles = array();
if (!empty($sText))
{ {
$aStyles[] = 'color: '.$sText; foreach ($oElement->attributes as $sAttributeName => /* @var $oAttributeNode \DOMNode */ $oAttributeNode)
$oElement->removeAttribute('text'); {
if ($oAttributeNode && isset($oAttributeNode->nodeValue))
{
$sAttributeNameLower = \strtolower($sAttributeName);
if (isset($aAttrs[$sAttributeNameLower]) && '' === $aAttrs[$sAttributeNameLower])
{
$aAttrs[$sAttributeNameLower] = array($sAttributeName, \trim($oAttributeNode->nodeValue));
}
}
}
} }
foreach ($aMargins as $iIndex => $sItem) $aStyles = array();
foreach ($aAttrs as $sIndex => $aItem)
{ {
if ('' !== $sItem) if (\is_array($aItem))
{ {
switch ($iIndex) { $oElement->removeAttribute($aItem[0]);
case 0:
$aStyles[] = 'margin-top: '.((int) $sItem).'px'; switch ($sIndex)
$oElement->removeAttribute('topmargin'); {
case 'text':
$aStyles[] = 'color: '.$aItem[1];
break; break;
case 1: case 'topmargin':
$aStyles[] = 'margin-left: '.((int) $sItem).'px'; $aStyles[] = 'margin-top: '.((int) $aItem[1]).'px';
$oElement->removeAttribute('leftmargin');
break; break;
case 2: case 'leftmargin':
$aStyles[] = 'margin-bottom: '.((int) $sItem).'px'; $aStyles[] = 'margin-left: '.((int) $aItem[1]).'px';
$oElement->removeAttribute('bottommargin');
break; break;
case 3: case 'bottommargin':
$aStyles[] = 'margin-right: '.((int) $sItem).'px'; $aStyles[] = 'margin-bottom: '.((int) $aItem[1]).'px';
$oElement->removeAttribute('rightmargin'); break;
case 'rightmargin':
$aStyles[] = 'margin-right: '.((int) $aItem[1]).'px';
break; break;
} }
} }
} }
if (0 < \count($aStyles))
{
$sStyles = $oElement->hasAttribute('style') ? $oElement->getAttribute('style') : ''; $sStyles = $oElement->hasAttribute('style') ? $oElement->getAttribute('style') : '';
$oElement->setAttribute('style', (empty($sStyles) ? '' : $sStyles.'; ').\implode('; ', $aStyles)); $oElement->setAttribute('style', (empty($sStyles) ? '' : $sStyles.'; ').\implode('; ', $aStyles));
} }
}
if ('iframe' === $sTagNameLower || 'frame' === $sTagNameLower) if ('iframe' === $sTagNameLower || 'frame' === $sTagNameLower)
{ {