require 'spec_helper'
require 'email'
describe Email::Styles do
def basic_fragment(html)
styler = Email::Styles.new(html)
styler.format_basic
Nokogiri::HTML.fragment(styler.to_html)
end
def html_fragment(html)
styler = Email::Styles.new(html)
styler.format_basic
styler.format_html
Nokogiri::HTML.fragment(styler.to_html)
end
context "basic formatter" do
it "works with an empty string" do
style = Email::Styles.new("")
style.format_basic
expect(style.to_html).to be_blank
end
it "adds a max-width to images" do
frag = basic_fragment("
")
expect(frag.at("img")["style"]).to match("max-width")
end
it "adds a width and height to images with an emoji path" do
frag = basic_fragment("
")
expect(frag.at("img")["width"]).to eq("20")
expect(frag.at("img")["height"]).to eq("20")
end
it "converts relative paths to absolute paths" do
frag = basic_fragment("
")
expect(frag.at("img")["src"]).to eq("#{Discourse.base_url}/some-image.png")
end
it "strips classes and ids" do
frag = basic_fragment("
')
frag.at('img')['src'].should == "http://test.localhost/blah.jpg"
end
end
context "with https" do
before do
SiteSetting.stubs(:use_https).returns(true)
end
it "rewrites the forum URL to have http" do
frag = html_fragment('hello')
frag.at('a')['href'].should == "https://test.localhost/discourse"
end
it "rewrites the src to have https" do
frag = html_fragment('
')
frag.at('img')['src'].should == "https://test.localhost/blah.jpg"
end
end
end
end