diff options
author | Xie Shi <xieshi@douban.com> | 2014-05-04 15:58:14 +0800 |
---|---|---|
committer | Xie Shi <xieshi@douban.com> | 2014-05-04 16:08:27 +0800 |
commit | 1288dea2b028ad4b4a12a2dad588f7cd0917a352 (patch) | |
tree | c3a4bb21205ef350d541e57037ed263225f4337c | |
parent | 6d0a8b09bae6f870d0b09232d0c2daf836c68bbd (diff) | |
download | mako-1288dea2b028ad4b4a12a2dad588f7cd0917a352.tar.gz |
url escape filter for non-unicode
-rw-r--r-- | mako/filters.py | 5 | ||||
-rw-r--r-- | test/test_filters.py | 22 |
2 files changed, 27 insertions, 0 deletions
diff --git a/mako/filters.py b/mako/filters.py index af7abc2..ed13cec 100644 --- a/mako/filters.py +++ b/mako/filters.py @@ -48,6 +48,10 @@ def url_escape(string): string = string.encode("utf8") return quote_plus(string) +def legacy_url_escape(string): + # convert into a list of octets + return quote_plus(string) + def url_unescape(string): text = unquote_plus(string) if not is_ascii_str(text): @@ -193,4 +197,5 @@ if compat.py3k: NON_UNICODE_ESCAPES = DEFAULT_ESCAPES.copy() NON_UNICODE_ESCAPES['h'] = 'filters.legacy_html_escape' +NON_UNICODE_ESCAPES['u'] = 'filters.legacy_url_escape' diff --git a/test/test_filters.py b/test/test_filters.py index 5bd9766..64f36f6 100644 --- a/test/test_filters.py +++ b/test/test_filters.py @@ -40,6 +40,16 @@ class FilterTest(TemplateTest): "foo <'some bar'>" ) + def test_url_escaping(self): + t = Template(""" + http://example.com/?bar=${bar | u}&v=1 + """) + + eq_( + flatten_result(t.render(bar=u"酒吧bar")), + "http://example.com/?bar=%E9%85%92%E5%90%A7bar&v=1" + ) + def test_entity(self): t = Template("foo ${bar | entity}") eq_( @@ -59,6 +69,18 @@ class FilterTest(TemplateTest): "foo <'привет'>" ) + @requires_python_2 + def test_url_escaping_non_unicode(self): + t = Template(""" + http://example.com/?bar=${bar | u}&v=1 + """, disable_unicode=True, + output_encoding=None) + + eq_( + flatten_result(t.render(bar="酒吧bar")), + "http://example.com/?bar=%E9%85%92%E5%90%A7bar&v=1" + ) + def test_def(self): t = Template(""" |