package mailhtml import ( "strings" "testing" "unicode/utf8" ) // A designed email: a stylesheet with classes, a media query, a :hover rule, // a hidden preheader and a table layout. This is the shape issue #393 reports. const designed = `
Hi AnaWe help teams like Acme book more meetings.
|
x
`) if !strings.Contains(plain, "color: blue") || strings.Contains(plain, "red") { t.Errorf("inline style should beat a sheet rule: %q", plain) } } func TestInlineCSSKeepsWhatItCannotInline(t *testing.T) { out := InlineCSS(designed) if !strings.Contains(out, "@media") { t.Errorf("media queries must stay in thet
`) if !strings.Contains(out, "color: blue") { t.Errorf("the id selector should win: %q", out) } } func TestInlineCSSShorthandPrecedesLonghand(t *testing.T) { out := InlineCSS(`| c |
hi
`) if strings.Contains(out, " into the head; dropping the head on // the way out would lose the media query with it. out := InlineCSS(`hi
`) if !strings.Contains(out, "@media") { t.Errorf("a fragment's media query must survive: %q", out) } } func TestInlineCSSIsANoOpWithoutAStylesheet(t *testing.T) { body := `Hi {{.FirstName}}, link
` if got := InlineCSS(body); got != body { t.Errorf("a body with nohi
` out := InlineCSS(body) if strings.Contains(out, `style="color`) { t.Errorf("an opted-out stylesheet must not be inlined: %q", out) } if !strings.Contains(out, ".x{color:red}") { t.Errorf("an opted-out stylesheet must survive verbatim: %q", out) } } func TestInlineCSSSkipsAPrintStylesheet(t *testing.T) { out := InlineCSS(`hi
`) if strings.Contains(out, `style="color`) { t.Errorf("a print stylesheet is not what the reader sees: %q", out) } } func TestInlineCSSSurvivesBrokenCSS(t *testing.T) { // Unterminated blocks, a comment inside a value, a url() holding braces // and semicolons: none of it may panic or lose the document. for _, css := range []string{ `.a{color:red`, `.a{background:url(data:image/png;base64,AA{BB)}`, `.a{color:/* c */red}`, `@media{`, `.a{content:"}"}`, } { out := InlineCSS(`hi
`) if !strings.Contains(out, "hi") { t.Errorf("CSS %q lost the body: %q", css, out) } } } func TestInsertBeforeBodyEndIgnoresAConditionalComment(t *testing.T) { // Outlook conditional comments carry their own . Inserting there // put the signature and the opt-out footer inside a comment. body := `a
` out := InsertBeforeBodyEnd(body, "[F]") if !strings.Contains(out, "[F]