-
Notifications
You must be signed in to change notification settings - Fork 2
/
html.go
330 lines (309 loc) · 7.49 KB
/
html.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
package gosubmit
import (
"fmt"
"io"
"net/http"
"net/url"
"regexp"
"strings"
"golang.org/x/net/html"
)
const ContentTypeForm = "application/x-www-form-urlencoded"
const ContentTypeMultipart = "multipart/form-data"
const (
ElementSelect = "select"
ElementInput = "input"
ElementButton = "button"
ElementTextArea = "textarea"
InputTypeText = "text"
InputTypeFile = "file"
InputTypeCheckbox = "checkbox"
InputTypeRadio = "radio"
InputTypeHidden = "hidden"
InputTypeSubmit = "submit"
InputTypeEmail = "email"
InputTypeURL = "url"
InputTypeDate = "date"
InputTypeNumber = "number"
)
// Parse all formsr in the HTML document and set the default URL if <form
// action="..."> attribute is missing
func ParseWithURL(r io.Reader, defaultURL string) (doc Document) {
doc = Parse(r)
for index, form := range doc.forms {
if form.URL == "" {
form.URL = defaultURL
doc.forms[index] = form
}
}
return
}
// Parse all forms in the HTML document.
func Parse(r io.Reader) (doc Document) {
n, err := html.Parse(r)
if err != nil {
doc.setError(fmt.Errorf("Error parsing html: %w", err))
return
}
doc = findForms(n)
return
}
func ParseResponse(r *http.Response, url *url.URL) Document {
return ParseWithURL(r.Body, url.EscapedPath())
}
var PatternEmail = regexp.MustCompile("[a-z0-9._%+-]+@[a-z0-9.-]+\\.[a-z]{2,}$")
var PatternURL = regexp.MustCompile("^https?://.+")
func findForms(n *html.Node) (doc Document) {
var recursivelyFindDocument func(n *html.Node)
recursivelyFindDocument = func(n *html.Node) {
if n.Type == html.ElementNode && n.Data == "form" {
form := createForm(n)
form.setError(doc.err)
doc.forms = append(doc.forms, form)
return
}
for c := n.FirstChild; c != nil; c = c.NextSibling {
recursivelyFindDocument(c)
}
}
recursivelyFindDocument(n)
return doc
}
func getCheckbox(inputs Inputs, name string) (checkbox Checkbox, ok bool) {
input, exists := inputs[name]
ok = exists
if !ok {
return
}
checkbox, isCheckbox := input.(Checkbox)
ok = isCheckbox
return
}
func getRadio(inputs Inputs, name string) (radio Radio, ok bool) {
input, exists := inputs[name]
ok = exists
if !ok {
return
}
radio, isRadio := input.(Radio)
ok = isRadio
return
}
func getPattern(n *html.Node, defaultPattern *regexp.Regexp) *regexp.Regexp {
p := getAttr(n, "pattern")
if p == "" {
return defaultPattern
}
if !strings.HasPrefix(p, "^") {
p = "^" + p
}
if !strings.HasSuffix(p, "$") {
p = p + "$"
}
return regexp.MustCompile(p)
}
func createForm(n *html.Node) (form Form) {
inputs := Inputs{}
var recursivelyFindInputs func(n *html.Node)
recursivelyFindInputs = func(n *html.Node) {
if n.Type != html.ElementNode {
return
}
inputType := getAttr(n, "type")
name := getAttr(n, "name")
required := hasAttr(n, "required")
switch n.Data {
case "select":
values, options, _ := findSelectOptions(n)
inputs[name] = Select{
inputWithOptions: inputWithOptions{
anyInput: anyInput{
name: name,
inputType: inputType,
values: values,
required: required,
},
multiple: hasAttr(n, "multiple"),
options: options,
},
}
case "input":
value := getAttr(n, "value")
anyInput := anyInput{
name: name,
inputType: inputType,
values: []string{value},
required: required,
}
switch inputType {
case InputTypeCheckbox:
i, ok := getCheckbox(inputs, name)
if !ok {
i = Checkbox{
inputWithOptions: inputWithOptions{
anyInput: anyInput,
options: []string{},
multiple: true,
},
}
i.values = []string{}
}
if hasAttr(n, "checked") {
i.values = append(i.values, value)
}
i.options = append(i.options, value)
i.required = i.required || hasAttr(n, "required")
inputs[name] = i
case InputTypeFile:
inputs[name] = FileInput{
anyInput: anyInput,
}
case InputTypeRadio:
i, ok := getRadio(inputs, name)
if !ok {
i = Radio{
inputWithOptions: inputWithOptions{
anyInput: anyInput,
options: []string{},
},
}
i.values = []string{}
}
i.options = append(i.options, value)
i.required = i.required || hasAttr(n, "required")
if hasAttr(n, "checked") {
i.values = append(i.values, value)
}
// need to reassing because map has plain struct (no pointers)
inputs[name] = i
case InputTypeHidden:
inputs[name] = HiddenInput{
anyInput: anyInput,
}
case InputTypeSubmit:
form.Buttons = append(form.Buttons, Button{
Name: name,
Value: getAttr(n, "value"),
})
case InputTypeEmail:
textInput := createTextInput(anyInput, n)
textInput.pattern = PatternEmail
inputs[name] = EmailInput{
TextInput: textInput,
}
case InputTypeURL:
textInput := createTextInput(anyInput, n)
textInput.pattern = PatternURL
inputs[name] = URLInput{
TextInput: textInput,
}
case InputTypeDate:
inputs[name] = DateInput{
anyInput: anyInput,
}
case InputTypeNumber:
inputs[name] = NumberInput{
anyInput: anyInput,
min: atoi(getAttr(n, "min")),
max: atoi(getAttr(n, "max")),
}
default:
inputs[name] = createTextInput(anyInput, n)
}
case ElementTextArea:
inputs[name] = TextInput{
anyInput: anyInput{
name: name,
inputType: "textarea",
values: []string{getText(n)},
},
minLength: atoi(getAttr(n, "minlength")),
maxLength: atoi(getAttr(n, "maxlength")),
}
case ElementButton:
if inputType == "submit" {
form.Buttons = append(form.Buttons, Button{
Name: name,
Value: getAttr(n, "value"),
})
}
default:
for c := n.FirstChild; c != nil; c = c.NextSibling {
recursivelyFindInputs(c)
}
}
}
recursivelyFindInputs(n)
form.Inputs = inputs
form.ContentType = getAttr(n, "enctype")
if form.ContentType == "" {
form.ContentType = ContentTypeForm
}
form.Method = strings.ToUpper(getAttr(n, "method"))
if form.Method == "" {
form.Method = http.MethodGet
}
form.ClassList = strings.Split(getAttr(n, "class"), " ")
form.URL = getAttr(n, "action")
form.Attr = n.Attr
return
}
func getText(n *html.Node) string {
var b strings.Builder
var recursivelyGetText func(n *html.Node)
recursivelyGetText = func(n *html.Node) {
if n.Type == html.TextNode {
b.WriteString(n.Data)
return
}
for c := n.FirstChild; c != nil; c = c.NextSibling {
recursivelyGetText(c)
}
}
recursivelyGetText(n)
return b.String()
}
func findSelectOptions(n *html.Node) (values []string, options []string, ok bool) {
ok = true
for c := n.FirstChild; c != nil; c = c.NextSibling {
if c.Type == html.ElementNode && c.Data == "option" && !hasAttr(c, "disabled") {
value := getAttr(c, "value")
options = append(options, value)
if hasAttr(c, "selected") {
values = append(values, value)
}
}
}
return
}
func getAttr(n *html.Node, key string) (value string) {
value, _ = getAttrOK(n, key)
return
}
func hasAttr(n *html.Node, name string) bool {
for _, attr := range n.Attr {
if attr.Key == name {
return true
}
}
return false
}
func getAttrOK(n *html.Node, key string) (value string, ok bool) {
ok = false
for _, attr := range n.Attr {
if attr.Key == key {
ok = true
value = attr.Val
return
}
}
return
}
func createTextInput(anyInput anyInput, n *html.Node) TextInput {
return TextInput{
anyInput: anyInput,
pattern: getPattern(n, nil),
minLength: atoi(getAttr(n, "minlength")),
maxLength: atoi(getAttr(n, "maxlength")),
}
}