Skip to content

Commit 1668e90

Browse files
committed
#31342: Handled ContentProcessorException in backward compatible way
1 parent 7f21ad7 commit 1668e90

File tree

2 files changed

+53
-44
lines changed
  • app/code/Magento/Email/Model/Template
  • dev/tests/integration/testsuite/Magento/Email/Model/Template

2 files changed

+53
-44
lines changed

app/code/Magento/Email/Model/Template/Filter.php

Lines changed: 50 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
* Copyright © Magento, Inc. All rights reserved.
44
* See COPYING.txt for license details.
55
*/
6+
declare(strict_types=1);
7+
68
namespace Magento\Email\Model\Template;
79

810
use Exception;
@@ -904,19 +906,17 @@ public function cssDirective($construction)
904906
return '/* ' . __('"file" parameter must be specified') . ' */';
905907
}
906908

907-
$css = $this->cssProcessor->process(
908-
$this->getCssFilesContent([$params['file']])
909-
);
909+
try {
910+
$css = $this->cssProcessor->process($this->getCssFilesContent([$params['file']]));
911+
} catch (ContentProcessorException $exception) {
912+
return '/*' . PHP_EOL . $exception->getMessage() . PHP_EOL . '*/';
913+
}
910914

911-
if (strpos($css, ContentProcessorInterface::ERROR_MESSAGE_PREFIX) !== false) {
912-
// Return compilation error wrapped in CSS comment
913-
return '/*' . PHP_EOL . $css . PHP_EOL . '*/';
914-
} elseif (!empty($css)) {
915-
return $css;
916-
} else {
917-
// Return CSS comment for debugging purposes
915+
if (empty($css)){
918916
return '/* ' . __('Contents of the specified CSS file could not be loaded or is empty') . ' */';
919917
}
918+
919+
return $css;
920920
}
921921

922922
/**
@@ -991,6 +991,7 @@ protected function getInlineCssFiles()
991991
* @param [] $files
992992
* @return string
993993
* @throws MailException
994+
* @throws ContentProcessorException
994995
*/
995996
public function getCssFilesContent(array $files)
996997
{
@@ -1014,10 +1015,6 @@ public function getCssFilesContent(array $files)
10141015
$css .= $asset->getContent();
10151016
}
10161017
}
1017-
} catch (ContentProcessorException $exception) {
1018-
throw new MailException(
1019-
__($exception->getMessage())
1020-
);
10211018
} catch (NotFoundException $exception) {
10221019
$css = '';
10231020
}
@@ -1037,39 +1034,51 @@ public function getCssFilesContent(array $files)
10371034
*/
10381035
public function applyInlineCss($html)
10391036
{
1040-
// Check to see if the {{inlinecss file=""}} directive set CSS file(s) to inline and then load those files
1041-
$cssToInline = $this->getCssFilesContent(
1042-
$this->getInlineCssFiles()
1043-
);
1037+
try {
1038+
// Check to see if the {{inlinecss file=""}} directive set CSS file(s) to inline and then load those files
1039+
$cssToInline = $this->getCssFilesContent($this->getInlineCssFiles());
1040+
} catch (ContentProcessorException $exception) {
1041+
return $this->getExceptionHtml($html, $exception);
1042+
}
1043+
10441044
$cssToInline = $this->cssProcessor->process($cssToInline);
10451045

10461046
// Only run Emogrify if HTML and CSS contain content
1047-
if ($html && $cssToInline) {
1048-
try {
1049-
// Don't try to compile CSS that has compilation errors
1050-
if (strpos($cssToInline, ContentProcessorInterface::ERROR_MESSAGE_PREFIX)
1051-
!== false
1052-
) {
1053-
throw new MailException(
1054-
__('<pre> %1 </pre>', PHP_EOL . $cssToInline . PHP_EOL)
1055-
);
1056-
}
1057-
$this->cssInliner->setHtml($html);
1058-
1059-
$this->cssInliner->setCss($cssToInline);
1060-
1061-
// Don't parse inline <style> tags, since existing tag is intentionally for non-inline styles
1062-
$this->cssInliner->disableStyleBlocksParsing();
1047+
if (!$html || !$cssToInline) {
1048+
return $html;
1049+
}
10631050

1064-
$processedHtml = $this->cssInliner->process();
1065-
} catch (Exception $e) {
1066-
$processedHtml = $html;
1067-
$this->_logger->error($e);
1051+
try {
1052+
// Don't try to compile CSS that has compilation errors
1053+
if (strpos($cssToInline, ContentProcessorInterface::ERROR_MESSAGE_PREFIX) !== false) {
1054+
throw new MailException(__('<pre> %1 </pre>', PHP_EOL . $cssToInline . PHP_EOL));
10681055
}
1069-
} else {
1070-
$processedHtml = $html;
1056+
$this->cssInliner->setHtml($html);
1057+
$this->cssInliner->setCss($cssToInline);
1058+
// Don't parse inline <style> tags, since existing tag is intentionally for non-inline styles
1059+
$this->cssInliner->disableStyleBlocksParsing();
1060+
return $this->cssInliner->process();
1061+
} catch (Exception $exception) {
1062+
return $this->getExceptionHtml($html, $exception);
1063+
}
1064+
}
1065+
1066+
/**
1067+
* Handle css inlining exception, log it, add to the content in developer mode
1068+
*
1069+
* @param string $html
1070+
* @param Exception $exception
1071+
* @return string
1072+
*/
1073+
private function getExceptionHtml(string $html, Exception $exception): string
1074+
{
1075+
$this->_logger->error($exception);
1076+
if ($this->_appState->getMode() == \Magento\Framework\App\State::MODE_DEVELOPER) {
1077+
return __('CSS inlining error:') . PHP_EOL . $exception->getMessage()
1078+
. PHP_EOL
1079+
. $html;
10711080
}
1072-
return $processedHtml;
1081+
return $html;
10731082
}
10741083

10751084
/**

dev/tests/integration/testsuite/Magento/Email/Model/Template/FilterTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -400,7 +400,7 @@ public function inlinecssDirectiveDataProvider()
400400
],
401401
'Non-existent file results in unmodified markup' => [
402402
'<html><p></p> {{inlinecss file="css/non-existent-file.css"}}</html>',
403-
'Compilation from source',
403+
'<html><p></p> </html>',
404404
],
405405
'Plain template mode results in unmodified markup' => [
406406
'<html><p></p> {{inlinecss file="css/email-inline-1.css"}}</html>',
@@ -417,12 +417,12 @@ public function inlinecssDirectiveDataProvider()
417417
],
418418
'Production mode - File with compilation error results in structurally unmodified markup' => [
419419
'<html><p></p> {{inlinecss file="css/file-with-error.css"}}</html>',
420-
'We\'re sorry, an error has occurred while generating this content',
420+
'<p></p>',
421421
true,
422422
],
423423
'Developer mode - File with compilation error results in error message' => [
424424
'<html><p></p> {{inlinecss file="css/file-with-error.css"}}</html>',
425-
'Error filtering template',
425+
'CSS inlining error:',
426426
false,
427427
],
428428
];

0 commit comments

Comments
 (0)