A platform for community discussion. Free, open, simple.
FIX: Prioritize !important CSS in emails (#32061)
David Taylor3f353a726bdb
diff --git a/lib/email/styles.rb b/lib/email/styles.rbindex c378d0d7a05..8130c41773c 100644--- a/lib/email/styles.rb+++ b/lib/email/styles.rb@@ -381,7 +381,11 @@ module Email.split(";").select(&:present?).map { _1.split(":", 2).map(&:strip) }- .each { |k, v| styles[k] = v if k.present? && v.present? }+ .each do |k, v|+ next if k.blank? || v.blank?+ next if styles[k]&.end_with?("!important") && !v.end_with?("!important")+ styles[k] = v+ endstyles.map { |k, v| "#{k}:#{v}" }.join(";")enddiff --git a/spec/lib/email/styles_spec.rb b/spec/lib/email/styles_spec.rbindex eec3491c555..03bcbed06c5 100644--- a/spec/lib/email/styles_spec.rb+++ b/spec/lib/email/styles_spec.rb@@ -177,6 +177,7 @@ RSpec.describe Email::Styles dostyled = Nokogiri::HTML5.fragment(styled)expect(styled.at("test")["style"]).to eq("color:red")end+it "handles whitespace correctly" dofrag ="<test style=' color : green ; ; ; color : red; background:white; background:yellow '>hello</test>"@@ -185,6 +186,15 @@ RSpec.describe Email::Styles dostyled = Nokogiri::HTML5.fragment(styled)expect(styled.at("test")["style"]).to eq("color:red;background:yellow")end++ it "respects !important" do+ frag = "<test style='color:yellow !important;color:green !important;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:green !important")+ endenddescribe "dark mode emails" do