package mailhtml
import (
"strings"
"testing"
)
// The shape issue #462 reports: an HTML email laid out as a card centred
// inside a full-width page table. Appended at the end of the document, the
// signature and the opt-out footer render against the left edge of the
// window, in the page background, styled by nothing.
const centredCard = `
` +
``
func TestAppendToContentLandsInsideTheCard(t *testing.T) {
out := AppendToContent(centredCard, "[F]")
if !strings.Contains(out, "Body
[F]") {
t.Fatalf("footer did not land in the card's own cell:\n%s", out)
}
}
// A preheader and a tracking pixel are both children of in a designed
// email. Counting either as layout stops the descent at the body and leaves
// the footer exactly where the bug put it.
func TestAppendToContentIgnoresThePreheaderAndThePixel(t *testing.T) {
body := `Quick question
` +
`` +
`
`
out := AppendToContent(body, "[F]")
if !strings.Contains(out, "Hi[F]") {
t.Fatalf("hidden siblings stopped the descent:\n%s", out)
}
}
// A written straight into a table is hoisted back out of it by every HTML
// parser, which lands it exactly where the bug put it. A table with rows of
// its own gets a row instead, spanning every column and picking up the side
// padding of the rows above so the footer lines up with the copy.
func TestAppendToContentAddsARowToATable(t *testing.T) {
body := `
` +
`| Banner | Logo |
` +
`| Copy | Aside |
` +
`
`
out := AppendToContent(body, "[F]")
want := `| [F] |
`
if !strings.Contains(out, want) {
t.Fatalf("expected %s in:\n%s", want, out)
}
}
// A builder export (Unlayer, Beefree, Stripo) is a stack of full-width rows
// each centring a card of its own, so the deepest element holding the whole
// message is the full-width cell. Appending there is inside the document but
// still hard left; the footer has to centre itself on the card's width.
func TestAppendToContentCentresItselfInABuilderExport(t *testing.T) {
row := ``
body := ``
out := AppendToContent(body, "[F]")
if !strings.Contains(out, `width="600"`) || !strings.Contains(out, `align="center"`) {
t.Fatalf("footer was not centred on the card's width:\n%s", out)
}
if !strings.Contains(out, "[F]") {
t.Fatalf("footer did not land at the end of the content:\n%s", out)
}
// The same card, written as one container rather than a stack of rows,
// needs no wrapper: the insertion point is already the content column.
if out := AppendToContent(centredCard, "[F]"); strings.Contains(out, "role=\"presentation\"") {
t.Fatalf("a width-constrained insertion point should not be wrapped:\n%s", out)
}
}
// An email with no layout is every campaign written in the visual editor.
// Nothing about those changes: the footer goes at the end, unwrapped.
func TestAppendToContentLeavesAPlainBodyAlone(t *testing.T) {
for in, want := range map[string]string{
"a
": "a
[F]",
"a
b
": "a
b
[F]",
"x
": "x
[F]",
"x": "x[F]",
"a
` +
`` +
`` +
`` +
`