plugin text syntaxes: handle empty content in cleanXHTML + don't use self-closing tags for non-void elements.
This commit is contained in:
parent
9cfbbce2d1
commit
8f9235e661
1 changed files with 34 additions and 1 deletions
|
|
@ -31,6 +31,7 @@ from sat.tools import xml_tools
|
||||||
try:
|
try:
|
||||||
from lxml import html
|
from lxml import html
|
||||||
from lxml.html import clean
|
from lxml.html import clean
|
||||||
|
from lxml import etree
|
||||||
except ImportError:
|
except ImportError:
|
||||||
raise exceptions.MissingModule(
|
raise exceptions.MissingModule(
|
||||||
u"Missing module lxml, please download/install it from http://lxml.de/"
|
u"Missing module lxml, please download/install it from http://lxml.de/"
|
||||||
|
|
@ -95,6 +96,25 @@ STYLES_WHITELIST = (
|
||||||
"width",
|
"width",
|
||||||
)
|
)
|
||||||
|
|
||||||
|
# cf. https://www.w3.org/TR/html/syntax.html#void-elements
|
||||||
|
VOID_ELEMENTS = (
|
||||||
|
"area",
|
||||||
|
"base",
|
||||||
|
"br",
|
||||||
|
"col",
|
||||||
|
"embed",
|
||||||
|
"hr",
|
||||||
|
"img",
|
||||||
|
"input",
|
||||||
|
"keygen",
|
||||||
|
"link",
|
||||||
|
"menuitem",
|
||||||
|
"meta",
|
||||||
|
"param",
|
||||||
|
"source",
|
||||||
|
"track",
|
||||||
|
"wbr")
|
||||||
|
|
||||||
SAFE_ATTRS = html.defs.safe_attrs.union(("style", "poster", "controls"))
|
SAFE_ATTRS = html.defs.safe_attrs.union(("style", "poster", "controls"))
|
||||||
STYLES_VALUES_REGEX = (
|
STYLES_VALUES_REGEX = (
|
||||||
r"^("
|
r"^("
|
||||||
|
|
@ -279,7 +299,13 @@ class TextSyntaxes(object):
|
||||||
"""
|
"""
|
||||||
|
|
||||||
if isinstance(xhtml, basestring):
|
if isinstance(xhtml, basestring):
|
||||||
xhtml_elt = html.fromstring(xhtml)
|
try:
|
||||||
|
xhtml_elt = html.fromstring(xhtml)
|
||||||
|
except etree.ParserError as e:
|
||||||
|
if not xhtml.strip():
|
||||||
|
return u""
|
||||||
|
log.error(u"Can't clean XHTML: {xhtml}".format(xhtml=xhtml))
|
||||||
|
raise e
|
||||||
elif isinstance(xhtml, html.HtmlElement):
|
elif isinstance(xhtml, html.HtmlElement):
|
||||||
xhtml_elt = xhtml
|
xhtml_elt = xhtml
|
||||||
else:
|
else:
|
||||||
|
|
@ -291,6 +317,13 @@ class TextSyntaxes(object):
|
||||||
xhtml_elt = cleaner.clean_html(xhtml_elt)
|
xhtml_elt = cleaner.clean_html(xhtml_elt)
|
||||||
for elt in xhtml_elt.xpath("//*[@style]"):
|
for elt in xhtml_elt.xpath("//*[@style]"):
|
||||||
elt.set("style", self.cleanStyle(elt.get("style")))
|
elt.set("style", self.cleanStyle(elt.get("style")))
|
||||||
|
# we remove self-closing elements for non-void elements
|
||||||
|
for element in xhtml_elt.iter(tag=etree.Element):
|
||||||
|
if not element.text:
|
||||||
|
if element.tag in VOID_ELEMENTS:
|
||||||
|
element.text = None
|
||||||
|
else:
|
||||||
|
element.text = u''
|
||||||
return html.tostring(xhtml_elt, encoding=unicode, method="xml")
|
return html.tostring(xhtml_elt, encoding=unicode, method="xml")
|
||||||
|
|
||||||
def convert(
|
def convert(
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue