A platform for community discussion. Free, open, simple.
FEATURE: support SCSS in custom email style
Neil Lalondef061aee818c5
diff --git a/app/assets/javascripts/admin/components/email-styles-editor.js.es6 b/app/assets/javascripts/admin/components/email-styles-editor.js.es6index 75811ec41c1..c0b6c732e98 100644--- a/app/assets/javascripts/admin/components/email-styles-editor.js.es6+++ b/app/assets/javascripts/admin/components/email-styles-editor.js.es6@@ -4,6 +4,11 @@ import computed from "ember-addons/ember-computed-decorators";export default Component.extend({editorId: Ember.computed.reads("fieldName"),+ @computed("fieldName")+ currentEditorMode(fieldName) {+ return fieldName === "css" ? "scss" : fieldName;+ },+@computed("fieldName", "styles.html", "styles.css")resetDisabled(fieldName) {return (diff --git a/app/assets/javascripts/admin/templates/components/email-styles-editor.hbs b/app/assets/javascripts/admin/templates/components/email-styles-editor.hbsindex 883149644d2..e03feb7cb7b 100644--- a/app/assets/javascripts/admin/templates/components/email-styles-editor.hbs+++ b/app/assets/javascripts/admin/templates/components/email-styles-editor.hbs@@ -9,7 +9,7 @@</div></div>-{{ace-editor content=editorContents mode=fieldName editorId=editorId}}+{{ace-editor content=editorContents mode=currentEditorMode editorId=editorId}}<div class='admin-footer'><div class='buttons'>diff --git a/app/models/email_style.rb b/app/models/email_style.rbindex 6d460ccbd19..d02de6a59a3 100644--- a/app/models/email_style.rb+++ b/app/models/email_style.rb@@ -17,6 +17,10 @@ class EmailStyleSiteSetting.email_custom_css || default_cssend+ def compiled_css+ SiteSetting.email_custom_css_compiled || self.class.default_css_compiled+ end+def default_htmlself.class.default_templateend@@ -34,4 +38,8 @@ class EmailStyledef self.default_css''end++ def self.default_css_compiled+ ''+ endenddiff --git a/app/services/email_style_updater.rb b/app/services/email_style_updater.rbindex d0fb9186365..d13e84b78d6 100644--- a/app/services/email_style_updater.rb+++ b/app/services/email_style_updater.rb@@ -10,26 +10,39 @@ class EmailStyleUpdaterenddef update(attrs)+ if attrs.has_key?(:html) && !attrs[:html].include?('%{email_content}')+ @errors << I18n.t(+ 'email_style.html_missing_placeholder',+ placeholder: '%{email_content}'+ )+ end++ if attrs.has_key?(:css)+ begin+ compiled_css = SassC::Engine.new(attrs[:css], style: :compressed).render+ rescue SassC::SyntaxError => e+ # @errors << I18n.t('email_style.css_syntax_error')+ @errors << e.message[0...(e.message.index("\n"))]+ end+ end++ return false unless @errors.empty?+if attrs.has_key?(:html)if attrs[:html] == EmailStyle.default_templateSiteSetting.remove_override!(:email_custom_template)else- if !attrs[:html].include?('%{email_content}')- @errors << I18n.t(- 'email_style.html_missing_placeholder',- placeholder: '%{email_content}'- )- else- SiteSetting.email_custom_template = attrs[:html]- end+ SiteSetting.email_custom_template = attrs[:html]endendif attrs.has_key?(:css)if attrs[:css] == EmailStyle.default_cssSiteSetting.remove_override!(:email_custom_css)+ SiteSetting.remove_override!(:email_custom_css_compiled)elseSiteSetting.email_custom_css = attrs[:css]+ SiteSetting.email_custom_css_compiled = compiled_cssendenddiff --git a/config/site_settings.yml b/config/site_settings.ymlindex d4ca816af7a..6a44f4b6099 100644--- a/config/site_settings.yml+++ b/config/site_settings.yml@@ -1044,6 +1044,9 @@ email:email_custom_css:default: ""hidden: true+ email_custom_css_compiled:+ default: ""+ hidden: trueemail_total_attachment_size_limit_kb:default: 0max: 51200diff --git a/lib/email/styles.rb b/lib/email/styles.rbindex b3ee7041037..9354f52666b 100644--- a/lib/email/styles.rb+++ b/lib/email/styles.rb@@ -36,7 +36,7 @@ module Emaildef custom_stylesreturn @custom_styles unless @custom_styles.nil?- css = EmailStyle.new.css+ css = EmailStyle.new.compiled_css@custom_styles = {}if !css.blank?diff --git a/spec/integration/email_style_spec.rb b/spec/integration/email_style_spec.rbindex e035aec1028..bfe08f16a33 100644--- a/spec/integration/email_style_spec.rb+++ b/spec/integration/email_style_spec.rb@@ -6,6 +6,7 @@ describe EmailStyle dobefore doSiteSetting.email_custom_template = "<body><h1>FOR YOU</h1><div>%{email_content}</div></body>"SiteSetting.email_custom_css = 'h1 { color: red; } div.body { color: #FAB; }'+ SiteSetting.email_custom_css_compiled = SiteSetting.email_custom_cssendafter do@@ -93,6 +94,7 @@ describe EmailStyle docontext 'with some bad css' dobefore doSiteSetting.email_custom_css = '@import "nope.css"; h1 {{{ size: really big; '+ SiteSetting.email_custom_css_compiled = SiteSetting.email_custom_cssendit "can render the html" dodiff --git a/spec/services/email_style_updater_spec.rb b/spec/services/email_style_updater_spec.rbindex 3ef6f1c7ea9..82281de7dd2 100644--- a/spec/services/email_style_updater_spec.rb+++ b/spec/services/email_style_updater_spec.rb@@ -7,6 +7,12 @@ describe EmailStyleUpdater dolet(:default_html) { File.read("#{Rails.root}/app/views/email/default_template.html") }let(:updater) { EmailStyleUpdater.new(admin) }+ def expect_settings_to_be_unset+ expect(SiteSetting.email_custom_template).to_not be_present+ expect(SiteSetting.email_custom_css).to_not be_present+ expect(SiteSetting.email_custom_css_compiled).to_not be_present+ end+describe 'update' doit 'can change the settings' doexpect(@@ -17,20 +23,20 @@ describe EmailStyleUpdater do).to eq(true)expect(SiteSetting.email_custom_template).to eq('For you: %{email_content}')expect(SiteSetting.email_custom_css).to eq('h1 { color: blue; }')+ expect(SiteSetting.email_custom_css_compiled.strip).to eq('h1{color:blue}')endit 'will not store defaults' doupdater.update(html: default_html, css: '')- expect(SiteSetting.email_custom_template).to_not be_present- expect(SiteSetting.email_custom_css).to_not be_present+ expect_settings_to_be_unsetendit 'can clear settings if defaults given' doSiteSetting.email_custom_template = 'For you: %{email_content}'SiteSetting.email_custom_css = 'h1 { color: blue; }'+ SiteSetting.email_custom_css_compiled = 'h1{color:blue}'updater.update(html: default_html, css: '')- expect(SiteSetting.email_custom_template).to_not be_present- expect(SiteSetting.email_custom_css).to_not be_present+ expect_settings_to_be_unsetendit 'fails if html is missing email_content' do@@ -41,6 +47,14 @@ describe EmailStyleUpdater doplaceholder: '%{email_content}'))+ expect_settings_to_be_unset+ end++ it 'fails if css is not valid scss' do+ expect(updater.update(html: 'For you: %{email_content}', css: 'h1 { color: blue;')).to eq(false)+ expect(updater.errors).to_not be_empty+ expect(updater.errors.first).to include('Invalid CSS after')+ expect_settings_to_be_unsetendendend