Commit bc315266 authored by Claude Paroz's avatar Claude Paroz
Browse files

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

Thanks Steve Pike for the report.
parent 6a0291bd
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -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:
+4 −2
Original line number Diff line number Diff line
@@ -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

+2 −1
Original line number Diff line number Diff line
@@ -20,7 +20,7 @@ from django.test import TestCase
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 @@ class DefaultFiltersTests(TestCase):

    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)
+3 −2
Original line number Diff line number Diff line
@@ -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'),