Skip to content

A platform for community discussion. Free, open, simple.

FIX: Skip unsupported email CSS selectors (#44806)

Samd4c675a92e25
diff --git a/lib/email/styles.rb b/lib/email/styles.rb
index 6ee6e086ff6..5c82f763d2d 100644
--- a/lib/email/styles.rb
+++ b/lib/email/styles.rb
@@ -351,7 +351,12 @@ module Email
end
def format_custom
- custom_styles.each { |selector, value| style(selector, value) }
+ custom_styles.each do |selector, value|
+ style(selector, value)
+ rescue Nokogiri::CSS::SyntaxError, Nokogiri::XML::XPath::SyntaxError
+ # Custom selectors that cannot be inlined must not prevent email delivery.
+ next
+ end
end
# this method is reserved for styles specific to plugin
diff --git a/spec/lib/email/styles_spec.rb b/spec/lib/email/styles_spec.rb
index 8de224f74fb..012b175900e 100644
--- a/spec/lib/email/styles_spec.rb
+++ b/spec/lib/email/styles_spec.rb
@@ -57,6 +57,27 @@ RSpec.describe Email::Styles do
end
end
+ describe "custom formatter" do
+ ["a:visited", "a:hover", "a["].each do |selector|
+ it "skips #{selector} while applying supported custom selectors" do
+ SiteSetting.email_custom_css_compiled = <<~CSS
+ p { color: red; }
+ #{selector}, a:first-child { letter-spacing: 3px; }
+ a { background-color: green; }
+ CSS
+
+ frag =
+ html_fragment(
+ '<p>Text</p><div><a href="/">Link</a><a href="/" id="other">Other link</a></div>',
+ )
+
+ expect(frag.at("p")["style"]).to include("color:red")
+ expect(frag.at("a")["style"]).to include("letter-spacing:3px", "background-color:green")
+ expect(frag.at("#other")["style"]).not_to include("letter-spacing:3px")
+ end
+ end
+ end
+
describe "html template formatter" do
it "attaches a style to h3 tags" do
frag = html_fragment("<h3>hello</h3>")