parseForms parses and returns all form elements beneath node. Form values include all input and textarea elements within the form. The values of radio and checkbox inputs are included only if they are checked. In the future, we might want to allow a custom selector to be passed in to further restr
(node *html.Node)
| 36 | // In the future, we might want to allow a custom selector to be passed in to |
| 37 | // further restrict what forms will be returned. |
| 38 | func parseForms(node *html.Node) (forms []*htmlForm) { |
| 39 | if node == nil { |
| 40 | return nil |
| 41 | } |
| 42 | |
| 43 | doc := goquery.NewDocumentFromNode(node) |
| 44 | doc.Find("form").Each(func(_ int, s *goquery.Selection) { |
| 45 | form := htmlForm{Values: url.Values{}} |
| 46 | form.Action, _ = s.Attr("action") |
| 47 | form.Method, _ = s.Attr("method") |
| 48 | |
| 49 | s.Find("input").Each(func(_ int, s *goquery.Selection) { |
| 50 | name, _ := s.Attr("name") |
| 51 | if name == "" { |
| 52 | return |
| 53 | } |
| 54 | |
| 55 | typ, _ := s.Attr("type") |
| 56 | typ = strings.ToLower(typ) |
| 57 | _, checked := s.Attr("checked") |
| 58 | if (typ == "radio" || typ == "checkbox") && !checked { |
| 59 | return |
| 60 | } |
| 61 | |
| 62 | value, _ := s.Attr("value") |
| 63 | form.Values.Add(name, value) |
| 64 | }) |
| 65 | s.Find("textarea").Each(func(_ int, s *goquery.Selection) { |
| 66 | name, _ := s.Attr("name") |
| 67 | if name == "" { |
| 68 | return |
| 69 | } |
| 70 | |
| 71 | value := s.Text() |
| 72 | form.Values.Add(name, value) |
| 73 | }) |
| 74 | forms = append(forms, &form) |
| 75 | }) |
| 76 | |
| 77 | return forms |
| 78 | } |
| 79 | |
| 80 | // fetchAndSubmitForm will fetch the page at urlStr, then parse and submit the first form found. |
| 81 | // setValues will be called with the parsed form values, allowing the caller to set any custom |
searching dependent graphs…