mirror of https://github.com/django/django.git
Use unicode.translate to speed up js escaping.
This commit is contained in:
parent
40e62a5ccd
commit
44767f2caf
|
@ -42,29 +42,26 @@ def escape(text):
|
||||||
return mark_safe(force_text(text).replace('&', '&').replace('<', '<').replace('>', '>').replace('"', '"').replace("'", '''))
|
return mark_safe(force_text(text).replace('&', '&').replace('<', '<').replace('>', '>').replace('"', '"').replace("'", '''))
|
||||||
escape = allow_lazy(escape, six.text_type)
|
escape = allow_lazy(escape, six.text_type)
|
||||||
|
|
||||||
_base_js_escapes = (
|
_js_escapes = {
|
||||||
('\\', '\\u005C'),
|
ord('\\'): '\\u005C',
|
||||||
('\'', '\\u0027'),
|
ord('\''): '\\u0027',
|
||||||
('"', '\\u0022'),
|
ord('"'): '\\u0022',
|
||||||
('>', '\\u003E'),
|
ord('>'): '\\u003E',
|
||||||
('<', '\\u003C'),
|
ord('<'): '\\u003C',
|
||||||
('&', '\\u0026'),
|
ord('&'): '\\u0026',
|
||||||
('=', '\\u003D'),
|
ord('='): '\\u003D',
|
||||||
('-', '\\u002D'),
|
ord('-'): '\\u002D',
|
||||||
(';', '\\u003B'),
|
ord(';'): '\\u003B',
|
||||||
('\u2028', '\\u2028'),
|
ord('\u2028'): '\\u2028',
|
||||||
('\u2029', '\\u2029')
|
ord('\u2029'): '\\u2029'
|
||||||
)
|
}
|
||||||
|
|
||||||
# Escape every ASCII character with a value less than 32.
|
# Escape every ASCII character with a value less than 32.
|
||||||
_js_escapes = (_base_js_escapes +
|
_js_escapes.update((ord('%c' % z), '\\u%04X' % z) for z in range(32))
|
||||||
tuple([('%c' % z, '\\u%04X' % z) for z in range(32)]))
|
|
||||||
|
|
||||||
def escapejs(value):
|
def escapejs(value):
|
||||||
"""Hex encodes characters for use in JavaScript strings."""
|
"""Hex encodes characters for use in JavaScript strings."""
|
||||||
for bad, good in _js_escapes:
|
return mark_safe(force_text(value).translate(_js_escapes))
|
||||||
value = mark_safe(force_text(value).replace(bad, good))
|
|
||||||
return value
|
|
||||||
escapejs = allow_lazy(escapejs, six.text_type)
|
escapejs = allow_lazy(escapejs, six.text_type)
|
||||||
|
|
||||||
def conditional_escape(text):
|
def conditional_escape(text):
|
||||||
|
|
Loading…
Reference in New Issue