Skip to content

Instantly share code, notes, and snippets.

@nicholasserra
Last active August 29, 2015 14:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nicholasserra/6bd1ff7650e398127f19 to your computer and use it in GitHub Desktop.
Save nicholasserra/6bd1ff7650e398127f19 to your computer and use it in GitHub Desktop.
def escape_text(self, text):
"""Use html5lib to escape evil html tags."""
parser = html5lib.HTMLParser(tokenizer=HTMLSanitizer)
walker = html5lib.treewalkers.getTreeWalker('etree')
stream = walker(parser.parseFragment(text))
serializer = HTMLSerializer(quote_attr_values=True, omit_optional_tags=False,
alphabetical_attributes=True)
return serializer.render(stream)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment