From 9513e7be6dc3260a9e1b13be27746424d7565c96 Mon Sep 17 00:00:00 2001 From: Martin Brennan Date: Thu, 1 Dec 2022 19:48:24 +1000 Subject: [PATCH] FIX: Email hashtag-cooked text replacement error (#19278) In some cases (e.g. user notification emails) we are passing an excerpted/stripped version of the post HTML to Email::Styles, at which point the elements surrounding the hashtag text have been stripped. This caused an error when trying to remove that element to replace the text. Instead we can just remove all elements inside a.hashtag-cooked and replace with the raw #hashtag text which will work in more cases. --- lib/email/styles.rb | 5 ++--- spec/lib/email/styles_spec.rb | 9 +++++++++ 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/lib/email/styles.rb b/lib/email/styles.rb index ba2b56fcaf6..cd561eb8b58 100644 --- a/lib/email/styles.rb +++ b/lib/email/styles.rb @@ -326,11 +326,10 @@ module Email def decorate_hashtags @fragment.search(".hashtag-cooked").each do |hashtag| - hashtag_text = hashtag.search("span").first - hashtag_text.add_next_sibling(<<~HTML) + hashtag.children.each(&:remove) + hashtag.add_child(<<~HTML) ##{hashtag["data-slug"]} HTML - hashtag_text.remove end end diff --git a/spec/lib/email/styles_spec.rb b/spec/lib/email/styles_spec.rb index 4a6f18bf0ef..62d31c4b479 100644 --- a/spec/lib/email/styles_spec.rb +++ b/spec/lib/email/styles_spec.rb @@ -104,6 +104,15 @@ RSpec.describe Email::Styles do expect(frag.at('a')).to be_present expect(frag.at('a')['href']).to eq(original_url) end + + it "replaces hashtag-cooked text with raw #hashtag" do + hashtag_html = "Dev Zone" + frag = html_fragment(hashtag_html) + expect(frag.at("a").text.chomp).to eq("#dev") + hashtag_html = "Dev Zone" + frag = html_fragment(hashtag_html) + expect(frag.at("a").text.chomp).to eq("#dev") + end end describe "rewriting protocol relative URLs to the forum" do