Files
GiteaMailTemplates/tools/builder/anchors.go
T
KenanZhu fec3ace600
Release / Validate Templates (push) Canceled after 0s
Release / Package & Release (push) Canceled after 0s
Release / Update Latest Release Documentation (push) Canceled after 0s
chore: migrate mail themes to shared framework and locked upstream inputs
2026-10-09 18:34:36 +08:00

43 lines
856 B
Go

package builder
import (
"bytes"
"fmt"
"golang.org/x/net/html"
"io"
)
func BodyAnchors(masked []byte) ([2]int, error) {
var anchors [2]int
opens, closes, offset := 0, 0, 0
z := html.NewTokenizer(bytes.NewReader(masked))
for {
kind := z.Next()
raw := z.Raw()
start := offset
offset += len(raw)
switch kind {
case html.ErrorToken:
if z.Err() != io.EOF {
return anchors, z.Err()
}
if opens != 1 || closes != 1 || anchors[0] >= anchors[1] {
return anchors, fmt.Errorf("expected exactly one static body start and end")
}
return anchors, nil
case html.StartTagToken:
tag, _ := z.TagName()
if bytes.Equal(tag, []byte("body")) {
opens++
anchors[0] = offset
}
case html.EndTagToken:
tag, _ := z.TagName()
if bytes.Equal(tag, []byte("body")) {
closes++
anchors[1] = start
}
}
}
}