Beispiel #1
0
// attributeOrDefault reads an attribute and returns it or the default value when it's empty.
func (bow *Browser) attrOrDefault(name, def string, sel *goquery.Selection) string {
	a, ok := sel.Attr(name)
	if ok {
		return a
	}
	return def
}
Beispiel #2
0
// attributeToUrl reads an attribute from an element and returns a url.
func (bow *Browser) attrToResolvedUrl(name string, sel *goquery.Selection) (*url.URL, error) {
	src, ok := sel.Attr(name)
	if !ok {
		return nil, errors.NewAttributeNotFound(
			"Attribute '%s' not found.", name)
	}
	ur, err := url.Parse(src)
	if err != nil {
		return nil, err
	}

	return bow.ResolveUrl(ur), nil
}
Beispiel #3
0
func formAttributes(bow Browsable, s *goquery.Selection) (string, string) {
	method, ok := s.Attr("method")
	if !ok {
		method = "GET"
	}
	action, ok := s.Attr("action")
	if !ok {
		action = bow.Url().String()
	}
	aurl, err := url.Parse(action)
	if err != nil {
		return "", ""
	}
	aurl = bow.ResolveUrl(aurl)

	return strings.ToUpper(method), aurl.String()
}
Beispiel #4
0
// 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
}