From 13426ca43c44a8a14f5d26b5fcad26b518383fc6 Mon Sep 17 00:00:00 2001 From: gloomy-ghost Date: Mon, 1 Oct 2018 02:52:20 +0000 Subject: [PATCH] fix tests --- isso/utils/html.py | 25 ++++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/isso/utils/html.py b/isso/utils/html.py index 306dbd2..1235b8a 100644 --- a/isso/utils/html.py +++ b/isso/utils/html.py @@ -27,10 +27,33 @@ class Sanitizer(object): # href for and align for self.attributes = ["align", "href"] + attributes + + def sanitize(self, text): - return bleach.clean(text, tags=self.elements, + clean_html = bleach.clean(text, tags=self.elements, attributes=self.attributes, strip=True) + def set_links(attrs, new=False): + href_key = (None, u'href') + + if href_key not in attrs: + return attrs + if attrs[href_key].startswith(u'mailto:'): + return attrs + + rel_key = (None, u'rel') + rel_values = [val for val in attrs.get(rel_key, u'').split(u' ') if val] + + for value in [u'nofollow', u'noopener']: + if value not in [rel_val.lower() for rel_val in rel_values]: + rel_values.append(value) + + attrs[rel_key] = u' '.join(rel_values) + return attrs + + linker = bleach.linkifier.Linker(callbacks=[set_links]) + return linker.linkify(clean_html) + def Markdown(extensions=("strikethrough", "superscript", "autolink", "fenced-code")):