-
-
Notifications
You must be signed in to change notification settings - Fork 235
/
htmlcommentparser.go
39 lines (31 loc) · 966 Bytes
/
htmlcommentparser.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
package parser
import (
"github.com/a-h/parse"
)
var htmlCommentStart = parse.String("<!--")
var htmlCommentEnd = parse.String("--")
type htmlCommentParser struct {
}
var htmlComment = htmlCommentParser{}
func (p htmlCommentParser) Parse(pi *parse.Input) (n Node, ok bool, err error) {
// Comment start.
start := pi.Position()
var c HTMLComment
if _, ok, err = htmlCommentStart.Parse(pi); err != nil || !ok {
return
}
// Once we've got the comment start sequence, parse anything until the end
// sequence as the comment contents.
if c.Contents, ok, err = parse.StringUntil(htmlCommentEnd).Parse(pi); err != nil || !ok {
err = parse.Error("expected end comment literal '-->' not found", start)
return
}
// Cut the end element.
_, _, _ = htmlCommentEnd.Parse(pi)
// Cut the gt.
if _, ok, err = gt.Parse(pi); err != nil || !ok {
err = parse.Error("comment contains invalid sequence '--'", pi.Position())
return
}
return c, true, nil
}