changeset 34694:2976cf87a60a

url: add cgi.escape equivalent for bytestrings This seems like a sensible enough place to put it. Differential Revision: https://phab.mercurial-scm.org/D1066
author Augie Fackler <augie@google.com>
date Sat, 14 Oct 2017 02:57:26 -0400
parents 56bb07a0b75c
children e178fcaa3933
files mercurial/url.py
diffstat 1 files changed, 15 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/mercurial/url.py	Thu Oct 05 14:15:05 2017 -0400
+++ b/mercurial/url.py	Sat Oct 14 02:57:26 2017 -0400
@@ -30,6 +30,21 @@
 urlerr = util.urlerr
 urlreq = util.urlreq
 
+def escape(s, quote=None):
+    '''Replace special characters "&", "<" and ">" to HTML-safe sequences.
+    If the optional flag quote is true, the quotation mark character (")
+    is also translated.
+
+    This is the same as cgi.escape in Python, but always operates on
+    bytes, whereas cgi.escape in Python 3 only works on unicodes.
+    '''
+    s = s.replace(b"&", b"&amp;")
+    s = s.replace(b"<", b"&lt;")
+    s = s.replace(b">", b"&gt;")
+    if quote:
+        s = s.replace(b'"', b"&quot;")
+    return s
+
 class passwordmgr(object):
     def __init__(self, ui, passwddb):
         self.ui = ui