A platform for community discussion. Free, open, simple.
DEV: Preserve ids/classes in emails (#32084)
David Taylor119295f687f9
diff --git a/lib/email/styles.rb b/lib/email/styles.rbindex 8130c41773c..16216604e50 100644--- a/lib/email/styles.rb+++ b/lib/email/styles.rb@@ -397,10 +397,7 @@ module Emailenddef 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_urlsdeduplicate_styles@@ -548,15 +545,6 @@ module Emailendend- def strip_classes_and_ids- @fragment- .css("*")- .each do |element|- element.delete("class")- element.delete("id")- end- end-def reset_tablesstyle("table", nil, cellspacing: "0", cellpadding: "0", border: "0")enddiff --git a/spec/integration/email_style_spec.rb b/spec/integration/email_style_spec.rbindex b0a2d9cafee..30e79c3a491 100644--- a/spec/integration/email_style_spec.rb+++ b/spec/integration/email_style_spec.rb@@ -72,7 +72,7 @@ RSpec.describe EmailStyle doSiteSetting.default_email_in_reply_to = trueexpect(mail_html.scan('<h1 style="color:red">FOR YOU</h1>').count).to eq(1)- matches = mail_html.match(/<div style="([^"]+)" dm=\"body\">#{post.raw}/)+ matches = mail_html.match(/<div class="body" style="([^"]+)" dm=\"body\">#{post.raw}/)expect(matches[1]).to include("color:#FAB") # customexpect(matches[1]).to include("padding-top:5px") # div.bodyenddiff --git a/spec/lib/email/sender_spec.rb b/spec/lib/email/sender_spec.rbindex 0fdfdd70f29..fd7b9d1c386 100644--- a/spec/lib/email/sender_spec.rb+++ b/spec/lib/email/sender_spec.rb@@ -592,7 +592,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}\" data-style-type=\"square\" style=\"text-decoration:none;font-weight:bold;color:#006699\"><span>#dev</span>+ <a class="hashtag-cooked" href=\"#{Discourse.base_url}#{category.url}\" data-type=\"category\" data-slug=\"dev\" data-id=\"#{category.id}\" data-style-type=\"square\" 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 03bcbed06c5..e3f2602f6a5 100644--- a/spec/lib/email/styles_spec.rb+++ b/spec/lib/email/styles_spec.rb@@ -42,9 +42,10 @@ RSpec.describe Email::Styles doexpect(frag.at("img")["src"]).to eq("#{Discourse.base_url}/some-image.png")end- it "strips classes and ids" do- frag = basic_fragment("<div class='foo' id='bar'><div class='foo' id='bar'></div></div>")- expect(frag.to_html).to eq("<div><div></div></div>")+ it "preserves classes and ids" do+ raw = '<div class="foo" id="bar"><div class="foo" id="bar"></div></div>'+ frag = basic_fragment(raw)+ expect(frag.to_html).to eq(raw)endend