diff --git a/common/lib/capa/capa/responsetypes.py b/common/lib/capa/capa/responsetypes.py index 9875c33f0d..5aa5b917d8 100644 --- a/common/lib/capa/capa/responsetypes.py +++ b/common/lib/capa/capa/responsetypes.py @@ -1143,19 +1143,33 @@ def sympy_check2(): return correct_map def clean_message_html(self, msg): + + # If *msg* is an empty string, then the code below + # will return "". To avoid this, we first check + # that *msg* is a non-empty string. if msg: - # try to clean up message html + + # When we parse *msg* using etree, there needs to be a root + # element, so we wrap the *msg* text in tags msg = '' + msg + '' + + # Replace < characters msg = msg.replace('<', '<') - #msg = msg.replace('<','<') + + # Use etree to prettify the HTML msg = etree.tostring(fromstring_bs(msg, convertEntities=None), pretty_print=True) - #msg = etree.tostring(fromstring_bs(msg),pretty_print=True) + msg = msg.replace(' ', '') - #msg = re.sub('(.*)','\\1',msg,flags=re.M|re.DOTALL) # python 2.7 + + # Remove the tags we introduced earlier, so we're + # left with just the prettified message markup msg = re.sub('(?ms)(.*)', '\\1', msg) + # Strip leading and trailing whitespace return msg.strip() + + # If we start with an empty string, then return an empty string else: return ""