summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorscoder <stefan_ml@behnel.de>2017-02-17 08:38:38 +0100
committerGitHub <noreply@github.com>2017-02-17 08:38:38 +0100
commit47741a8e3948b19888c7fd03f574cea1ff74be7b (patch)
tree42d2e3be6dfcf1f0b4c74a4e1c41fd203ac81553
parentd468ed25fca8ee21121741511c58a64cc26b04ef (diff)
parentc75a91308cb3c39dba27a917c5a10eb53c77470f (diff)
downloadpython-lxml-47741a8e3948b19888c7fd03f574cea1ff74be7b.tar.gz
Merge pull request #228 from advarisk/fix-select-option-values
fix html.SelectElement stripping whitespace from <option> values
-rw-r--r--src/lxml/html/__init__.py25
-rw-r--r--src/lxml/html/tests/test_forms.txt26
2 files changed, 31 insertions, 20 deletions
diff --git a/src/lxml/html/__init__.py b/src/lxml/html/__init__.py
index b0ab2c1e..c0297d67 100644
--- a/src/lxml/html/__init__.py
+++ b/src/lxml/html/__init__.py
@@ -1327,9 +1327,7 @@ class SelectElement(InputMixin, HtmlElement):
if el.get('selected') is not None:
value = el.get('value')
if value is None:
- value = el.text or ''
- if value:
- value = value.strip()
+ value = (el.text or '').strip()
return value
return None
@@ -1344,13 +1342,10 @@ class SelectElement(InputMixin, HtmlElement):
return
checked_option = None
if value is not None:
- value = value.strip()
for el in _options_xpath(self):
opt_value = el.get('value')
if opt_value is None:
- opt_value = el.text or ''
- if opt_value:
- opt_value = opt_value.strip()
+ opt_value = (el.text or '').strip()
if opt_value == value:
checked_option = el
break
@@ -1381,9 +1376,7 @@ class SelectElement(InputMixin, HtmlElement):
for el in _options_xpath(self):
value = el.get('value')
if value is None:
- value = el.text or ''
- if value:
- value = value.strip()
+ value = (el.text or '').strip()
options.append(value)
return options
@@ -1428,18 +1421,14 @@ class MultipleSelectOptions(SetMixin):
if 'selected' in option.attrib:
opt_value = option.get('value')
if opt_value is None:
- opt_value = option.text or ''
- if opt_value:
- opt_value = opt_value.strip()
+ opt_value = (option.text or '').strip()
yield opt_value
def add(self, item):
for option in self.options:
opt_value = option.get('value')
if opt_value is None:
- opt_value = option.text or ''
- if opt_value:
- opt_value = opt_value.strip()
+ opt_value = (option.text or '').strip()
if opt_value == item:
option.set('selected', '')
break
@@ -1451,9 +1440,7 @@ class MultipleSelectOptions(SetMixin):
for option in self.options:
opt_value = option.get('value')
if opt_value is None:
- opt_value = option.text or ''
- if opt_value:
- opt_value = opt_value.strip()
+ opt_value = (option.text or '').strip()
if opt_value == item:
if 'selected' in option.attrib:
del option.attrib['selected']
diff --git a/src/lxml/html/tests/test_forms.txt b/src/lxml/html/tests/test_forms.txt
index 25773013..e475587b 100644
--- a/src/lxml/html/tests/test_forms.txt
+++ b/src/lxml/html/tests/test_forms.txt
@@ -28,6 +28,14 @@
... <option value="3">number 3</option>
... <option>number 4</option>
... </select>
+... <select name="select3">
+... <option value="01 " selected>text 1</option>
+... <option value=" 02">text 2</option>
+... </select>
+... <select name="select4" multiple>
+... <option value="01 " selected>text 1</option>
+... <option value=" 02">text 2</option>
+... </select>
... <input type="file" name="file_field" value="nonsense_value">
... <input type="submit" name="submit1" value="submit">
... <input type="submit" name="submit2" value="submit">
@@ -133,10 +141,24 @@ ValueError: There is no option with the value 'asdf'
>>> select.value.remove('number 4')
>>> select.value_options
['1', '2', '3', 'number 4']
+>>> select = f.inputs['select3']
+>>> select.value
+'01 '
+>>> select.value_options
+['01 ', ' 02']
+>>> select.value = " 02"
+>>> select.value
+' 02'
+>>> select = f.inputs['select4']
+>>> select.value # doctest:+NOPARSE_MARKUP
+<MultipleSelectOptions {'01 '} for select name='select4'>
+>>> select.value.add(' 02')
+>>> select.value # doctest:+NOPARSE_MARKUP
+<MultipleSelectOptions {'01 ', ' 02'} for select name='select4'>
>>> try: from urllib import urlencode
... except ImportError: from urllib.parse import urlencode
>>> print(urlencode(f.form_values()))
-hidden_field=new+value&text_field=text_value&single_checkbox=on&single_checkbox2=good&check_group=1&check_group=2&check_group=3&textarea_field=some+text&select1=No+value&select2=2
+hidden_field=new+value&text_field=text_value&single_checkbox=on&single_checkbox2=good&check_group=1&check_group=2&check_group=3&textarea_field=some+text&select1=No+value&select2=2&select3=+02&select4=01+&select4=+02
>>> fields = f.fields
>>> fields # doctest:+NOPARSE_MARKUP
<FieldsDict for form 0>
@@ -149,6 +171,8 @@ radios: None
reset1: None
select1: 'No value'
select2: <MultipleSelectOptions {'2'} for select name='select2'>
+select3: ' 02'
+select4: <MultipleSelectOptions {'01 ', ' 02'} for select name='select4'>
single_checkbox: 'on'
single_checkbox2: 'good'
submit1: 'submit'