From bc315266c86f371ab04d05c43383775267e8595a Mon Sep 17 00:00:00 2001 From: Claude Paroz Date: Sat, 22 Mar 2014 16:33:37 +0100 Subject: [PATCH] Fixed #22294 -- Prevented converting length filter output to string Thanks Steve Pike for the report. --- django/template/defaultfilters.py | 2 +- docs/ref/templates/builtins.txt | 6 ++++-- tests/defaultfilters/tests.py | 3 ++- tests/template_tests/filters.py | 5 +++-- 4 files changed, 10 insertions(+), 6 deletions(-) diff --git a/django/template/defaultfilters.py b/django/template/defaultfilters.py index f55aa072b7e..be192d0ebff 100644 --- a/django/template/defaultfilters.py +++ b/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: diff --git a/docs/ref/templates/builtins.txt b/docs/ref/templates/builtins.txt index 34dd42252a5..b5734f234f7 100644 --- a/docs/ref/templates/builtins.txt +++ b/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 diff --git a/tests/defaultfilters/tests.py b/tests/defaultfilters/tests.py index 966e86bbaa9..9a794bd9e7d 100644 --- a/tests/defaultfilters/tests.py +++ b/tests/defaultfilters/tests.py @@ -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) diff --git a/tests/template_tests/filters.py b/tests/template_tests/filters.py index 371d0325b62..9fa6918f926 100644 --- a/tests/template_tests/filters.py +++ b/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'),