Skip to content

Commit 1df3c71

Browse files
committed
Replaced the use of (at least in Django 1.11) buggy urlize() with bleach.linkify(), to avoid some exceptions caused by malformed urlize output, exposed by the new sanitize_html(). Also removed some dead code.
- Legacy-Id: 14751
1 parent cd10ba5 commit 1df3c71

1 file changed

Lines changed: 8 additions & 26 deletions

File tree

ietf/doc/templatetags/ietf_filters.py

Lines changed: 8 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,17 @@
11
# Copyright The IETF Trust 2007, All Rights Reserved
22

3-
import re
3+
import bleach
44
import datetime
5+
import re
6+
57
import types
8+
69
from email.utils import parseaddr
710

811
from django import template
912
from django.conf import settings
1013
from django.utils.html import escape
11-
from django.template.defaultfilters import truncatewords_html, linebreaksbr, stringfilter, striptags, urlize
14+
from django.template.defaultfilters import truncatewords_html, linebreaksbr, stringfilter, striptags
1215
from django.utils.safestring import mark_safe, SafeData
1316
from django.utils.html import strip_tags
1417

@@ -367,7 +370,9 @@ def format_history_text(text, trunc_words=25):
367370
def format_snippet(text, trunc_words=25):
368371
# urlize if there aren't already links present
369372
if not 'href=' in text:
370-
text = urlize(text)
373+
# django's urlize() is buggy in at least Django 1.11; use
374+
# bleach.linkify instead
375+
text = bleach.linkify(text)
371376
full = keep_spacing(collapsebr(linebreaksbr(mark_safe(sanitize_html(text)))))
372377
snippet = truncatewords_html(full, trunc_words)
373378
if snippet != full:
@@ -473,29 +478,6 @@ def lower_allcaps(text):
473478
result = result.replace(token, token.lower())
474479
return result
475480

476-
# See https://djangosnippets.org/snippets/2072/ and
477-
# https://stackoverflow.com/questions/9939248/how-to-prevent-django-basic-inlines-from-autoescaping
478-
@register.filter
479-
def urlize_html(html, autoescape=False):
480-
"""
481-
Returns urls found in an (X)HTML text node element as urls via Django urlize filter.
482-
"""
483-
try:
484-
from BeautifulSoup import BeautifulSoup
485-
except ImportError:
486-
if settings.DEBUG:
487-
raise template.TemplateSyntaxError, "Error in urlize_html The Python BeautifulSoup libraries aren't installed."
488-
return html
489-
else:
490-
soup = BeautifulSoup(html)
491-
492-
textNodes = soup.findAll(text=True)
493-
for textNode in textNodes:
494-
urlizedText = urlize(textNode, autoescape=autoescape)
495-
textNode.replaceWith(BeautifulSoup(urlizedText))
496-
497-
return str(soup)
498-
499481
@register.filter
500482
def emailwrap(email):
501483
email = str(email)

0 commit comments

Comments
 (0)