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

login
register
mail settings
Submitter phabricator
Date Oct. 14, 2017, 7:03 a.m.
Message ID <differential-rev-PHID-DREV-o6m6covb6qmgiiwl76ha-req@phab.mercurial-scm.org>
Download mbox | patch
Permalink /patch/24880/
State Superseded
Headers show

Comments

phabricator - Oct. 14, 2017, 7:03 a.m.
durin42 created this revision.
Herald added a subscriber: mercurial-devel.
Herald added a reviewer: hg-reviewers.

REVISION SUMMARY
  This seems like a sensible enough place to put it.

REPOSITORY
  rHG Mercurial

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

AFFECTED FILES
  mercurial/url.py

CHANGE DETAILS




To: durin42, #hg-reviewers
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