Kaydet (Commit) 4603487d authored tarafından Ezio Melotti's avatar Ezio Melotti

#18020: improve html.escape speed by an order of magnitude. Patch by Matt Bryant.

üst 071029fa
...@@ -2,11 +2,6 @@ ...@@ -2,11 +2,6 @@
General functions for HTML manipulation. General functions for HTML manipulation.
""" """
_escape_map = {ord('&'): '&amp;', ord('<'): '&lt;', ord('>'): '&gt;'}
_escape_map_full = {ord('&'): '&amp;', ord('<'): '&lt;', ord('>'): '&gt;',
ord('"'): '&quot;', ord('\''): '&#x27;'}
# NB: this is a candidate for a bytes/string polymorphic interface # NB: this is a candidate for a bytes/string polymorphic interface
def escape(s, quote=True): def escape(s, quote=True):
...@@ -16,6 +11,10 @@ def escape(s, quote=True): ...@@ -16,6 +11,10 @@ def escape(s, quote=True):
characters, both double quote (") and single quote (') characters are also characters, both double quote (") and single quote (') characters are also
translated. translated.
""" """
s = s.replace("&", "&amp;") # Must be done first!
s = s.replace("<", "&lt;")
s = s.replace(">", "&gt;")
if quote: if quote:
return s.translate(_escape_map_full) s = s.replace('"', "&quot;")
return s.translate(_escape_map) s = s.replace('\'', "&#x27;")
return s
...@@ -172,6 +172,7 @@ Dave Brueck ...@@ -172,6 +172,7 @@ Dave Brueck
Francisco Martín Brugué Francisco Martín Brugué
Ian Bruntlett Ian Bruntlett
Floris Bruynooghe Floris Bruynooghe
Matt Bryant
Stan Bubrouski Stan Bubrouski
Erik de Bueger Erik de Bueger
Jan-Hein Bührman Jan-Hein Bührman
......
...@@ -142,6 +142,9 @@ Core and Builtins ...@@ -142,6 +142,9 @@ Core and Builtins
Library Library
------- -------
- Issue #18020: improve html.escape speed by an order of magnitude.
Patch by Matt Bryant.
- Issue #18347: ElementTree's html serializer now preserves the case of - Issue #18347: ElementTree's html serializer now preserves the case of
closing tags. closing tags.
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment