This repository has been archived on 2022-08-17. You can view files and clone it, but cannot push or open issues or pull requests.
dex/pkg/html/html.go

44 lines
836 B
Go
Raw Normal View History

2015-08-18 05:57:27 +05:30
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
}