1
0
mirror of https://github.com/django/django.git synced 2025-07-04 09:49:12 +00:00

magic-removal: Fixed #1198 -- Modified handling of escape characters in filter arguments, and updated unit tests to reflect the change.

git-svn-id: http://code.djangoproject.com/svn/django/branches/magic-removal@2713 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
Russell Keith-Magee 2006-04-18 13:41:21 +00:00
parent 6112223100
commit 4b3048d8a9
4 changed files with 61 additions and 42 deletions

View File

@ -519,9 +519,9 @@ class FilterExpression(object):
args = [] args = []
constant_arg, i18n_arg, var_arg = match.group("constant_arg", "i18n_arg", "var_arg") constant_arg, i18n_arg, var_arg = match.group("constant_arg", "i18n_arg", "var_arg")
if i18n_arg: if i18n_arg:
args.append((False, _(i18n_arg.replace('\\', '')))) args.append((False, _(i18n_arg.replace(r'\"', '"'))))
elif constant_arg: elif constant_arg:
args.append((False, constant_arg.replace('\\', ''))) args.append((False, constant_arg.replace(r'\"', '"')))
elif var_arg: elif var_arg:
args.append((True, var_arg)) args.append((True, var_arg))
filter_func = parser.find_filter(filter_name) filter_func = parser.find_filter(filter_name)

View File

@ -1,14 +1,16 @@
""" r"""
>>> format(my_birthday, '') >>> format(my_birthday, '')
'' ''
>>> format(my_birthday, 'a') >>> format(my_birthday, 'a')
'p.m.' 'p.m.'
>>> format(my_birthday, 'A') >>> format(my_birthday, 'A')
'PM' 'PM'
>>> format(my_birthday, 'd')
'08'
>>> format(my_birthday, 'j') >>> format(my_birthday, 'j')
'7' '8'
>>> format(my_birthday, 'l') >>> format(my_birthday, 'l')
'Saturday' 'Sunday'
>>> format(my_birthday, 'L') >>> format(my_birthday, 'L')
'False' 'False'
>>> format(my_birthday, 'm') >>> format(my_birthday, 'm')
@ -24,7 +26,7 @@
>>> format(my_birthday, 'P') >>> format(my_birthday, 'P')
'10 p.m.' '10 p.m.'
>>> format(my_birthday, 'r') >>> format(my_birthday, 'r')
'Sat, 7 Jul 1979 22:00:00 +0100' 'Sun, 8 Jul 1979 22:00:00 +0100'
>>> format(my_birthday, 's') >>> format(my_birthday, 's')
'00' '00'
>>> format(my_birthday, 'S') >>> format(my_birthday, 'S')
@ -34,9 +36,9 @@
>>> format(my_birthday, 'T') >>> format(my_birthday, 'T')
'CET' 'CET'
>>> format(my_birthday, 'U') >>> format(my_birthday, 'U')
'300445200' '300531600'
>>> format(my_birthday, 'w') >>> format(my_birthday, 'w')
'6' '0'
>>> format(my_birthday, 'W') >>> format(my_birthday, 'W')
'27' '27'
>>> format(my_birthday, 'y') >>> format(my_birthday, 'y')
@ -44,7 +46,7 @@
>>> format(my_birthday, 'Y') >>> format(my_birthday, 'Y')
'1979' '1979'
>>> format(my_birthday, 'z') >>> format(my_birthday, 'z')
'188' '189'
>>> format(my_birthday, 'Z') >>> format(my_birthday, 'Z')
'3600' '3600'
@ -57,8 +59,11 @@
>>> format(wintertime, 'O') >>> format(wintertime, 'O')
'+0100' '+0100'
>>> format(my_birthday, 'Y z \\C\\E\\T') >>> format(my_birthday, r'Y z \C\E\T')
'1979 188 CET' '1979 189 CET'
>>> format(my_birthday, r'jS o\f F')
'8th of July'
""" """
from django.utils import dateformat, translation from django.utils import dateformat, translation
@ -70,6 +75,6 @@ translation.activate('en-us')
time.tzset() time.tzset()
my_birthday = datetime.datetime(1979, 7, 7, 22, 00) my_birthday = datetime.datetime(1979, 7, 8, 22, 00)
summertime = datetime.datetime(2005, 10, 30, 1, 00) summertime = datetime.datetime(2005, 10, 30, 1, 00)
wintertime = datetime.datetime(2005, 10, 30, 4, 00) wintertime = datetime.datetime(2005, 10, 30, 4, 00)

View File

