好的,我想我将使用一组混合的自定义函数:
编码:使用enpreURIComponent(),然后放回斜杠。
解码:对找到的所有%hex值进行解码。
这是我最终使用的内容的更完整变体(它也可以正确处理Unipre):
function quoteUrl(url, safe) { if (typeof(safe) !== 'string') { safe = '/'; // Don't escape slashes by default } url = enpreURIComponent(url); // Unescape characters that were in the safe list toUnenpre = [ ]; for (var i = safe.length - 1; i >= 0; --i) { var enpred = enpreURIComponent(safe[i]); if (enpred !== safe.charAt(i)) { // Ignore safe char if it wasn't escaped toUnenpre.push(enpred); } } url = url.replace(new RegExp(toUnenpre.join('|'), 'ig'), depreURIComponent); return url;}var unquoteUrl = depreURIComponent; // Make alias to have symmetric function names请注意,如果在编码时(你并不需要“安全”的字符
'/'默认的Python),那么你可以使用内置的
enpreURIComponent()和
depreURIComponent()直接的功能。
另外,如果字符串中包含Unipre字符(即,代码点> =
128的字符),则为了保持与Javascript的兼容性
enpreURIComponent(),Python
quote_url()必须为:
def quote_url(url, safe): """URL-enpres a string (either str (i.e. ASCII) or unipre); uses de-facto UTF-8 encoding to handle Unipre prepoints in given string. """ return urllib.quote(unipre(url).enpre('utf-8'), safe)并且
unquote_url()将是:
def unquote_url(url): """Depres a URL that was enpred using quote_url. Returns a unipre instance. """ return urllib.unquote(url).depre('utf-8')


