Code

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

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

More test fixes, minimum size of one

Line 
1Index: django/newforms/widgets.py
2===================================================================
3--- django/newforms/widgets.py  (Revision 7351)
4+++ django/newforms/widgets.py  (Arbeitskopie)
5@@ -375,14 +375,22 @@
6     def render(self, name, value, attrs=None, choices=()):
7         if value is None: value = []
8         final_attrs = self.build_attrs(attrs, name=name)
9-        output = [u'<select multiple="multiple"%s>' % flatatt(final_attrs)]
10+        output = []
11+        choice_count = 0
12         str_values = set([force_unicode(v) for v in value]) # Normalize to strings.
13         for option_value, option_label in chain(self.choices, choices):
14+            choice_count += 1
15             option_value = force_unicode(option_value)
16             selected_html = (option_value in str_values) and ' selected="selected"' or ''
17             output.append(u'<option value="%s"%s>%s</option>' % (
18                     escape(option_value), selected_html,
19                     conditional_escape(force_unicode(option_label))))
20+        # default size attribute for cross-browser consistent rendering
21+        if 'size' not in final_attrs:
22+            # emulate the behaviour of firefox: adjust the size to the number of choices, with an upper boundary
23+            final_attrs['size'] = min(10, max(1, choice_count))
24+        # we have to prepend because choices can be any iterable and we cannot call len() so we have to count them first
25+        output = [u'<select multiple="multiple"%s>' % flatatt(final_attrs)] + output
26         output.append(u'</select>')
27         return mark_safe(u'\n'.join(output))
28 
29Index: tests/modeltests/model_forms/models.py
30===================================================================
31--- tests/modeltests/model_forms/models.py      (Revision 7351)
32+++ tests/modeltests/model_forms/models.py      (Arbeitskopie)
33@@ -330,7 +330,7 @@
34 <option value="2">Pending</option>
35 <option value="3">Live</option>
36 </select></td></tr>
37-<tr><th>Categories:</th><td><select multiple="multiple" name="categories">
38+<tr><th>Categories:</th><td><select multiple="multiple" name="categories" size="3">
39 <option value="1">Entertainment</option>
40 <option value="2">It&#39;s a test</option>
41 <option value="3">Third test</option>
42@@ -386,7 +386,7 @@
43 <option value="2">Pending</option>
44 <option value="3">Live</option>
45 </select></li>
46-<li>Categories: <select multiple="multiple" name="categories">
47+<li>Categories: <select multiple="multiple" name="categories" size="3">
48 <option value="1">Entertainment</option>
49 <option value="2">It&#39;s a test</option>
50 <option value="3">Third test</option>
51@@ -447,7 +447,7 @@
52 <option value="2">Pending</option>
53 <option value="3">Live</option>
54 </select></li>
55-<li>Categories: <select multiple="multiple" name="categories">
56+<li>Categories: <select multiple="multiple" name="categories" size="3">
57 <option value="1" selected="selected">Entertainment</option>
58 <option value="2">It&#39;s a test</option>
59 <option value="3">Third test</option>
60@@ -563,7 +563,7 @@
61 <option value="2">Pending</option>
62 <option value="3">Live</option>
63 </select></li>
64-<li>Categories: <select multiple="multiple" name="categories">
65+<li>Categories: <select multiple="multiple" name="categories" size="3">
66 <option value="1">Entertainment</option>
67 <option value="2">It&#39;s a test</option>
68 <option value="3">Third</option>
69@@ -589,7 +589,7 @@
70 <option value="2">Pending</option>
71 <option value="3">Live</option>
72 </select></li>
73-<li>Categories: <select multiple="multiple" name="categories">
74+<li>Categories: <select multiple="multiple" name="categories" size="4">
75 <option value="1">Entertainment</option>
76 <option value="2">It&#39;s a test</option>
77 <option value="3">Third</option>
78Index: tests/regressiontests/admin_widgets/models.py
79===================================================================
80--- tests/regressiontests/admin_widgets/models.py       (Revision 7351)
81+++ tests/regressiontests/admin_widgets/models.py       (Arbeitskopie)
82@@ -38,7 +38,7 @@
83 
84 >>> w = FilteredSelectMultiple('test', False)
85 >>> print conditional_escape(w.render('test', 'test'))
86-<select multiple="multiple" name="test">
87+<select multiple="multiple" name="test" size="1">
88 </select><script type="text/javascript">addEvent(window, "load", function(e) {SelectFilter.init("id_test", "test", 0, "%(ADMIN_MEDIA_PREFIX)s"); });</script>
89 <BLANKLINE>
90 
91Index: tests/regressiontests/forms/extra.py
92===================================================================
93--- tests/regressiontests/forms/extra.py        (Revision 7351)
94+++ tests/regressiontests/forms/extra.py        (Arbeitskopie)
95@@ -259,7 +259,7 @@
96 >>> w = ComplexMultiWidget()
97 >>> print w.render('name', 'some text,JP,2007-04-25 06:24:00')
98 <input type="text" name="name_0" value="some text" />
99-<select multiple="multiple" name="name_1">
100+<select multiple="multiple" name="name_1" size="4">
101 <option value="J" selected="selected">John</option>
102 <option value="P" selected="selected">Paul</option>
103 <option value="G">George</option>
104@@ -300,7 +300,7 @@
105 >>> f = ComplexFieldForm()
106 >>> print f
107 <tr><th><label for="id_field1_0">Field1:</label></th><td><input type="text" name="field1_0" id="id_field1_0" />
108-<select multiple="multiple" name="field1_1" id="id_field1_1">
109+<select multiple="multiple" size="4" name="field1_1" id="id_field1_1">
110 <option value="J">John</option>
111 <option value="P">Paul</option>
112 <option value="G">George</option>
113@@ -311,7 +311,7 @@
114 >>> f = ComplexFieldForm({'field1_0':'some text','field1_1':['J','P'], 'field1_2_0':'2007-04-25', 'field1_2_1':'06:24:00'})
115 >>> print f
116 <tr><th><label for="id_field1_0">Field1:</label></th><td><input type="text" name="field1_0" value="some text" id="id_field1_0" />
117-<select multiple="multiple" name="field1_1" id="id_field1_1">
118+<select multiple="multiple" size="4" name="field1_1" id="id_field1_1">
119 <option value="J" selected="selected">John</option>
120 <option value="P" selected="selected">Paul</option>
121 <option value="G">George</option>
122Index: tests/regressiontests/forms/forms.py
123===================================================================
124--- tests/regressiontests/forms/forms.py        (Revision 7351)
125+++ tests/regressiontests/forms/forms.py        (Arbeitskopie)
126@@ -475,14 +475,14 @@
127 ...     composers = MultipleChoiceField()
128 >>> f = SongForm(auto_id=False)
129 >>> print f['composers']
130-<select multiple="multiple" name="composers">
131+<select multiple="multiple" name="composers" size="1">
132 </select>
133 >>> class SongForm(Form):
134 ...     name = CharField()
135 ...     composers = MultipleChoiceField(choices=[('J', 'John Lennon'), ('P', 'Paul McCartney')])
136 >>> f = SongForm(auto_id=False)
137 >>> print f['composers']
138-<select multiple="multiple" name="composers">
139+<select multiple="multiple" name="composers" size="2">
140 <option value="J">John Lennon</option>
141 <option value="P">Paul McCartney</option>
142 </select>
143@@ -490,7 +490,7 @@
144 >>> print f['name']
145 <input type="text" name="name" value="Yesterday" />
146 >>> print f['composers']
147-<select multiple="multiple" name="composers">
148+<select multiple="multiple" name="composers" size="2">
149 <option value="J">John Lennon</option>
150 <option value="P" selected="selected">Paul McCartney</option>
151 </select>