Ticket #4785: div-widgets.diff

File div-widgets.diff, 2.3 KB (added by Michal Ludvig <michal@…>, 8 years ago)

Proposed patch

  • django/newforms/widgets.py

     
    239239        return u'<input%s />' % flatatt(final_attrs)
    240240
    241241class RadioFieldRenderer(StrAndUnicode):
     242    tags = { 'block' : u'<ul>\n%s\n</ul>', 'item': u'<li>%s</li>' }
    242243    "An object used by RadioSelect to enable customization of radio widgets."
    243244    def __init__(self, name, value, attrs, choices):
    244245        self.name, self.value, self.attrs = name, value, attrs
     
    254255
    255256    def __unicode__(self):
    256257        "Outputs a <ul> for this set of radio fields."
    257         return u'<ul>\n%s\n</ul>' % u'\n'.join([u'<li>%s</li>' % force_unicode(w) for w in self])
     258        return self.tags['block'] % u'\n'.join([self.tags['item'] % force_unicode(w) for w in self])
    258259
    259260class RadioSelect(Select):
    260261    def render(self, name, value, attrs=None, choices=()):
     
    275276    id_for_label = classmethod(id_for_label)
    276277
    277278class CheckboxSelectMultiple(SelectMultiple):
     279    tags = { 'block' : u'<ul>\n%s\n</ul>', 'item' : u'<li><label>%(input)s %(label)s</label></li>' }
    278280    def render(self, name, value, attrs=None, choices=()):
    279281        if value is None: value = []
    280282        has_id = attrs and 'id' in attrs
    281283        final_attrs = self.build_attrs(attrs, name=name)
    282         output = [u'<ul>']
     284        output = []
    283285        str_values = set([force_unicode(v) for v in value]) # Normalize to strings.
    284286        for i, (option_value, option_label) in enumerate(chain(self.choices, choices)):
    285287            # If an ID attribute was given, add a numeric index as a suffix,
     
    289291            cb = CheckboxInput(final_attrs, check_test=lambda value: value in str_values)
    290292            option_value = force_unicode(option_value)
    291293            rendered_cb = cb.render(name, option_value)
    292             output.append(u'<li><label>%s %s</label></li>' % (rendered_cb, escape(force_unicode(option_label))))
    293         output.append(u'</ul>')
    294         return u'\n'.join(output)
     294            output.append(self.tags['item'] % {'input': rendered_cb, 'label': escape(force_unicode(option_label))})
     295        return self.tags['block'] % u'\n'.join(output)
    295296
    296297    def id_for_label(self, id_):
    297298        # See the comment for RadioSelect.id_for_label()
Back to Top