| 1 |
package gosubmit |
| 2 |
|
| 3 |
import ( |
| 4 |
"fmt" |
| 5 |
"io" |
| 6 |
"net/http" |
| 7 |
"net/url" |
| 8 |
"regexp" |
| 9 |
"strings" |
| 10 |
|
| 11 |
"golang.org/x/net/html" |
| 12 |
) |
| 13 |
|
| 14 |
const ContentTypeForm = "application/x-www-form-urlencoded" |
| 15 |
const ContentTypeMultipart = "multipart/form-data" |
| 16 |
|
| 17 |
const ( |
| 18 |
ElementSelect = "select" |
| 19 |
ElementInput = "input" |
| 20 |
ElementButton = "button" |
| 21 |
ElementTextArea = "textarea" |
| 22 |
|
| 23 |
InputTypeText = "text" |
| 24 |
InputTypeFile = "file" |
| 25 |
InputTypeCheckbox = "checkbox" |
| 26 |
InputTypeRadio = "radio" |
| 27 |
InputTypeHidden = "hidden" |
| 28 |
InputTypeSubmit = "submit" |
| 29 |
InputTypeEmail = "email" |
| 30 |
InputTypeURL = "url" |
| 31 |
InputTypeDate = "date" |
| 32 |
InputTypeNumber = "number" |
| 33 |
) |
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
func ParseWithURL(r io.Reader, defaultURL string) (doc Document) { |
| 38 |
doc = Parse(r) |
| 39 |
for index, form := range doc.forms { |
| 40 |
if form.URL == "" { |
| 41 |
form.URL = defaultURL |
| 42 |
doc.forms[index] = form |
| 43 |
} |
| 44 |
} |
| 45 |
return |
| 46 |
} |
| 47 |
|
| 48 |
|
| 49 |
func Parse(r io.Reader) (doc Document) { |
| 50 |
n, err := html.Parse(r) |
| 51 |
if err != nil { |
| 52 |
doc.setError(fmt.Errorf("Error parsing html: %w", err)) |
| 53 |
return |
| 54 |
} |
| 55 |
|
| 56 |
doc = findForms(n) |
| 57 |
return |
| 58 |
} |
| 59 |
|
| 60 |
func ParseResponse(r *http.Response, url *url.URL) Document { |
| 61 |
return ParseWithURL(r.Body, url.EscapedPath()) |
| 62 |
} |
| 63 |
|
| 64 |
var PatternEmail = regexp.MustCompile("[a-z0-9._%+-]+@[a-z0-9.-]+\\.[a-z]{2,}$") |
| 65 |
var PatternURL = regexp.MustCompile("^https?://.+") |
| 66 |
|
| 67 |
func findForms(n *html.Node) (doc Document) { |
| 68 |
var recursivelyFindDocument func(n *html.Node) |
| 69 |
recursivelyFindDocument = func(n *html.Node) { |
| 70 |
if n.Type == html.ElementNode && n.Data == "form" { |
| 71 |
form := createForm(n) |
| 72 |
form.setError(doc.err) |
| 73 |
doc.forms = append(doc.forms, form) |
| 74 |
return |
| 75 |
} |
| 76 |
for c := n.FirstChild; c != nil; c = c.NextSibling { |
| 77 |
recursivelyFindDocument(c) |
| 78 |
} |
| 79 |
} |
| 80 |
recursivelyFindDocument(n) |
| 81 |
return doc |
| 82 |
} |
| 83 |
|
| 84 |
func getCheckbox(inputs Inputs, name string) (checkbox Checkbox, ok bool) { |
| 85 |
input, exists := inputs[name] |
| 86 |
ok = exists |
| 87 |
if !ok { |
| 88 |
return |
| 89 |
} |
| 90 |
checkbox, isCheckbox := input.(Checkbox) |
| 91 |
ok = isCheckbox |
| 92 |
return |
| 93 |
} |
| 94 |
|
| 95 |
func getRadio(inputs Inputs, name string) (radio Radio, ok bool) { |
| 96 |
input, exists := inputs[name] |
| 97 |
ok = exists |
| 98 |
if !ok { |
| 99 |
return |
| 100 |
} |
| 101 |
radio, isRadio := input.(Radio) |
| 102 |
ok = isRadio |
| 103 |
return |
| 104 |
} |
| 105 |
|
| 106 |
func getPattern(n *html.Node, defaultPattern *regexp.Regexp) *regexp.Regexp { |
| 107 |
p := getAttr(n, "pattern") |
| 108 |
if p == "" { |
| 109 |
return defaultPattern |
| 110 |
} |
| 111 |
if !strings.HasPrefix(p, "^") { |
| 112 |
p = "^" + p |
| 113 |
} |
| 114 |
if !strings.HasSuffix(p, "$") { |
| 115 |
p = p + "$" |
| 116 |
} |
| 117 |
return regexp.MustCompile(p) |
| 118 |
} |
| 119 |
|
| 120 |
func createForm(n *html.Node) (form Form) { |
| 121 |
inputs := Inputs{} |
| 122 |
var recursivelyFindInputs func(n *html.Node) |
| 123 |
recursivelyFindInputs = func(n *html.Node) { |
| 124 |
if n.Type != html.ElementNode { |
| 125 |
return |
| 126 |
} |
| 127 |
inputType := getAttr(n, "type") |
| 128 |
name := getAttr(n, "name") |
| 129 |
required := hasAttr(n, "required") |
| 130 |
switch n.Data { |
| 131 |
case "select": |
| 132 |
values, options, _ := findSelectOptions(n) |
| 133 |
inputs[name] = Select{ |
| 134 |
inputWithOptions: inputWithOptions{ |
| 135 |
anyInput: anyInput{ |
| 136 |
name: name, |
| 137 |
inputType: inputType, |
| 138 |
values: values, |
| 139 |
required: required, |
| 140 |
}, |
| 141 |
multiple: hasAttr(n, "multiple"), |
| 142 |
options: options, |
| 143 |
}, |
| 144 |
} |
| 145 |
case "input": |
| 146 |
value := getAttr(n, "value") |
| 147 |
anyInput := anyInput{ |
| 148 |
name: name, |
| 149 |
inputType: inputType, |
| 150 |
values: []string{value}, |
| 151 |
required: required, |
| 152 |
} |
| 153 |
switch inputType { |
| 154 |
case InputTypeCheckbox: |
| 155 |
i, ok := getCheckbox(inputs, name) |
| 156 |
if !ok { |
| 157 |
i = Checkbox{ |
| 158 |
inputWithOptions: inputWithOptions{ |
| 159 |
anyInput: anyInput, |
| 160 |
options: []string{}, |
| 161 |
multiple: true, |
| 162 |
}, |
| 163 |
} |
| 164 |
i.values = []string{} |
| 165 |
} |
| 166 |
if hasAttr(n, "checked") { |
| 167 |
i.values = append(i.values, value) |
| 168 |
} |
| 169 |
i.options = append(i.options, value) |
| 170 |
i.required = i.required || hasAttr(n, "required") |
| 171 |
inputs[name] = i |
| 172 |
case InputTypeFile: |
| 173 |
inputs[name] = FileInput{ |
| 174 |
anyInput: anyInput, |
| 175 |
} |
| 176 |
case InputTypeRadio: |
| 177 |
i, ok := getRadio(inputs, name) |
| 178 |
if !ok { |
| 179 |
i = Radio{ |
| 180 |
inputWithOptions: inputWithOptions{ |
| 181 |
anyInput: anyInput, |
| 182 |
options: []string{}, |
| 183 |
}, |
| 184 |
} |
| 185 |
i.values = []string{} |
| 186 |
} |
| 187 |
i.options = append(i.options, value) |
| 188 |
i.required = i.required || hasAttr(n, "required") |
| 189 |
if hasAttr(n, "checked") { |
| 190 |
i.values = append(i.values, value) |
| 191 |
} |
| 192 |
|
| 193 |
inputs[name] = i |
| 194 |
case InputTypeHidden: |
| 195 |
inputs[name] = HiddenInput{ |
| 196 |
anyInput: anyInput, |
| 197 |
} |
| 198 |
case InputTypeSubmit: |
| 199 |
form.Buttons = append(form.Buttons, Button{ |
| 200 |
Name: name, |
| 201 |
Value: getAttr(n, "value"), |
| 202 |
}) |
| 203 |
case InputTypeEmail: |
| 204 |
textInput := createTextInput(anyInput, n) |
| 205 |
textInput.pattern = PatternEmail |
| 206 |
inputs[name] = EmailInput{ |
| 207 |
TextInput: textInput, |
| 208 |
} |
| 209 |
case InputTypeURL: |
| 210 |
textInput := createTextInput(anyInput, n) |
| 211 |
textInput.pattern = PatternURL |
| 212 |
inputs[name] = URLInput{ |
| 213 |
TextInput: textInput, |
| 214 |
} |
| 215 |
case InputTypeDate: |
| 216 |
inputs[name] = DateInput{ |
| 217 |
anyInput: anyInput, |
| 218 |
} |
| 219 |
case InputTypeNumber: |
| 220 |
inputs[name] = NumberInput{ |
| 221 |
anyInput: anyInput, |
| 222 |
min: atoi(getAttr(n, "min")), |
| 223 |
max: atoi(getAttr(n, "max")), |
| 224 |
} |
| 225 |
default: |
| 226 |
inputs[name] = createTextInput(anyInput, n) |
| 227 |
} |
| 228 |
case ElementTextArea: |
| 229 |
inputs[name] = TextInput{ |
| 230 |
anyInput: anyInput{ |
| 231 |
name: name, |
| 232 |
inputType: "textarea", |
| 233 |
values: []string{getText(n)}, |
| 234 |
}, |
| 235 |
minLength: atoi(getAttr(n, "minlength")), |
| 236 |
maxLength: atoi(getAttr(n, "maxlength")), |
| 237 |
} |
| 238 |
case ElementButton: |
| 239 |
if inputType == "submit" { |
| 240 |
form.Buttons = append(form.Buttons, Button{ |
| 241 |
Name: name, |
| 242 |
Value: getAttr(n, "value"), |
| 243 |
}) |
| 244 |
} |
| 245 |
default: |
| 246 |
for c := n.FirstChild; c != nil; c = c.NextSibling { |
| 247 |
recursivelyFindInputs(c) |
| 248 |
} |
| 249 |
} |
| 250 |
} |
| 251 |
recursivelyFindInputs(n) |
| 252 |
form.Inputs = inputs |
| 253 |
form.ContentType = getAttr(n, "enctype") |
| 254 |
if form.ContentType == "" { |
| 255 |
form.ContentType = ContentTypeForm |
| 256 |
} |
| 257 |
form.Method = strings.ToUpper(getAttr(n, "method")) |
| 258 |
if form.Method == "" { |
| 259 |
form.Method = http.MethodGet |
| 260 |
} |
| 261 |
form.ClassList = strings.Split(getAttr(n, "class"), " ") |
| 262 |
form.URL = getAttr(n, "action") |
| 263 |
form.Attr = n.Attr |
| 264 |
return |
| 265 |
} |
| 266 |
|
| 267 |
func getText(n *html.Node) string { |
| 268 |
var b strings.Builder |
| 269 |
var recursivelyGetText func(n *html.Node) |
| 270 |
recursivelyGetText = func(n *html.Node) { |
| 271 |
if n.Type == html.TextNode { |
| 272 |
b.WriteString(n.Data) |
| 273 |
return |
| 274 |
} |
| 275 |
for c := n.FirstChild; c != nil; c = c.NextSibling { |
| 276 |
recursivelyGetText(c) |
| 277 |
} |
| 278 |
} |
| 279 |
recursivelyGetText(n) |
| 280 |
return b.String() |
| 281 |
} |
| 282 |
|
| 283 |
func findSelectOptions(n *html.Node) (values []string, options []string, ok bool) { |
| 284 |
ok = true |
| 285 |
for c := n.FirstChild; c != nil; c = c.NextSibling { |
| 286 |
if c.Type == html.ElementNode && c.Data == "option" && !hasAttr(c, "disabled") { |
| 287 |
value := getAttr(c, "value") |
| 288 |
options = append(options, value) |
| 289 |
if hasAttr(c, "selected") { |
| 290 |
values = append(values, value) |
| 291 |
} |
| 292 |
} |
| 293 |
} |
| 294 |
return |
| 295 |
} |
| 296 |
|
| 297 |
func getAttr(n *html.Node, key string) (value string) { |
| 298 |
value, _ = getAttrOK(n, key) |
| 299 |
return |
| 300 |
} |
| 301 |
|
| 302 |
func hasAttr(n *html.Node, name string) bool { |
| 303 |
for _, attr := range n.Attr { |
| 304 |
if attr.Key == name { |
| 305 |
return true |
| 306 |
} |
| 307 |
} |
| 308 |
return false |
| 309 |
} |
| 310 |
|
| 311 |
func getAttrOK(n *html.Node, key string) (value string, ok bool) { |
| 312 |
ok = false |
| 313 |
for _, attr := range n.Attr { |
| 314 |
if attr.Key == key { |
| 315 |
ok = true |
| 316 |
value = attr.Val |
| 317 |
return |
| 318 |
} |
| 319 |
} |
| 320 |
return |
| 321 |
} |
| 322 |
|
| 323 |
func createTextInput(anyInput anyInput, n *html.Node) TextInput { |
| 324 |
return TextInput{ |
| 325 |
anyInput: anyInput, |
| 326 |
pattern: getPattern(n, nil), |
| 327 |
minLength: atoi(getAttr(n, "minlength")), |
| 328 |
maxLength: atoi(getAttr(n, "maxlength")), |
| 329 |
} |
| 330 |
} |