Get rid of double html parsing during render

[MAILPOET-1797]
This commit is contained in:
Rostislav Wolny
2019-03-05 10:40:41 +01:00
committed by M. Shull
parent 59d960b42f
commit 66ae3edf78
3 changed files with 8 additions and 12 deletions

View File

@ -11,7 +11,6 @@ if (!defined('ABSPATH')) exit;
class Renderer { class Renderer {
public $blocks_renderer; public $blocks_renderer;
public $columns_renderer; public $columns_renderer;
public $DOM_parser;
public $CSS_inliner; public $CSS_inliner;
public $newsletter; public $newsletter;
public $preview; public $preview;
@ -26,7 +25,6 @@ class Renderer {
$this->preview = $preview; $this->preview = $preview;
$this->blocks_renderer = new Blocks\Renderer($this->newsletter); $this->blocks_renderer = new Blocks\Renderer($this->newsletter);
$this->columns_renderer = new Columns\Renderer(); $this->columns_renderer = new Columns\Renderer();
$this->DOM_parser = new pQuery();
$this->CSS_inliner = new \MailPoet\Util\CSS(); $this->CSS_inliner = new \MailPoet\Util\CSS();
$this->template = file_get_contents(dirname(__FILE__) . '/' . self::NEWSLETTER_TEMPLATE); $this->template = file_get_contents(dirname(__FILE__) . '/' . self::NEWSLETTER_TEMPLATE);
$this->premium_activated = License::getLicense(); $this->premium_activated = License::getLicense();
@ -62,8 +60,8 @@ class Renderer {
$newsletter['preheader'], $newsletter['preheader'],
$rendered_body $rendered_body
)); ));
$template = $this->inlineCSSStyles($template); $template_dom = $this->inlineCSSStyles($template);
$template = $this->postProcessTemplate($template); $template = $this->postProcessTemplate($template_dom);
$rendered_newsletter = array( $rendered_newsletter = array(
'html' => $template, 'html' => $template,
@ -150,8 +148,7 @@ class Renderer {
return @\Html2Text\Html2Text::convert($template); return @\Html2Text\Html2Text::convert($template);
} }
private function postProcessTemplate($template) { private function postProcessTemplate($DOM) {
$DOM = $this->DOM_parser->parseStr($template);
// replace spaces in image tag URLs // replace spaces in image tag URLs
foreach ($DOM->query('img') as $image) { foreach ($DOM->query('img') as $image) {
$image->src = str_replace(' ', '%20', $image->src); $image->src = str_replace(' ', '%20', $image->src);

View File

@ -35,7 +35,6 @@ class CSS {
*/ */
function inlineCSS($url, $contents=null) { function inlineCSS($url, $contents=null) {
$html = \pQuery::parseStr($contents); $html = \pQuery::parseStr($contents);
if (!is_object($html)) { if (!is_object($html)) {
return false; return false;
} }
@ -124,7 +123,7 @@ class CSS {
} }
// Let simple_html_dom give us back our HTML with inline CSS! // Let simple_html_dom give us back our HTML with inline CSS!
return (string)$html; return $html;
} }
function parseCSS($text) { function parseCSS($text) {

View File

@ -41,7 +41,7 @@ class CSSTest extends \MailPoetUnitTest {
$styles = 'p { color: red; }'; $styles = 'p { color: red; }';
$content = '<p>Foo</p>'; $content = '<p>Foo</p>';
$html = $this->buildHtml($styles, $content); $html = $this->buildHtml($styles, $content);
$result_html = $this->css->inlineCSS(null, $html); $result_html = (string)$this->css->inlineCSS(null, $html);
$this->assertContains('<p style="color:red">', $result_html); $this->assertContains('<p style="color:red">', $result_html);
} }
@ -49,7 +49,7 @@ class CSSTest extends \MailPoetUnitTest {
$styles = 'p { color: red; } .blue { color: blue; }'; $styles = 'p { color: red; } .blue { color: blue; }';
$content = '<p class="blue">Foo</p>'; $content = '<p class="blue">Foo</p>';
$html = $this->buildHtml($styles, $content); $html = $this->buildHtml($styles, $content);
$result_html = $this->css->inlineCSS(null, $html); $result_html = (string)$this->css->inlineCSS(null, $html);
$this->assertContains('<p class="blue" style="color:blue">', $result_html); $this->assertContains('<p class="blue" style="color:blue">', $result_html);
} }
@ -57,7 +57,7 @@ class CSSTest extends \MailPoetUnitTest {
$styles = 'p { color: red; }'; $styles = 'p { color: red; }';
$content = '<p style="color:green">Foo</p>'; $content = '<p style="color:green">Foo</p>';
$html = $this->buildHtml($styles, $content); $html = $this->buildHtml($styles, $content);
$result_html = $this->css->inlineCSS(null, $html); $result_html = (string)$this->css->inlineCSS(null, $html);
$this->assertContains('<p style="color:green">', $result_html); $this->assertContains('<p style="color:green">', $result_html);
} }
@ -65,7 +65,7 @@ class CSSTest extends \MailPoetUnitTest {
$styles = 'p { color: red !important; }'; $styles = 'p { color: red !important; }';
$content = '<p style="color:green !important">Foo</p>'; $content = '<p style="color:green !important">Foo</p>';
$html = $this->buildHtml($styles, $content); $html = $this->buildHtml($styles, $content);
$result_html = $this->css->inlineCSS(null, $html); $result_html = (string)$this->css->inlineCSS(null, $html);
$this->assertContains('<p style="color:red">', $result_html); $this->assertContains('<p style="color:red">', $result_html);
} }