Changed |tojson filter to quote single attributes
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Armin Ronacher · committed by Armin Ronacher · parent 2713ea9 · Browse files · 3 files·+28 added−2 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
3 files+28 added−2 removed
- other:+28 added−2 removed
Provenance
- Commit
c502dfb- Tree
398b349- Author
Armin Ronacher <armin.ronacher@active-4.com>- Committer
Armin Ronacher <armin.ronacher@active-4.com>- Parent
2713ea9
3 files changed+28 added−2 removed
modified:flask/testsuite/helpers.py+6 added
102 unmodified lines103 rv = render('{{ "<!--<script>"|tojson }}')104 self.assert_equal(rv, '"\\u003c!--\\u003cscript\\u003e"')105 rv = render('{{ "&"|tojson }}')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>')113 114 def test_json_customization(self):115 class X(object):116 def __init__(self, val):477 unmodified linesmodified:flask/json.py+17 added-2 removed
163 unmodified lines164 this is available in templates through the ``|tojson`` filter which will165 also mark the result as safe. Due to how this function escapes certain166 characters this is safe even if used outside of ``<script>`` tags.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 the176+ notable exception of double quoted attributes. In that case single177+ quote your attributes or HTML escape it in addition.178+179 .. versionchanged:: 0.10180 This function's return value is now always safe for HTML usage, even170- if outside of script tags or if used in XHTML.181+ if outside of script tags or if used in XHTML. This rule does not182+ hold true when using this function in HTML attributes that are double183+ quoted. Always single quote attributes if you use the ``|tojson``184+ filter. Alternatively use ``|tojson|forceescape``.185 """186 rv = dumps(obj, **kwargs) \187 .replace(u'<', u'\\u003c') \188 .replace(u'>', u'\\u003e') \175- .replace(u'&', u'\\u0026')189+ .replace(u'&', u'\\u0026') \190+ .replace(u"'", u'\\u0027')191 if not _slash_escape:192 rv = rv.replace('\\/', '/')193 return rv194 49 unmodified linesmodified:CHANGES+5 added
6 unmodified lines7 --------------8 9 Pending bugfix release.10 11+- Fixed an issue where ``|tojson`` was not quoting single quotes which12+ made the filter not work properly in HTML attributes. Now it's13+ possible to use that filter in single quoted attributes. This should14+ make using that filter with angular.js easier.15+16 Version 0.1017 ------------18 19 Released on June 13nd 2013, codename Limoncello.441 unmodified lines