Opened 16 years ago

Closed 16 years ago

Last modified 16 years ago

#8966 closed (fixed)

{% if "x"|length_is:n %} always evaluates True

Reported by: Thomas Steinacher <tom@…> Owned by: Carl Meyer
Component: Template system Version: dev
Severity: Keywords:
Cc: Triage Stage: Ready for checkin
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

{% if "x"|capfirst|length_is:n %} is always true. Example:

In [1]: from django.template import Template, Context

In [2]: Template('{% if "x"|capfirst|length_is:0 %}This is wrong{% endif %}').render(Context({}))
Out[2]: u'This is wrong'

Using latest Django SVN.

Attachments (3)

8966_test.diff (866 bytes ) - added by Carl Meyer 16 years ago.
failing test
8966_naive_fix.diff (471 bytes ) - added by Carl Meyer 16 years ago.
simple fix (remove is_safe from length_is filter)
8966_with_docs.diff (1.3 KB ) - added by Carl Meyer 16 years ago.
same patch, with warning note in docs

Download all attachments as: .zip

Change History (9)

comment:1 by Carl Meyer, 16 years ago

Has patch: set
Summary: {% if "x"|capfirst|length_is:n %} always evaluates True{% if "x"|length_is:n %} always evaluates True
Version: 1.0SVN

capfirst is irrelevant. This also fails:

>>> Template('{% if "x"|length_is:0 %}Wrong{% endif %}').render(Context({}))
u'Wrong'

Changed summary to reflect this. Also changed version to SVN, as this problem is still present in trunk.

The problem here is that the is_safe attribute is set on the length_is tag, which causes mark_safe to be called on the return value, and mark_safe(False) returns the string 'False'. Not sure where in that chain is the proper place for the fix. It's possible that mark_safe needs to be smart about not always coercing its argument to a string, or FilterExpression.resolve needs to check if the return value is a string before calling mark_safe on it. But the naive fix is just to remove the is_safe attribute from length_is, since auto-escaping will never harm its return value.

by Carl Meyer, 16 years ago

Attachment: 8966_test.diff added

failing test

by Carl Meyer, 16 years ago

Attachment: 8966_naive_fix.diff added

simple fix (remove is_safe from length_is filter)

comment:2 by Ivan Giuliani, 16 years ago

Component: UncategorizedTemplate system

comment:3 by Chris Beaven, 16 years ago

Triage Stage: UnreviewedReady for checkin

Removal of is_safe is the right thing to do. It's a gotcha though -- a note explaining that is_safe shouldn't be used if you're relying on a non-string return object in docs/howto/custom-template-tags.txt would be useful.

comment:4 by Carl Meyer, 16 years ago

Owner: changed from nobody to Carl Meyer

by Carl Meyer, 16 years ago

Attachment: 8966_with_docs.diff added

same patch, with warning note in docs

comment:5 by Karen Tracey, 16 years ago

Resolution: fixed
Status: newclosed

(In [9291]) Fixed #8966 -- Changed is_safe for length_is filter to False, since its return value is a boolean, not a string.

Thanks Thomas Steinacher and carljm.

comment:6 by Karen Tracey, 16 years ago

(In [9292]) [1.0.X] Fixed #8966 -- Changed is_safe for length_is filter to False, since its return value is a boolean, not a string.

Thanks Thomas Steinacher, carljm, and SmileyChris.

Backport of r9291 from trunk.

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