-
-
Notifications
You must be signed in to change notification settings - Fork 273
/
doctypeparser.go
53 lines (43 loc) · 1.19 KB
/
doctypeparser.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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
package parser
import (
"io"
"github.com/a-h/lexical/parse"
)
func newDocTypeParser() docTypeParser {
return docTypeParser{}
}
type docTypeParser struct {
}
var doctypeStartParser = parse.StringInsensitive("<!doctype ")
func (p docTypeParser) Parse(pi parse.Input) parse.Result {
var r DocType
from := NewPositionFromInput(pi)
dtr := doctypeStartParser(pi)
if dtr.Error != nil && dtr.Error != io.EOF {
return dtr
}
if !dtr.Success {
return parse.Failure("docTypeParser", nil)
}
// Once a doctype has started, take everything until the end.
tagOpen := parse.Rune('<')
tagClose := parse.Rune('>')
dtr = parse.StringUntil(parse.Or(tagClose, tagOpen))(pi)
if dtr.Error != nil && dtr.Error != io.EOF {
return dtr
}
if !dtr.Success {
return parse.Failure("docTypeParser", newParseError("unclosed DOCTYPE", from, NewPositionFromInput(pi)))
}
r.Value = dtr.Item.(string)
// Clear the final '>'.
from = NewPositionFromInput(pi)
dtr = tagClose(pi)
if dtr.Error != nil && dtr.Error != io.EOF {
return dtr
}
if !dtr.Success {
return parse.Failure("docTypeParser", newParseError("unclosed DOCTYPE", from, NewPositionFromInput(pi)))
}
return parse.Success("docTypeParser", r, nil)
}