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")["style"]).to match("width:") expect(frag.at("img")["style"]).to match("height:") 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 end 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 frag = html_fragment("

hello

") expect(frag.at('h3')['style']).to be_present end it "attaches a style to hr tags" do frag = html_fragment("hello
") expect(frag.at('hr')['style']).to be_present end it "attaches a style to a tags" do frag = html_fragment("wat") expect(frag.at('a')['style']).to be_present end it "attaches a style to a tags" do frag = html_fragment("wat") expect(frag.at('a')['style']).to be_present end it "attaches a style to ul and li tags" do frag = html_fragment("") expect(frag.at('ul')['style']).to be_present expect(frag.at('li')['style']).to be_present end it "removes pre tags but keeps their contents" do style = Email::Styles.new("
hello
") style.format_basic style.format_html expect(style.to_html).to eq("hello") end end end