Ticket #6271: 6271.diff
File 6271.diff, 9.5 KB (added by , 17 years ago) |
---|
-
django/utils/text.py
196 196 return str(ustring_re.sub(fix, s)) 197 197 javascript_quote = allow_lazy(javascript_quote, unicode) 198 198 199 smart_split_re = re.compile('("(?:[^"\\\\]*(?:\\\\.[^"\\\\]*)*)"|\'(?:[^\'\\\\]*(?:\\\\.[^\'\\\\]*)*)\'|[^\\s]+)') 199 smart_split_re = re.compile('([^"\\\\ ]*"(?:[^"\\\\]*(?:\\\\.[^"\\\\]*)*)"[^"\\\\ ]*' 200 "|[^'\\\\ ]*'(?:[^'\\\\]*(?:\\\\.[^'\\\\]*)*)'[^'\\\\ ]*" 201 '|[^\\s]+)') 200 202 def smart_split(text): 201 203 """ 202 204 Generator that splits a string by spaces, leaving quoted phrases together. -
django/template/defaulttags.py
160 160 def __init__(self, nodelist, *varlist): 161 161 self.nodelist = nodelist 162 162 self._last_seen = None 163 self._varlist = map(Variable, varlist)163 self._varlist = varlist 164 164 165 165 def render(self, context): 166 166 if 'forloop' in context and context['forloop']['first']: … … 611 611 ========================== ================================================ 612 612 613 613 """ 614 bits = token. contents.split()614 bits = token.split_contents() 615 615 if len(bits) < 4: 616 616 raise TemplateSyntaxError("'for' statements should have at least four" 617 617 " words: %s" % token.contents) … … 635 635 do_for = register.tag("for", do_for) 636 636 637 637 def do_ifequal(parser, token, negate): 638 bits = list(token.split_contents())638 bits = token.split_contents() 639 639 if len(bits) != 3: 640 640 raise TemplateSyntaxError, "%r takes two arguments" % bits[0] 641 641 end_tag = 'end' + bits[0] … … 799 799 {% endifchanged %} 800 800 {% endfor %} 801 801 """ 802 bits = token. contents.split()802 bits = token.split_contents() 803 803 nodelist = parser.parse(('endifchanged',)) 804 804 parser.delete_first_token() 805 return IfChangedNode(nodelist, * bits[1:])805 return IfChangedNode(nodelist, *[parser.compile_filter(bit) for bit in bits[1:]]) 806 806 ifchanged = register.tag(ifchanged) 807 807 808 808 #@register.tag … … 923 923 {% regroup people|dictsort:"gender" by gender as grouped %} 924 924 925 925 """ 926 bits = token. contents.split()926 bits = token.split_contents() 927 927 if len(bits) != 6: 928 928 raise TemplateSyntaxError, "'regroup' tag takes five arguments" 929 929 target = parser.compile_filter(bits[1]) … … 1088 1088 {{ total }} object{{ total|pluralize }} 1089 1089 {% endwith %} 1090 1090 """ 1091 bits = list(token.split_contents())1091 bits = token.split_contents() 1092 1092 if len(bits) != 4 or bits[2] != "as": 1093 1093 raise TemplateSyntaxError("%r expected format is 'value as name'" % 1094 1094 bits[0]) -
tests/regressiontests/templates/tests.py
8 8 9 9 import os 10 10 import unittest 11 from datetime import datetime, timedelta11 from datetime import datetime, date 12 12 13 13 from django import template 14 14 from django.template import loader … … 922 922 # implementation details (fortunately, the (no)autoescape block 923 923 # tags can be used in those cases) 924 924 'autoescape-filtertag01': ("{{ first }}{% filter safe %}{{ first }} x<y{% endfilter %}", {"first": "<a>"}, template.TemplateSyntaxError), 925 926 ### Tags using a filtered argument that is passed an argument ##### 927 ### with a space (see #6271). ##################################### 928 'argfilter01': ('{% regroup data by bar|date:"F Y" as grouped %}' + \ 929 '{% for group in grouped %}' + \ 930 '{{ group.grouper }}:' + \ 931 '{% for item in group.list %}' + \ 932 '{{ item.foo }}' + \ 933 '{% endfor %},' + \ 934 '{% endfor %}', 935 {'data': [{'foo':'c', 'bar':datetime(2000,1,1)}, 936 {'foo':'d', 'bar':datetime(2000,1,1)}, 937 {'foo':'a', 'bar':datetime(2000,2,1)}, 938 {'foo':'b', 'bar':datetime(2000,2,1)}, 939 {'foo':'x', 'bar':datetime(2000,3,1)} ]}, 940 'January 2000:cd,February 2000:ab,March 2000:x,'), 941 # The firstof, if, ifequal, and ifnotequal tags need some more work to accept 942 # arguments with filters. 943 # 'argfilter02': ('{% firstof date|date:"F Y" "text" %}', 944 # {'date': date(2007, 6, 2)}, 'June 2007'), 945 # # This test fails when TEMPLATE_STRING_IF_INVALID='INVALID' 946 # #'argfilter03': ('{% firstof date|date:"F Y" "text" %}', 947 # # {}, 'text'), 948 # 'argfilter04': ('{% firstof "text" date|date:"F Y" %}', 949 # {'date': date(2007, 6, 2)}, 'text'), 950 # 'argfilter05': ('{% for letter in date|date:"F Y" %}{{ letter }}-{% endfor %}', 951 # {'date': date(2007, 6, 2)}, 'J-u-n-e- -2-0-0-7-'), 952 # 'argfilter06': ('{% ifequal date|date:"F Y" "June 2007" %}equal{% endifequal %}', 953 # {'date': date(2007, 6, 2)}, 'equal'), 954 # 'argfilter07': ('{% ifnotequal date|date:"F Y" "June 2008" %}notequal{% endifnotequal %}', 955 # {'date': date(2007, 6, 2)}, 'notequal'), 956 # 'argfilter08': ('{% if date|date:"F Y" %}true{% endif %}', 957 # {'date': date(2007, 6, 2)}, 'true'), 958 # 'argfilter09': ('{% if date|date:"F Y" %}{% else %}false{% endif %}', 959 # {}, 'false'), 960 'argfilter10': ('{% for date in data %}{% ifchanged date|date:"F Y" %}changed{% endifchanged %}{% endfor %}', 961 {'data': [date(2007, 6, 2), 962 date(2007, 6, 10), 963 date(2007, 12, 30), 964 date(2007, 12, 31), 965 ]}, 966 'changedchanged'), 967 'argfilter11': ('{% with date|date:"F Y" as formatted_date %}{{ formatted_date }}{% endwith %}', 968 {'date': date(2007, 6, 2)}, 'June 2007'), 925 969 } 926 970 927 971 if __name__ == "__main__": -
tests/regressiontests/utils/tests.py
4 4 5 5 from unittest import TestCase 6 6 7 from django.utils import html, checksums 7 from django.utils import html, checksums, text 8 8 9 9 import timesince 10 10 import datastructures … … 15 15 'datastructures': datastructures, 16 16 } 17 17 18 class TestUtilsHtml(TestCase):19 18 19 class UtilsTestCase(TestCase): 20 20 21 def check_output(self, function, value, output=None): 21 22 """ 22 23 Check that function(value) equals output. If output is None, … … 26 27 output = value 27 28 self.assertEqual(function(value), output) 28 29 30 def check_generator_output(self, function, value, output=None): 31 """ 32 Check that list(function(value)) equals output. If output is None, 33 check that list(function(value)) equals value. 34 """ 35 if output is None: 36 output = value 37 self.assertEqual(list(function(value)), output) 38 39 class TestUtilsText(UtilsTestCase): 40 41 def test_smart_split(self): 42 f = text.smart_split 43 items = ( 44 # Double quotes. 45 ('arg1 arg2|filter:"1" arg3', ['arg1', 'arg2|filter:"1"', 'arg3']), 46 ('arg1 arg2|filter:"1 2" arg3', ['arg1', 'arg2|filter:"1 2"', 'arg3']), 47 ('arg1 arg2|filter:"1 2" arg3', ['arg1', 'arg2|filter:"1 2"', 'arg3']), 48 ('arg1 arg2|filter:"1\t2" arg3', ['arg1', 'arg2|filter:"1\t2"', 'arg3']), 49 #('arg1 arg2|filter:"1\"1" arg3', ['arg1', 'arg2|filter:"1"1', 'arg3']), 50 # Singe quotes. 51 ("arg1 arg2|filter:'1' arg3", ['arg1', "arg2|filter:'1'", 'arg3']), 52 ("arg1 arg2|filter:'1 2' arg3", ['arg1', "arg2|filter:'1 2'", 'arg3']), 53 ("arg1 arg2|filter:'1 2' arg3", ['arg1', "arg2|filter:'1 2'", 'arg3']), 54 ("arg1 arg2|filter:'1\t2' arg3", ['arg1', "arg2|filter:'1\t2'", 'arg3']), 55 ) 56 for value, output in items: 57 self.check_generator_output(f, value, output) 58 59 60 class TestUtilsHtml(UtilsTestCase): 61 29 62 def test_escape(self): 30 63 f = html.escape 31 64 items = ( … … 123 156 for value, output in items: 124 157 self.check_output(f, value, output) 125 158 126 class TestUtilsChecksums( TestCase):159 class TestUtilsChecksums(UtilsTestCase): 127 160 128 def check_output(self, function, value, output=None):129 """130 Check that function(value) equals output. If output is None,131 check that function(value) equals value.132 """133 if output is None:134 output = value135 self.assertEqual(function(value), output)136 137 161 def test_luhn(self): 138 162 f = checksums.luhn 139 163 items = (