Skip to content

Commit

Permalink
Merge pull request #718 from ayalash/issue_709
Browse files Browse the repository at this point in the history
Make tojson always safe (fix #709)
  • Loading branch information
davidism committed May 23, 2017
2 parents 5b335b7 + 8634697 commit 3f49ba2
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 3 deletions.
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

0 comments on commit 3f49ba2

Please sign in to comment.