Code

Ticket #4117: 4117.diff

File 4117.diff, 1.7 KB (added by Gary Wilson <gary.wilson@…>, 7 years ago)

attributes for containers of RadioSelect and CheckboxSelectMultiple widgets

Line 
1=== modified file 'django/newforms/widgets.py'
2--- django/newforms/widgets.py  2007-05-07 16:38:08 +0000
3+++ django/newforms/widgets.py  2007-05-15 18:52:42 +0000
4@@ -253,7 +253,8 @@
5 
6     def __unicode__(self):
7         "Outputs a <ul> for this set of radio fields."
8-        return u'<ul>\n%s\n</ul>' % u'\n'.join([u'<li>%s</li>' % w for w in self])
9+        contents = u'\n'.join([u'<li>%s</li>' % w for w in self])
10+        return u'<ul%s>\n%s\n</ul>' % (flatatt(self.attrs), contents)
11 
12 class RadioSelect(Select):
13     def render(self, name, value, attrs=None, choices=()):
14@@ -278,7 +279,7 @@
15         if value is None: value = []
16         has_id = attrs and 'id' in attrs
17         final_attrs = self.build_attrs(attrs, name=name)
18-        output = [u'<ul>']
19+        output = [u'<ul%s>' % flatatt(final_attrs)]
20         str_values = set([smart_unicode(v) for v in value]) # Normalize to strings.
21         for i, (option_value, option_label) in enumerate(chain(self.choices, choices)):
22             # If an ID attribute was given, add a numeric index as a suffix,
23@@ -287,8 +288,10 @@
24                 final_attrs = dict(final_attrs, id='%s_%s' % (attrs['id'], i))
25             cb = CheckboxInput(final_attrs, check_test=lambda value: value in str_values)
26             option_value = smart_unicode(option_value)
27+            option_label = escape(smart_unicode(option_label))
28             rendered_cb = cb.render(name, option_value)
29-            output.append(u'<li><label>%s %s</label></li>' % (rendered_cb, escape(smart_unicode(option_label))))
30+            contents = u'<label>%s %s</label>' % (rendered_cb, option_label)
31+            output.append(u'<li%s>%s</li>' % (flatatt(final_attrs), contents))
32         output.append(u'</ul>')
33         return u'\n'.join(output)
34 
35