MCPcopy Index your code
hub / github.com/google/go-github / parseForms

Function parseForms

scrape/forms.go:38–78  ·  view source on GitHub ↗

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)

Source from the content-addressed store, hash-verified

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.
38func 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

Callers 2

fetchAndSubmitFormFunction · 0.85
Test_ParseFormsFunction · 0.85

Calls 1

AddMethod · 0.80

Tested by 1

Test_ParseFormsFunction · 0.68

Used in the wild real call sites across dependent graphs

searching dependent graphs…