package gosubmit import ( "fmt" "net/http" "golang.org/x/net/html" ) type errorContainer struct { err error } type Forms []Form func (e *errorContainer) setError(err error) { if e.err == nil { e.err = err } } func (e *errorContainer) Err() error { return e.err } type Document struct { errorContainer forms Forms } type Inputs map[string]Input func (d Document) Forms() Forms { return d.forms } func (forms Forms) First() (form Form) { if len(forms) == 0 { form.Inputs = make(Inputs) form.setError(fmt.Errorf("No forms found")) return } return forms[0] } func (forms Forms) Last() (form Form) { size := len(forms) if size == 0 { form.Inputs = make(Inputs) form.setError(fmt.Errorf("No forms found")) return } return forms[size-1] } func (d Document) FirstForm() (form Form) { return d.forms.First() } func (d Document) FindForm(attrKey string, attrValue string) (form Form) { for _, f := range d.forms { for _, attr := range f.Attr { if attr.Key == attrKey && attr.Val == attrValue { return f } } } form.Inputs = make(Inputs) form.setError(fmt.Errorf("No form with attributes %s='%s' found", attrKey, attrValue)) return } func (d Document) FindFormsByClass(className string) (forms Forms) { for _, f := range d.forms { for _, class := range f.ClassList { if class == className { forms = append(forms, f) } } } return } type Form struct { errorContainer // All html attributes of the form. Used to find the form by attribute Attr []html.Attribute ClassList []string // Value of Enctype attribute, default is application/x-www-form-urlencoded. // For forms with file uploads it should be multipart/form-data. ContentType string // All found inputs Inputs Inputs // Value of form method attribute Method string // Value form action attribute URL string // All found