- Enables dynamic line-height based on font-size

This commit is contained in:
Vlad
2016-04-08 15:16:11 -04:00
parent 813db1ae33
commit ba40437eb9
5 changed files with 26 additions and 11 deletions

View File

@ -7,6 +7,9 @@ class Footer {
static function render($element) { static function render($element) {
$element['text'] = preg_replace('/\n/', '<br /><br />', $element['text']); $element['text'] = preg_replace('/\n/', '<br /><br />', $element['text']);
$element['text'] = preg_replace('/(<\/?p.*?>)/i', '', $element['text']); $element['text'] = preg_replace('/(<\/?p.*?>)/i', '', $element['text']);
$line_height = sprintf(
'%spx', StylesHelper::$line_height_multiplier * (int) $element['styles']['text']['fontSize']
);
$DOM_parser = new \pQuery(); $DOM_parser = new \pQuery();
$DOM = $DOM_parser->parseStr($element['text']); $DOM = $DOM_parser->parseStr($element['text']);
if(isset($element['styles']['link'])) { if(isset($element['styles']['link'])) {
@ -25,7 +28,7 @@ class Footer {
$template = ' $template = '
<tr> <tr>
<td class="mailpoet_header_footer_padded mailpoet_footer" ' . $background_color . ' <td class="mailpoet_header_footer_padded mailpoet_footer" ' . $background_color . '
style="line-height: ' . StylesHelper::$line_height . ';' . StylesHelper::getBlockStyles($element) . StylesHelper::getStyles($element['styles'], 'text') . '"> style="line-height: ' . $line_height . ';' . StylesHelper::getBlockStyles($element) . StylesHelper::getStyles($element['styles'], 'text') . '">
' . $DOM->html() . ' ' . $DOM->html() . '
</td> </td>
</tr>'; </tr>';

View File

@ -6,7 +6,10 @@ use MailPoet\Newsletter\Renderer\StylesHelper;
class Header { class Header {
static function render($element) { static function render($element) {
$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']);
$line_height = sprintf(
'%spx', StylesHelper::$line_height_multiplier * (int) $element['styles']['text']['fontSize']
);
$DOM_parser = new \pQuery(); $DOM_parser = new \pQuery();
$DOM = $DOM_parser->parseStr($element['text']); $DOM = $DOM_parser->parseStr($element['text']);
if(isset($element['styles']['link'])) { if(isset($element['styles']['link'])) {
@ -25,8 +28,8 @@ class Header {
$template = ' $template = '
<tr> <tr>
<td class="mailpoet_header_footer_padded mailpoet_header" ' . $background_color . ' <td class="mailpoet_header_footer_padded mailpoet_header" ' . $background_color . '
style="' . StylesHelper::getBlockStyles($element) . StylesHelper::getStyles($element['styles'], 'text') . '"> style="line-height: ' . $line_height . ';' . StylesHelper::getBlockStyles($element) . StylesHelper::getStyles($element['styles'], 'text') . '">
' . $DOM->html() . ' ' . $DOM->html() . '
</td> </td>
</tr>'; </tr>';
return $template; return $template;

View File

@ -95,7 +95,7 @@ class Text {
$line_breaks = ($next_element && preg_match('/<li>/i', $next_element->getInnerText())) ? '<br />' : $line_breaks; $line_breaks = ($next_element && preg_match('/<li>/i', $next_element->getInnerText())) ? '<br />' : $line_breaks;
$paragraph->html(' $paragraph->html('
<tr> <tr>
<td class="mailpoet_paragraph" style="line-height:' . StylesHelper::$line_height . ';word-break:break-word;word-wrap:break-word;' . $style . '"> <td class="mailpoet_paragraph" style="word-break:break-word;word-wrap:break-word;' . $style . '">
' . $contents . $line_breaks . ' ' . $contents . $line_breaks . '
</td> </td>
</tr>' </tr>'
@ -117,7 +117,7 @@ class Text {
$list->class = 'mailpoet_paragraph'; $list->class = 'mailpoet_paragraph';
$list->style .= 'padding-top:0;padding-bottom:0;margin-top:10px;'; $list->style .= 'padding-top:0;padding-bottom:0;margin-top:10px;';
} }
$list->style .= 'margin-bottom:10px;line-height:' . StylesHelper::$line_height . ';'; $list->style .= 'margin-bottom:10px;';
} }
return $DOM->__toString(); return $DOM->__toString();
} }
@ -128,7 +128,7 @@ class Text {
$headings = $DOM->query('h1, h2, h3, h4'); $headings = $DOM->query('h1, h2, h3, h4');
if(!$headings->count()) return $html; if(!$headings->count()) return $html;
foreach($headings as $heading) { foreach($headings as $heading) {
$heading->style .= 'line-height:' . StylesHelper::$line_height . ';padding:0;font-style:normal;font-weight:normal;'; $heading->style .= 'padding:0;font-style:normal;font-weight:normal;';
} }
return $DOM->__toString(); return $DOM->__toString();
} }

View File

@ -27,7 +27,8 @@ class StylesHelper {
'Trebuchet MS' => "'Trebuchet MS', 'Lucida Grande', 'Lucida Sans Unicode', 'Lucida Sans', Tahoma, sans-serif", 'Trebuchet MS' => "'Trebuchet MS', 'Lucida Grande', 'Lucida Sans Unicode', 'Lucida Sans', Tahoma, sans-serif",
'Verdana' => 'Verdana, Geneva, sans-serif' 'Verdana' => 'Verdana, Geneva, sans-serif'
); );
static $line_height = 1.61803398875; static $line_height_multiplier = 1.6;
static $heading_margin_multiplier = 0.3;
static $padding_width = 20; static $padding_width = 20;
static function getBlockStyles($element, $ignore_specific_styles = false) { static function getBlockStyles($element, $ignore_specific_styles = false) {
@ -55,6 +56,7 @@ class StylesHelper {
static function setStyle($style, $selector) { static function setStyle($style, $selector) {
$css = $selector . '{' . PHP_EOL; $css = $selector . '{' . PHP_EOL;
$style = self::applyHeadingMargin($style, $selector); $style = self::applyHeadingMargin($style, $selector);
$style = self::applyLineHeight($style, $selector);
foreach($style as $attribute => $individual_style) { foreach($style as $attribute => $individual_style) {
$individual_style = self::applyFontFamily($attribute, $individual_style); $individual_style = self::applyFontFamily($attribute, $individual_style);
$css .= self::translateCSSAttribute($attribute) . ':' . $individual_style . ';' . PHP_EOL; $css .= self::translateCSSAttribute($attribute) . ':' . $individual_style . ';' . PHP_EOL;
@ -87,9 +89,16 @@ class StylesHelper {
} }
static function applyHeadingMargin($style, $selector) { static function applyHeadingMargin($style, $selector) {
if (!preg_match('/h[1|2|3|4]/i', $selector)) return $style; if (!preg_match('/h[1-4]/i', $selector)) return $style;
$font_size = (int) $style['fontSize']; $font_size = (int) $style['fontSize'];
$style['margin'] = sprintf('0 0 %spx 0', 0.3 * $font_size); $style['margin'] = sprintf('0 0 %spx 0', self::$heading_margin_multiplier * $font_size);
return $style;
}
static function applyLineHeight($style, $selector) {
if (!preg_match('/mailpoet_paragraph|h[1-4]/i', $selector)) return $style;
$font_size = (int) $style['fontSize'];
$style['lineHeight'] = sprintf('%spx', self::$line_height_multiplier * $font_size);
return $style; return $style;
} }
} }

View File

@ -210,7 +210,7 @@ class NewsletterRendererTest extends MailPoetTest {
->contains('padding-top:0;padding-bottom:0;margin-top:10px;margin-bottom:10px;'); ->contains('padding-top:0;padding-bottom:0;margin-top:10px;margin-bottom:10px;');
// headings should be styled // headings should be styled
expect($DOM('tr > td.mailpoet_text > h1', 0)->attr('style')) expect($DOM('tr > td.mailpoet_text > h1', 0)->attr('style'))
->contains('margin:0 0 20px;padding:0;font-style:normal;font-weight:normal;'); ->contains('padding:0;font-style:normal;font-weight:normal;');
} }
function testItRendersDivider() { function testItRendersDivider() {