1
0
mirror of https://github.com/django/django.git synced 2025-10-31 09:41:08 +00:00

Fixed #12945 -- Corrected the parsing of arguments in {% url %} when the argument list has spaces between commas. This is a revised version of r12503, which was a fix for #12072. Thanks to SmileyChris for the patch, and to dmoisset for finding all the places in the docs that the old style syntax was used.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@12889 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
Russell Keith-Magee
2010-03-30 12:44:30 +00:00
parent 273a002544
commit dafc077e4a
4 changed files with 67 additions and 41 deletions

View File

@@ -14,6 +14,8 @@ from django.utils.itercompat import groupby
from django.utils.safestring import mark_safe
register = Library()
# Regex for token keyword arguments
kwarg_re = re.compile(r"(?:(\w+)=)?(.+)")
class AutoEscapeControlNode(Node):
"""Implements the actions of the autoescape tag."""
@@ -1063,12 +1065,9 @@ def templatetag(parser, token):
return TemplateTagNode(tag)
templatetag = register.tag(templatetag)
# Regex for URL arguments including filters
url_arg_re = re.compile(
r"(?:(%(name)s)=)?(%(value)s(?:\|%(name)s(?::%(value)s)?)*)" % {
'name':'\w+',
'value':'''(?:(?:'[^']*')|(?:"[^"]*")|(?:[\w\.-]+))'''},
re.VERBOSE)
# Backwards compatibility check which will fail against for old comma
# separated arguments in the url tag.
url_backwards_re = re.compile(r'''(('[^']*'|"[^"]*"|[^,]+)=?)+$''')
def url(parser, token):
"""
@@ -1077,7 +1076,11 @@ def url(parser, token):
This is a way to define links that aren't tied to a particular URL
configuration::
{% url path.to.some_view arg1,arg2,name1=value1 %}
{% url path.to.some_view arg1 arg2 %}
or
{% url path.to.some_view name1=value1 name2=value2 %}
The first argument is a path to a view. It can be an absolute python path
or just ``app_name.view_name`` without the project name if the view is
@@ -1109,27 +1112,28 @@ def url(parser, token):
args = []
kwargs = {}
asvar = None
bits = bits[2:]
if len(bits) >= 2 and bits[-2] == 'as':
asvar = bits[-1]
bits = bits[:-2]
if len(bits) > 2:
bits = iter(bits[2:])
# Backwards compatibility: {% url urlname arg1,arg2 %} or
# {% url urlname arg1,arg2 as foo %} cases.
if bits:
old_args = ''.join(bits)
if not url_backwards_re.match(old_args):
bits = old_args.split(",")
if len(bits):
for bit in bits:
if bit == 'as':
asvar = bits.next()
break
match = kwarg_re.match(bit)
if not match:
raise TemplateSyntaxError("Malformed arguments to url tag")
name, value = match.groups()
if name:
kwargs[name] = parser.compile_filter(value)
else:
end = 0
for i, match in enumerate(url_arg_re.finditer(bit)):
if (i == 0 and match.start() != 0) or \
(i > 0 and (bit[end:match.start()] != ',')):
raise TemplateSyntaxError("Malformed arguments to url tag")
end = match.end()
name, value = match.group(1), match.group(2)
if name:
kwargs[name] = parser.compile_filter(value)
else:
args.append(parser.compile_filter(value))
if end != len(bit):
raise TemplateSyntaxError("Malformed arguments to url tag")
args.append(parser.compile_filter(value))
return URLNode(viewname, args, kwargs, asvar)
url = register.tag(url)