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 } } } }