Skip to content

Commit 63d9ede

Browse files
committed
Merge branch '0.10-maintenance'
2 parents 89731d8 + c502dfb commit 63d9ede

File tree

3 files changed

+28
-2
lines changed

3 files changed

+28
-2
lines changed

CHANGES

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,11 @@ Version 0.10.1
88

99
Pending bugfix release.
1010

11+
- Fixed an issue where ``|tojson`` was not quoting single quotes which
12+
made the filter not work properly in HTML attributes. Now it's
13+
possible to use that filter in single quoted attributes. This should
14+
make using that filter with angular.js easier.
15+
1116
Version 0.10
1217
------------
1318

flask/json.py

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -165,14 +165,29 @@ def htmlsafe_dumps(obj, **kwargs):
165165
also mark the result as safe. Due to how this function escapes certain
166166
characters this is safe even if used outside of ``<script>`` tags.
167167
168+
The following characters are escaped in strings:
169+
170+
- ``<``
171+
- ``>``
172+
- ``&``
173+
- ``'``
174+
175+
This makes it safe to embed such strings in any place in HTML with the
176+
notable exception of double quoted attributes. In that case single
177+
quote your attributes or HTML escape it in addition.
178+
168179
.. versionchanged:: 0.10
169180
This function's return value is now always safe for HTML usage, even
170-
if outside of script tags or if used in XHTML.
181+
if outside of script tags or if used in XHTML. This rule does not
182+
hold true when using this function in HTML attributes that are double
183+
quoted. Always single quote attributes if you use the ``|tojson``
184+
filter. Alternatively use ``|tojson|forceescape``.
171185
"""
172186
rv = dumps(obj, **kwargs) \
173187
.replace(u'<', u'\\u003c') \
174188
.replace(u'>', u'\\u003e') \
175-
.replace(u'&', u'\\u0026')
189+
.replace(u'&', u'\\u0026') \
190+
.replace(u"'", u'\\u0027')
176191
if not _slash_escape:
177192
rv = rv.replace('\\/', '/')
178193
return rv

flask/testsuite/helpers.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,12 @@ def test_template_escaping(self):
104104
self.assert_equal(rv, '"\\u003c!--\\u003cscript\\u003e"')
105105
rv = render('{{ "&"|tojson }}')
106106
self.assert_equal(rv, '"\\u0026"')
107+
rv = render('{{ "\'"|tojson }}')
108+
self.assert_equal(rv, '"\\u0027"')
109+
rv = render("<a ng-data='{{ data|tojson }}'></a>",
110+
data={'x': ["foo", "bar", "baz'"]})
111+
self.assert_equal(rv,
112+
'<a ng-data=\'{"x": ["foo", "bar", "baz\\u0027"]}\'></a>')
107113

108114
def test_json_customization(self):
109115
class X(object):

0 commit comments

Comments
 (0)