diff options
-rw-r--r-- | src/lxml/html/__init__.py | 25 | ||||
-rw-r--r-- | src/lxml/html/tests/test_forms.txt | 26 |
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' |