3
3
* Copyright © Magento, Inc. All rights reserved.
4
4
* See COPYING.txt for license details.
5
5
*/
6
+ declare (strict_types=1 );
7
+
6
8
namespace Magento \Email \Model \Template ;
7
9
8
10
use Exception ;
@@ -904,19 +906,17 @@ public function cssDirective($construction)
904
906
return '/* ' . __ ('"file" parameter must be specified ' ) . ' */ ' ;
905
907
}
906
908
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
+ }
910
914
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 )){
918
916
return '/* ' . __ ('Contents of the specified CSS file could not be loaded or is empty ' ) . ' */ ' ;
919
917
}
918
+
919
+ return $ css ;
920
920
}
921
921
922
922
/**
@@ -991,6 +991,7 @@ protected function getInlineCssFiles()
991
991
* @param [] $files
992
992
* @return string
993
993
* @throws MailException
994
+ * @throws ContentProcessorException
994
995
*/
995
996
public function getCssFilesContent (array $ files )
996
997
{
@@ -1014,10 +1015,6 @@ public function getCssFilesContent(array $files)
1014
1015
$ css .= $ asset ->getContent ();
1015
1016
}
1016
1017
}
1017
- } catch (ContentProcessorException $ exception ) {
1018
- throw new MailException (
1019
- __ ($ exception ->getMessage ())
1020
- );
1021
1018
} catch (NotFoundException $ exception ) {
1022
1019
$ css = '' ;
1023
1020
}
@@ -1037,39 +1034,51 @@ public function getCssFilesContent(array $files)
1037
1034
*/
1038
1035
public function applyInlineCss ($ html )
1039
1036
{
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
+
1044
1044
$ cssToInline = $ this ->cssProcessor ->process ($ cssToInline );
1045
1045
1046
1046
// 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
+ }
1063
1050
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 ) );
1068
1055
}
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 ;
1071
1080
}
1072
- return $ processedHtml ;
1081
+ return $ html ;
1073
1082
}
1074
1083
1075
1084
/**
0 commit comments