Opened 11 years ago

Closed 11 years ago

#1127 closed defect (duplicate)

ifequal template tag does not accept hardcoded strings containing spaces

Reported by: elcio@… Owned by: Adrian Holovaty
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.

Change History (7)

comment:1 Changed 11 years ago by anonymous

priority: normalhigh
Severity: normalcritical

comment:2 Changed 11 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 11 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 11 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 11 years ago by Adrian Holovaty

priority: highnormal
Severity: criticalnormal

comment:6 Changed 11 years ago by Chris Beaven

Patch #1522 would fix this

comment:7 Changed 11 years ago by Adrian Holovaty

Resolution: duplicate
Status: newclosed

Closed in favor of #1522.

Note: See TracTickets for help on using tickets.
Back to Top