Debian Codegraph @

form.go

raw symbols SCIP
1 package gosubmit
2
3 import (
4 "fmt"
5 "net/http"
6
7 "golang.org/x/net/html"
8 )
9
10 type errorContainer struct {
11 err error
12 }
13
14 type Forms []Form
15
16 func (e *errorContainer) setError(err error) {
17 if e.err == nil {
18 e.err = err
19 }
20 }
21
22 func (e *errorContainer) Err() error {
23 return e.err
24 }
25
26 type Document struct {
27 errorContainer
28 forms Forms
29 }
30
31 type Inputs map[string]Input
32
33 func (d Document) Forms() Forms {
34 return d.forms
35 }
36
37 func (forms Forms) First() (form Form) {
38 if len(forms) == 0 {
39 form.Inputs = make(Inputs)
40 form.setError(fmt.Errorf("No forms found"))
41 return
42 }
43 return forms[0]
44 }
45
46 func (forms Forms) Last() (form Form) {
47 size := len(forms)
48 if size == 0 {
49 form.Inputs = make(Inputs)
50 form.setError(fmt.Errorf("No forms found"))
51 return
52 }
53 return forms[size-1]
54 }
55
56 func (d Document) FirstForm() (form Form) {
57 return d.forms.First()
58 }
59
60 func (d Document) FindForm(attrKey string, attrValue string) (form Form) {
61 for _, f := range d.forms {
62 for _, attr := range f.Attr {
63 if attr.Key == attrKey && attr.Val == attrValue {
64 return f
65 }
66 }
67 }
68
69 form.Inputs = make(Inputs)
70 form.setError(fmt.Errorf("No form with attributes %s='%s' found", attrKey, attrValue))
71 return
72 }
73
74 func (d Document) FindFormsByClass(className string) (forms Forms) {
75 for _, f := range d.forms {
76 for _, class := range f.ClassList {
77 if class == className {
78 forms = append(forms, f)
79 }
80 }
81 }
82 return
83 }
84
85 type Form struct {
86 errorContainer
87 // All html attributes of the form. Used to find the form by attribute
88 Attr []html.Attribute
89 ClassList []string
90 // Value of Enctype attribute, default is application/x-www-form-urlencoded.
91 // For forms with file uploads it should be multipart/form-data.
92 ContentType string
93 // All found inputs
94 Inputs Inputs
95 // Value of form method attribute
96 Method string
97 // Value form action attribute
98 URL string
99 // All found <button type="submit"> and <input type="submit"> elements.
100 Buttons []Button
101 }
102
103 // Returns true if field is required, false otherwise.
104 func (f Form) IsRequired(name string) bool {
105 input, ok := f.Inputs[name]
106 return ok && input.Required()
107 }
108
109 func (f Form) newFiller(opts []Option) (filler *filler, err error) {
110 if f.err != nil {
111 err = f.err
112 return
113 }
114 filler, err = newFiller(f, opts)
115 return
116 }
117
118 // Fills the form and returns an error if there was an error. Useful for
119 // testing.
120 func (f Form) Validate(opts ...Option) error {
121 _, err := f.newFiller(opts)
122 return err
123 }
124
125 // Fills the form and returns a new request. If there was any error in the
126 // parsing or if the form was filled incorrectly, it will return an error.
127 func (f Form) NewRequest(opts ...Option) (*http.Request, error) {
128 filler, err := f.newFiller(opts)
129 if err != nil {
130 return nil, err
131 }
132 return filler.NewRequest()
133 }
134
135 // Fills the form and returns a new test request. If there was any error in the
136 // parsing or if the form was filled incorrectly, it will return an error.
137 func (f Form) NewTestRequest(opts ...Option) (*http.Request, error) {
138 filler, err := f.newFiller(opts)
139 if err != nil {
140 return nil, err
141 }
142 return filler.NewTestRequest()
143 }
144
145 // Fills the form and returns parameters for a multipart request. If there was
146 // any error in the parsing or if the form was filled incorrectly, it will
147 // return an error.
148 func (f Form) MultipartParams(opts ...Option) (boundary string, data []byte, err error) {
149 filler, err := f.newFiller(opts)
150 if err != nil {
151 return "", nil, err
152 }
153 return filler.BuildMultipart()
154 }
155
156 // Fills the form and returns query parameters for a GET request.
157 func (f Form) GetParams(opts ...Option) (string, error) {
158 filler, err := f.newFiller(opts)
159 if err != nil {
160 return "", err
161 }
162 return filler.BuildGet()
163 }
164
165 // Fills the form and returns body for a POST request.
166 func (f Form) PostParams(opts ...Option) ([]byte, error) {
167 filler, err := f.newFiller(opts)
168 if err != nil {
169 return nil, err
170 }
171 return filler.BuildPost()
172 }
173
174 // Returns a list of available input values for elements with options
175 // (checkbox, radio or select).
176 func (f Form) GetOptionsFor(name string) (options []string) {
177 input, ok := f.Inputs[name]
178 if !ok {
179 return
180 }
181 return input.Options()
182 }
183
184 func (f Form) Testing(t test) TestingForm {
185 return TestingForm{form: f, t: t}
186 }