Code

Ticket #12072: patch-12072-e.diff

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

Tidied up the regex even more

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,13 @@
31     return TemplateTagNode(tag)
32 templatetag = register.tag(templatetag)
33 
34+# Regex for URL arguments including filters
35+url_arg_re = re.compile(
36+    r"(?:(%(name)s)=)?(%(value)s(?:\|%(name)s(?::%(value)s)?)*)" % {
37+        'name':'\w+',
38+        'value':'''(?:(?:'[^']*')|(?:"[^"]*")|(?:[\w\.-]+))'''},
39+    re.VERBOSE)
40+
41 def url(parser, token):
42     """
43     Returns an absolute URL matching given view with its parameters.
44@@ -1118,13 +1125,20 @@
45                 asvar = bits.next()
46                 break
47             else:
48-                for arg in bit.split(","):
49-                    if '=' in arg:
50-                        k, v = arg.split('=', 1)
51-                        k = k.strip()
52-                        kwargs[k] = parser.compile_filter(v)
53-                    elif arg:
54-                        args.append(parser.compile_filter(arg))
55+                end = 0
56+                for i, match in enumerate(url_arg_re.finditer(bit)):
57+                    if (i == 0 and match.start() != 0) or \
58+                          (i > 0 and (bit[end:match.start()] != ',')):
59+                        raise TemplateSyntaxError("Malformed arguments to url tag")
60+                    end = match.end()
61+                    name, value = match.group(1), match.group(2)
62+                    if name:
63+                        kwargs[name] = parser.compile_filter(value)
64+                    else:
65+                        args.append(parser.compile_filter(value))
66+                if end != len(bit):
67+                    raise TemplateSyntaxError("Malformed arguments to url tag")
68+
69     return URLNode(viewname, args, kwargs, asvar)
70 url = register.tag(url)
71