Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make tojson always safe (fix #709) #718

Merged
merged 2 commits into from May 23, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 10 additions & 0 deletions CHANGES
@@ -1,6 +1,16 @@
Jinja2 Changelog
================

Version 2.9.7
-------------

(bugfix release, in development)

- ``tojson`` filter marks output as safe to match documented behavior.
(`#718`_)

.. _#718: https://github.com/pallets/jinja/pull/718

Version 2.9.6
-------------
(bugfix release, released on April 3rd 2017)
Expand Down
2 changes: 1 addition & 1 deletion jinja2/utils.py
Expand Up @@ -567,7 +567,7 @@ def htmlsafe_json_dumps(obj, dumper=None, **kwargs):
.replace(u'>', u'\\u003e') \
.replace(u'&', u'\\u0026') \
.replace(u"'", u'\\u0027')
return rv
return Markup(rv)


@implements_iterator
Expand Down
5 changes: 3 additions & 2 deletions tests/test_filters.py
Expand Up @@ -580,8 +580,9 @@ def __init__(self, id, name):
def test_json_dump(self):
env = Environment(autoescape=True)
t = env.from_string('{{ x|tojson }}')
assert t.render(x={'foo': 'bar'}) == '{"foo": "bar"}'
assert t.render(x='"bar\'') == r'"\"bar\u0027"'
assert t.render(x={'foo': 'bar'}) == '{"foo": "bar"}'
assert t.render(x='"ba&r\'') == r'"\"ba\u0026r\u0027"'
assert t.render(x='<bar>') == r'"\u003cbar\u003e"'

def my_dumps(value, **options):
assert options == {'foo': 'bar'}
Expand Down