// Serialize converts the form fields into a url.Values type. // Returns two url.Value types. The first is the form field values, and the // second is the form button values. func serializeForm(sel *goquery.Selection) ([]*Field, []*Checkbox, []*Button) { var fields []*Field var checkboxes []*Checkbox var buttons []*Button input := sel.Find("input,button,textarea,select") if input.Length() > 0 { input.Each(func(_ int, s *goquery.Selection) { name, ok := s.Attr("name") if ok { typ, ok := s.Attr("type") if s.Is("input") && ok || s.Is("textarea") { if typ == "submit" { val, ok := s.Attr("value") if !ok { val = "" } buttons = append(buttons, &Button{ name: name, value: val, }) } else if typ == "radio" { val, ok := s.Attr("value") if !ok { val = "" } _, ok = s.Attr("checked") if !ok { val = "" } if val != "" { fields = append(fields, &Field{ name: name, value: val, }) } } else if typ == "checkbox" { val, ok := s.Attr("value") if !ok { val = "" } checked := true _, ok = s.Attr("checked") if !ok { checked = false } checkboxes = append(checkboxes, &Checkbox{ name: name, value: val, checked: checked, }) } else { val, ok := s.Attr("value") if !ok { val = "" } fields = append(fields, &Field{ name: name, value: val, }) } } else if s.Is("select") { options := s.Find("option") val := "" options.Each(func(idx int, option *goquery.Selection) { _, ok := option.Attr("selected") if idx == 0 || ok { value, ok := option.Attr("value") if ok { val = value } } }) fields = append(fields, &Field{ name: name, value: val, }) } } }) } return fields, checkboxes, buttons }