Patchwork D1066: url: add cgi.escape equivalent for bytestrings

login
register
mail settings
Submitter phabricator
Date Oct. 14, 2017, 11:47 a.m.
Message ID <9b703029481ea91207a256ea0cfa029c@localhost.localdomain>
Download mbox | patch
Permalink /patch/24895/
State Not Applicable
Headers show

Comments

phabricator - Oct. 14, 2017, 11:47 a.m.
This revision was automatically updated to reflect the committed changes.
Closed by commit rHG2976cf87a60a: url: add cgi.escape equivalent for bytestrings (authored by durin42, committed by ).

REPOSITORY
  rHG Mercurial

CHANGES SINCE LAST UPDATE
  https://phab.mercurial-scm.org/D1066?vs=2723&id=2732

REVISION DETAIL
  https://phab.mercurial-scm.org/D1066

AFFECTED FILES
  mercurial/url.py

CHANGE DETAILS




To: durin42, #hg-reviewers, yuja
Cc: mercurial-devel

Patch

diff --git a/mercurial/url.py b/mercurial/url.py
--- a/mercurial/url.py
+++ b/mercurial/url.py
@@ -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