summaryrefslogtreecommitdiff
path: root/django/utils/html.py
diff options
context:
space:
mode:
authorTom <tom@tomforb.es>2017-10-13 02:02:04 +0100
committerTim Graham <timograham@gmail.com>2017-10-13 09:20:13 -0400
commitabb636c1af7b2fd00a624985f60b7aff07374580 (patch)
tree5cae22e50ad86121886a085eb1a12c0c5a3cf599 /django/utils/html.py
parent6c92f711eaf382113e811e43900f4fabd0f95c26 (diff)
Improved performance of utils.html.escape().
Diffstat (limited to 'django/utils/html.py')
-rw-r--r--django/utils/html.py13
1 files changed, 9 insertions, 4 deletions
diff --git a/django/utils/html.py b/django/utils/html.py
index e365cd41f6..4fefbc6355 100644
--- a/django/utils/html.py
+++ b/django/utils/html.py
@@ -30,6 +30,14 @@ simple_url_re = re.compile(r'^https?://\[?\w', re.IGNORECASE)
simple_url_2_re = re.compile(r'^www\.|^(?!http)\w[^@]+\.(com|edu|gov|int|mil|net|org)($|/.*)$', re.IGNORECASE)
simple_email_re = re.compile(r'^\S+@\S+\.\S+$')
+_html_escapes = {
+ ord('&'): '&amp;',
+ ord('<'): '&lt;',
+ ord('>'): '&gt;',
+ ord('"'): '&quot;',
+ ord("'"): '&#39;',
+}
+
@keep_lazy(str, SafeText)
def escape(text):
@@ -41,10 +49,7 @@ def escape(text):
This may result in double-escaping. If this is a concern, use
conditional_escape() instead.
"""
- return mark_safe(
- str(text).replace('&', '&amp;').replace('<', '&lt;')
- .replace('>', '&gt;').replace('"', '&quot;').replace("'", '&#39;')
- )
+ return mark_safe(str(text).translate(_html_escapes))
_js_escapes = {