package tasks import ( "html" htmltemplate "html/template" "regexp" "strconv" "strings" "github.com/warmbly/warmbly/internal/pkg/tmplfuncs" ) // htmlBody is a compiled HTML body plus the comments shielded from the escaper, // which would otherwise strip them (Outlook's conditional comments among them). type htmlBody struct { t *htmltemplate.Template comments []string } var htmlTmplCache tmplfuncs.TypedCache[*htmlBody] var htmlComment = regexp.MustCompile(`(?s)`) // Placeholders hold NUL, which the escaper never emits and the source is cleared of. const ( shieldOpen = "\x00wo\x00" shieldClose = "\x00wc\x00" shieldBang = "\x00wb\x00" ) var shieldToken = regexp.MustCompile("\x00w(o|c|b|[0-9]+)\x00") // shieldComments swaps each comment for a placeholder; a comment holding an // action keeps its body in place, so that action is escaped like any other. func shieldComments(src string) (string, []string) { var comments []string out := htmlComment.ReplaceAllStringFunc(src, func(c string) string { if strings.Contains(c, "{{") { return shieldOpen + strings.ReplaceAll(c[4:len(c)-3], "" case 'b': return "= len(h.comments) { return "" } return h.comments[i] }) } // compiledHTMLTemplate returns the cached HTML body for tmpl, nil when it does not compile. func compiledHTMLTemplate(tmpl string) *htmlBody { if body, ok := htmlTmplCache.Load(tmpl); ok { return body } src, comments := shieldComments(strings.ReplaceAll(tmpl, "\x00", "")) t, err := tmplfuncs.CompileHTML("body", normalizeTemplate(src)) if err != nil { htmlTmplCache.Store(tmpl, nil) return nil } body := &htmlBody{t: t, comments: comments} htmlTmplCache.Store(tmpl, body) return body } type htmlState int const ( inText htmlState = iota inComment inTag inQuotedValue inUnquotedValue inRawText ) // htmlScanner tracks where a fallback render is in the document, so each value // it inserts is escaped for that place. type htmlScanner struct { state htmlState quote byte name strings.Builder attr string tag string started bool } // htmlPosition is where one value lands. type htmlPosition struct { state htmlState attr string tag string started bool } func (s *htmlScanner) position() htmlPosition { return htmlPosition{state: s.state, attr: s.attr, tag: s.tag, started: s.started} } // sawValue records that a value was written into the current attribute. func (s *htmlScanner) sawValue() { if s.state == inQuotedValue { s.started = true } } func isLetter(c byte) bool { return c|0x20 >= 'a' && c|0x20 <= 'z' } func (s *htmlScanner) feed(text string) { for i := 0; i < len(text); i++ { c := text[i] switch s.state { case inText: switch { case strings.HasPrefix(text[i:], "") { s.state, i = inText, i+2 } case inTag: switch { case c == '>': s.state = inText if s.tag == "script" || s.tag == "style" || s.tag == "textarea" || s.tag == "title" { s.state = inRawText } case c == '=': s.attr = strings.ToLower(strings.TrimSpace(s.name.String())) s.name.Reset() j := i + 1 for j < len(text) && (text[j] == ' ' || text[j] == '\t' || text[j] == '\n' || text[j] == '\r') { j++ } if j < len(text) && (text[j] == '"' || text[j] == '\'') { s.state, s.quote, s.started, i = inQuotedValue, text[j], false, j } else { s.state, i = inUnquotedValue, j-1 } case c == ' ' || c == '\t' || c == '\n' || c == '\r' || c == '/': s.name.Reset() default: s.name.WriteByte(c) } case inQuotedValue: if c == s.quote { s.state, s.attr = inTag, "" } else if c != ' ' && c != '\t' && c != '\n' && c != '\r' { s.started = true } case inUnquotedValue: switch c { case ' ', '\t', '\n', '\r': s.state, s.attr = inTag, "" case '>': s.state, s.attr = inTag, "" i-- } case inRawText: if c == '<' && strings.HasPrefix(strings.ToLower(text[i:]), "= '0' && c <= '9' || c == '-' || c == '.' || c == '_' || c == '~' { b.WriteByte(c) continue } b.WriteByte('%') b.WriteByte(hex[c>>4]) b.WriteByte(hex[c&15]) } return b.String() }