Code

Ticket #5971: 5971-parser_consistency_literals_and_filters-r10246.diff

File 5971-parser_consistency_literals_and_filters-r10246.diff, 4.2 KB (added by ramiro, 5 years ago)

Patch updated to port r10118

Line 
1diff -r 56d1a7a5bb3e django/template/__init__.py
2--- a/django/template/__init__.py       Tue Mar 31 04:54:48 2009 -0300
3+++ b/django/template/__init__.py       Tue Mar 31 07:23:08 2009 -0300
4@@ -411,6 +411,20 @@
5         "A microparser that parses for a value: some string constant or variable name."
6         subject = self.subject
7         i = self.pointer
8+
9+        def __next_space_index(subject, i):
10+            """Increment pointer until a real space (i.e. a space not within quotes) is encountered"""
11+            while i < len(subject) and subject[i] not in (' ', '\t'):
12+                if subject[i] in ('"', "'"):
13+                    c = subject[i]
14+                    i += 1
15+                    while i < len(subject) and subject[i] != c:
16+                        i += 1
17+                    if i >= len(subject):
18+                        raise TemplateSyntaxError("Searching for value. Unexpected end of string in column %d: %s" % (i, subject))
19+               i += 1
20+            return i
21+
22         if i >= len(subject):
23             raise TemplateSyntaxError("Searching for value. Expected another value but found end of string: %s" % subject)
24         if subject[i] in ('"', "'"):
25@@ -421,6 +435,10 @@
26             if i >= len(subject):
27                 raise TemplateSyntaxError("Searching for value. Unexpected end of string in column %d: %s" % (i, subject))
28             i += 1
29+
30+            # Continue parsing until next "real" space, so that filters are also included
31+            i = __next_space_index(subject, i)
32+
33             res = subject[p:i]
34             while i < len(subject) and subject[i] in (' ', '\t'):
35                 i += 1
36@@ -429,15 +447,7 @@
37             return res
38         else:
39             p = i
40-            while i < len(subject) and subject[i] not in (' ', '\t'):
41-                if subject[i] in ('"', "'"):
42-                    c = subject[i]
43-                    i += 1
44-                    while i < len(subject) and subject[i] != c:
45-                        i += 1
46-                    if i >= len(subject):
47-                        raise TemplateSyntaxError("Searching for value. Unexpected end of string in column %d: %s" % (i, subject))
48-                i += 1
49+            i = __next_space_index(subject, i)
50             s = subject[p:i]
51             while i < len(subject) and subject[i] in (' ', '\t'):
52                 i += 1
53diff -r 56d1a7a5bb3e tests/regressiontests/templates/parser.py
54--- a/tests/regressiontests/templates/parser.py Tue Mar 31 04:54:48 2009 -0300
55+++ b/tests/regressiontests/templates/parser.py Tue Mar 31 07:23:08 2009 -0300
56@@ -1,5 +1,54 @@
57 """
58 Testing some internals of the template processing. These are *not* examples to be copied in user code.
59+"""
60+
61+token_parsing=r"""
62+Tests for TokenParser behavior in the face of quoted strings with spaces.
63+
64+>>> from django.template import TokenParser
65+
66+
67+Test case 1: {% tag thevar|filter sometag %}
68+
69+>>> p = TokenParser("tag thevar|filter sometag")
70+>>> p.tagname
71+'tag'
72+>>> p.value()
73+'thevar|filter'
74+>>> p.more()
75+True
76+>>> p.tag()
77+'sometag'
78+>>> p.more()
79+False
80+
81+Test case 2: {% tag "a value"|filter sometag %}
82+
83+>>> p = TokenParser('tag "a value"|filter sometag')
84+>>> p.tagname
85+'tag'
86+>>> p.value()
87+'"a value"|filter'
88+>>> p.more()
89+True
90+>>> p.tag()
91+'sometag'
92+>>> p.more()
93+False
94+
95+Test case 3: {% tag 'a value'|filter sometag %}
96+
97+>>> p = TokenParser("tag 'a value'|filter sometag")
98+>>> p.tagname
99+'tag'
100+>>> p.value()
101+"'a value'|filter"
102+>>> p.more()
103+True
104+>>> p.tag()
105+'sometag'
106+>>> p.more()
107+False
108 """
109 
110 filter_parsing = r"""
111diff -r 56d1a7a5bb3e tests/regressiontests/templates/tests.py
112--- a/tests/regressiontests/templates/tests.py  Tue Mar 31 04:54:48 2009 -0300
113+++ b/tests/regressiontests/templates/tests.py  Tue Mar 31 07:23:08 2009 -0300
114@@ -22,7 +22,7 @@
115 
116 from context import context_tests
117 from custom import custom_filters
118-from parser import filter_parsing, variable_parsing
119+from parser import token_parsing, filter_parsing, variable_parsing
120 from unicode import unicode_tests
121 
122 try:
123@@ -36,7 +36,9 @@
124 __test__ = {
125     'unicode': unicode_tests,
126     'context': context_tests,
127+    'token_parsing': token_parsing,
128     'filter_parsing': filter_parsing,
129+    'variable_parsing': variable_parsing,
130     'custom_filters': custom_filters,
131 }
132