plugin text syntaxes: clean_xhtml now accept lxml's HtmlElement to avoid parsing two times the same xml

This commit is contained in:
Goffi 2013-11-14 17:53:47 +01:00
commit e743df999e

View file

@ -22,6 +22,7 @@ from logging import debug, info, error, warning
from wokkel import disco, pubsub
from twisted.internet import defer
from twisted.internet.threads import deferToThread
from sat.core import exceptions
from lxml import html
from lxml.html import clean
import re
@ -131,7 +132,7 @@ class TextSyntaxes(object):
def clean_xhtml(self, xhtml):
""" Clean XHTML text by removing potentially dangerous/malicious parts
@param xhtml: raw xhtml text to clean
@param xhtml: raw xhtml text to clean (or lxml's HtmlElement)
"""
def blocking_cleaning(xhtml):
""" Clean XHTML and style attributes """
@ -157,7 +158,13 @@ class TextSyntaxes(object):
cleaned_styles.append((key, value))
return "; ".join(["%s: %s" % (key, value) for key, value in cleaned_styles])
xhtml_elt = html.fromstring(xhtml)
if isinstance(xhtml, basestring):
xhtml_elt = html.fromstring(xhtml)
elif isinstance(xhtml, html.HtmlElement):
xhtml_elt = xhtml
else:
error("Only strings and HtmlElements can be cleaned")
raise exceptions.DataError
cleaner = clean.Cleaner(style=False,
add_nofollow=False,
safe_attrs=SAFE_ATTRS)
@ -166,8 +173,7 @@ class TextSyntaxes(object):
elt.set("style", clean_style(elt.get('style')))
return html.tostring(xhtml_elt, method='xml')
d = deferToThread(blocking_cleaning, xhtml)
return d
return deferToThread(blocking_cleaning, xhtml)
def convert(self, text, syntax_from, syntax_to=_SYNTAX_XHTML, safe=True):
""" Convert a text between two syntaxes