Skip to content

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

FIX: elided content in email should always have an href

Régis Hanold4af30f26dd7
diff --git a/plugins/discourse-details/plugin.rb b/plugins/discourse-details/plugin.rb
index 2668042db2a..eba95c33a30 100644
--- a/plugins/discourse-details/plugin.rb
+++ b/plugins/discourse-details/plugin.rb
@@ -37,7 +37,7 @@ after_initialize do
.each do |el|
text = el.css("summary").text
link = fragment.document.create_element("a")
- link["href"] = post.url if post
+ link["href"] = post&.url.presence || Discourse.base_url
link.content = I18n.t("details.excerpt_details")
el.replace CGI.escapeHTML(text) + " " + link.to_html
end
diff --git a/plugins/discourse-details/spec/components/pretty_text_spec.rb b/plugins/discourse-details/spec/components/pretty_text_spec.rb
index 3356f3a192a..cf1e87e6011 100644
--- a/plugins/discourse-details/spec/components/pretty_text_spec.rb
+++ b/plugins/discourse-details/spec/components/pretty_text_spec.rb
@@ -6,43 +6,54 @@ RSpec.describe PrettyText do
let(:post) { Fabricate(:post) }
it "supports details tag" do
- cooked_html = <<~HTML.gsub("\n", "")
+ cooked_html = PrettyText.cook <<~MARKDOWN
+ [details="foo"]
+ bar
+ [/details]
+ MARKDOWN
+
+ expect(cooked_html).to match_html <<~HTML
<details>
- <summary>
- foo</summary>
- <p>bar</p>
+ <summary>foo</summary>
+ <p>bar</p>
</details>
HTML
-
- expect(cooked_html).to match_html(cooked_html)
- expect(PrettyText.cook("[details=foo]\nbar\n[/details]").gsub("\n", "")).to match_html(
- cooked_html,
- )
end
it "deletes elided content" do
- cooked_html = PrettyText.cook("Hello World\n\n<details class='elided'>42</details>")
- mail_html = "<p>Hello World</p>\n<a href=\"http://test.localhost\">(click for more details)</a>"
+ cooked_html = PrettyText.cook <<~MARKDOWN
+ Hello World
+
+ <details class='elided'>42</details>
+ MARKDOWN
- expect(PrettyText.format_for_email(cooked_html)).to match_html(mail_html)
+ email_html = PrettyText.format_for_email(cooked_html)
+
+ expect(email_html).to match_html <<~HTML
+ <p>Hello World</p>
+ <a href="#{Discourse.base_url}">#{I18n.t("details.excerpt_details")}</a>
+ HTML
end
it "can replace spoilers in emails" do
- md = PrettyText.cook(<<~MD)
+ cooked_html = PrettyText.cook <<~MARKDOWN
hello
[details="Summary"]
world
[/details]
- MD
- md = PrettyText.format_for_email(md, post)
- html = "<p>hello</p>\n\nSummary <a href=\"#{post.full_url}\">(click for more details)</a>"
+ MARKDOWN
+
+ email_html = PrettyText.format_for_email(cooked_html, post)
- expect(md).to eq(html)
+ expect(email_html).to match_html <<~HTML
+ <p>hello</p>
+ Summary <a href="#{post.full_url}">#{I18n.t("details.excerpt_details")}</a>
+ HTML
end
it "properly handles multiple spoiler blocks in a post" do
- md = PrettyText.cook(<<~MD)
+ cooked_html = PrettyText.cook <<~MARKDOWN
[details="First"]
body secret stuff very long
[/details]
@@ -55,24 +66,31 @@ RSpec.describe PrettyText do
[details="Third"]
body secret stuff very long
[/details]
- MD
+ MARKDOWN
+
+ email_html = PrettyText.format_for_email(cooked_html, post)
- md = PrettyText.format_for_email(md, post)
- expect(md).not_to include("secret stuff")
- expect(md.scan(/First/).size).to eq(1)
- expect(md.scan(/Third/).size).to eq(1)
- expect(md.scan(I18n.t("details.excerpt_details")).size).to eq(3)
+ expect(email_html).to match_html <<~HTML
+ First <a href="#{post.full_url}">#{I18n.t("details.excerpt_details")}</a>
+ Second <a href="#{post.full_url}">#{I18n.t("details.excerpt_details")}</a>
+ <p>Hey there.</p>
+ Third <a href="#{post.full_url}">#{I18n.t("details.excerpt_details")}</a>
+ HTML
end
it "escapes summary text" do
- md = PrettyText.cook(<<~MD)
+ cooked_html = PrettyText.cook <<~MARKDOWN
<script>alert('hello')</script>
+
[details="<script>alert('hello')</script>"]
<script>alert('hello')</script>
[/details]
- MD
- md = PrettyText.format_for_email(md, post)
+ MARKDOWN
+
+ email_html = PrettyText.format_for_email(cooked_html, post)
- expect(md).not_to include("<script>")
+ expect(email_html).to match_html <<~HTML
+ &lt;script&gt;alert('hello')&lt;/script&gt; <a href="#{post.full_url}">#{I18n.t("details.excerpt_details")}</a>
+ HTML
end
end