Removed fix_ampersands template filter per deprecation timeline.

Also removed related utility functions:
* django.utils.html.fix_ampersands
* django.utils.html.clean_html
This commit is contained in:
Tim Graham 2014-03-21 08:32:01 -04:00
parent 99339c77f6
commit 8b81dee60c
7 changed files with 3 additions and 144 deletions

View File

@ -12,7 +12,7 @@ from django.conf import settings
from django.utils import formats
from django.utils.dateformat import format, time_format
from django.utils.encoding import force_text, iri_to_uri
from django.utils.html import (conditional_escape, escapejs, fix_ampersands,
from django.utils.html import (conditional_escape, escapejs,
escape, urlize as _urlize, linebreaks, strip_tags, avoid_wrapping,
remove_tags)
from django.utils.http import urlquote
@ -82,12 +82,6 @@ def escapejs_filter(value):
return escapejs(value)
@register.filter("fix_ampersands", is_safe=True)
@stringfilter
def fix_ampersands_filter(value):
"""Replaces ampersands with ``&`` entities."""
return fix_ampersands(value)
# Values for testing floatformat input against infinity and NaN representations,
# which differ across platforms and Python versions. Some (i.e. old Windows
# ones) are not recognized by Decimal but we want to return them unchanged vs.

View File

@ -3,9 +3,7 @@
from __future__ import unicode_literals
import re
import warnings
from django.utils.deprecation import RemovedInDjango18Warning
from django.utils.encoding import force_text, force_str
from django.utils.functional import allow_lazy
from django.utils.safestring import SafeData, mark_safe
@ -174,15 +172,6 @@ def strip_entities(value):
strip_entities = allow_lazy(strip_entities, six.text_type)
def fix_ampersands(value):
"""Returns the given HTML with all unencoded ampersands encoded correctly."""
# As fix_ampersands is wrapped in allow_lazy, stacklevel 3 is more useful than 2.
warnings.warn("The fix_ampersands function is deprecated and will be removed in Django 1.8.",
RemovedInDjango18Warning, stacklevel=3)
return unencoded_ampersands_re.sub('&', force_text(value))
fix_ampersands = allow_lazy(fix_ampersands, six.text_type)
def smart_urlquote(url):
"Quotes a URL if it isn't already quoted."
# Handle IDN before quoting.
@ -283,44 +272,6 @@ def urlize(text, trim_url_limit=None, nofollow=False, autoescape=False):
urlize = allow_lazy(urlize, six.text_type)
def clean_html(text):
"""
Clean the given HTML. Specifically, do the following:
* Convert <b> and <i> to <strong> and <em>.
* Encode all ampersands correctly.
* Remove all "target" attributes from <a> tags.
* Remove extraneous HTML, such as presentational tags that open and
immediately close and <br clear="all">.
* Convert hard-coded bullets into HTML unordered lists.
* Remove stuff like "<p>&nbsp;&nbsp;</p>", but only if it's at the
bottom of the text.
"""
# As clean_html is wrapped in allow_lazy, stacklevel 3 is more useful than 2.
warnings.warn("The clean_html function is deprecated and will be removed in Django 1.8.",
RemovedInDjango18Warning, stacklevel=3)
text = normalize_newlines(text)
text = re.sub(r'<(/?)\s*b\s*>', '<\\1strong>', text)
text = re.sub(r'<(/?)\s*i\s*>', '<\\1em>', text)
text = fix_ampersands(text)
# Remove all target="" attributes from <a> tags.
text = link_target_attribute_re.sub('\\1', text)
# Trim stupid HTML such as <br clear="all">.
text = html_gunk_re.sub('', text)
# Convert hard-coded bullets into HTML unordered lists.
def replace_p_tags(match):
s = match.group().replace('</p>', '</li>')
for d in DOTS:
s = s.replace('<p>%s' % d, '<li>')
return '<ul>\n%s\n</ul>' % s
text = hard_coded_bullets_re.sub(replace_p_tags, text)
# Remove stuff like "<p>&nbsp;&nbsp;</p>", but only if it's at the bottom
# of the text.
text = trailing_empty_content_re.sub('', text)
return text
clean_html = allow_lazy(clean_html, six.text_type)
def avoid_wrapping(value):
"""
Avoid text wrapping in the middle of a phrase by adding non-breaking

View File

@ -1572,35 +1572,6 @@ For example::
If ``value`` is the list ``['a', 'b', 'c']``, the output will be ``'a'``.
.. templatefilter:: fix_ampersands
fix_ampersands
^^^^^^^^^^^^^^
.. note::
This is rarely useful as ampersands are automatically escaped. See
:tfilter:`escape` for more information.
.. deprecated:: 1.7
This filter has been deprecated and will be removed in Django 1.8.
Replaces ampersands with ``&amp;`` entities.
For example::
{{ value|fix_ampersands }}
If ``value`` is ``Tom & Jerry``, the output will be ``Tom &amp; Jerry``.
However, ampersands used in named entities and numeric character references
will not be replaced. For example, if ``value`` is ``Caf&eacute;``, the output
will *not* be ``Caf&amp;eacute;`` but remain ``Caf&eacute;``. This means that
in some edge cases, such as acronyms followed by semicolons, this filter will
not replace ampersands that need replacing. For example, if ``value`` is
``Contact the R&D;``, the output will remain unchanged because ``&D;``
resembles a named entity.
.. templatefilter:: floatformat
floatformat

View File

@ -4,12 +4,11 @@ from __future__ import unicode_literals
import datetime
import decimal
import unittest
import warnings
from django.template.defaultfilters import (
add, addslashes, capfirst, center, cut, date, default, default_if_none,
dictsort, dictsortreversed, divisibleby, escape, escapejs_filter,
filesizeformat, first, fix_ampersands_filter, floatformat, force_escape,
filesizeformat, first, floatformat, force_escape,
get_digit, iriencode, join, length, length_is, linebreaksbr,
linebreaks_filter, linenumbers, ljust, lower, make_list,
phone2numeric_filter, pluralize, removetags, rjust, slice_filter, slugify,
@ -20,7 +19,6 @@ from django.template.defaultfilters import (
from django.test import TestCase
from django.utils import six
from django.utils import translation
from django.utils.deprecation import RemovedInDjango18Warning
from django.utils.encoding import python_2_unicode_compatible
from django.utils.safestring import SafeData
@ -125,12 +123,6 @@ class DefaultFiltersTests(TestCase):
escapejs_filter('paragraph separator:\u2029and line separator:\u2028'),
'paragraph separator:\\u2029and line separator:\\u2028')
def test_fix_ampersands(self):
with warnings.catch_warnings():
warnings.simplefilter("ignore", RemovedInDjango18Warning)
self.assertEqual(fix_ampersands_filter('Jack & Jill & Jeroboam'),
'Jack &amp; Jill &amp; Jeroboam')
def test_linenumbers(self):
self.assertEqual(linenumbers('line 1\nline 2'),
'1. line 1\n2. line 2')

View File

@ -104,11 +104,6 @@ def get_filter_tests():
'filter-capfirst01': ("{% autoescape off %}{{ a|capfirst }} {{ b|capfirst }}{% endautoescape %}", {"a": "fred>", "b": mark_safe("fred&gt;")}, "Fred> Fred&gt;"),
'filter-capfirst02': ("{{ a|capfirst }} {{ b|capfirst }}", {"a": "fred>", "b": mark_safe("fred&gt;")}, "Fred&gt; Fred&gt;"),
# Note that applying fix_ampsersands in autoescape mode leads to
# double escaping.
'filter-fix_ampersands01': ("{% autoescape off %}{{ a|fix_ampersands }} {{ b|fix_ampersands }}{% endautoescape %}", {"a": "a&b", "b": mark_safe("a&b")}, "a&amp;b a&amp;b"),
'filter-fix_ampersands02': ("{{ a|fix_ampersands }} {{ b|fix_ampersands }}", {"a": "a&b", "b": mark_safe("a&b")}, "a&amp;amp;b a&amp;b"),
'filter-floatformat01': ("{% autoescape off %}{{ a|floatformat }} {{ b|floatformat }}{% endautoescape %}", {"a": "1.42", "b": mark_safe("1.42")}, "1.4 1.4"),
'filter-floatformat02': ("{{ a|floatformat }} {{ b|floatformat }}", {"a": "1.42", "b": mark_safe("1.42")}, "1.4 1.4"),

View File

@ -602,10 +602,7 @@ class TemplateTests(TestCase):
failures.append("Template test (Cached='%s', TEMPLATE_STRING_IF_INVALID='%s', TEMPLATE_DEBUG=%s): %s -- FAILED. Template loading invoked method that shouldn't have been invoked." % (is_cached, invalid_str, template_debug, name))
try:
with warnings.catch_warnings():
# Ignore deprecation of fix_ampersands
warnings.filterwarnings("ignore", category=DeprecationWarning, module='django.template.defaultfilters')
output = self.render(test_template, vals)
output = self.render(test_template, vals)
except ShouldNotExecuteException:
failures.append("Template test (Cached='%s', TEMPLATE_STRING_IF_INVALID='%s', TEMPLATE_DEBUG=%s): %s -- FAILED. Template rendering invoked method that shouldn't have been invoked." % (is_cached, invalid_str, template_debug, name))
except ContextStackException:

View File

@ -4,11 +4,9 @@ from __future__ import unicode_literals
from datetime import datetime
import os
from unittest import TestCase
import warnings
from django.utils import html, safestring
from django.utils._os import upath
from django.utils.deprecation import RemovedInDjango18Warning
from django.utils.encoding import force_text
@ -131,31 +129,6 @@ class TestUtilsHtml(TestCase):
for in_pattern, output in patterns:
self.check_output(f, in_pattern % {'entity': entity}, output)
def test_fix_ampersands(self):
with warnings.catch_warnings():
warnings.simplefilter("ignore", RemovedInDjango18Warning)
f = html.fix_ampersands
# Strings without ampersands or with ampersands already encoded.
values = ("a&#1;", "b", "&a;", "&amp; &x; ", "asdf")
patterns = (
("%s", "%s"),
("&%s", "&amp;%s"),
("&%s&", "&amp;%s&amp;"),
)
for value in values:
for in_pattern, out_pattern in patterns:
self.check_output(f, in_pattern % value, out_pattern % value)
# Strings with ampersands that need encoding.
items = (
("&#;", "&amp;#;"),
("&#875 ;", "&amp;#875 ;"),
("&#4abc;", "&amp;#4abc;"),
)
for value, output in items:
self.check_output(f, value, output)
def test_escapejs(self):
f = html.escapejs
items = (
@ -168,20 +141,6 @@ class TestUtilsHtml(TestCase):
for value, output in items:
self.check_output(f, value, output)
def test_clean_html(self):
f = html.clean_html
items = (
('<p>I <i>believe</i> in <b>semantic markup</b>!</p>', '<p>I <em>believe</em> in <strong>semantic markup</strong>!</p>'),
('I escape & I don\'t <a href="#" target="_blank">target</a>', 'I escape &amp; I don\'t <a href="#" >target</a>'),
('<p>I kill whitespace</p><br clear="all"><p>&nbsp;</p>', '<p>I kill whitespace</p>'),
# also a regression test for #7267: this used to raise an UnicodeDecodeError
('<p>* foo</p><p>* bar</p>', '<ul>\n<li> foo</li><li> bar</li>\n</ul>'),
)
with warnings.catch_warnings():
warnings.simplefilter("ignore", RemovedInDjango18Warning)
for value, output in items:
self.check_output(f, value, output)
def test_remove_tags(self):
f = html.remove_tags
items = (