diff -r 56d1a7a5bb3e django/template/__init__.py
a
|
b
|
|
411 | 411 | "A microparser that parses for a value: some string constant or variable name." |
412 | 412 | subject = self.subject |
413 | 413 | 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 | |
414 | 428 | if i >= len(subject): |
415 | 429 | raise TemplateSyntaxError("Searching for value. Expected another value but found end of string: %s" % subject) |
416 | 430 | if subject[i] in ('"', "'"): |
… |
… |
|
421 | 435 | if i >= len(subject): |
422 | 436 | raise TemplateSyntaxError("Searching for value. Unexpected end of string in column %d: %s" % (i, subject)) |
423 | 437 | i += 1 |
| 438 | |
| 439 | # Continue parsing until next "real" space, so that filters are also included |
| 440 | i = __next_space_index(subject, i) |
| 441 | |
424 | 442 | res = subject[p:i] |
425 | 443 | while i < len(subject) and subject[i] in (' ', '\t'): |
426 | 444 | i += 1 |
… |
… |
|
429 | 447 | return res |
430 | 448 | else: |
431 | 449 | 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) |
441 | 451 | s = subject[p:i] |
442 | 452 | while i < len(subject) and subject[i] in (' ', '\t'): |
443 | 453 | i += 1 |
diff -r 56d1a7a5bb3e tests/regressiontests/templates/parser.py
a
|
b
|
|
1 | 1 | """ |
2 | 2 | Testing some internals of the template processing. These are *not* examples to be copied in user code. |
| 3 | """ |
| 4 | |
| 5 | token_parsing=r""" |
| 6 | Tests for TokenParser behavior in the face of quoted strings with spaces. |
| 7 | |
| 8 | >>> from django.template import TokenParser |
| 9 | |
| 10 | |
| 11 | Test 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() |
| 19 | True |
| 20 | >>> p.tag() |
| 21 | 'sometag' |
| 22 | >>> p.more() |
| 23 | False |
| 24 | |
| 25 | Test 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() |
| 33 | True |
| 34 | >>> p.tag() |
| 35 | 'sometag' |
| 36 | >>> p.more() |
| 37 | False |
| 38 | |
| 39 | Test 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() |
| 47 | True |
| 48 | >>> p.tag() |
| 49 | 'sometag' |
| 50 | >>> p.more() |
| 51 | False |
3 | 52 | """ |
4 | 53 | |
5 | 54 | filter_parsing = r""" |
diff -r 56d1a7a5bb3e tests/regressiontests/templates/tests.py
a
|
b
|
|
22 | 22 | |
23 | 23 | from context import context_tests |
24 | 24 | from custom import custom_filters |
25 | | from parser import filter_parsing, variable_parsing |
| 25 | from parser import token_parsing, filter_parsing, variable_parsing |
26 | 26 | from unicode import unicode_tests |
27 | 27 | |
28 | 28 | try: |
… |
… |
|
36 | 36 | __test__ = { |
37 | 37 | 'unicode': unicode_tests, |
38 | 38 | 'context': context_tests, |
| 39 | 'token_parsing': token_parsing, |
39 | 40 | 'filter_parsing': filter_parsing, |
| 41 | 'variable_parsing': variable_parsing, |
40 | 42 | 'custom_filters': custom_filters, |
41 | 43 | } |
42 | 44 | |