diff --git a/documentation/index.rst b/documentation/index.rst index b7ec2754e..259e81ce8 100644 --- a/documentation/index.rst +++ b/documentation/index.rst @@ -873,3 +873,18 @@ have any public members. attributes in Python 2 and 3. *old_mod* is the name of the Python 2 module. *new_mod* is the name of the Python 3 module. If *new_attr* is not given, it defaults to *old_attr*. If neither is given, they both default to *name*. + + +Deprecated and Removed Modules and Attributes +>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> + +.. function:: html_escape(s, quote=True) + + :func:`py2:cgi.escape` was deprecated in version 3.7 and is no longer + available in versions 3.8+. The intended replacement is + :func:`py3:html.escape`, which differs in two ways: + + 1. The default value for the *quote* parameter is ``True`` rather than + ``False``. + 2. When *quote* is ``True``, single quote characters (``'``) are also + quoted. diff --git a/six.py b/six.py index d0aece89f..05b6a63ea 100644 --- a/six.py +++ b/six.py @@ -938,6 +938,18 @@ def python_2_unicode_compatible(klass): return klass +if sys.version_info[:2] >= (3, 2): + import html + def html_escape(s, quote=True): + return html.escape(s, quote) +else: + import cgi + def html_escape(s, quote=True): + escaped = cgi.escape(s, quote) + if quote: + escaped = escaped.replace("'", "'") + return escaped + # Complete the moves implementation. # This code is at the end of this module to speed up module loading. # Turn this module into a package. diff --git a/test_six.py b/test_six.py index 0b7206741..5e88deae5 100644 --- a/test_six.py +++ b/test_six.py @@ -1002,6 +1002,13 @@ def __bytes__(self): assert getattr(six.moves.builtins, 'bytes', str)(my_test) == six.b("hello") +def test_html_escape(): + assert six.html_escape('\'
"&something;"
\'') == \ + ''<div>"&something;"</div>'' + assert six.html_escape('\'
"&something;"
\'', False) == \ + '\'<div>"&something;"</div>\'' + + class EnsureTests: # grinning face emoji