jmcarp / robobrowser

BSD 3-Clause "New" or "Revised" License
3.7k stars 337 forks source link

Checkboxes with same name will not be grouped if some other inputs exists between them #65

Open jackyspy opened 8 years ago

jackyspy commented 8 years ago

I found that checkboxes with same name will not be grouped if some other inputs exists between them.

from robobrowser.forms.form import _parse_fields

html = '''
            <input type="checkbox" name="member" value="mercury" checked />vocals<br />
            <input type="checkbox" name="member" value="may" />guitar<br />
            <input type="text" /><br />
            <input type="checkbox" name="member" value="taylor" />drums<br />
            <input type="checkbox" name="member" value="deacon" checked />bass<br />
        '''
_fields = _parse_fields(BeautifulSoup(html))
for cbx in _fields:
    print(cbx.name, cbx.options)

which output

member ['mercury', 'may']
member ['taylor', 'deacon']

As seen, 2 robobrowser.forms.fields.Checkbox instances were created, with the same name, options different. I thought it would be one instance, with 4 options.

Maybe it's a bug? I have no idea.

relevant code

def _group_flat_tags(tag, tags):
    """Extract tags sharing the same name as the provided tag. Used to collect
    options for radio and checkbox inputs.
    :param Tag tag: BeautifulSoup tag
    :param list tags: List of tags
    :return: List of matching tags
    """
    grouped = [tag]
    name = tag.get('name', '').lower()
    while tags and tags[0].get('name', '').lower() == name: # <----  HERE
        grouped.append(tags.pop(0))
    return grouped
jasonslay commented 7 years ago

I can confirm this. Same applies to radio buttons as well.