Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Add urlescape to default filters.

  • Loading branch information...
commit 06a8b1c02b2091b31ef4f39ddde3184f25f58260 1 parent 80b4fe6
@aaronsw aaronsw authored
Showing with 14 additions and 2 deletions.
  1. +9 −1 jinja2/filters.py
  2. +5 −1 jinja2/testsuite/filters.py
View
10 jinja2/filters.py
@@ -10,6 +10,7 @@
"""
import re
import math
+import urllib
from random import choice
from operator import itemgetter
from itertools import imap, groupby
@@ -69,6 +70,9 @@ def do_forceescape(value):
value = value.__html__()
return escape(unicode(value))
+def do_urlescape(value):
+ """Escape for use in URLs."""
+ return urllib.quote(value.encode('utf8'))
@evalcontextfilter
def do_replace(eval_ctx, s, old, new, count=None):
@@ -797,5 +801,9 @@ def do_attr(environment, obj, name):
'round': do_round,
'groupby': do_groupby,
'safe': do_mark_safe,
- 'xmlattr': do_xmlattr
+ 'xmlattr': do_xmlattr,
+ 'urlescape': do_urlescape,
+ 'urle': do_urlescape,
+ 'urlencode': do_urlescape,
+ 'urlquote': do_urlescape
}
View
6 jinja2/testsuite/filters.py
@@ -366,7 +366,11 @@ def test_safe(self):
assert tmpl.render() == '<div>foo</div>'
tmpl = env.from_string('{{ "<div>foo</div>" }}')
assert tmpl.render() == '&lt;div&gt;foo&lt;/div&gt;'
-
+
+ def test_urlescape(self):
+ env = Environment(autoescape=True)
+ tmpl = env.from_string('{{ "Hello, world!"|urle }}')
+ assert tmpl.render() == 'Hello%2C%20world%21'
def suite():
suite = unittest.TestSuite()
Please sign in to comment.
Something went wrong with that request. Please try again.