Skip to content

Commit

Permalink
Ensure script_json_encode allows arrays by default
Browse files Browse the repository at this point in the history
  • Loading branch information
amol- committed Aug 18, 2016
1 parent 178785d commit 274ce63
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 2 deletions.
4 changes: 4 additions & 0 deletions tests/test_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,10 @@ def test_script_json_encode(self):
rv = "<a ng-data='%s'></a>" % script_json_encode({'x': ["foo", "bar", "baz'"]})
assert rv == '<a ng-data=\'{"x": ["foo", "bar", "baz\\u0027"]}\'></a>'

def test_script_json_encode_array(self):
rv = "<a ng-data='%s'></a>" % script_json_encode(['1', 2, 5])
assert rv == '<a ng-data=\'["1", 2, 5]\'></a>', rv


class TestFilesUtils(object):
def test_safe_filename(self):
Expand Down
8 changes: 6 additions & 2 deletions tg/util/html.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
from ..jsonify import JSONEncoder
from ..jsonify import encode as json_encode


def script_json_encode(obj, **kwargs):
_script_json_encoder = JSONEncoder(isodates=True, allow_lists=True)


def script_json_encode(obj, encoder=_script_json_encoder, **kwargs):
"""Works exactly like :func:`tg.jsonify.encode` but is safe
for use in ``<script>`` tags.
Expand All @@ -15,7 +19,7 @@ def script_json_encode(obj, **kwargs):
notable exception of double quoted attributes. In that case single
quote your attributes or HTML escape it in addition.
"""
rv = json_encode(obj, **kwargs) \
rv = json_encode(obj, encoder=encoder, **kwargs) \
.replace('<', '\\u003c') \
.replace('>', '\\u003e') \
.replace('&', '\\u0026') \
Expand Down

0 comments on commit 274ce63

Please sign in to comment.