File tree Expand file tree Collapse file tree 3 files changed +28
-2
lines changed Expand file tree Collapse file tree 3 files changed +28
-2
lines changed Original file line number Diff line number Diff line change @@ -8,6 +8,11 @@ Version 0.10.1
8
8
9
9
Pending bugfix release.
10
10
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
+
11
16
Version 0.10
12
17
------------
13
18
Original file line number Diff line number Diff line change @@ -165,14 +165,29 @@ def htmlsafe_dumps(obj, **kwargs):
165
165
also mark the result as safe. Due to how this function escapes certain
166
166
characters this is safe even if used outside of ``<script>`` tags.
167
167
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
+
168
179
.. versionchanged:: 0.10
169
180
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``.
171
185
"""
172
186
rv = dumps (obj , ** kwargs ) \
173
187
.replace (u'<' , u'\\ u003c' ) \
174
188
.replace (u'>' , u'\\ u003e' ) \
175
- .replace (u'&' , u'\\ u0026' )
189
+ .replace (u'&' , u'\\ u0026' ) \
190
+ .replace (u"'" , u'\\ u0027' )
176
191
if not _slash_escape :
177
192
rv = rv .replace ('\\ /' , '/' )
178
193
return rv
Original file line number Diff line number Diff line change @@ -104,6 +104,12 @@ def test_template_escaping(self):
104
104
self .assert_equal (rv , '"\\ u003c!--\\ u003cscript\\ u003e"' )
105
105
rv = render ('{{ "&"|tojson }}' )
106
106
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>' )
107
113
108
114
def test_json_customization (self ):
109
115
class X (object ):
You can’t perform that action at this time.
0 commit comments