Code

Opened 8 years ago

Closed 8 years ago

#1127 closed defect (duplicate)

ifequal template tag does not accept hardcoded strings containing spaces

Reported by: elcio@… Owned by: adrian
Component: Template system Version: master
Severity: normal Keywords:
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: UI/UX:

Description

This works:

{% ifequal test "test" %}
    foo...
{% endifequal %}

This not:

{% ifequal test "test 2" %}
    foo...
{% endifequal %}

Suggested patch:

django/trunk/django/core/defaulttags.py
===================================================================
--- defaulttags.py      (revision 1782)
+++ defaulttags.py      (workcopy)
@@ -479,7 +479,16 @@
             ...
         {% endifnotequal %}
     """
-    bits = token.contents.split()
+    tmpbits = token.contents.split()
+    bits=[]
+    sep=""
+    for i in tmpbits:
+       if(sep):
+           bits[-1]+=" "+i
+           if(i[-1]==sep):sep=""
+       else:
+           bits.append(i)
+           if(i[0]=="'" or i[0]=='"'):sep=i
     if len(bits) != 3:
         raise TemplateSyntaxError, "%r takes two arguments" % bits[0]
     end_tag = 'end' + bits[0]

I'm afraid this is not an elegant solution. I think it must to be done in other template tags that uses token.contents.split() and are supposed to accept hardcoded strings. I think django/trunk/tests/othertests/templates.py must be also changed.

Attachments (0)

Change History (7)

comment:1 Changed 8 years ago by anonymous

  • priority changed from normal to high
  • Severity changed from normal to critical

comment:2 Changed 8 years ago by Armin Ronacher <armin.ronacher@…>

Hm. Not so good patch. I would split it using this code:

from re import split
bits = split(r'\s+', token.contents, 1)
if len(bits) != 2 or (bits[1][:1] not in '"\'' or bits[1][-1:] not in '"\'':
    raise TemplateSyntaxError, "%r takes two arguments" % bits[0]

Otherwise you will loose double spaces.

comment:3 Changed 8 years ago by hugo

Actually there is a miniparser for template tag parameters in the code that can be used to parse stuff that might contain spaces. It is used in django.templatetags.i18n for example in the blocktrans tag.

comment:4 Changed 8 years ago by Karsu

invalid syntax (line 3)

Fixed code:

from re import split
bits = split(r'\s+', token.contents, 1)
if len(bits) != 2 or bits[1][:1] not in '"\'' or bits[1][-1:] not in '"\'':
    raise TemplateSyntaxError, "%r takes two arguments" % bits[0]

comment:5 Changed 8 years ago by adrian

  • priority changed from high to normal
  • Severity changed from critical to normal

comment:6 Changed 8 years ago by SmileyChris

Patch #1522 would fix this

comment:7 Changed 8 years ago by adrian

  • Resolution set to duplicate
  • Status changed from new to closed

Closed in favor of #1522.

Add Comment

Modify Ticket

Change Properties
<Author field>
Action
as closed
as The resolution will be set. Next status will be 'closed'
The resolution will be deleted. Next status will be 'new'
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.