Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Fixed #8462 -- Made `length` and `length_is` template filters fail si…

…lently when given a value that has undefined length and added tests for both filters. Based on patch from marcelor, rob, and SmileyChris.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@10193 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit 432f7f624ac4b6b5fab9a10ae450e71d1f8ecf64 1 parent a6d7b41
Gary Wilson Jr. authored March 30, 2009
10  django/template/defaultfilters.py
@@ -515,12 +515,18 @@ def last(value):
515 515
 
516 516
 def length(value):
517 517
     """Returns the length of the value - useful for lists."""
518  
-    return len(value)
  518
+    try:
  519
+        return len(value)
  520
+    except (ValueError, TypeError):
  521
+        return ''
519 522
 length.is_safe = True
520 523
 
521 524
 def length_is(value, arg):
522 525
     """Returns a boolean of whether the value's length is the argument."""
523  
-    return len(value) == int(arg)
  526
+    try:
  527
+        return len(value) == int(arg)
  528
+    except (ValueError, TypeError):
  529
+        return ''
524 530
 length_is.is_safe = False
525 531
 
526 532
 def random(value):
26  tests/regressiontests/templates/filters.py
@@ -281,13 +281,33 @@ def get_filter_tests():
281 281
         'escapejs01': (r'{{ a|escapejs }}', {'a': 'testing\r\njavascript \'string" <b>escaping</b>'}, 'testing\\x0D\\x0Ajavascript \\x27string\\x22 \\x3Cb\\x3Eescaping\\x3C/b\\x3E'),
282 282
         'escapejs02': (r'{% autoescape off %}{{ a|escapejs }}{% endautoescape %}', {'a': 'testing\r\njavascript \'string" <b>escaping</b>'}, 'testing\\x0D\\x0Ajavascript \\x27string\\x22 \\x3Cb\\x3Eescaping\\x3C/b\\x3E'),
283 283
 
  284
+
  285
+        # length filter.
  286
+        'length01': ('{{ list|length }}', {'list': ['4', None, True, {}]}, '4'),
  287
+        'length02': ('{{ list|length }}', {'list': []}, '0'),
  288
+        'length03': ('{{ string|length }}', {'string': ''}, '0'),
  289
+        'length04': ('{{ string|length }}', {'string': 'django'}, '6'),
  290
+        # Invalid uses that should fail silently.
  291
+        'length05': ('{{ int|length }}', {'int': 7}, ''),
  292
+        'length06': ('{{ None|length }}', {'None': None}, ''),
  293
+
  294
+        # length_is filter.
  295
+        'length_is01': ('{% if some_list|length_is:"4" %}Four{% endif %}', {'some_list': ['4', None, True, {}]}, 'Four'),
  296
+        'length_is02': ('{% if some_list|length_is:"4" %}Four{% else %}Not Four{% endif %}', {'some_list': ['4', None, True, {}, 17]}, 'Not Four'),
  297
+        'length_is03': ('{% if mystring|length_is:"4" %}Four{% endif %}', {'mystring': 'word'}, 'Four'),
  298
+        'length_is04': ('{% if mystring|length_is:"4" %}Four{% else %}Not Four{% endif %}', {'mystring': 'Python'}, 'Not Four'),
  299
+        'length_is05': ('{% if mystring|length_is:"4" %}Four{% else %}Not Four{% endif %}', {'mystring': ''}, 'Not Four'),
  300
+        'length_is06': ('{% with var|length as my_length %}{{ my_length }}{% endwith %}', {'var': 'django'}, '6'),
284 301
         # Boolean return value from length_is should not be coerced to a string
285  
-        'lengthis01': (r'{% if "X"|length_is:0 %}Length is 0{% else %}Length not 0{% endif %}', {}, 'Length not 0'),
286  
-        'lengthis02': (r'{% if "X"|length_is:1 %}Length is 1{% else %}Length not 1{% endif %}', {}, 'Length is 1'),
  302
+        'length_is07': (r'{% if "X"|length_is:0 %}Length is 0{% else %}Length not 0{% endif %}', {}, 'Length not 0'),
  303
+        'length_is08': (r'{% if "X"|length_is:1 %}Length is 1{% else %}Length not 1{% endif %}', {}, 'Length is 1'),
  304
+        # Invalid uses that should fail silently.
  305
+        'length_is09': ('{{ var|length_is:"fish" }}', {'var': 'django'}, ''),
  306
+        'length_is10': ('{{ int|length_is:"1" }}', {'int': 7}, ''),
  307
+        'length_is11': ('{{ none|length_is:"1" }}', {'none': None}, ''),
287 308
 
288 309
         'join01': (r'{{ a|join:", " }}', {'a': ['alpha', 'beta & me']}, 'alpha, beta &amp; me'),
289 310
         'join02': (r'{% autoescape off %}{{ a|join:", " }}{% endautoescape %}', {'a': ['alpha', 'beta & me']}, 'alpha, beta & me'),
290 311
         'join03': (r'{{ a|join:" &amp; " }}', {'a': ['alpha', 'beta & me']}, 'alpha &amp; beta &amp; me'),
291 312
         'join04': (r'{% autoescape off %}{{ a|join:" &amp; " }}{% endautoescape %}', {'a': ['alpha', 'beta & me']}, 'alpha &amp; beta & me'),
292 313
     }
293  
-

0 notes on commit 432f7f6

Please sign in to comment.
Something went wrong with that request. Please try again.