diff options
| author | Luke Plant <L.Plant.98@cantab.net> | 2012-06-30 18:54:38 +0100 |
|---|---|---|
| committer | Luke Plant <L.Plant.98@cantab.net> | 2012-07-03 22:20:12 +0100 |
| commit | bee498f3a2f66210db39f0be244ec4fa888b6940 (patch) | |
| tree | 95252d804377899b9d5bb3bff1a1c8dc6206d835 /django/utils | |
| parent | f33e15036907d6e4bda6116dc84097e9e590d2c8 (diff) | |
Added 'format_html' utility for formatting HTML fragments safely
Diffstat (limited to 'django/utils')
| -rw-r--r-- | django/utils/html.py | 31 |
1 files changed, 31 insertions, 0 deletions
diff --git a/django/utils/html.py b/django/utils/html.py index fe2e6b7a29..390c45dcec 100644 --- a/django/utils/html.py +++ b/django/utils/html.py @@ -72,6 +72,37 @@ def conditional_escape(text): else: return escape(text) +def format_html(format_string, *args, **kwargs): + """ + Similar to str.format, but passes all arguments through conditional_escape, + and calls 'mark_safe' on the result. This function should be used instead + of str.format or % interpolation to build up small HTML fragments. + """ + args_safe = map(conditional_escape, args) + kwargs_safe = dict([(k, conditional_escape(v)) for (k, v) in + kwargs.iteritems()]) + return mark_safe(format_string.format(*args_safe, **kwargs_safe)) + +def format_html_join(sep, format_string, args_generator): + """ + A wrapper format_html, for the common case of a group of arguments that need + to be formatted using the same format string, and then joined using + 'sep'. 'sep' is also passed through conditional_escape. + + 'args_generator' should be an iterator that returns the sequence of 'args' + that will be passed to format_html. + + Example: + + format_html_join('\n', "<li>{0} {1}</li>", ((u.first_name, u.last_name) + for u in users)) + + """ + return mark_safe(conditional_escape(sep).join( + format_html(format_string, *tuple(args)) + for args in args_generator)) + + def linebreaks(value, autoescape=False): """Converts newlines into <p> and <br />s.""" value = normalize_newlines(value) |
