From 534c0d8442e9646c10e746b45c48b8092c105d35 Mon Sep 17 00:00:00 2001 From: Malcolm Tredinnick Date: Sat, 19 May 2007 18:49:35 +0000 Subject: [PATCH] Fixed #4316 -- Added docstring and tests for django.newforms.utils.flatatt(). Thanks, Gary Wilson. git-svn-id: http://code.djangoproject.com/svn/django/trunk@5291 bcc190cf-cafb-0310-a4f2-bffc1f526a37 --- django/newforms/util.py | 11 ++++++++--- tests/regressiontests/forms/tests.py | 9 +++++++++ 2 files changed, 17 insertions(+), 3 deletions(-) diff --git a/django/newforms/util.py b/django/newforms/util.py index cc17ff4f7f..891585cba2 100644 --- a/django/newforms/util.py +++ b/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): """ diff --git a/tests/regressiontests/forms/tests.py b/tests/regressiontests/forms/tests.py index 5afa3d198d..5939f7806c 100644 --- a/tests/regressiontests/forms/tests.py +++ b/tests/regressiontests/forms/tests.py @@ -3515,6 +3515,15 @@ u'\u0160\u0110\u0106\u017d\u0107\u017e\u0161\u0111' 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__ = {