# frozen_string_literal: true
require 'rails_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 large images" do
frag = basic_fragment("
")
expect(frag.at("img")["style"]).to match("max-width")
end
it "adds a width and height to emojis" do
frag = basic_fragment("
")
expect(frag.at("img")["width"]).to eq("20")
expect(frag.at("img")["height"]).to eq("20")
end
it "adds a width and height to custom emojis" 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("
')
expect(frag.at('img')['src']).to eq("http://test.localhost/blah.jpg")
end
end
context "with https" do
before do
SiteSetting.force_https = true
end
it "rewrites the forum URL to have https" do
frag = html_fragment('hello')
expect(frag.at('a')['href']).to eq("https://test.localhost/discourse")
end
it "rewrites the href for attachment files to have https" do
frag = html_fragment('attachment_file.txt')
expect(frag.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('
')
expect(frag.at('img')['src']).to eq("https://test.localhost/blah.jpg")
end
end
end
context "strip_avatars_and_emojis" do
it "works for lonesome emoji with no title" do
emoji = "
"
style = Email::Styles.new(emoji)
style.strip_avatars_and_emojis
expect(style.to_html).to match_html(emoji)
end
it "works for lonesome emoji with title" do
emoji = "
"
style = Email::Styles.new(emoji)
style.strip_avatars_and_emojis
expect(style.to_html).to match_html("cry_cry")
end
it "works if img tag has no attrs" do
cooked = "Create a method for click on image and use ng-click in " do fragment = html_fragment('') expect(fragment.to_s.squish).to match(/^$/) end end end