mirror of
https://github.com/django/django.git
synced 2025-10-26 07:06:08 +00:00
Fixed #24469 -- Refined escaping of Django's form elements in non-Django templates.
This commit is contained in:
committed by
Tim Graham
parent
dc5b01ad05
commit
1f2abf784a
@@ -360,3 +360,34 @@ def avoid_wrapping(value):
|
||||
spaces where there previously were normal spaces.
|
||||
"""
|
||||
return value.replace(" ", "\xa0")
|
||||
|
||||
|
||||
def html_safe(klass):
|
||||
"""
|
||||
A decorator that defines the __html__ method. This helps non-Django
|
||||
templates to detect classes whose __str__ methods return SafeText.
|
||||
"""
|
||||
if '__html__' in klass.__dict__:
|
||||
raise ValueError(
|
||||
"can't apply @html_safe to %s because it defines "
|
||||
"__html__()." % klass.__name__
|
||||
)
|
||||
if six.PY2:
|
||||
if '__unicode__' not in klass.__dict__:
|
||||
raise ValueError(
|
||||
"can't apply @html_safe to %s because it doesn't "
|
||||
"define __unicode__()." % klass.__name__
|
||||
)
|
||||
klass_unicode = klass.__unicode__
|
||||
klass.__unicode__ = lambda self: mark_safe(klass_unicode(self))
|
||||
klass.__html__ = lambda self: unicode(self)
|
||||
else:
|
||||
if '__str__' not in klass.__dict__:
|
||||
raise ValueError(
|
||||
"can't apply @html_safe to %s because it doesn't "
|
||||
"define __str__()." % klass.__name__
|
||||
)
|
||||
klass_str = klass.__str__
|
||||
klass.__str__ = lambda self: mark_safe(klass_str(self))
|
||||
klass.__html__ = lambda self: str(self)
|
||||
return klass
|
||||
|
||||
Reference in New Issue
Block a user