def get_body_style(payload, target='#email-html-part'): "Get the style attribute of the body tag" try: html = local_fromstring(payload) [body] = html.xpath('//body') raw = sanitize_style(body.attrib.get('style', u'')) body_class = body.attrib.get('class', u'') style = "%s {%s}" % (target, raw) if raw else u'' return style, body_class except (ValueError, XMLSyntaxError): return None, None
def clean_styles(payload): "Cleanup styles" if not payload: return '' html = local_fromstring(payload) walker = html.getiterator() for tag in walker: if 'style' in tag.keys(): newstyle = sanitize_style(tag.get('style')) if newstyle: tag.set('style', newstyle) if XSS_RE.findall(tag.get('style')): tag.attrib.pop('style') else: tag.attrib.pop('style') if tag.tag == 'style': tag.drop_tree() return tostring(html)