Files
summercms/modules/postcard/inline.go
Jakub Zych d320733510 feat(postcard): inline mail CSS at render with a per-layout opt-out
- github.com/vanng822/go-premailer v1.38.0 (default options) inlines the
  rendered HTML after the safety checks; the result is checked again,
  HTML without a style element is left as it is, an inliner error sends
  the style block only and warns (key inline:<layout>)
- opt-out: layout header key disable_auto_inline_css, the layouts column
  of the same name (migration 202611130003), LayoutFile/LayoutOverride/
  Draft.DisableAutoInlineCSS; SyncIndex mirrors and ResetOverride restores
  the file's value
- cabana layout form switch on the CSS tab, mapped into the layout draft
  and the save validation; en/pl copy
- tests: TestMailPartialsSmoke/inline, TestMailEditorSmoke/inline_flag,
  the layouts switch in TestMailScreensSmoke; CSS-slot tests read the
  style block through a no-op inliner
- 35-06 Task 2 (G-35-8)
2026-11-13 21:12:01 +01:00

32 lines
1019 B
Go

package postcard
import (
"fmt"
"github.com/vanng822/go-premailer/premailer"
)
// inlineCSS copies the rules of html's style elements onto the elements they
// match, as WinterCMS does with CssToInlineStyles, so that mail clients that
// drop the style block still show the design. Rules that cannot be inlined,
// such as media queries and pseudo-classes, are kept in a style element.
// Classes are kept, and CSS widths and heights are mirrored to the width
// and height attributes (go-premailer's default options). A panic in the
// inliner is returned as an error.
func inlineCSS(html string) (out string, err error) {
defer func() {
if r := recover(); r != nil {
out, err = "", fmt.Errorf("postcard: inline CSS: %v", r)
}
}()
p, err := premailer.NewPremailerFromString(html, premailer.NewOptions())
if err != nil {
return "", fmt.Errorf("postcard: inline CSS: %w", err)
}
out, err = p.Transform()
if err != nil {
return "", fmt.Errorf("postcard: inline CSS: %w", err)
}
return out, nil
}