summaryrefslogtreecommitdiff
path: root/django/utils
diff options
context:
space:
mode:
authorRamiro Morales <cramm0@gmail.com>2011-01-02 17:34:52 +0000
committerRamiro Morales <cramm0@gmail.com>2011-01-02 17:34:52 +0000
commit0f783b7f4eac037e22875eeeb6dc85c26b2a65f4 (patch)
tree7600364217fb5f354bfa1283a98e9562cbfbec6a /django/utils
parent544ab30ed71e0d78c4c061008758de29ff79e8f7 (diff)
Fixed #2986 -- Made the JavaScript code that drives related model instance addition in a popup window handle a model representation containing new lines. Also, moved the escapejs functionality yoo django.utils.html so it can be used from Python code. Thanks andrewwatts for the patch.
git-svn-id: http://code.djangoproject.com/svn/django/trunk@15131 bcc190cf-cafb-0310-a4f2-bffc1f526a37
Diffstat (limited to 'django/utils')
-rw-r--r--django/utils/html.py25
1 files changed, 25 insertions, 0 deletions
diff --git a/django/utils/html.py b/django/utils/html.py
index 951b3f2a59..094bc6660d 100644
--- a/django/utils/html.py
+++ b/django/utils/html.py
@@ -34,6 +34,31 @@ def escape(html):
return mark_safe(force_unicode(html).replace('&', '&amp;').replace('<', '&lt;').replace('>', '&gt;').replace('"', '&quot;').replace("'", '&#39;'))
escape = allow_lazy(escape, unicode)
+_base_js_escapes = (
+ ('\\', r'\u005C'),
+ ('\'', r'\u0027'),
+ ('"', r'\u0022'),
+ ('>', r'\u003E'),
+ ('<', r'\u003C'),
+ ('&', r'\u0026'),
+ ('=', r'\u003D'),
+ ('-', r'\u002D'),
+ (';', r'\u003B'),
+ (u'\u2028', r'\u2028'),
+ (u'\u2029', r'\u2029')
+)
+
+# Escape every ASCII character with a value less than 32.
+_js_escapes = (_base_js_escapes +
+ tuple([('%c' % z, '\\u%04X' % z) for z in range(32)]))
+
+def escapejs(value):
+ """Hex encodes characters for use in JavaScript strings."""
+ for bad, good in _js_escapes:
+ value = mark_safe(force_unicode(value).replace(bad, good))
+ return value
+escapejs = allow_lazy(escapejs, unicode)
+
def conditional_escape(html):
"""
Similar to escape(), except that it doesn't operate on pre-escaped strings.