2015-10-11 17:41:23 +08:00
|
|
|
require 'rails_helper'
|
2013-06-04 04:12:24 +08:00
|
|
|
require 'email'
|
|
|
|
|
2013-06-11 03:33:37 +08:00
|
|
|
describe Email::Styles do
|
2013-06-04 04:12:24 +08:00
|
|
|
|
2016-01-29 18:13:59 +08:00
|
|
|
def basic_fragment(html)
|
2013-06-14 00:15:05 +08:00
|
|
|
styler = Email::Styles.new(html)
|
|
|
|
styler.format_basic
|
2016-01-29 18:13:59 +08:00
|
|
|
Nokogiri::HTML.fragment(styler.to_html)
|
2013-06-04 04:12:24 +08:00
|
|
|
end
|
|
|
|
|
2016-01-29 18:13:59 +08:00
|
|
|
def html_fragment(html)
|
2013-06-14 00:15:05 +08:00
|
|
|
styler = Email::Styles.new(html)
|
|
|
|
styler.format_basic
|
|
|
|
styler.format_html
|
2016-01-29 18:13:59 +08:00
|
|
|
Nokogiri::HTML.fragment(styler.to_html)
|
2016-01-28 07:44:49 +08:00
|
|
|
end
|
|
|
|
|
2013-06-14 00:15:05 +08:00
|
|
|
context "basic formatter" do
|
2013-06-04 04:12:24 +08:00
|
|
|
|
2013-06-14 00:15:05 +08:00
|
|
|
it "works with an empty string" do
|
|
|
|
style = Email::Styles.new("")
|
|
|
|
style.format_basic
|
|
|
|
expect(style.to_html).to be_blank
|
|
|
|
end
|
2013-06-04 04:12:24 +08:00
|
|
|
|
2016-01-29 18:13:59 +08:00
|
|
|
# Pending due to email effort @coding-horror made in d2fb2bc4c
|
|
|
|
skip "adds a max-width to images" do
|
|
|
|
frag = basic_fragment("<img src='gigantic.jpg'>")
|
|
|
|
expect(frag.at("img")["style"]).to match("max-width")
|
2016-01-28 07:44:49 +08:00
|
|
|
end
|
|
|
|
|
2013-06-14 00:15:05 +08:00
|
|
|
it "adds a width and height to images with an emoji path" do
|
2016-01-29 18:13:59 +08:00
|
|
|
frag = basic_fragment("<img src='/images/emoji/fish.png' class='emoji'>")
|
|
|
|
expect(frag.at("img")["width"]).to eq("20")
|
|
|
|
expect(frag.at("img")["height"]).to eq("20")
|
2013-06-14 00:15:05 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
it "converts relative paths to absolute paths" do
|
2016-01-29 18:13:59 +08:00
|
|
|
frag = basic_fragment("<img src='/some-image.png'>")
|
|
|
|
expect(frag.at("img")["src"]).to eq("#{Discourse.base_url}/some-image.png")
|
2013-06-14 00:15:05 +08:00
|
|
|
end
|
2013-06-04 04:12:24 +08:00
|
|
|
|
2013-07-26 15:27:46 +08:00
|
|
|
it "strips classes and ids" do
|
2016-01-29 18:13:59 +08:00
|
|
|
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>")
|
2013-07-26 15:27:46 +08:00
|
|
|
end
|
|
|
|
|
2013-06-04 04:12:24 +08:00
|
|
|
end
|
|
|
|
|
2013-06-14 00:15:05 +08:00
|
|
|
context "html template formatter" do
|
|
|
|
it "works with an empty string" do
|
|
|
|
style = Email::Styles.new("")
|
|
|
|
style.format_html
|
|
|
|
expect(style.to_html).to be_blank
|
|
|
|
end
|
|
|
|
|
|
|
|
it "attaches a style to h3 tags" do
|
2016-01-29 18:13:59 +08:00
|
|
|
frag = html_fragment("<h3>hello</h3>")
|
|
|
|
expect(frag.at('h3')['style']).to be_present
|
2013-06-14 00:15:05 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
it "attaches a style to hr tags" do
|
2016-01-29 18:13:59 +08:00
|
|
|
frag = html_fragment("hello<hr>")
|
|
|
|
expect(frag.at('hr')['style']).to be_present
|
2013-06-14 00:15:05 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
it "attaches a style to a tags" do
|
2016-01-29 18:13:59 +08:00
|
|
|
frag = html_fragment("<a href>wat</a>")
|
|
|
|
expect(frag.at('a')['style']).to be_present
|
2013-06-14 00:15:05 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
it "attaches a style to a tags" do
|
2016-01-29 18:13:59 +08:00
|
|
|
frag = html_fragment("<a href>wat</a>")
|
|
|
|
expect(frag.at('a')['style']).to be_present
|
2013-06-14 00:15:05 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
it "attaches a style to ul and li tags" do
|
2016-01-29 18:13:59 +08:00
|
|
|
frag = html_fragment("<ul><li>hello</li></ul>")
|
|
|
|
expect(frag.at('ul')['style']).to be_present
|
|
|
|
expect(frag.at('li')['style']).to be_present
|
2013-06-14 00:15:05 +08:00
|
|
|
end
|
2014-07-15 04:41:05 +08:00
|
|
|
|
|
|
|
it "converts iframes to links" do
|
|
|
|
iframe_url = "http://www.youtube.com/embed/7twifrxOTQY?feature=oembed&wmode=opaque"
|
2016-01-29 18:13:59 +08:00
|
|
|
frag = html_fragment("<iframe src=\"#{iframe_url}\"></iframe>")
|
|
|
|
expect(frag.at('iframe')).to be_blank
|
|
|
|
expect(frag.at('a')).to be_present
|
|
|
|
expect(frag.at('a')['href']).to eq(iframe_url)
|
2014-07-15 04:41:05 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
it "won't allow non URLs in iframe src, strips them with no link" do
|
|
|
|
iframe_url = "alert('xss hole')"
|
2016-01-29 18:13:59 +08:00
|
|
|
frag = html_fragment("<iframe src=\"#{iframe_url}\"></iframe>")
|
|
|
|
expect(frag.at('iframe')).to be_blank
|
|
|
|
expect(frag.at('a')).to be_blank
|
2016-01-28 11:07:21 +08:00
|
|
|
end
|
2016-01-28 07:44:49 +08:00
|
|
|
end
|
|
|
|
|
2014-06-14 05:11:04 +08:00
|
|
|
context "rewriting protocol relative URLs to the forum" do
|
|
|
|
it "doesn't rewrite a url to another site" do
|
2016-01-29 18:13:59 +08:00
|
|
|
frag = html_fragment('<a href="//youtube.com/discourse">hello</a>')
|
|
|
|
expect(frag.at('a')['href']).to eq("//youtube.com/discourse")
|
2014-06-14 05:11:04 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
context "without https" do
|
|
|
|
before do
|
|
|
|
SiteSetting.stubs(:use_https).returns(false)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "rewrites the href to have http" do
|
2016-01-29 18:13:59 +08:00
|
|
|
frag = html_fragment('<a href="//test.localhost/discourse">hello</a>')
|
|
|
|
expect(frag.at('a')['href']).to eq("http://test.localhost/discourse")
|
2014-06-14 05:11:04 +08:00
|
|
|
end
|
|
|
|
|
2014-10-28 02:21:55 +08:00
|
|
|
it "rewrites the href for attachment files to have http" do
|
2016-01-29 18:13:59 +08:00
|
|
|
frag = html_fragment('<a class="attachment" href="//try-discourse.global.ssl.fastly.net/uploads/default/368/40b610b0aa90cfcf.txt">attachment_file.txt</a>')
|
|
|
|
expect(frag.at('a')['href']).to eq("http://try-discourse.global.ssl.fastly.net/uploads/default/368/40b610b0aa90cfcf.txt")
|
2014-10-28 02:21:55 +08:00
|
|
|
end
|
|
|
|
|
2014-06-14 05:11:04 +08:00
|
|
|
it "rewrites the src to have http" do
|
2016-01-29 18:13:59 +08:00
|
|
|
frag = html_fragment('<img src="//test.localhost/blah.jpg">')
|
|
|
|
expect(frag.at('img')['src']).to eq("http://test.localhost/blah.jpg")
|
2014-06-14 05:11:04 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context "with https" do
|
|
|
|
before do
|
|
|
|
SiteSetting.stubs(:use_https).returns(true)
|
|
|
|
end
|
|
|
|
|
2014-10-28 02:21:55 +08:00
|
|
|
it "rewrites the forum URL to have https" do
|
2016-01-29 18:13:59 +08:00
|
|
|
frag = html_fragment('<a href="//test.localhost/discourse">hello</a>')
|
|
|
|
expect(frag.at('a')['href']).to eq("https://test.localhost/discourse")
|
2014-06-14 05:11:04 +08:00
|
|
|
end
|
|
|
|
|
2014-10-28 02:21:55 +08:00
|
|
|
it "rewrites the href for attachment files to have https" do
|
2016-01-29 18:13:59 +08:00
|
|
|
frag = html_fragment('<a class="attachment" href="//try-discourse.global.ssl.fastly.net/uploads/default/368/40b610b0aa90cfcf.txt">attachment_file.txt</a>')
|
|
|
|
expect(frag.at('a')['href']).to eq("https://try-discourse.global.ssl.fastly.net/uploads/default/368/40b610b0aa90cfcf.txt")
|
2014-10-28 02:21:55 +08:00
|
|
|
end
|
|
|
|
|
2014-06-14 05:11:04 +08:00
|
|
|
it "rewrites the src to have https" do
|
2016-01-29 18:13:59 +08:00
|
|
|
frag = html_fragment('<img src="//test.localhost/blah.jpg">')
|
|
|
|
expect(frag.at('img')['src']).to eq("https://test.localhost/blah.jpg")
|
2014-06-14 05:11:04 +08:00
|
|
|
end
|
|
|
|
end
|
2013-06-14 00:15:05 +08:00
|
|
|
|
2013-06-04 04:12:24 +08:00
|
|
|
end
|
|
|
|
|
2015-08-19 07:12:08 +08:00
|
|
|
context "strip_avatars_and_emojis" do
|
|
|
|
it "works for lonesome emoji with no title" do
|
|
|
|
emoji = "<img src='/images/emoji/emoji_one/crying_cat_face.png'>"
|
|
|
|
style = Email::Styles.new(emoji)
|
|
|
|
style.strip_avatars_and_emojis
|
2016-01-29 18:13:59 +08:00
|
|
|
expect(style.to_html).to match_html(emoji)
|
2015-08-19 07:12:08 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
it "works for lonesome emoji with title" do
|
|
|
|
emoji = "<img title='cry_cry' src='/images/emoji/emoji_one/crying_cat_face.png'>"
|
|
|
|
style = Email::Styles.new(emoji)
|
|
|
|
style.strip_avatars_and_emojis
|
2016-01-29 18:13:59 +08:00
|
|
|
expect(style.to_html).to match_html("cry_cry")
|
2015-08-19 07:12:08 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-01-29 18:13:59 +08:00
|
|
|
|
2013-06-04 04:12:24 +08:00
|
|
|
end
|