@ -1,4 +1,4 @@
""" r"""
>>> floatformat(7.7) >>> floatformat(7.7)
'7.7' '7.7'
>>> floatformat(7.0) >>> floatformat(7.0)
@ -12,8 +12,8 @@
>>> floatformat(0.0) >>> floatformat(0.0)
'0' '0'
>>> addslashes('"double quotes" and \\'single quotes\\'') >>> addslashes('"double quotes" and \'single quotes\'')
'\\\\"double quotes\\\\" and \\\\\\'single quotes\\\\\\'' '\\"double quotes\\" and \\\'single quotes\\\''
>>> capfirst('hello world') >>> capfirst('hello world')
'Hello world' 'Hello world'
@ -21,17 +21,17 @@
>>> fix_ampersands('Jack & Jill & Jeroboam') >>> fix_ampersands('Jack & Jill & Jeroboam')
'Jack & Jill & Jeroboam' 'Jack & Jill & Jeroboam'
>>> linenumbers('line 1\\nline 2') >>> linenumbers('line 1\nline 2')
'1. line 1\\n2. line 2' '1. line 1\n2. line 2'
>>> linenumbers('\\n'.join(['x'] * 10)) >>> linenumbers('\n'.join(['x'] * 10))
'01. x\\n02. x\\n03. x\\n04. x\\n05. x\\n06. x\\n07. x\\n08. x\\n09. x\\n10. x' '01. x\n02. x\n03. x\n04. x\n05. x\n06. x\n07. x\n08. x\n09. x\n10. x'
>>> lower('TEST') >>> lower('TEST')
'test' 'test'
>>> lower(u'\\xcb') # uppercase E umlaut >>> lower(u'\xcb') # uppercase E umlaut
u'\\xeb' u'\xeb'
>>> make_list('abc') >>> make_list('abc')
['a', 'b', 'c'] ['a', 'b', 'c']
@ -48,7 +48,7 @@ u'\\xeb'
>>> stringformat(1, 'z') >>> stringformat(1, 'z')
'' ''
>>> title('a nice title, isn\\'t it?') >>> title('a nice title, isn\'t it?')
"A Nice Title, Isn't It?" "A Nice Title, Isn't It?"
@ -68,8 +68,8 @@ u'\\xeb'
>>> upper('Mixed case input') >>> upper('Mixed case input')
'MIXED CASE INPUT' 'MIXED CASE INPUT'
>>> upper(u'\\xeb') # lowercase e umlaut >>> upper(u'\xeb') # lowercase e umlaut
u'\\xcb' u'\xcb'
>>> urlencode('jack & jill') >>> urlencode('jack & jill')
@ -91,8 +91,8 @@ u'\\xcb'
>>> wordcount('lots of words') >>> wordcount('lots of words')
3 3
>>> wordwrap('this is a long paragraph of text that really needs to be wrapped I\\'m afraid', 14) >>> wordwrap('this is a long paragraph of text that really needs to be wrapped I\'m afraid', 14)
"this is a long\\nparagraph of\\ntext that\\nreally needs\\nto be wrapped\\nI'm afraid" "this is a long\nparagraph of\ntext that\nreally needs\nto be wrapped\nI'm afraid"
>>> ljust('test', 10) >>> ljust('test', 10)
'test ' 'test '
@ -124,7 +124,7 @@ u'\\xcb'
>>> linebreaks('line 1') >>> linebreaks('line 1')
'<p>line 1</p>' '<p>line 1</p>'
>>> linebreaks('line 1\\nline 2') >>> linebreaks('line 1\nline 2')
'<p>line 1<br />line 2</p>' '<p>line 1<br />line 2</p>'
>>> removetags('some <b>html</b> with <script>alert("You smell")</script> disallowed <img /> tags', 'script img') >>> removetags('some <b>html</b> with <script>alert("You smell")</script> disallowed <img /> tags', 'script img')
@ -133,19 +133,15 @@ u'\\xcb'
>>> striptags('some <b>html</b> with <script>alert("You smell")</script> disallowed <img /> tags') >>> striptags('some <b>html</b> with <script>alert("You smell")</script> disallowed <img /> tags')
'some html with alert("You smell") disallowed tags' 'some html with alert("You smell") disallowed tags'
>>> dictsort([{'age': 23, 'name': 'Barbara-Ann'},\ >>> dictsort([{'age': 23, 'name': 'Barbara-Ann'},
{'age': 63, 'name': 'Ra Ra Rasputin'},\ ... {'age': 63, 'name': 'Ra Ra Rasputin'},
{'name': 'Jonny B Goode', 'age': 18}], 'age') ... {'name': 'Jonny B Goode', 'age': 18}], 'age')
[{'age': 18, 'name': 'Jonny B Goode'},\ [{'age': 18, 'name': 'Jonny B Goode'}, {'age': 23, 'name': 'Barbara-Ann'}, {'age': 63, 'name': 'Ra Ra Rasputin'}]
{'age': 23, 'name': 'Barbara-Ann'},\
{'age': 63, 'name': 'Ra Ra Rasputin'}]
>>> dictsortreversed([{'age': 23, 'name': 'Barbara-Ann'},\ >>> dictsortreversed([{'age': 23, 'name': 'Barbara-Ann'},
{'age': 63, 'name': 'Ra Ra Rasputin'},\ ... {'age': 63, 'name': 'Ra Ra Rasputin'},
{'name': 'Jonny B Goode', 'age': 18}], 'age') ... {'name': 'Jonny B Goode', 'age': 18}], 'age')
[{'age': 63, 'name': 'Ra Ra Rasputin'},\ [{'age': 63, 'name': 'Ra Ra Rasputin'}, {'age': 23, 'name': 'Barbara-Ann'}, {'age': 18, 'name': 'Jonny B Goode'}]
{'age': 23, 'name': 'Barbara-Ann'},\
{'age': 18, 'name': 'Jonny B Goode'}]
>>> first([0,1,2]) >>> first([0,1,2])
0 0
@ -196,13 +192,13 @@ False
'aceg' 'aceg'
>>> unordered_list(['item 1', []]) >>> unordered_list(['item 1', []])
'\\t<li>item 1</li>' '\t<li>item 1</li>'
>>> unordered_list(['item 1', [['item 1.1', []]]]) >>> unordered_list(['item 1', [['item 1.1', []]]])
'\\t<li>item 1\\n\\t<ul>\\n\\t\\t<li>item 1.1</li>\\n\\t</ul>\\n\\t</li>' '\t<li>item 1\n\t<ul>\n\t\t<li>item 1.1</li>\n\t</ul>\n\t</li>'
>>> unordered_list(['item 1', [['item 1.1', []], ['item 1.2', []]]]) >>> unordered_list(['item 1', [['item 1.1', []], ['item 1.2', []]]])
'\\t<li>item 1\\n\\t<ul>\\n\\t\\t<li>item 1.1</li>\\n\\t\\t<li>item 1.2</li>\\n\\t</ul>\\n\\t</li>' '\t<li>item 1\n\t<ul>\n\t\t<li>item 1.1</li>\n\t\t<li>item 1.2</li>\n\t</ul>\n\t</li>'
>>> add('1', '2') >>> add('1', '2')
3 3
@ -228,6 +224,8 @@ False
# real testing of date() is in dateformat.py # real testing of date() is in dateformat.py
>>> date(datetime.datetime(2005, 12, 29), "d F Y") >>> date(datetime.datetime(2005, 12, 29), "d F Y")
'29 December 2005' '29 December 2005'
>>> date(datetime.datetime(2005, 12, 29), r'jS o\f F')
'29th of December'
# real testing of time() is done in dateformat.py # real testing of time() is done in dateformat.py
>>> time(datetime.time(13), "h") >>> time(datetime.time(13), "h")

