Merge pull request #298 from mailpoet/text_block_rendering_fix
Text block formatting fix
This commit is contained in:
@ -5,21 +5,23 @@ use MailPoet\Newsletter\Renderer\StylesHelper;
|
|||||||
|
|
||||||
class Footer {
|
class Footer {
|
||||||
static function render($element) {
|
static function render($element) {
|
||||||
if(isset($element['styles']['link'])) {
|
|
||||||
$element['text'] = str_replace(
|
|
||||||
'<a',
|
|
||||||
'<a style="'
|
|
||||||
. StylesHelper::getStyles($element['styles'], 'link')
|
|
||||||
. '"', $element['text']
|
|
||||||
);
|
|
||||||
}
|
|
||||||
$element['text'] = preg_replace('/\n/', '<br /><br />', $element['text']);
|
$element['text'] = preg_replace('/\n/', '<br /><br />', $element['text']);
|
||||||
$element['text'] = preg_replace('/(<\/?p>)/', '', $element['text']);
|
$element['text'] = preg_replace('/(<\/?p.*?>)/i', '', $element['text']);
|
||||||
|
$DOM_parser = new \pQuery();
|
||||||
|
$DOM = $DOM_parser->parseStr($element['text']);
|
||||||
|
if(isset($element['styles']['link'])) {
|
||||||
|
$links = $DOM->query('a');
|
||||||
|
if($links->count()) {
|
||||||
|
foreach($links as $link) {
|
||||||
|
$link->style = StylesHelper::getStyles($element['styles'], 'link');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
$template = '
|
$template = '
|
||||||
<tr>
|
<tr>
|
||||||
<td class="mailpoet_padded_header_footer mailpoet_footer" bgcolor="' . $element['styles']['block']['backgroundColor'] . '"
|
<td class="mailpoet_padded_header_footer mailpoet_footer" bgcolor="' . $element['styles']['block']['backgroundColor'] . '"
|
||||||
style="' . StylesHelper::getBlockStyles($element) . StylesHelper::getStyles($element['styles'], 'text') . '">
|
style="' . StylesHelper::getBlockStyles($element) . StylesHelper::getStyles($element['styles'], 'text') . '">
|
||||||
' . $element['text'] . '
|
' . $DOM->html() . '
|
||||||
</td>
|
</td>
|
||||||
</tr>';
|
</tr>';
|
||||||
return $template;
|
return $template;
|
||||||
|
@ -5,21 +5,23 @@ use MailPoet\Newsletter\Renderer\StylesHelper;
|
|||||||
|
|
||||||
class Header {
|
class Header {
|
||||||
static function render($element) {
|
static function render($element) {
|
||||||
if(isset($element['styles']['link'])) {
|
|
||||||
$element['text'] = str_replace(
|
|
||||||
'<a',
|
|
||||||
'<a style="'
|
|
||||||
. StylesHelper::getStyles($element['styles'], 'link')
|
|
||||||
. '"', $element['text']
|
|
||||||
);
|
|
||||||
}
|
|
||||||
$element['text'] = preg_replace('/\n/', '<br /><br />', $element['text']);
|
$element['text'] = preg_replace('/\n/', '<br /><br />', $element['text']);
|
||||||
$element['text'] = preg_replace('/(<\/?p>)/', '', $element['text']);
|
$element['text'] = preg_replace('/(<\/?p.*?>)/', '', $element['text']);
|
||||||
|
$DOM_parser = new \pQuery();
|
||||||
|
$DOM = $DOM_parser->parseStr($element['text']);
|
||||||
|
if(isset($element['styles']['link'])) {
|
||||||
|
$links = $DOM->query('a');
|
||||||
|
if($links->count()) {
|
||||||
|
foreach($links as $link) {
|
||||||
|
$link->style = StylesHelper::getStyles($element['styles'], 'link');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
$template = '
|
$template = '
|
||||||
<tr>
|
<tr>
|
||||||
<td class="mailpoet_padded_header_footer mailpoet_header" bgcolor="' . $element['styles']['block']['backgroundColor'] . '"
|
<td class="mailpoet_padded_header_footer mailpoet_header" bgcolor="' . $element['styles']['block']['backgroundColor'] . '"
|
||||||
style="' . StylesHelper::getBlockStyles($element) . StylesHelper::getStyles($element['styles'], 'text') . '">
|
style="' . StylesHelper::getBlockStyles($element) . StylesHelper::getStyles($element['styles'], 'text') . '">
|
||||||
' . $element['text'] . '
|
' . $DOM->html() . '
|
||||||
</td>
|
</td>
|
||||||
</tr>';
|
</tr>';
|
||||||
return $template;
|
return $template;
|
||||||
|
@ -6,10 +6,9 @@ class Text {
|
|||||||
$html = $element['text'];
|
$html = $element['text'];
|
||||||
$html = self::convertBlockquotesToTables($html);
|
$html = self::convertBlockquotesToTables($html);
|
||||||
$html = self::convertParagraphsToTables($html);
|
$html = self::convertParagraphsToTables($html);
|
||||||
$html = self::addLineBreakAfterTags($html);
|
|
||||||
$html = self::styleLists($html);
|
$html = self::styleLists($html);
|
||||||
$html = self::styleHeadings($html);
|
$html = self::styleHeadings($html);
|
||||||
$html = self::removeLastElementBreakLine($html);
|
$html = self::addLineBreakAfterTags($html);
|
||||||
$template = '
|
$template = '
|
||||||
<tr>
|
<tr>
|
||||||
<td class="mailpoet_text mailpoet_padded" valign="top" style="word-break:break-word;word-wrap:break-word;">
|
<td class="mailpoet_text mailpoet_padded" valign="top" style="word-break:break-word;word-wrap:break-word;">
|
||||||
@ -19,42 +18,26 @@ class Text {
|
|||||||
return $template;
|
return $template;
|
||||||
}
|
}
|
||||||
|
|
||||||
static function convertParagraphsToTables($html) {
|
|
||||||
$html = preg_replace('/<p>(.*?)<\/p>/', '
|
|
||||||
<table width="100%" cellpadding="0" cellspacing="0" style="border-spacing:0;mso-table-lspace:0;mso-table-rspace:0">
|
|
||||||
<tr>
|
|
||||||
<td class="mailpoet_paragraph" style="word-break:break-word;word-wrap:break-word;">
|
|
||||||
$1
|
|
||||||
<br /><br />
|
|
||||||
</td>
|
|
||||||
</tr>
|
|
||||||
</table>'
|
|
||||||
, $html);
|
|
||||||
$html = preg_replace('/<p style=\"(.*)\">(.*?)<\/p>/', '
|
|
||||||
<table width="100%" cellpadding="0" cellspacing="0" style="border-spacing:0;mso-table-lspace:0;mso-table-rspace:0">
|
|
||||||
<tr>
|
|
||||||
<td class="mailpoet_paragraph" style="word-break:break-word;word-wrap:break-word;$1">
|
|
||||||
$2
|
|
||||||
<br /><br />
|
|
||||||
</td>
|
|
||||||
</tr>
|
|
||||||
</table>'
|
|
||||||
, $html);
|
|
||||||
return $html;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
static function removeLastElementBreakLine($html) {
|
|
||||||
return preg_replace('/<br\/>([^<br\/>]*)$/s', '', $html);
|
|
||||||
}
|
|
||||||
|
|
||||||
static function addLineBreakAfterTags($html) {
|
|
||||||
return preg_replace('/(<\/(ul|ol|h\d)>)/', '$1<br />', $html);
|
|
||||||
}
|
|
||||||
|
|
||||||
static function convertBlockquotesToTables($html) {
|
static function convertBlockquotesToTables($html) {
|
||||||
$template = '
|
$DOM_parser = new \pQuery();
|
||||||
<table width="100%" cellpadding="0" cellspacing="0" border="0">
|
$DOM = $DOM_parser->parseStr($html);
|
||||||
|
$blockquotes = $DOM->query('blockquote');
|
||||||
|
if(!$blockquotes->count()) return $html;
|
||||||
|
foreach($blockquotes as $blockquote) {
|
||||||
|
$paragraphs = $blockquote->query('p', 0);
|
||||||
|
foreach($paragraphs as $index => $paragraph) {
|
||||||
|
$contents[] = $paragraph->html();
|
||||||
|
if($index + 1 < $paragraphs->count()) $contents[] = '<br />';
|
||||||
|
$paragraph->remove();
|
||||||
|
}
|
||||||
|
$paragraph->remove();
|
||||||
|
$blockquote->setTag('table');
|
||||||
|
$blockquote->addClass('mailpoet_blockquote');
|
||||||
|
$blockquote->width = '100%';
|
||||||
|
$blockquote->spacing = 0;
|
||||||
|
$blockquote->border = 0;
|
||||||
|
$blockquote->cellpadding = 0;
|
||||||
|
$blockquote->html('
|
||||||
<tbody>
|
<tbody>
|
||||||
<tr>
|
<tr>
|
||||||
<td width="2" bgcolor="#565656"></td>
|
<td width="2" bgcolor="#565656"></td>
|
||||||
@ -63,48 +46,90 @@ class Text {
|
|||||||
<table style="border-spacing:0;mso-table-lspace:0;mso-table-rspace:0">
|
<table style="border-spacing:0;mso-table-lspace:0;mso-table-rspace:0">
|
||||||
<tr>
|
<tr>
|
||||||
<td class="mailpoet_blockquote">
|
<td class="mailpoet_blockquote">
|
||||||
$1
|
' . implode('', $contents) . '
|
||||||
</td>
|
</td>
|
||||||
</tr>
|
</tr>
|
||||||
</table>
|
</table>
|
||||||
</td>
|
</td>
|
||||||
</tr>
|
</tr>
|
||||||
</tbody>
|
</tbody>'
|
||||||
</table>
|
|
||||||
<br/>';
|
|
||||||
preg_match('/<blockquote>.*?<\/blockquote>/s', $html, $blockquotes);
|
|
||||||
foreach($blockquotes as $index => $blockquote) {
|
|
||||||
$blockquote = preg_replace('/<\/p>\n<p>/', '<br/><br/>', $blockquote);
|
|
||||||
$blockquote = preg_replace('/<\/?p>/', '', $blockquote);
|
|
||||||
$blockquote = preg_replace(
|
|
||||||
'/<blockquote>(.*?)<\/blockquote>/s',
|
|
||||||
$template,
|
|
||||||
$blockquote
|
|
||||||
);
|
|
||||||
$html = preg_replace(
|
|
||||||
'/' . preg_quote($blockquotes[$index], '/') . '/',
|
|
||||||
$blockquote,
|
|
||||||
$html
|
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
return $html;
|
return $DOM->__toString();
|
||||||
}
|
}
|
||||||
|
|
||||||
static function styleHeadings($html) {
|
static function convertParagraphsToTables($html) {
|
||||||
return preg_replace(
|
$DOM_parser = new \pQuery();
|
||||||
'/<(h[1-6])(?:.+style=\"(.*)?\")?>/',
|
$DOM = $DOM_parser->parseStr($html);
|
||||||
'<$1 style="margin:0;font-style:normal;font-weight:normal;$2">',
|
$paragraphs = $DOM->query('p');
|
||||||
$html
|
if(!$paragraphs->count()) return $html;
|
||||||
|
foreach($paragraphs as $paragraph) {
|
||||||
|
// remove empty paragraphs
|
||||||
|
if(!trim($paragraph->html())) {
|
||||||
|
$paragraph->remove();
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
$style = $paragraph->style;
|
||||||
|
$contents = $paragraph->html();
|
||||||
|
$paragraph->setTag('table');
|
||||||
|
$paragraph->style = 'border-spacing:0;mso-table-lspace:0;mso-table-rspace:0;';
|
||||||
|
$paragraph->width = '100%';
|
||||||
|
$paragraph->cellpadding = 0;
|
||||||
|
$paragraph->html('
|
||||||
|
<tr>
|
||||||
|
<td class="mailpoet_paragraph" style="word-break:break-word;word-wrap:break-word;' . $style . '">
|
||||||
|
' . $contents . '
|
||||||
|
<br /><br />
|
||||||
|
</td>
|
||||||
|
</tr>'
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
return $DOM->__toString();
|
||||||
|
}
|
||||||
|
|
||||||
static function styleLists($html) {
|
static function styleLists($html) {
|
||||||
$html = preg_replace(
|
$DOM_parser = new \pQuery();
|
||||||
'/<(ul|ol)>/',
|
$DOM = $DOM_parser->parseStr($html);
|
||||||
'<$1 class="mailpoet_paragraph" style="padding-top:0;padding-bottom:0;margin-top:0;margin-bottom:0;">',
|
$lists = $DOM->query('ol, ul, li');
|
||||||
$html
|
if(!$lists->count()) return $html;
|
||||||
|
foreach($lists as $list) {
|
||||||
|
if($list->tag === 'li') {
|
||||||
|
$list->class = 'mailpoet_paragraph';
|
||||||
|
} else {
|
||||||
|
$list->class = 'mailpoet_paragraph';
|
||||||
|
$list->style .= 'padding-top:0;padding-bottom:0;margin-top:0;margin-bottom:0;';
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return $DOM->__toString();
|
||||||
|
}
|
||||||
|
|
||||||
|
static function styleHeadings($html) {
|
||||||
|
$DOM_parser = new \pQuery();
|
||||||
|
$DOM = $DOM_parser->parseStr($html);
|
||||||
|
$headings = $DOM->query('h1, h2, h3, h4');
|
||||||
|
if(!$headings->count()) return $html;
|
||||||
|
foreach($headings as $heading) {
|
||||||
|
$heading->style .= 'margin:0;font-style:normal;font-weight:normal;';
|
||||||
|
}
|
||||||
|
return $DOM->__toString();
|
||||||
|
}
|
||||||
|
|
||||||
|
static function addLineBreakAfterTags($html) {
|
||||||
|
$DOM_parser = new \pQuery();
|
||||||
|
$DOM = $DOM_parser->parseStr($html);
|
||||||
|
$tags = $DOM->query('ul, ol, h1, h2, h3, h4, table.mailpoet_blockquote');
|
||||||
|
if(!$tags->count()) return $html;
|
||||||
|
foreach($tags as $tag) {
|
||||||
|
$tag->parent->insertChild(
|
||||||
|
array(
|
||||||
|
'tag_name' => 'br',
|
||||||
|
'self_close' => true,
|
||||||
|
'attributes' => array()
|
||||||
|
),
|
||||||
|
$tag->index() + 1
|
||||||
);
|
);
|
||||||
$html = preg_replace('/<li>/', '<li class="mailpoet_paragraph">', $html);
|
}
|
||||||
return $html;
|
// remove last line break
|
||||||
|
return preg_replace('/(^)?(<br.*?\/?>)+$/i', '', $DOM->__toString());
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -69,7 +69,7 @@ class Renderer {
|
|||||||
$selector = 'h3';
|
$selector = 'h3';
|
||||||
break;
|
break;
|
||||||
case 'text':
|
case 'text':
|
||||||
$selector = '.mailpoet_paragraph, .mailpoet_blockquote';
|
$selector = '.mailpoet_paragraph, td.mailpoet_blockquote';
|
||||||
break;
|
break;
|
||||||
case 'body':
|
case 'body':
|
||||||
$selector = 'body, .mailpoet-wrapper';
|
$selector = 'body, .mailpoet-wrapper';
|
||||||
|
Reference in New Issue
Block a user