2019-05-03 06:17:27 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-07-07 01:46:47 +08:00
|
|
|
require "pretty_text"
|
|
|
|
|
2022-07-28 10:27:38 +08:00
|
|
|
RSpec.describe PrettyText do
|
2019-05-24 23:57:03 +08:00
|
|
|
let(:post) { Fabricate(:post) }
|
|
|
|
|
2016-07-07 01:46:47 +08:00
|
|
|
it "supports details tag" do
|
2024-05-22 16:42:58 +08:00
|
|
|
cooked_html = PrettyText.cook <<~MARKDOWN
|
|
|
|
[details="foo"]
|
|
|
|
bar
|
|
|
|
[/details]
|
|
|
|
MARKDOWN
|
|
|
|
|
|
|
|
expect(cooked_html).to match_html <<~HTML
|
2017-07-19 02:44:49 +08:00
|
|
|
<details>
|
2024-05-22 16:42:58 +08:00
|
|
|
<summary>foo</summary>
|
|
|
|
<p>bar</p>
|
2017-07-19 02:44:49 +08:00
|
|
|
</details>
|
|
|
|
HTML
|
2018-02-08 07:01:11 +08:00
|
|
|
end
|
|
|
|
|
2024-10-08 10:13:44 +08:00
|
|
|
it "supports open attribute" do
|
|
|
|
cooked_html = PrettyText.cook <<~MARKDOWN
|
|
|
|
[details open]
|
|
|
|
bar
|
|
|
|
[/details]
|
|
|
|
MARKDOWN
|
|
|
|
|
|
|
|
expect(cooked_html).to match_html <<~HTML
|
|
|
|
<details open="">
|
|
|
|
<summary></summary>
|
|
|
|
<p>bar</p>
|
|
|
|
</details>
|
|
|
|
HTML
|
|
|
|
end
|
|
|
|
|
2018-02-08 07:01:11 +08:00
|
|
|
it "deletes elided content" do
|
2024-05-22 16:42:58 +08:00
|
|
|
cooked_html = PrettyText.cook <<~MARKDOWN
|
|
|
|
Hello World
|
2024-10-08 10:13:44 +08:00
|
|
|
|
2024-05-22 16:42:58 +08:00
|
|
|
<details class='elided'>42</details>
|
|
|
|
MARKDOWN
|
2018-02-08 07:01:11 +08:00
|
|
|
|
2024-05-22 16:42:58 +08:00
|
|
|
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
|
2016-07-07 01:46:47 +08:00
|
|
|
end
|
|
|
|
|
2019-05-24 23:57:03 +08:00
|
|
|
it "can replace spoilers in emails" do
|
2024-05-22 16:42:58 +08:00
|
|
|
cooked_html = PrettyText.cook <<~MARKDOWN
|
2019-05-24 23:57:03 +08:00
|
|
|
hello
|
|
|
|
|
|
|
|
[details="Summary"]
|
|
|
|
world
|
|
|
|
[/details]
|
2024-05-22 16:42:58 +08:00
|
|
|
MARKDOWN
|
|
|
|
|
|
|
|
email_html = PrettyText.format_for_email(cooked_html, post)
|
2019-05-24 23:57:03 +08:00
|
|
|
|
2024-05-22 16:42:58 +08:00
|
|
|
expect(email_html).to match_html <<~HTML
|
|
|
|
<p>hello</p>
|
|
|
|
Summary <a href="#{post.full_url}">#{I18n.t("details.excerpt_details")}</a>
|
|
|
|
HTML
|
2019-05-24 23:57:03 +08:00
|
|
|
end
|
|
|
|
|
2020-07-14 09:49:36 +08:00
|
|
|
it "properly handles multiple spoiler blocks in a post" do
|
2024-05-22 16:42:58 +08:00
|
|
|
cooked_html = PrettyText.cook <<~MARKDOWN
|
2020-07-14 09:49:36 +08:00
|
|
|
[details="First"]
|
|
|
|
body secret stuff very long
|
|
|
|
[/details]
|
|
|
|
[details="Second"]
|
|
|
|
body secret stuff very long
|
|
|
|
[/details]
|
|
|
|
|
|
|
|
Hey there.
|
|
|
|
|
|
|
|
[details="Third"]
|
|
|
|
body secret stuff very long
|
|
|
|
[/details]
|
2024-05-22 16:42:58 +08:00
|
|
|
MARKDOWN
|
|
|
|
|
|
|
|
email_html = PrettyText.format_for_email(cooked_html, post)
|
2020-07-14 09:49:36 +08:00
|
|
|
|
2024-05-22 16:42:58 +08:00
|
|
|
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
|
2020-07-14 09:49:36 +08:00
|
|
|
end
|
|
|
|
|
2019-06-26 21:37:01 +08:00
|
|
|
it "escapes summary text" do
|
2024-05-22 16:42:58 +08:00
|
|
|
cooked_html = PrettyText.cook <<~MARKDOWN
|
2019-06-26 21:37:01 +08:00
|
|
|
<script>alert('hello')</script>
|
2024-05-22 16:42:58 +08:00
|
|
|
|
2019-06-26 21:37:01 +08:00
|
|
|
[details="<script>alert('hello')</script>"]
|
|
|
|
<script>alert('hello')</script>
|
|
|
|
[/details]
|
2024-05-22 16:42:58 +08:00
|
|
|
MARKDOWN
|
|
|
|
|
|
|
|
email_html = PrettyText.format_for_email(cooked_html, post)
|
2019-06-26 21:37:01 +08:00
|
|
|
|
2024-05-22 16:42:58 +08:00
|
|
|
expect(email_html).to match_html <<~HTML
|
|
|
|
<script>alert('hello')</script> <a href="#{post.full_url}">#{I18n.t("details.excerpt_details")}</a>
|
|
|
|
HTML
|
2019-06-26 21:37:01 +08:00
|
|
|
end
|
2016-07-07 01:46:47 +08:00
|
|
|
end
|