Skip to content

Commit

Permalink
Fixed #4316 -- Added docstring and tests for django.newforms.utils.fl…
Browse files Browse the repository at this point in the history
…atatt().

Thanks, Gary Wilson.


git-svn-id: http://code.djangoproject.com/svn/django/trunk@5291 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information
malcolmt committed May 19, 2007
1 parent 5ab9c3a commit 534c0d8
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 3 deletions.
11 changes: 8 additions & 3 deletions django/newforms/util.py
@@ -1,9 +1,14 @@
from django.utils.html import escape
from django.utils.encoding import smart_unicode

# Converts a dictionary to a single string with key="value", XML-style with
# a leading space. Assumes keys do not need to be XML-escaped.
flatatt = lambda attrs: u''.join([u' %s="%s"' % (k, escape(v)) for k, v in attrs.items()])
def flatatt(attrs):
"""
Convert a dictionary of attributes to a single string.
The returned string will contain a leading space followed by key="value",
XML-style pairs. It is assumed that the keys do not need to be XML-escaped.
If the passed dictionary is empty, then return an empty string.
"""
return u''.join([u' %s="%s"' % (k, escape(v)) for k, v in attrs.items()])

class ErrorDict(dict):
"""
Expand Down
9 changes: 9 additions & 0 deletions tests/regressiontests/forms/tests.py
Expand Up @@ -3515,6 +3515,15 @@
u'1'
>>> smart_unicode('foo')
u'foo'
# flatatt tests
>>> from django.newforms.util import flatatt
>>> flatatt({'id': "header"})
u' id="header"'
>>> flatatt({'class': "news", 'title': "Read this"})
u' class="news" title="Read this"'
>>> flatatt({})
u''
"""

__test__ = {
Expand Down

0 comments on commit 534c0d8

Please sign in to comment.