Skip to content

Commit

Permalink
Fix options with value="" get selected
Browse files Browse the repository at this point in the history
Closes issue formencode#3
  • Loading branch information
lambacck committed Sep 14, 2012
1 parent 772a938 commit ab9eea5
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 2 deletions.
4 changes: 2 additions & 2 deletions formencode/htmlfill.py
Original file line number Diff line number Diff line change
Expand Up @@ -521,7 +521,7 @@ def handle_option(self, attrs):
"<option> outside of <select> 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)
Expand All @@ -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__'):
Expand Down
28 changes: 28 additions & 0 deletions tests/test_htmlfill.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = """
<select name="select-1" class="my_select">
<option value="">this is option-1</option>
</select>
"""
expected_html = """
<select name="select-1" class="my_select">
<option value="" selected="selected">this is option-1</option>
</select>
"""
rendered_html = htmlfill.render(html, defaults={"select-1": ""})
assert expected_html == rendered_html, rendered_html

def test_select_empty_option_value_not_selected():
html = """
<select name="select-1" class="my_select">
<option value="">this is option-1</option>
</select>
"""
expected_html = """
<select name="select-1" class="my_select">
<option value="">this is option-1</option>
</select>
"""
rendered_html = htmlfill.render(html, defaults={})
assert expected_html == rendered_html, rendered_html


def test_script_quoting():
html = """
Expand Down

0 comments on commit ab9eea5

Please sign in to comment.