View File

@ -4,6 +4,7 @@ from django.conf import settings
from django import template from django import template
from django.template import loader from django.template import loader
from django.utils.translation import activate, deactivate, install from django.utils.translation import activate, deactivate, install
from datetime import datetime
import traceback import traceback
################################# #################################
@ -152,6 +153,12 @@ TEMPLATE_TESTS = {
# the exception propogates # the exception propogates
'basic-syntax34': (r'1{{ var.method4 }}2', {"var": SomeClass()}, SomeOtherException), 'basic-syntax34': (r'1{{ var.method4 }}2', {"var": SomeClass()}, SomeOtherException),
# Escaped backslash in argument
'basic-syntax35': (r'{{ var|default_if_none:"foo\bar" }}', {"var": None}, r'foo\bar'),
# Escaped backslash using known escape char
'basic-syntax35': (r'{{ var|default_if_none:"foo\now" }}', {"var": None}, r'foo\now'),
### COMMENT TAG ########################################################### ### COMMENT TAG ###########################################################
'comment-tag01': ("{% comment %}this is hidden{% endcomment %}hello", {}, "hello"), 'comment-tag01': ("{% comment %}this is hidden{% endcomment %}hello", {}, "hello"),
'comment-tag02': ("{% comment %}this is hidden{% endcomment %}hello{% comment %}foo{% endcomment %}", {}, "hello"), 'comment-tag02': ("{% comment %}this is hidden{% endcomment %}hello{% comment %}foo{% endcomment %}", {}, "hello"),
@ -433,6 +440,15 @@ TEMPLATE_TESTS = {
'widthratio08': ('{% widthratio %}', {}, template.TemplateSyntaxError), 'widthratio08': ('{% widthratio %}', {}, template.TemplateSyntaxError),
'widthratio09': ('{% widthratio a b %}', {'a':50,'b':100}, template.TemplateSyntaxError), 'widthratio09': ('{% widthratio a b %}', {'a':50,'b':100}, template.TemplateSyntaxError),
'widthratio10': ('{% widthratio a b 100.0 %}', {'a':50,'b':100}, template.TemplateSyntaxError), 'widthratio10': ('{% widthratio a b 100.0 %}', {'a':50,'b':100}, template.TemplateSyntaxError),
### NOW TAG ########################################################
# Simple case
'now01' : ('{% now "j n Y"%}', {}, str(datetime.now().day) + ' ' + str(datetime.now().month) + ' ' + str(datetime.now().year)),
# Check parsing of escaped and special characters
'now02' : ('{% now "j "n" Y"%}', {}, template.TemplateSyntaxError),
# 'now03' : ('{% now "j \"n\" Y"%}', {}, str(datetime.now().day) + '"' + str(datetime.now().month) + '"' + str(datetime.now().year)),
# 'now04' : ('{% now "j \nn\n Y"%}', {}, str(datetime.now().day) + '\n' + str(datetime.now().month) + '\n' + str(datetime.now().year))
} }
def test_template_loader(template_name, template_dirs=None): def test_template_loader(template_name, template_dirs=None):