Fixed #5971 - Fixed inconsistent behaviour of the TokenParser when parsing filters that follow constant strings or variables. Thanks Dmitri Fedortchenko, Adam Vandenberg and Ramiro Morales.
git-svn-id: http://code.djangoproject.com/svn/django/trunk@12471 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
parent
a88cbc141d
commit
b459f5b7e3
1
AUTHORS
1
AUTHORS
|
@ -462,6 +462,7 @@ answer newbie questions, and generally made Django that much better:
|
||||||
tt@gurgle.no
|
tt@gurgle.no
|
||||||
David Tulig <david.tulig@gmail.com>
|
David Tulig <david.tulig@gmail.com>
|
||||||
Amit Upadhyay <http://www.amitu.com/blog/>
|
Amit Upadhyay <http://www.amitu.com/blog/>
|
||||||
|
Adam Vandenberg
|
||||||
Geert Vanderkelen
|
Geert Vanderkelen
|
||||||
Vasil Vangelovski
|
Vasil Vangelovski
|
||||||
I.S. van Oostveen <v.oostveen@idca.nl>
|
I.S. van Oostveen <v.oostveen@idca.nl>
|
||||||
|
|
|
@ -421,6 +421,20 @@ class TokenParser(object):
|
||||||
"A microparser that parses for a value: some string constant or variable name."
|
"A microparser that parses for a value: some string constant or variable name."
|
||||||
subject = self.subject
|
subject = self.subject
|
||||||
i = self.pointer
|
i = self.pointer
|
||||||
|
|
||||||
|
def next_space_index(subject, i):
|
||||||
|
"Increment pointer until a real space (i.e. a space not within quotes) is encountered"
|
||||||
|
while i < len(subject) and subject[i] not in (' ', '\t'):
|
||||||
|
if subject[i] in ('"', "'"):
|
||||||
|
c = subject[i]
|
||||||
|
i += 1
|
||||||
|
while i < len(subject) and subject[i] != c:
|
||||||
|
i += 1
|
||||||
|
if i >= len(subject):
|
||||||
|
raise TemplateSyntaxError("Searching for value. Unexpected end of string in column %d: %s" % (i, subject))
|
||||||
|
i += 1
|
||||||
|
return i
|
||||||
|
|
||||||
if i >= len(subject):
|
if i >= len(subject):
|
||||||
raise TemplateSyntaxError("Searching for value. Expected another value but found end of string: %s" % subject)
|
raise TemplateSyntaxError("Searching for value. Expected another value but found end of string: %s" % subject)
|
||||||
if subject[i] in ('"', "'"):
|
if subject[i] in ('"', "'"):
|
||||||
|
@ -431,6 +445,10 @@ class TokenParser(object):
|
||||||
if i >= len(subject):
|
if i >= len(subject):
|
||||||
raise TemplateSyntaxError("Searching for value. Unexpected end of string in column %d: %s" % (i, subject))
|
raise TemplateSyntaxError("Searching for value. Unexpected end of string in column %d: %s" % (i, subject))
|
||||||
i += 1
|
i += 1
|
||||||
|
|
||||||
|
# Continue parsing until next "real" space, so that filters are also included
|
||||||
|
i = next_space_index(subject, i)
|
||||||
|
|
||||||
res = subject[p:i]
|
res = subject[p:i]
|
||||||
while i < len(subject) and subject[i] in (' ', '\t'):
|
while i < len(subject) and subject[i] in (' ', '\t'):
|
||||||
i += 1
|
i += 1
|
||||||
|
@ -439,15 +457,7 @@ class TokenParser(object):
|
||||||
return res
|
return res
|
||||||
else:
|
else:
|
||||||
p = i
|
p = i
|
||||||
while i < len(subject) and subject[i] not in (' ', '\t'):
|
i = next_space_index(subject, i)
|
||||||
if subject[i] in ('"', "'"):
|
|
||||||
c = subject[i]
|
|
||||||
i += 1
|
|
||||||
while i < len(subject) and subject[i] != c:
|
|
||||||
i += 1
|
|
||||||
if i >= len(subject):
|
|
||||||
raise TemplateSyntaxError("Searching for value. Unexpected end of string in column %d: %s" % (i, subject))
|
|
||||||
i += 1
|
|
||||||
s = subject[p:i]
|
s = subject[p:i]
|
||||||
while i < len(subject) and subject[i] in (' ', '\t'):
|
while i < len(subject) and subject[i] in (' ', '\t'):
|
||||||
i += 1
|
i += 1
|
||||||
|
|
|
@ -2,6 +2,55 @@
|
||||||
Testing some internals of the template processing. These are *not* examples to be copied in user code.
|
Testing some internals of the template processing. These are *not* examples to be copied in user code.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
token_parsing=r"""
|
||||||
|
Tests for TokenParser behavior in the face of quoted strings with spaces.
|
||||||
|
|
||||||
|
>>> from django.template import TokenParser
|
||||||
|
|
||||||
|
|
||||||
|
Test case 1: {% tag thevar|filter sometag %}
|
||||||
|
|
||||||
|
>>> p = TokenParser("tag thevar|filter sometag")
|
||||||
|
>>> p.tagname
|
||||||
|
'tag'
|
||||||
|
>>> p.value()
|
||||||
|
'thevar|filter'
|
||||||
|
>>> p.more()
|
||||||
|
True
|
||||||
|
>>> p.tag()
|
||||||
|
'sometag'
|
||||||
|
>>> p.more()
|
||||||
|
False
|
||||||
|
|
||||||
|
Test case 2: {% tag "a value"|filter sometag %}
|
||||||
|
|
||||||
|
>>> p = TokenParser('tag "a value"|filter sometag')
|
||||||
|
>>> p.tagname
|
||||||
|
'tag'
|
||||||
|
>>> p.value()
|
||||||
|
'"a value"|filter'
|
||||||
|
>>> p.more()
|
||||||
|
True
|
||||||
|
>>> p.tag()
|
||||||
|
'sometag'
|
||||||
|
>>> p.more()
|
||||||
|
False
|
||||||
|
|
||||||
|
Test case 3: {% tag 'a value'|filter sometag %}
|
||||||
|
|
||||||
|
>>> p = TokenParser("tag 'a value'|filter sometag")
|
||||||
|
>>> p.tagname
|
||||||
|
'tag'
|
||||||
|
>>> p.value()
|
||||||
|
"'a value'|filter"
|
||||||
|
>>> p.more()
|
||||||
|
True
|
||||||
|
>>> p.tag()
|
||||||
|
'sometag'
|
||||||
|
>>> p.more()
|
||||||
|
False
|
||||||
|
"""
|
||||||
|
|
||||||
filter_parsing = r"""
|
filter_parsing = r"""
|
||||||
>>> from django.template import FilterExpression, Parser
|
>>> from django.template import FilterExpression, Parser
|
||||||
|
|
||||||
|
|
|
@ -22,7 +22,7 @@ from django.utils.tzinfo import LocalTimezone
|
||||||
|
|
||||||
from context import context_tests
|
from context import context_tests
|
||||||
from custom import custom_filters
|
from custom import custom_filters
|
||||||
from parser import filter_parsing, variable_parsing
|
from parser import token_parsing, filter_parsing, variable_parsing
|
||||||
from unicode import unicode_tests
|
from unicode import unicode_tests
|
||||||
from smartif import *
|
from smartif import *
|
||||||
|
|
||||||
|
@ -37,7 +37,9 @@ import filters
|
||||||
__test__ = {
|
__test__ = {
|
||||||
'unicode': unicode_tests,
|
'unicode': unicode_tests,
|
||||||
'context': context_tests,
|
'context': context_tests,
|
||||||
|
'token_parsing': token_parsing,
|
||||||
'filter_parsing': filter_parsing,
|
'filter_parsing': filter_parsing,
|
||||||
|
'variable_parsing': variable_parsing,
|
||||||
'custom_filters': custom_filters,
|
'custom_filters': custom_filters,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue