Improved performance of utils.html.escape().
This commit is contained in:
parent
6c92f711ea
commit
abb636c1af
|
@ -1,4 +1,5 @@
|
||||||
import copy
|
import copy
|
||||||
|
import itertools
|
||||||
import operator
|
import operator
|
||||||
from functools import total_ordering, wraps
|
from functools import total_ordering, wraps
|
||||||
|
|
||||||
|
@ -189,7 +190,7 @@ def keep_lazy(*resultclasses):
|
||||||
|
|
||||||
@wraps(func)
|
@wraps(func)
|
||||||
def wrapper(*args, **kwargs):
|
def wrapper(*args, **kwargs):
|
||||||
for arg in list(args) + list(kwargs.values()):
|
for arg in itertools.chain(args, kwargs.values()):
|
||||||
if isinstance(arg, Promise):
|
if isinstance(arg, Promise):
|
||||||
break
|
break
|
||||||
else:
|
else:
|
||||||
|
|
|
@ -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_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+$')
|
simple_email_re = re.compile(r'^\S+@\S+\.\S+$')
|
||||||
|
|
||||||
|
_html_escapes = {
|
||||||
|
ord('&'): '&',
|
||||||
|
ord('<'): '<',
|
||||||
|
ord('>'): '>',
|
||||||
|
ord('"'): '"',
|
||||||
|
ord("'"): ''',
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
@keep_lazy(str, SafeText)
|
@keep_lazy(str, SafeText)
|
||||||
def escape(text):
|
def escape(text):
|
||||||
|
@ -41,10 +49,7 @@ def escape(text):
|
||||||
This may result in double-escaping. If this is a concern, use
|
This may result in double-escaping. If this is a concern, use
|
||||||
conditional_escape() instead.
|
conditional_escape() instead.
|
||||||
"""
|
"""
|
||||||
return mark_safe(
|
return mark_safe(str(text).translate(_html_escapes))
|
||||||
str(text).replace('&', '&').replace('<', '<')
|
|
||||||
.replace('>', '>').replace('"', '"').replace("'", ''')
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
_js_escapes = {
|
_js_escapes = {
|
||||||
|
|
Loading…
Reference in New Issue