Ticket #6873: SelectMultiple-default-size-01.diff

File SelectMultiple-default-size-01.diff, 7.0 KB (added by jarrow, 7 years ago)

More test fixes, minimum size of one

  • django/newforms/widgets.py

     
    375375    def render(self, name, value, attrs=None, choices=()):
    376376        if value is None: value = []
    377377        final_attrs = self.build_attrs(attrs, name=name)
    378         output = [u'<select multiple="multiple"%s>' % flatatt(final_attrs)]
     378        output = []
     379        choice_count = 0
    379380        str_values = set([force_unicode(v) for v in value]) # Normalize to strings.
    380381        for option_value, option_label in chain(self.choices, choices):
     382            choice_count += 1
    381383            option_value = force_unicode(option_value)
    382384            selected_html = (option_value in str_values) and ' selected="selected"' or ''
    383385            output.append(u'<option value="%s"%s>%s</option>' % (
    384386                    escape(option_value), selected_html,
    385387                    conditional_escape(force_unicode(option_label))))
     388        # default size attribute for cross-browser consistent rendering
     389        if 'size' not in final_attrs:
     390            # emulate the behaviour of firefox: adjust the size to the number of choices, with an upper boundary
     391            final_attrs['size'] = min(10, max(1, choice_count))
     392        # we have to prepend because choices can be any iterable and we cannot call len() so we have to count them first
     393        output = [u'<select multiple="multiple"%s>' % flatatt(final_attrs)] + output
    386394        output.append(u'</select>')
    387395        return mark_safe(u'\n'.join(output))
    388396
  • tests/modeltests/model_forms/models.py

     
    330330<option value="2">Pending</option>
    331331<option value="3">Live</option>
    332332</select></td></tr>
    333 <tr><th>Categories:</th><td><select multiple="multiple" name="categories">
     333<tr><th>Categories:</th><td><select multiple="multiple" name="categories" size="3">
    334334<option value="1">Entertainment</option>
    335335<option value="2">It&#39;s a test</option>
    336336<option value="3">Third test</option>
     
    386386<option value="2">Pending</option>
    387387<option value="3">Live</option>
    388388</select></li>
    389 <li>Categories: <select multiple="multiple" name="categories">
     389<li>Categories: <select multiple="multiple" name="categories" size="3">
    390390<option value="1">Entertainment</option>
    391391<option value="2">It&#39;s a test</option>
    392392<option value="3">Third test</option>
     
    447447<option value="2">Pending</option>
    448448<option value="3">Live</option>
    449449</select></li>
    450 <li>Categories: <select multiple="multiple" name="categories">
     450<li>Categories: <select multiple="multiple" name="categories" size="3">
    451451<option value="1" selected="selected">Entertainment</option>
    452452<option value="2">It&#39;s a test</option>
    453453<option value="3">Third test</option>
     
    563563<option value="2">Pending</option>
    564564<option value="3">Live</option>
    565565</select></li>
    566 <li>Categories: <select multiple="multiple" name="categories">
     566<li>Categories: <select multiple="multiple" name="categories" size="3">
    567567<option value="1">Entertainment</option>
    568568<option value="2">It&#39;s a test</option>
    569569<option value="3">Third</option>
     
    589589<option value="2">Pending</option>
    590590<option value="3">Live</option>
    591591</select></li>
    592 <li>Categories: <select multiple="multiple" name="categories">
     592<li>Categories: <select multiple="multiple" name="categories" size="4">
    593593<option value="1">Entertainment</option>
    594594<option value="2">It&#39;s a test</option>
    595595<option value="3">Third</option>
  • tests/regressiontests/admin_widgets/models.py

     
    3838
    3939>>> w = FilteredSelectMultiple('test', False)
    4040>>> print conditional_escape(w.render('test', 'test'))
    41 <select multiple="multiple" name="test">
     41<select multiple="multiple" name="test" size="1">
    4242</select><script type="text/javascript">addEvent(window, "load", function(e) {SelectFilter.init("id_test", "test", 0, "%(ADMIN_MEDIA_PREFIX)s"); });</script>
    4343<BLANKLINE>
    4444
  • tests/regressiontests/forms/extra.py

     
    259259>>> w = ComplexMultiWidget()
    260260>>> print w.render('name', 'some text,JP,2007-04-25 06:24:00')
    261261<input type="text" name="name_0" value="some text" />
    262 <select multiple="multiple" name="name_1">
     262<select multiple="multiple" name="name_1" size="4">
    263263<option value="J" selected="selected">John</option>
    264264<option value="P" selected="selected">Paul</option>
    265265<option value="G">George</option>
     
    300300>>> f = ComplexFieldForm()
    301301>>> print f
    302302<tr><th><label for="id_field1_0">Field1:</label></th><td><input type="text" name="field1_0" id="id_field1_0" />
    303 <select multiple="multiple" name="field1_1" id="id_field1_1">
     303<select multiple="multiple" size="4" name="field1_1" id="id_field1_1">
    304304<option value="J">John</option>
    305305<option value="P">Paul</option>
    306306<option value="G">George</option>
     
    311311>>> f = ComplexFieldForm({'field1_0':'some text','field1_1':['J','P'], 'field1_2_0':'2007-04-25', 'field1_2_1':'06:24:00'})
    312312>>> print f
    313313<tr><th><label for="id_field1_0">Field1:</label></th><td><input type="text" name="field1_0" value="some text" id="id_field1_0" />
    314 <select multiple="multiple" name="field1_1" id="id_field1_1">
     314<select multiple="multiple" size="4" name="field1_1" id="id_field1_1">
    315315<option value="J" selected="selected">John</option>
    316316<option value="P" selected="selected">Paul</option>
    317317<option value="G">George</option>
  • tests/regressiontests/forms/forms.py

     
    475475...     composers = MultipleChoiceField()
    476476>>> f = SongForm(auto_id=False)
    477477>>> print f['composers']
    478 <select multiple="multiple" name="composers">
     478<select multiple="multiple" name="composers" size="1">
    479479</select>
    480480>>> class SongForm(Form):
    481481...     name = CharField()
    482482...     composers = MultipleChoiceField(choices=[('J', 'John Lennon'), ('P', 'Paul McCartney')])
    483483>>> f = SongForm(auto_id=False)
    484484>>> print f['composers']
    485 <select multiple="multiple" name="composers">
     485<select multiple="multiple" name="composers" size="2">
    486486<option value="J">John Lennon</option>
    487487<option value="P">Paul McCartney</option>
    488488</select>
     
    490490>>> print f['name']
    491491<input type="text" name="name" value="Yesterday" />
    492492>>> print f['composers']
    493 <select multiple="multiple" name="composers">
     493<select multiple="multiple" name="composers" size="2">
    494494<option value="J">John Lennon</option>
    495495<option value="P" selected="selected">Paul McCartney</option>
    496496</select>
Back to Top