A platform for community discussion. Free, open, simple.
DEV: deduplicate inline styles in emails (#30015)
Régis Hanol7d5879375986
diff --git a/lib/email/styles.rb b/lib/email/styles.rbindex 48b051cd88a..c378d0d7a05 100644--- a/lib/email/styles.rb+++ b/lib/email/styles.rb@@ -374,12 +374,31 @@ module Emailendend+ def deduplicate_style(style)+ styles = {}++ style+ .split(";")+ .select(&:present?)+ .map { _1.split(":", 2).map(&:strip) }+ .each { |k, v| styles[k] = v if k.present? && v.present? }++ styles.map { |k, v| "#{k}:#{v}" }.join(";")+ end++ def deduplicate_styles+ @fragment+ .css("[style]")+ .each { |element| element["style"] = deduplicate_style element["style"] }+ end+def to_html# needs to be before class + id strip because we need to style redacted# media and also not double-redact already redacted from lower levelsreplace_secure_uploads_urls if SiteSetting.secure_uploads?strip_classes_and_idsreplace_relative_urls+ deduplicate_styles@fragment.to_htmlenddiff --git a/spec/integration/email_style_spec.rb b/spec/integration/email_style_spec.rbindex 49fff4d3930..b0a2d9cafee 100644--- a/spec/integration/email_style_spec.rb+++ b/spec/integration/email_style_spec.rb@@ -30,13 +30,13 @@ RSpec.describe EmailStyle dolet(:invite_mail) { InviteMailer.send_invite(invite) }it "applies customizations" do- expect(mail_html.scan('<h1 style="color: red;">FOR YOU</h1>').count).to eq(1)+ expect(mail_html.scan('<h1 style="color:red">FOR YOU</h1>').count).to eq(1)expect(mail_html).to match("#{Discourse.base_url}/invites/#{invite.invite_key}")endit "applies customizations if compiled is missing" doSiteSetting.remove_override!(:email_custom_css_compiled)- expect(mail_html.scan('<h1 style="color: red;">FOR YOU</h1>').count).to eq(1)+ expect(mail_html.scan('<h1 style="color:red">FOR YOU</h1>').count).to eq(1)expect(mail_html).to match("#{Discourse.base_url}/invites/#{invite.invite_key}")end@@ -71,10 +71,10 @@ RSpec.describe EmailStyle doit "customizations are applied to html part of emails" doSiteSetting.default_email_in_reply_to = true- expect(mail_html.scan('<h1 style="color: red;">FOR YOU</h1>').count).to eq(1)+ expect(mail_html.scan('<h1 style="color:red">FOR YOU</h1>').count).to eq(1)matches = mail_html.match(/<div style="([^"]+)" dm=\"body\">#{post.raw}/)- expect(matches[1]).to include("color: #FAB;") # custom- expect(matches[1]).to include("padding-top:5px;") # div.body+ expect(matches[1]).to include("color:#FAB") # custom+ expect(matches[1]).to include("padding-top:5px") # div.bodyend# TODO: translation override@@ -86,7 +86,7 @@ RSpec.describe EmailStyle dolet(:signup_mail) { UserNotifications.signup(Fabricate(:user)) }it "customizations are applied to html part of emails" do- expect(mail_html.scan('<h1 style="color: red;">FOR YOU</h1>').count).to eq(1)+ expect(mail_html.scan('<h1 style="color:red">FOR YOU</h1>').count).to eq(1)expect(mail_html).to include("activate-account")end@@ -107,7 +107,7 @@ RSpec.describe EmailStyle doendit "applies customizations when translation override exists" do- expect(mail_html.scan('<h1 style="color: red;">FOR YOU</h1>').count).to eq(1)+ expect(mail_html.scan('<h1 style="color:red">FOR YOU</h1>').count).to eq(1)expect(mail_html.scan("CLICK THAT LINK").count).to eq(1)endend@@ -135,13 +135,13 @@ RSpec.describe EmailStyle dolet(:summary_email) { UserNotifications.digest(Fabricate(:user)) }it "customizations are applied to html part of emails" do- expect(mail_html.scan('<h1 style="color: red;">FOR YOU</h1>').count).to eq(1)+ expect(mail_html.scan('<h1 style="color:red">FOR YOU</h1>').count).to eq(1)expect(mail_html).to include(popular_topic.title)endit "doesn't apply customizations if apply_custom_styles_to_digest is disabled" doSiteSetting.apply_custom_styles_to_digest = false- expect(mail_html).to_not include('<h1 style="color: red;">FOR YOU</h1>')+ expect(mail_html).to_not include('<h1 style="color:red">FOR YOU</h1>')expect(mail_html).to_not include("FOR YOU")expect(mail_html).to include(popular_topic.title)enddiff --git a/spec/lib/email/sender_spec.rb b/spec/lib/email/sender_spec.rbindex 918ec03c524..aacfb3eeab9 100644--- a/spec/lib/email/sender_spec.rb+++ b/spec/lib/email/sender_spec.rb@@ -591,7 +591,7 @@ RSpec.describe Email::Sender doreply.rebake!Email::Sender.new(message, :valid_type).sendexpected = <<~HTML- <a href=\"#{Discourse.base_url}#{category.url}\" data-type=\"category\" data-slug=\"dev\" data-id=\"#{category.id}\" style=\"text-decoration: none; font-weight: bold; color: #006699;\"><span>#dev</span>+ <a href=\"#{Discourse.base_url}#{category.url}\" data-type=\"category\" data-slug=\"dev\" data-id=\"#{category.id}\" style=\"text-decoration:none;font-weight:bold;color:#006699\"><span>#dev</span>HTMLexpect(message.html_part.body.to_s).to include(expected.chomp)enddiff --git a/spec/lib/email/styles_spec.rb b/spec/lib/email/styles_spec.rbindex e7822da2cf9..eec3491c555 100644--- a/spec/lib/email/styles_spec.rb+++ b/spec/lib/email/styles_spec.rb@@ -168,6 +168,25 @@ RSpec.describe Email::Styles doendend+ describe "deduplicate styles" do+ it "removes double definitions" do+ frag = "<test style='color:green;color:red'>hello</test>"++ styler = Email::Styles.new(frag)+ styled = styler.to_html+ styled = Nokogiri::HTML5.fragment(styled)+ expect(styled.at("test")["style"]).to eq("color:red")+ end+ it "handles whitespace correctly" do+ frag =+ "<test style=' color : green ; ; ; color : red; background:white; background:yellow '>hello</test>"+ styler = Email::Styles.new(frag)+ styled = styler.to_html+ styled = Nokogiri::HTML5.fragment(styled)+ expect(styled.at("test")["style"]).to eq("color:red;background:yellow")+ end+ end+describe "dark mode emails" doit "adds dark_mode_styles when site setting active" dofrag = html_fragment('<div class="body">test</div>')