Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed: Parsing of empty select boxes failed #40

Merged
merged 4 commits into from Apr 18, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 deletions robobrowser/forms/fields.py
Expand Up @@ -41,7 +41,7 @@ class BaseField(object):

"""
def __init__(self, parsed):
self._parsed = helpers.ensure_soup(parsed)
self._parsed = helpers.ensure_soup(parsed, use_builtin_parser=True)
self._value = None
self.name = self._get_name(self._parsed)

Expand Down Expand Up @@ -240,7 +240,7 @@ def _set_initial(self, initial):

"""
super(Select, self)._set_initial(initial)
if not self._value:
if not self._value and self.options:
self.value = self.options[0]


Expand Down
10 changes: 7 additions & 3 deletions robobrowser/helpers.py
Expand Up @@ -57,10 +57,11 @@ def find(soup, name=None, attrs=None, recursive=True, text=None, **kwargs):
return tags[0]


def ensure_soup(value):
def ensure_soup(value, use_builtin_parser=False):
"""Coerce a value (or list of values) to Tag (or list of Tag).

:param value: String, BeautifulSoup, Tag, or list of the above
:param use_builtin_parser: Boolean - if True, then force uses builtin parser instead of lxml
:return: Tag or list of Tags

"""
Expand All @@ -70,10 +71,13 @@ def ensure_soup(value):
return value
if isinstance(value, list):
return [
ensure_soup(item)
ensure_soup(item, use_builtin_parser)
for item in value
]
parsed = BeautifulSoup(value)
if use_builtin_parser:
parsed = BeautifulSoup(value, 'html.parser')
else:
parsed = BeautifulSoup(value)
return parsed.find()


Expand Down
3 changes: 2 additions & 1 deletion setup.py
Expand Up @@ -4,6 +4,7 @@
import os
import re
import sys
import pip

from setuptools import setup, find_packages
from pip.req import parse_requirements
Expand Down Expand Up @@ -35,7 +36,7 @@ def find_version(fname):
history = open('HISTORY.rst').read().replace('.. :changelog:', '')
requirements = [
str(requirement.req)
for requirement in parse_requirements('requirements.txt')
for requirement in parse_requirements('requirements.txt', session=pip.download.PipSession())
]

setup(
Expand Down
14 changes: 12 additions & 2 deletions tests/test_forms.py
Expand Up @@ -218,6 +218,16 @@ def test_parse_select(self):
assert_equal(len(_fields), 1)
assert_true(isinstance(_fields[0], fields.Select))

def test_parse_empty_select(self):
html = '''
<select name="instrument"></select>
'''
_fields = _parse_fields(BeautifulSoup(html))
assert_equal(len(_fields), 1)
assert_true(isinstance(_fields[0], fields.Select))
assert_equal(_fields[0].value, '')
assert_equal(_fields[0].options, [])

def test_parse_select_multi(self):
html = '''
<select name="instrument" multiple>
Expand Down Expand Up @@ -615,7 +625,7 @@ def test_select_default(self):
<select name="select">
<option>opt</option>
</select>
''')
''', 'html.parser')
select = fields.Select(parsed)
assert_equal(select.options, ['sel'])

Expand All @@ -624,6 +634,6 @@ def test_multi_select_default(self):
<select name="select" multiple>
<option>opt</option>
</select>
''')
''', 'html.parser')
select = fields.Select(parsed)
assert_equal(select.options, ['sel'])