diff --git a/formencode/htmlfill.py b/formencode/htmlfill.py index b46c44ae..717e65a4 100644 --- a/formencode/htmlfill.py +++ b/formencode/htmlfill.py @@ -521,7 +521,7 @@ def handle_option(self, attrs): " outside of at %i:%i" % self.getpos()) if self.in_select != False: if self.force_defaults or self.in_select in self.defaults: - if self.selected_multiple(self.defaults.get(self.in_select, ''), + if self.selected_multiple(self.defaults.get(self.in_select), self.get_attr(attrs, 'value', '')): self.set_attr(attrs, 'selected', 'selected') self.add_key(self.in_select) @@ -537,7 +537,7 @@ def selected_multiple(self, obj, value): identity is used. """ if obj is None: - return value == "" + return False if isinstance(obj, (str, unicode)): return obj == value if hasattr(obj, '__contains__'): diff --git a/tests/test_htmlfill.py b/tests/test_htmlfill.py index 99f10508..33209b1b 100644 --- a/tests/test_htmlfill.py +++ b/tests/test_htmlfill.py @@ -388,6 +388,34 @@ def test_force_defaults_select(): rendered_html = htmlfill.render(html, defaults=dict()) assert expected_html == rendered_html, rendered_html +def test_select_empty_option_value_selected(): + html = """ + + this is option-1 + +""" + expected_html = """ + + this is option-1 + +""" + rendered_html = htmlfill.render(html, defaults={"select-1": ""}) + assert expected_html == rendered_html, rendered_html + +def test_select_empty_option_value_not_selected(): + html = """ + + this is option-1 + +""" + expected_html = """ + + this is option-1 + +""" + rendered_html = htmlfill.render(html, defaults={}) + assert expected_html == rendered_html, rendered_html + def test_script_quoting(): html = """