add layout for notifications

This commit is contained in:
scossar
2016-01-19 16:42:56 -08:00
parent 6a7bdfecc8
commit 3f09ec2aca
4 changed files with 122 additions and 68 deletions

View File

@ -3,17 +3,17 @@ require 'email'
describe Email::Styles do
def basic_fragment(html)
def basic_doc(html)
styler = Email::Styles.new(html)
styler.format_basic
Nokogiri::HTML.fragment(styler.to_html)
Nokogiri::HTML(styler.to_html)
end
def html_fragment(html)
def html_doc(html)
styler = Email::Styles.new(html)
styler.format_basic
styler.format_html
Nokogiri::HTML.fragment(styler.to_html)
Nokogiri::HTML(styler.to_html)
end
context "basic formatter" do
@ -26,24 +26,24 @@ describe Email::Styles do
# 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")
doc = basic_doc("<img src='gigantic.jpg'>")
expect(doc.at("img")["style"]).to match("max-width")
end
it "adds a width and height to images with an emoji path" do
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")
doc = basic_doc("<img src='/images/emoji/fish.png' class='emoji'>")
expect(doc.at("img")["width"]).to eq("20")
expect(doc.at("img")["height"]).to eq("20")
end
it "converts relative paths to absolute paths" do
frag = basic_fragment("<img src='/some-image.png'>")
expect(frag.at("img")["src"]).to eq("#{Discourse.base_url}/some-image.png")
doc = basic_doc("<img src='/some-image.png'>")
expect(doc.at("img")["src"]).to eq("#{Discourse.base_url}/some-image.png")
end
it "strips classes and ids" do
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>")
doc = basic_doc("<div class='foo' id='bar'><div class='foo' id='bar'></div></div>")
expect(doc.to_html).to match(/<div><div><\/div><\/div>/)
end
end
@ -56,51 +56,51 @@ describe Email::Styles do
end
it "attaches a style to h3 tags" do
frag = html_fragment("<h3>hello</h3>")
expect(frag.at('h3')['style']).to be_present
doc = html_doc("<h3>hello</h3>")
expect(doc.at('h3')['style']).to be_present
end
it "attaches a style to hr tags" do
frag = html_fragment("hello<hr>")
expect(frag.at('hr')['style']).to be_present
doc = html_doc("hello<hr>")
expect(doc.at('hr')['style']).to be_present
end
it "attaches a style to a tags" do
frag = html_fragment("<a href>wat</a>")
expect(frag.at('a')['style']).to be_present
doc = html_doc("<a href>wat</a>")
expect(doc.at('a')['style']).to be_present
end
it "attaches a style to a tags" do
frag = html_fragment("<a href>wat</a>")
expect(frag.at('a')['style']).to be_present
doc = html_doc("<a href>wat</a>")
expect(doc.at('a')['style']).to be_present
end
it "attaches a style to ul and li tags" do
frag = html_fragment("<ul><li>hello</li></ul>")
expect(frag.at('ul')['style']).to be_present
expect(frag.at('li')['style']).to be_present
doc = html_doc("<ul><li>hello</li></ul>")
expect(doc.at('ul')['style']).to be_present
expect(doc.at('li')['style']).to be_present
end
it "converts iframes to links" do
iframe_url = "http://www.youtube.com/embed/7twifrxOTQY?feature=oembed&wmode=opaque"
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)
doc = html_doc("<iframe src=\"#{iframe_url}\"></iframe>")
expect(doc.at('iframe')).to be_blank
expect(doc.at('a')).to be_present
expect(doc.at('a')['href']).to eq(iframe_url)
end
it "won't allow non URLs in iframe src, strips them with no link" do
iframe_url = "alert('xss hole')"
frag = html_fragment("<iframe src=\"#{iframe_url}\"></iframe>")
expect(frag.at('iframe')).to be_blank
expect(frag.at('a')).to be_blank
doc = html_doc("<iframe src=\"#{iframe_url}\"></iframe>")
expect(doc.at('iframe')).to be_blank
expect(doc.at('a')).to be_blank
end
end
context "rewriting protocol relative URLs to the forum" do
it "doesn't rewrite a url to another site" do
frag = html_fragment('<a href="//youtube.com/discourse">hello</a>')
expect(frag.at('a')['href']).to eq("//youtube.com/discourse")
doc = html_doc('<a href="//youtube.com/discourse">hello</a>')
expect(doc.at('a')['href']).to eq("//youtube.com/discourse")
end
context "without https" do
@ -109,18 +109,18 @@ describe Email::Styles do
end
it "rewrites the href to have http" do
frag = html_fragment('<a href="//test.localhost/discourse">hello</a>')
expect(frag.at('a')['href']).to eq("http://test.localhost/discourse")
doc = html_doc('<a href="//test.localhost/discourse">hello</a>')
expect(doc.at('a')['href']).to eq("http://test.localhost/discourse")
end
it "rewrites the href for attachment files to have http" do
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")
doc = html_doc('<a class="attachment" href="//try-discourse.global.ssl.fastly.net/uploads/default/368/40b610b0aa90cfcf.txt">attachment_file.txt</a>')
expect(doc.at('a')['href']).to eq("http://try-discourse.global.ssl.fastly.net/uploads/default/368/40b610b0aa90cfcf.txt")
end
it "rewrites the src to have http" do
frag = html_fragment('<img src="//test.localhost/blah.jpg">')
expect(frag.at('img')['src']).to eq("http://test.localhost/blah.jpg")
doc = html_doc('<img src="//test.localhost/blah.jpg">')
expect(doc.at('img')['src']).to eq("http://test.localhost/blah.jpg")
end
end
@ -130,18 +130,18 @@ describe Email::Styles do
end
it "rewrites the forum URL to have https" do
frag = html_fragment('<a href="//test.localhost/discourse">hello</a>')
expect(frag.at('a')['href']).to eq("https://test.localhost/discourse")
doc = html_doc('<a href="//test.localhost/discourse">hello</a>')
expect(doc.at('a')['href']).to eq("https://test.localhost/discourse")
end
it "rewrites the href for attachment files to have https" do
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")
doc = html_doc('<a class="attachment" href="//try-discourse.global.ssl.fastly.net/uploads/default/368/40b610b0aa90cfcf.txt">attachment_file.txt</a>')
expect(doc.at('a')['href']).to eq("https://try-discourse.global.ssl.fastly.net/uploads/default/368/40b610b0aa90cfcf.txt")
end
it "rewrites the src to have https" do
frag = html_fragment('<img src="//test.localhost/blah.jpg">')
expect(frag.at('img')['src']).to eq("https://test.localhost/blah.jpg")
doc = html_doc('<img src="//test.localhost/blah.jpg">')
expect(doc.at('img')['src']).to eq("https://test.localhost/blah.jpg")
end
end
@ -152,14 +152,14 @@ describe Email::Styles do
emoji = "<img src='/images/emoji/emoji_one/crying_cat_face.png'>"
style = Email::Styles.new(emoji)
style.strip_avatars_and_emojis
expect(style.to_html).to match_html(emoji)
expect(style.to_html).to match_html("<!DOCTYPE html PUBLIC \"-//W3C//DTD HTML 4.0 Transitional//EN\" \"http://www.w3.org/TR/REC-html40/loose.dtd\"><html><body>#{emoji}</body></html>")
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
expect(style.to_html).to match_html("cry_cry")
expect(style.to_html).to match_html("<!DOCTYPE html PUBLIC \"-//W3C//DTD HTML 4.0 Transitional//EN\" \"http://www.w3.org/TR/REC-html40/loose.dtd\"><html><body>cry_cry</body></html>")
end
end