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

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

All tests ok now

  • 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>
  • tests/regressiontests/forms/widgets.py

     
    527527
    528528>>> w = SelectMultiple()
    529529>>> print w.render('beatles', ['J'], choices=(('J', 'John'), ('P', 'Paul'), ('G', 'George'), ('R', 'Ringo')))
    530 <select multiple="multiple" name="beatles">
     530<select multiple="multiple" name="beatles" size="4">
    531531<option value="J" selected="selected">John</option>
    532532<option value="P">Paul</option>
    533533<option value="G">George</option>
    534534<option value="R">Ringo</option>
    535535</select>
    536536>>> print w.render('beatles', ['J', 'P'], choices=(('J', 'John'), ('P', 'Paul'), ('G', 'George'), ('R', 'Ringo')))
    537 <select multiple="multiple" name="beatles">
     537<select multiple="multiple" name="beatles" size="4">
    538538<option value="J" selected="selected">John</option>
    539539<option value="P" selected="selected">Paul</option>
    540540<option value="G">George</option>
    541541<option value="R">Ringo</option>
    542542</select>
    543543>>> print w.render('beatles', ['J', 'P', 'R'], choices=(('J', 'John'), ('P', 'Paul'), ('G', 'George'), ('R', 'Ringo')))
    544 <select multiple="multiple" name="beatles">
     544<select multiple="multiple" name="beatles" size="4">
    545545<option value="J" selected="selected">John</option>
    546546<option value="P" selected="selected">Paul</option>
    547547<option value="G">George</option>
     
    550550
    551551If the value is None, none of the options are selected:
    552552>>> print w.render('beatles', None, choices=(('J', 'John'), ('P', 'Paul'), ('G', 'George'), ('R', 'Ringo')))
    553 <select multiple="multiple" name="beatles">
     553<select multiple="multiple" name="beatles" size="4">
    554554<option value="J">John</option>
    555555<option value="P">Paul</option>
    556556<option value="G">George</option>
     
    559559
    560560If the value corresponds to a label (but not to an option value), none of the options are selected:
    561561>>> print w.render('beatles', ['John'], choices=(('J', 'John'), ('P', 'Paul'), ('G', 'George'), ('R', 'Ringo')))
    562 <select multiple="multiple" name="beatles">
     562<select multiple="multiple" name="beatles" size="4">
    563563<option value="J">John</option>
    564564<option value="P">Paul</option>
    565565<option value="G">George</option>
     
    568568
    569569If multiple values are given, but some of them are not valid, the valid ones are selected:
    570570>>> print w.render('beatles', ['J', 'G', 'foo'], choices=(('J', 'John'), ('P', 'Paul'), ('G', 'George'), ('R', 'Ringo')))
    571 <select multiple="multiple" name="beatles">
     571<select multiple="multiple" name="beatles" size="4">
    572572<option value="J" selected="selected">John</option>
    573573<option value="P">Paul</option>
    574574<option value="G" selected="selected">George</option>
     
    577577
    578578The value is compared to its str():
    579579>>> print w.render('nums', [2], choices=[('1', '1'), ('2', '2'), ('3', '3')])
    580 <select multiple="multiple" name="nums">
     580<select multiple="multiple" name="nums" size="3">
    581581<option value="1">1</option>
    582582<option value="2" selected="selected">2</option>
    583583<option value="3">3</option>
    584584</select>
    585585>>> print w.render('nums', ['2'], choices=[(1, 1), (2, 2), (3, 3)])
    586 <select multiple="multiple" name="nums">
     586<select multiple="multiple" name="nums" size="3">
    587587<option value="1">1</option>
    588588<option value="2" selected="selected">2</option>
    589589<option value="3">3</option>
    590590</select>
    591591>>> print w.render('nums', [2], choices=[(1, 1), (2, 2), (3, 3)])
    592 <select multiple="multiple" name="nums">
     592<select multiple="multiple" name="nums" size="3">
    593593<option value="1">1</option>
    594594<option value="2" selected="selected">2</option>
    595595<option value="3">3</option>
     
    600600...     for i in range(5):
    601601...         yield (i, i)
    602602>>> print w.render('nums', [2], choices=get_choices())
    603 <select multiple="multiple" name="nums">
     603<select multiple="multiple" name="nums" size="5">
    604604<option value="0">0</option>
    605605<option value="1">1</option>
    606606<option value="2" selected="selected">2</option>
     
    611611You can also pass 'choices' to the constructor:
    612612>>> w = SelectMultiple(choices=[(1, 1), (2, 2), (3, 3)])
    613613>>> print w.render('nums', [2])
    614 <select multiple="multiple" name="nums">
     614<select multiple="multiple" name="nums" size="3">
    615615<option value="1">1</option>
    616616<option value="2" selected="selected">2</option>
    617617<option value="3">3</option>
     
    619619
    620620If 'choices' is passed to both the constructor and render(), then they'll both be in the output:
    621621>>> print w.render('nums', [2], choices=[(4, 4), (5, 5)])
    622 <select multiple="multiple" name="nums">
     622<select multiple="multiple" name="nums" size="5">
    623623<option value="1">1</option>
    624624<option value="2" selected="selected">2</option>
    625625<option value="3">3</option>
     
    629629
    630630# Choices are escaped correctly
    631631>>> print w.render('escape', None, choices=(('bad', 'you & me'), ('good', mark_safe('you &gt; me'))))
    632 <select multiple="multiple" name="escape">
     632<select multiple="multiple" name="escape" size="5">
    633633<option value="1">1</option>
    634634<option value="2">2</option>
    635635<option value="3">3</option>
     
    639639
    640640# Unicode choices are correctly rendered as HTML
    641641>>> w.render('nums', ['ŠĐĆŽćžšđ'], choices=[('ŠĐĆŽćžšđ', 'ŠĐabcĆŽćžšđ'), ('ćžšđ', 'abcćžšđ')])
    642 u'<select multiple="multiple" name="nums">\n<option value="1">1</option>\n<option value="2">2</option>\n<option value="3">3</option>\n<option value="\u0160\u0110\u0106\u017d\u0107\u017e\u0161\u0111" selected="selected">\u0160\u0110abc\u0106\u017d\u0107\u017e\u0161\u0111</option>\n<option value="\u0107\u017e\u0161\u0111">abc\u0107\u017e\u0161\u0111</option>\n</select>'
     642u'<select multiple="multiple" name="nums" size="5">\n<option value="1">1</option>\n<option value="2">2</option>\n<option value="3">3</option>\n<option value="\u0160\u0110\u0106\u017d\u0107\u017e\u0161\u0111" selected="selected">\u0160\u0110abc\u0106\u017d\u0107\u017e\u0161\u0111</option>\n<option value="\u0107\u017e\u0161\u0111">abc\u0107\u017e\u0161\u0111</option>\n</select>'
    643643
    644644# Test the usage of _has_changed
    645645>>> w._has_changed(None, None)
     
    658658# Choices can be nested one level in order to create HTML optgroups:
    659659>>> w.choices = (('outer1', 'Outer 1'), ('Group "1"', (('inner1', 'Inner 1'), ('inner2', 'Inner 2'))))
    660660>>> print w.render('nestchoice', None)
    661 <select multiple="multiple" name="nestchoice">
     661<select multiple="multiple" name="nestchoice" size="4">
    662662<option value="outer1">Outer 1</option>
    663663<optgroup label="Group &quot;1&quot;">
    664664<option value="inner1">Inner 1</option>
     
    667667</select>
    668668
    669669>>> print w.render('nestchoice', ['outer1'])
    670 <select multiple="multiple" name="nestchoice">
     670<select multiple="multiple" name="nestchoice" size="4">
    671671<option value="outer1" selected="selected">Outer 1</option>
    672672<optgroup label="Group &quot;1&quot;">
    673673<option value="inner1">Inner 1</option>
     
    676676</select>
    677677
    678678>>> print w.render('nestchoice', ['inner1'])
    679 <select multiple="multiple" name="nestchoice">
     679<select multiple="multiple" name="nestchoice" size="4">
    680680<option value="outer1">Outer 1</option>
    681681<optgroup label="Group &quot;1&quot;">
    682682<option value="inner1" selected="selected">Inner 1</option>
     
    685685</select>
    686686
    687687>>> print w.render('nestchoice', ['outer1', 'inner2'])
    688 <select multiple="multiple" name="nestchoice">
     688<select multiple="multiple" name="nestchoice" size="4">
    689689<option value="outer1" selected="selected">Outer 1</option>
    690690<optgroup label="Group &quot;1&quot;">
    691691<option value="inner1">Inner 1</option>
Back to Top