forked from dexidp/dex
-
Notifications
You must be signed in to change notification settings - Fork 0
/
html.go
43 lines (37 loc) · 836 Bytes
/
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
package html
import (
"io"
"net/url"
"github.com/PuerkitoBio/goquery"
"golang.org/x/net/html"
)
// FormValues will return the values of a form on an html document.
func FormValues(formSelector string, body io.Reader) (url.Values, error) {
doc, err := goquery.NewDocumentFromReader(body)
if err != nil {
return nil, err
}
values := url.Values{}
form := doc.Find(formSelector)
inputs := form.Find("input")
for _, input := range inputs.Nodes {
inputName, ok := attrValue(input.Attr, "name")
if !ok {
continue
}
val, ok := attrValue(input.Attr, "value")
if !ok {
continue
}
values.Add(inputName, val)
}
return values, nil
}
func attrValue(attrs []html.Attribute, name string) (string, bool) {
for _, attr := range attrs {
if attr.Key == name {
return attr.Val, true
}
}
return "", false
}