Skip to content
This repository has been archived by the owner on Nov 28, 2024. It is now read-only.

Commit

Permalink
Merge pull request #18 from yannik-ammann/master
Browse files Browse the repository at this point in the history
support for format_html & conditional_escape
  • Loading branch information
yannik-ammann committed Apr 29, 2015
2 parents 85e0778 + 86a471a commit 2c0404a
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 1 deletion.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,10 @@ See a full example [here](https://github.com/arteria/django-hijack/blob/4966d886
* atomic or commit_on_success (commit_on_success replaced by atomic in Django >=1.8)
* BytesIO
* clean_manytomany_helptext
* conditional_escape
* EmailValidator
* force_text
* format_html
* GenericForeignKey
* get_ident
* get_model_name
Expand Down
33 changes: 32 additions & 1 deletion compat/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,34 @@ def create_permissions(*args, **kwargs):
from .json_response import JsonResponse


# format_html (django 1.6)

try:
from django.utils.html import format_html, conditional_escape
except ImportError:
# support django < 1.5. Taken from django.utils.html
from django.utils import six
def conditional_escape(text):
"""
Similar to escape(), except that it doesn't operate on pre-escaped strings.
"""
if isinstance(text, SafeData):
return 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
six.iteritems(kwargs)])
return mark_safe(format_string.format(*args_safe, **kwargs_safe))


### Undocumented ###

try:
Expand Down Expand Up @@ -328,5 +356,8 @@ def create_permissions(*args, **kwargs):
'slugify',
'GenericForeignKey',
'SortedDict',
'atomic', 'commit_on_success', # alias
'atomic',
'commit_on_success', # alias
'format_html',
'conditional_escape',
]

0 comments on commit 2c0404a

Please sign in to comment.