diff --git a/lib/Newsletter/Renderer/Blocks/Footer.php b/lib/Newsletter/Renderer/Blocks/Footer.php index 4147d4514d..cff57f1bf0 100644 --- a/lib/Newsletter/Renderer/Blocks/Footer.php +++ b/lib/Newsletter/Renderer/Blocks/Footer.php @@ -5,21 +5,23 @@ use MailPoet\Newsletter\Renderer\StylesHelper; class Footer { static function render($element) { - if(isset($element['styles']['link'])) { - $element['text'] = str_replace( - '
', $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 = ' - ' . $element['text'] . ' + ' . $DOM->html() . ' '; return $template; diff --git a/lib/Newsletter/Renderer/Blocks/Header.php b/lib/Newsletter/Renderer/Blocks/Header.php index 14ffbe06ff..57e4674dd3 100644 --- a/lib/Newsletter/Renderer/Blocks/Header.php +++ b/lib/Newsletter/Renderer/Blocks/Header.php @@ -5,21 +5,23 @@ use MailPoet\Newsletter\Renderer\StylesHelper; class Header { static function render($element) { - if(isset($element['styles']['link'])) { - $element['text'] = str_replace( - '
', $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 = ' - ' . $element['text'] . ' + ' . $DOM->html() . ' '; return $template; diff --git a/lib/Newsletter/Renderer/Blocks/Text.php b/lib/Newsletter/Renderer/Blocks/Text.php index 6fdf79c36b..23093ea9c4 100644 --- a/lib/Newsletter/Renderer/Blocks/Text.php +++ b/lib/Newsletter/Renderer/Blocks/Text.php @@ -6,10 +6,9 @@ class Text { $html = $element['text']; $html = self::convertBlockquotesToTables($html); $html = self::convertParagraphsToTables($html); - $html = self::addLineBreakAfterTags($html); $html = self::styleLists($html); $html = self::styleHeadings($html); - $html = self::removeLastElementBreakLine($html); + $html = self::addLineBreakAfterTags($html); $template = ' @@ -19,42 +18,26 @@ class Text { return $template; } - static function convertParagraphsToTables($html) { - $html = preg_replace('/

(.*?)<\/p>/', ' - - - - -
- $1 -

-
' - , $html); - $html = preg_replace('/

(.*?)<\/p>/', ' - - - - -
- $2 -

-
' - , $html); - return $html; - } - - - static function removeLastElementBreakLine($html) { - return preg_replace('/([^]*)$/s', '', $html); - } - - static function addLineBreakAfterTags($html) { - return preg_replace('/(<\/(ul|ol|h\d)>)/', '$1
', $html); - } - static function convertBlockquotesToTables($html) { - $template = ' - + $DOM_parser = new \pQuery(); + $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[] = '
'; + $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(' @@ -63,48 +46,90 @@ class Text {
- $1 + ' . implode('', $contents) . '
- - -
'; - preg_match('/

.*?<\/blockquote>/s', $html, $blockquotes); - foreach($blockquotes as $index => $blockquote) { - $blockquote = preg_replace('/<\/p>\n

/', '

', $blockquote); - $blockquote = preg_replace('/<\/?p>/', '', $blockquote); - $blockquote = preg_replace( - '/

(.*?)<\/blockquote>/s', - $template, - $blockquote - ); - $html = preg_replace( - '/' . preg_quote($blockquotes[$index], '/') . '/', - $blockquote, - $html + ' ); } - return $html; + return $DOM->__toString(); } - static function styleHeadings($html) { - return preg_replace( - '/<(h[1-6])(?:.+style=\"(.*)?\")?>/', - '<$1 style="margin:0;font-style:normal;font-weight:normal;$2">', - $html - ); + static function convertParagraphsToTables($html) { + $DOM_parser = new \pQuery(); + $DOM = $DOM_parser->parseStr($html); + $paragraphs = $DOM->query('p'); + 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(' + + + ' . $contents . ' +

+ + ' + ); + } + return $DOM->__toString(); } static function styleLists($html) { - $html = preg_replace( - '/<(ul|ol)>/', - '<$1 class="mailpoet_paragraph" style="padding-top:0;padding-bottom:0;margin-top:0;margin-bottom:0;">', - $html - ); - $html = preg_replace('/
  • /', '
  • ', $html); - return $html; + $DOM_parser = new \pQuery(); + $DOM = $DOM_parser->parseStr($html); + $lists = $DOM->query('ol, ul, li'); + 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 + ); + } + // remove last line break + return preg_replace('/(^)?()+$/i', '', $DOM->__toString()); } } \ No newline at end of file diff --git a/lib/Newsletter/Renderer/Renderer.php b/lib/Newsletter/Renderer/Renderer.php index 83487f4099..dde46a0f2e 100644 --- a/lib/Newsletter/Renderer/Renderer.php +++ b/lib/Newsletter/Renderer/Renderer.php @@ -69,7 +69,7 @@ class Renderer { $selector = 'h3'; break; case 'text': - $selector = '.mailpoet_paragraph, .mailpoet_blockquote'; + $selector = '.mailpoet_paragraph, td.mailpoet_blockquote'; break; case 'body': $selector = 'body, .mailpoet-wrapper';