Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Fixed #22294 -- Prevented converting length filter output to string

Thanks Steve Pike for the report.
  • Loading branch information...
commit bc315266c86f371ab04d05c43383775267e8595a 1 parent 6a0291b
@claudep claudep authored
View
2  django/template/defaultfilters.py
@@ -572,7 +572,7 @@ def last(value):
return ''
-@register.filter(is_safe=True)
+@register.filter(is_safe=False)
def length(value):
"""Returns the length of the value - useful for lists."""
try:
View
6 docs/ref/templates/builtins.txt
@@ -1680,7 +1680,8 @@ For example::
{{ value|length }}
-If ``value`` is ``['a', 'b', 'c', 'd']``, the output will be ``4``.
+If ``value`` is ``['a', 'b', 'c', 'd']`` or ``"abcd"``, the output will be
+``4``.
.. templatefilter:: length_is
@@ -1693,7 +1694,8 @@ For example::
{{ value|length_is:"4" }}
-If ``value`` is ``['a', 'b', 'c', 'd']``, the output will be ``True``.
+If ``value`` is ``['a', 'b', 'c', 'd']`` or ``"abcd"``, the output will be
+``True``.
.. templatefilter:: linebreaks
View
3  tests/defaultfilters/tests.py
@@ -20,7 +20,7 @@
from django.utils import six
from django.utils import translation
from django.utils.encoding import python_2_unicode_compatible
-from django.utils.safestring import SafeData
+from django.utils.safestring import mark_safe, SafeData
class DefaultFiltersTests(TestCase):
@@ -495,6 +495,7 @@ def test_join(self):
def test_length(self):
self.assertEqual(length('1234'), 4)
+ self.assertEqual(length(mark_safe('1234')), 4)
self.assertEqual(length([1, 2, 3, 4]), 4)
self.assertEqual(length_is([], 0), True)
self.assertEqual(length_is([], 1), False)
View
5 tests/template_tests/filters.py
@@ -320,9 +320,10 @@ def get_filter_tests():
'length02': ('{{ list|length }}', {'list': []}, '0'),
'length03': ('{{ string|length }}', {'string': ''}, '0'),
'length04': ('{{ string|length }}', {'string': 'django'}, '6'),
+ 'length05': ('{% if string|length == 6 %}Pass{% endif %}', {'string': mark_safe('django')}, 'Pass'),
# Invalid uses that should fail silently.
- 'length05': ('{{ int|length }}', {'int': 7}, ''),
- 'length06': ('{{ None|length }}', {'None': None}, ''),
+ 'length06': ('{{ int|length }}', {'int': 7}, ''),
+ 'length07': ('{{ None|length }}', {'None': None}, ''),
# length_is filter.
'length_is01': ('{% if some_list|length_is:"4" %}Four{% endif %}', {'some_list': ['4', None, True, {}]}, 'Four'),

1 comment on commit bc31526

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