Code

Ticket #12072: patch-12072-c.diff

File patch-12072-c.diff, 4.1 KB (added by alexdutton, 4 years ago)

Tidier patch

Line 
1Index: tests/regressiontests/templates/tests.py
2===================================================================
3--- tests/regressiontests/templates/tests.py    (revision 12494)
4+++ tests/regressiontests/templates/tests.py    (working copy)
5@@ -1031,11 +1031,20 @@
6             'url08': (u'{% url метка_оператора v %}', {'v': 'Ω'}, '/url_tag/%D0%AE%D0%BD%D0%B8%D0%BA%D0%BE%D0%B4/%CE%A9/'),
7             'url09': (u'{% url метка_оператора_2 tag=v %}', {'v': 'Ω'}, '/url_tag/%D0%AE%D0%BD%D0%B8%D0%BA%D0%BE%D0%B4/%CE%A9/'),
8             'url10': ('{% url regressiontests.templates.views.client_action id=client.id,action="two words" %}', {'client': {'id': 1}}, '/url_tag/client/1/two%20words/'),
9+            'url11': ('{% url regressiontests.templates.views.client_action id=client.id,action="==" %}', {'client': {'id': 1}}, '/url_tag/client/1/==/'),
10+            'url12': ('{% url regressiontests.templates.views.client_action id=client.id,action="," %}', {'client': {'id': 1}}, '/url_tag/client/1/,/'),
11+            'url12': ('{% url regressiontests.templates.views.client_action id=client.id,action=arg|join:"-" %}', {'client': {'id': 1}, 'arg':['a','b']}, '/url_tag/client/1/a-b/'),
12 
13             # Failures
14             'url-fail01': ('{% url %}', {}, template.TemplateSyntaxError),
15             'url-fail02': ('{% url no_such_view %}', {}, urlresolvers.NoReverseMatch),
16             'url-fail03': ('{% url regressiontests.templates.views.client %}', {}, urlresolvers.NoReverseMatch),
17+            'url-fail04': ('{% url view id, %}', {}, template.TemplateSyntaxError),
18+            'url-fail05': ('{% url view id= %}', {}, template.TemplateSyntaxError),
19+            'url-fail06': ('{% url view a.id=id %}', {}, template.TemplateSyntaxError),
20+            'url-fail07': ('{% url view a.id!id %}', {}, template.TemplateSyntaxError),
21+            'url-fail08': ('{% url view id="unterminatedstring %}', {}, template.TemplateSyntaxError),
22+            'url-fail09': ('{% url view id=", %}', {}, template.TemplateSyntaxError),
23 
24             # {% url ... as var %}
25             'url-asvar01': ('{% url regressiontests.templates.views.index as url %}', {}, ''),
26Index: django/template/defaulttags.py
27===================================================================
28--- django/template/defaulttags.py      (revision 12494)
29+++ django/template/defaulttags.py      (working copy)
30@@ -1071,6 +1071,18 @@
31     return TemplateTagNode(tag)
32 templatetag = register.tag(templatetag)
33 
34+# Regex for URL arguments including filters
35+value = '''(?:(?:'[^']*')|(?:"[^"]*")|(?:[\w\.-]+))'''
36+value = r"(%(value)s(?:\|%(name)s(?::%(value)s)?)*)" % {'name':'(?:\w+)', 'value':value}
37+url_arg_re = re.compile('''
38+        %(name)s=%(value)s
39+        |
40+        %(value)s
41+    ''' % {
42+            'name': '(\w+)',
43+            'value': value
44+        }, re.VERBOSE)
45+
46 def url(parser, token):
47     """
48     Returns an absolute URL matching given view with its parameters.
49@@ -1118,13 +1130,19 @@
50                 asvar = bits.next()
51                 break
52             else:
53-                for arg in bit.split(","):
54-                    if '=' in arg:
55-                        k, v = arg.split('=', 1)
56-                        k = k.strip()
57-                        kwargs[k] = parser.compile_filter(v)
58-                    elif arg:
59-                        args.append(parser.compile_filter(arg))
60+                for i, match in enumerate(url_arg_re.finditer(bit)):
61+                    if (i == 0 and match.start() != 0) or \
62+                          (i > 0 and (bit[end:match.start()] != ',')):
63+                        raise TemplateSyntaxError("Malformed arguments to url tag")
64+                    end = match.end()
65+                    name, value = match.group(1), match.group(2) or match.group(3)
66+                    if name:
67+                        kwargs[name] = parser.compile_filter(value)
68+                    else:
69+                        args.append(parser.compile_filter(value))
70+                if end != len(bit):
71+                    raise TemplateSyntaxError("Malformed arguments to url tag")
72+
73     return URLNode(viewname, args, kwargs, asvar)
74 url = register.tag(url)
75