headzoo / surf

Stateful programmatic web browsing in Go.
MIT License
1.49k stars 160 forks source link

Check for radio input fails #109

Open klokare opened 6 years ago

klokare commented 6 years ago

The code in serializeForm only adds radio fields that are checked but this code also does not properly identify the checked attribute properly:

if c, found := s.Attr("checked"); found && strings.ToLower(c) == "checked" {
    fields.Add(name, val)
}

s.Attr("checked") returns an empty string even if the element is found because goquery.Selection.Attr returns the value not the name of the attribute and checked attributes have no value, they are either present or not. So c will not equal "checked". Since only 1 radio input for a given name can be checked, the above code could be rewritten as

if c, found := s.Attr("checked"); found {
    fields.Add(name, val)
}

If you believe I am correct about this and feel like it is a good solution, I am happy to put together a pull request.

jtwatson commented 6 years ago

HTML 4 & 5 will accept the condensed version <input type="radio" value="2" checked> but XHTML will not. So the change would need to support both versions.

<input type="radio" value="2" checked> and <input type="radio" value="2" checked="checked">

A fix was just done for the <select> tag in #108. Looks like we need to extend this to <input> tag for radio and checkbox.