Skip to content

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

DEV: Add apply_modifier in Email::Renderer for html modifications (#25205)

Mark VanLandingham66fb2257cf12
diff --git a/lib/email/renderer.rb b/lib/email/renderer.rb
index a375176e2c8..8b43864dcce 100644
--- a/lib/email/renderer.rb
+++ b/lib/email/renderer.rb
@@ -32,6 +32,7 @@ module Email
style.format_basic
style.format_html
+ DiscoursePluginRegistry.apply_modifier(:email_renderer_html, style, @message)
style.to_html
end
end
diff --git a/spec/lib/email/renderer_spec.rb b/spec/lib/email/renderer_spec.rb
index 53796146ea6..6a89b270e80 100644
--- a/spec/lib/email/renderer_spec.rb
+++ b/spec/lib/email/renderer_spec.rb
@@ -11,14 +11,28 @@ RSpec.describe Email::Renderer do
mail.html_part =
Mail::Part.new do
content_type "text/html; charset=UTF-8"
- body "<h1>Key &amp; Peele</h1>"
+ body "<h1>Key &amp; Peele</h1> <a href=\"https://discourse.org\">Discourse link</a>"
end
mail
end
+ let(:renderer) { Email::Renderer.new(message) }
+
it "escapes HTML entities from text" do
- renderer = Email::Renderer.new(message)
expect(renderer.text).to eq("Key & Peele")
end
+
+ context "with email_renderer_html modifier" do
+ it "can modify the html" do
+ Plugin::Instance
+ .new
+ .register_modifier(:email_renderer_html) do |styles, _|
+ styles.fragment.css("a").each { |link| link["href"] = "httpz://hijacked.sorry" }
+ end
+
+ expect(renderer.html).not_to include("href=\"https://discourse.org\"")
+ expect(renderer.html).to include("href=\"httpz://hijacked.sorry\"")
+ end
+ end
end