Skip to content

Commit

Permalink
Inline css does not mangle html5 now
Browse files Browse the repository at this point in the history
  • Loading branch information
mlandauer committed Oct 20, 2023
1 parent 44891ba commit f0b254d
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 25 deletions.
8 changes: 4 additions & 4 deletions lib/filters/inline_css.rb
Expand Up @@ -3,12 +3,12 @@
module Filters
class InlineCss < Filters::Mail
def filter_html(input)
premailer = Premailer.new(
Premailer.new(
input,
with_html_string: true,
input_encoding: input.encoding.to_s
)
premailer.to_inline_css
input_encoding: input.encoding.to_s,
adapter: (html5?(input) ? :nokogumbo : :nokogiri)
).to_inline_css
end
end
end
50 changes: 29 additions & 21 deletions spec/lib/filters/inline_css_spec.rb
Expand Up @@ -3,31 +3,39 @@
require "spec_helper"

describe Filters::InlineCss do
context "with html email with no styling" do
let(:html) { "<p>This is HTML with “some” UTF-8</p>" }
describe "#filter_html" do
context "with html email with no styling" do
let(:html) { "<p>This is HTML with “some” UTF-8</p>" }

it "#filter_html" do
expect(described_class.new.filter_html(html)).to eq <<~HTML
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">
<html><body><p>This is HTML with “some” UTF-8</p></body></html>
HTML
it do
expect(described_class.new.filter_html(html)).to eq <<~HTML
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">
<html><body><p>This is HTML with “some” UTF-8</p></body></html>
HTML
end
end
end

context "with html email with style block" do
let(:html) do
"<head><style>p { font-size: 20px; }</style></head>" \
"<body><p>This is HTML with “some” UTF-8</p></body>"
end
context "with html email with style block" do
let(:html) do
"<head><style>p { font-size: 20px; }</style></head>" \
"<body><p>This is HTML with “some” UTF-8</p></body>"
end

it "#filter_html" do
expect(described_class.new.filter_html(html)).to eq <<~HTML
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">
<html>
<head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"></head>
<body><p style="font-size: 20px;">This is HTML with “some” UTF-8</p></body>
</html>
HTML
it do
expect(described_class.new.filter_html(html)).to eq <<~HTML
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">
<html>
<head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"></head>
<body><p style="font-size: 20px;">This is HTML with “some” UTF-8</p></body>
</html>
HTML
end
end
end

it "doesn't mangle valid html 5" do
html = '<!DOCTYPE html><a href="#"><table></table></a>'
expected = '<!DOCTYPE html><html><head></head><body><a href="#"><table></table></a></body></html>'
expect(described_class.new.filter_html(html).gsub("\n","")).to eq(expected)
end
end

0 comments on commit f0b254d

Please sign in to comment.