mirror of https://github.com/dexidp/dex.git
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
44 lines
836 B
44 lines
836 B
|
11 years ago
|
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
|
||
|
|
}
|