Ticket #4316: flatatt.diff

File flatatt.diff, 1.4 KB (added by Gary Wilson <gary.wilson@…>, 18 years ago)

more readable, yes? Added a couple tests too.

  • django/newforms/util.py

    === modified file 'django/newforms/util.py'
     
    33from django.utils.functional import Promise, lazy
    44from django.utils.encoding import smart_unicode
    55
    6 # Converts a dictionary to a single string with key="value", XML-style with
    7 # a leading space. Assumes keys do not need to be XML-escaped.
    8 flatatt = lambda attrs: u''.join([u' %s="%s"' % (k, escape(v)) for k, v in attrs.items()])
     6def flatatt(attrs):
     7    """
     8    Convert a dictionary of attributes to a single string.
     9    The returned string will contain a leading space followed by key="value",
     10    XML-style pairs.  It is assumed that the keys do not need to be XML-escaped.
     11    If the passed dictionary is empty, then return an empty string.
     12    """
     13    return u''.join([u' %s="%s"' % (k, escape(v)) for k, v in attrs.items()])
    914
    1015class ErrorDict(dict):
    1116    """
  • tests/regressiontests/forms/tests.py

    === modified file 'tests/regressiontests/forms/tests.py'
     
    35153515u'1'
    35163516>>> smart_unicode('foo')
    35173517u'foo'
     3518
     3519# flatatt tests
     3520>>> from django.newforms.util import flatatt
     3521>>> flatatt({'id': "header"})
     3522u' id="header"'
     3523>>> flatatt({'class': "news", 'title': "Read this"})
     3524u' class="news" title="Read this"'
     3525>>> flatatt({})
     3526u''
    35183527"""
    35193528
    35203529__test__ = {
Back to Top