Commits

George Xie committed 1288dea

url escape filter for non-unicode

Comments (0)

Files changed (2)

     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):
 
 NON_UNICODE_ESCAPES = DEFAULT_ESCAPES.copy()
 NON_UNICODE_ESCAPES['h'] = 'filters.legacy_html_escape'
+NON_UNICODE_ESCAPES['u'] = 'filters.legacy_url_escape'
 

test/test_filters.py

             "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_(
             "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("""