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 Morales, 15 years ago)

Patch updated to port r10118

  • django/template/__init__.py

    diff -r 56d1a7a5bb3e django/template/__init__.py
    a b  
    411411        "A microparser that parses for a value: some string constant or variable name."
    412412        subject = self.subject
    413413        i = self.pointer
     414
     415        def __next_space_index(subject, i):
     416            """Increment pointer until a real space (i.e. a space not within quotes) is encountered"""
     417            while i < len(subject) and subject[i] not in (' ', '\t'):
     418                if subject[i] in ('"', "'"):
     419                    c = subject[i]
     420                    i += 1
     421                    while i < len(subject) and subject[i] != c:
     422                        i += 1
     423                    if i >= len(subject):
     424                        raise TemplateSyntaxError("Searching for value. Unexpected end of string in column %d: %s" % (i, subject))
     425                i += 1
     426            return i
     427
    414428        if i >= len(subject):
    415429            raise TemplateSyntaxError("Searching for value. Expected another value but found end of string: %s" % subject)
    416430        if subject[i] in ('"', "'"):
     
    421435            if i >= len(subject):
    422436                raise TemplateSyntaxError("Searching for value. Unexpected end of string in column %d: %s" % (i, subject))
    423437            i += 1
     438
     439            # Continue parsing until next "real" space, so that filters are also included
     440            i = __next_space_index(subject, i)
     441
    424442            res = subject[p:i]
    425443            while i < len(subject) and subject[i] in (' ', '\t'):
    426444                i += 1
     
    429447            return res
    430448        else:
    431449            p = i
    432             while i < len(subject) and subject[i] not in (' ', '\t'):
    433                 if subject[i] in ('"', "'"):
    434                     c = subject[i]
    435                     i += 1
    436                     while i < len(subject) and subject[i] != c:
    437                         i += 1
    438                     if i >= len(subject):
    439                         raise TemplateSyntaxError("Searching for value. Unexpected end of string in column %d: %s" % (i, subject))
    440                 i += 1
     450            i = __next_space_index(subject, i)
    441451            s = subject[p:i]
    442452            while i < len(subject) and subject[i] in (' ', '\t'):
    443453                i += 1
  • tests/regressiontests/templates/parser.py

    diff -r 56d1a7a5bb3e tests/regressiontests/templates/parser.py
    a b  
    11"""
    22Testing some internals of the template processing. These are *not* examples to be copied in user code.
     3"""
     4
     5token_parsing=r"""
     6Tests for TokenParser behavior in the face of quoted strings with spaces.
     7
     8>>> from django.template import TokenParser
     9
     10
     11Test case 1: {% tag thevar|filter sometag %}
     12
     13>>> p = TokenParser("tag thevar|filter sometag")
     14>>> p.tagname
     15'tag'
     16>>> p.value()
     17'thevar|filter'
     18>>> p.more()
     19True
     20>>> p.tag()
     21'sometag'
     22>>> p.more()
     23False
     24
     25Test case 2: {% tag "a value"|filter sometag %}
     26
     27>>> p = TokenParser('tag "a value"|filter sometag')
     28>>> p.tagname
     29'tag'
     30>>> p.value()
     31'"a value"|filter'
     32>>> p.more()
     33True
     34>>> p.tag()
     35'sometag'
     36>>> p.more()
     37False
     38
     39Test case 3: {% tag 'a value'|filter sometag %}
     40
     41>>> p = TokenParser("tag 'a value'|filter sometag")
     42>>> p.tagname
     43'tag'
     44>>> p.value()
     45"'a value'|filter"
     46>>> p.more()
     47True
     48>>> p.tag()
     49'sometag'
     50>>> p.more()
     51False
    352"""
    453
    554filter_parsing = r"""
  • tests/regressiontests/templates/tests.py

    diff -r 56d1a7a5bb3e tests/regressiontests/templates/tests.py
    a b  
    2222
    2323from context import context_tests
    2424from custom import custom_filters
    25 from parser import filter_parsing, variable_parsing
     25from parser import token_parsing, filter_parsing, variable_parsing
    2626from unicode import unicode_tests
    2727
    2828try:
     
    3636__test__ = {
    3737    'unicode': unicode_tests,
    3838    'context': context_tests,
     39    'token_parsing': token_parsing,
    3940    'filter_parsing': filter_parsing,
     41    'variable_parsing': variable_parsing,
    4042    'custom_filters': custom_filters,
    4143}
    4244
Back to Top