Skip to content

Instantly share code, notes, and snippets.

@tomotaka
Created September 13, 2013 08:09
Show Gist options
  • Save tomotaka/6547953 to your computer and use it in GitHub Desktop.
Save tomotaka/6547953 to your computer and use it in GitHub Desktop.
Q. How does Jinja2 escape filter HTML escape? A. MarkupSafe.
#!/usr/bin/python
# -*- coding: utf-8 -*-
from nose.tools import eq_
import markupsafe
jinja_escape = lambda s: '%s' % markupsafe.escape(s)
# note: lambda s: str(markupsafe.escape(s)) doesn't work in some cases
scraped_string = soup.find('p', id='important').string # should be escaped by jinja2 escape filter
expect_string = jinja_escape('M & M') # => 'M & M'
eq_(scraped_string, expect_string)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment