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

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

Updated to r8287, tests still fail with UnicodeDecodeError

  • django/forms/widgets.py

     
    345345        if value is None: value = ''
    346346        final_attrs = self.build_attrs(attrs, name=name)
    347347        output = [u'<select%s>' % flatatt(final_attrs)]
    348         options = self.render_options(choices, [value])
     348        options, dont_care = self.render_options(choices, [value])
    349349        if options:
    350350            output.append(options)
    351351        output.append('</select>')
     
    361361        # Normalize to strings.
    362362        selected_choices = set([force_unicode(v) for v in selected_choices])
    363363        output = []
     364        output_length = 0
    364365        for option_value, option_label in chain(self.choices, choices):
     366            output_length += 1
    365367            if isinstance(option_label, (list, tuple)):
    366368                output.append(u'<optgroup label="%s">' % escape(force_unicode(option_value)))
    367369                for option in option_label:
     370                    output_length += 1
    368371                    output.append(render_option(*option))
    369372                output.append(u'</optgroup>')
    370373            else:
    371374                output.append(render_option(option_value, option_label))
    372         return u'\n'.join(output)
     375        return u'\n'.join(output), output_length
    373376
    374377class NullBooleanSelect(Select):
    375378    """
     
    398401class SelectMultiple(Select):
    399402    def render(self, name, value, attrs=None, choices=()):
    400403        if value is None: value = []
     404        options, options_length = self.render_options(choices, value)
    401405        final_attrs = self.build_attrs(attrs, name=name)
     406        # default size attribute for cross-browser consistent rendering
     407        if 'size' not in final_attrs:
     408            # emulate the behaviour of firefox: adjust the size to the number of choices, with an upper boundary
     409            final_attrs['size'] = min(10, max(1, options_length))
    402410        output = [u'<select multiple="multiple"%s>' % flatatt(final_attrs)]
    403         options = self.render_options(choices, value)
    404411        if options:
    405412            output.append(options)
    406413        output.append('</select>')
  • tests/modeltests/model_forms/models.py

     
    342342<option value="2">Pending</option>
    343343<option value="3">Live</option>
    344344</select></td></tr>
    345 <tr><th>Categories:</th><td><select multiple="multiple" name="categories">
     345<tr><th>Categories:</th><td><select multiple="multiple" name="categories" size="3">
    346346<option value="1">Entertainment</option>
    347347<option value="2">It&#39;s a test</option>
    348348<option value="3">Third test</option>
     
    398398<option value="2">Pending</option>
    399399<option value="3">Live</option>
    400400</select></li>
    401 <li>Categories: <select multiple="multiple" name="categories">
     401<li>Categories: <select multiple="multiple" name="categories" size="3">
    402402<option value="1">Entertainment</option>
    403403<option value="2">It&#39;s a test</option>
    404404<option value="3">Third test</option>
     
    459459<option value="2">Pending</option>
    460460<option value="3">Live</option>
    461461</select></li>
    462 <li>Categories: <select multiple="multiple" name="categories">
     462<li>Categories: <select multiple="multiple" name="categories" size="3">
    463463<option value="1" selected="selected">Entertainment</option>
    464464<option value="2">It&#39;s a test</option>
    465465<option value="3">Third test</option>
     
    575575<option value="2">Pending</option>
    576576<option value="3">Live</option>
    577577</select></li>
    578 <li>Categories: <select multiple="multiple" name="categories">
     578<li>Categories: <select multiple="multiple" name="categories" size="3">
    579579<option value="1">Entertainment</option>
    580580<option value="2">It&#39;s a test</option>
    581581<option value="3">Third</option>
     
    601601<option value="2">Pending</option>
    602602<option value="3">Live</option>
    603603</select></li>
    604 <li>Categories: <select multiple="multiple" name="categories">
     604<li>Categories: <select multiple="multiple" name="categories" size="4">
    605605<option value="1">Entertainment</option>
    606606<option value="2">It&#39;s a test</option>
    607607<option value="3">Third</option>
  • tests/regressiontests/admin_widgets/models.py

     
    4141
    4242>>> w = FilteredSelectMultiple('test', False)
    4343>>> print conditional_escape(w.render('test', 'test'))
    44 <select multiple="multiple" name="test">
     44<select multiple="multiple" name="test" size="1">
    4545</select><script type="text/javascript">addEvent(window, "load", function(e) {SelectFilter.init("id_test", "test", 0, "%(ADMIN_MEDIA_PREFIX)s"); });</script>
    4646<BLANKLINE>
    4747
  • 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

     
    476476...     composers = MultipleChoiceField()
    477477>>> f = SongForm(auto_id=False)
    478478>>> print f['composers']
    479 <select multiple="multiple" name="composers">
     479<select multiple="multiple" name="composers" size="1">
    480480</select>
    481481>>> class SongForm(Form):
    482482...     name = CharField()
    483483...     composers = MultipleChoiceField(choices=[('J', 'John Lennon'), ('P', 'Paul McCartney')])
    484484>>> f = SongForm(auto_id=False)
    485485>>> print f['composers']
    486 <select multiple="multiple" name="composers">
     486<select multiple="multiple" name="composers" size="2">
    487487<option value="J">John Lennon</option>
    488488<option value="P">Paul McCartney</option>
    489489</select>
     
    491491>>> print f['name']
    492492<input type="text" name="name" value="Yesterday" />
    493493>>> print f['composers']
    494 <select multiple="multiple" name="composers">
     494<select multiple="multiple" name="composers" size="2">
    495495<option value="J">John Lennon</option>
    496496<option value="P" selected="selected">Paul McCartney</option>
    497497</select>
Back to Top