Commit b1b59fe3 authored by Rich's avatar Rich
Browse files

Improve handling of Smarty {literal} insertion to guard style

Previously, inserting literal was handled if CIVICRM_MAIL_SMARTY was
set, which is irrelevant to non-CiviMail mailings.

Also, it previously only supported HTML5 style (no type attribute).

Now it wraps style in literal tags and wraps those in HTML comments, so
if smarty doesn't process it, it still works.
parent 5a6fe6d7
......@@ -99,16 +99,21 @@ function civicrm_api3_job_mosaico_msg_sync($params) {
* Template HTML, as appropriate for MessageTemplates.
function _civicrm_api3_job_mosaico_msg_filter($html) {
// keep head section in literal to avoid smarty errors. Specially when CIVICRM_MAIL_SMARTY is turned on.
$html = str_ireplace(array('<head>', '</head>'),
array('{literal}<head>', '</head>{/literal}'), $html);
elseif (defined('CIVICRM_MAIL_SMARTY') && CIVICRM_MAIL_SMARTY == 0) {
// get rid of any injected literal tags to avoid them appearing in emails
$html = str_ireplace(array('{literal}<head>', '</head>{/literal}'),
array('<head>', '</head>'), $html);
// We never want Smarty parsing CSS blocks in <style> elements, so we wrap them in {literal} tags.
// We don't want {literal} coming through *literally* in the case that Smarty
// is not running (e.g. CIVICRM_MAIL_SMARTY is 0 and this is a CiviMail
// mailing) so we wrap those in HTML comments.
$html = preg_replace_callback('@<(/)?style(?:\s[^>]+)?>@i', function($matches) {
if (empty($matches[1])) {
// Opening <style> element
return "<!--{literal}-->$matches[0]";
else {
// Closing element.
return "$matches[0]<!--{/literal}-->";
}, $html);
$html_filter = new CRM_Mosaico_UrlFilter();
return $html_filter->filterHtml($html);
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment