comparison MoinMoin/themes/__init__.py @ 1144:9ab81e421631

Use dumps() for json escaping
author Sam Toyer <samATqxcvDOTnet>
date Fri, 09 Dec 2011 11:16:16 +1000
parents 69cc30e95345
children 5421be737643
comparison
equal deleted inserted replaced
1143:088b3356ef01 1144:9ab81e421631
8 """ 8 """
9 9
10 10
11 import urllib 11 import urllib
12 12
13 from json import dumps
13 from operator import itemgetter 14 from operator import itemgetter
14 15
15 from flask import current_app as app 16 from flask import current_app as app
16 from flask import g as flaskg 17 from flask import g as flaskg
17 from flask import url_for, request 18 from flask import url_for, request
335 result['uri'] = uri 336 result['uri'] = uri
336 if email: 337 if email:
337 result['email'] = email 338 result['email'] = email
338 return result 339 return result
339 340
340 def js_string_escape(data): 341 def json_dumps(data):
341 """ 342 """
342 Escape the given string so it is safe to use as a Javscript string 343 Escape the given string so it is safe to use as a Javscript string
343 344
344 :param data: string to be escaped 345 :param data: string to be escaped
345 :rtype: str 346 :rtype: str
346 :returns: Javascript escaped version 347 :returns: Javascript escaped version
347 """ 348 """
348 return data.replace("\\", r"\\").replace("'", r"\'").replace('"', r'\"') 349 return dumps(data)
349 350
350 def shorten_item_name(name, length=25): 351 def shorten_item_name(name, length=25):
351 """ 352 """
352 Shorten item names 353 Shorten item names
353 354
418 419
419 def setup_jinja_env(): 420 def setup_jinja_env():
420 app.jinja_env.filters['shorten_item_name'] = shorten_item_name 421 app.jinja_env.filters['shorten_item_name'] = shorten_item_name
421 app.jinja_env.filters['shorten_id'] = shorten_id 422 app.jinja_env.filters['shorten_id'] = shorten_id
422 app.jinja_env.filters['contenttype_to_class'] = contenttype_to_class 423 app.jinja_env.filters['contenttype_to_class'] = contenttype_to_class
423 app.jinja_env.filters['js_string_escape'] = js_string_escape 424 app.jinja_env.filters['json_dumps'] = json_dumps
424 # please note that these filters are installed by flask-babel: 425 # please note that these filters are installed by flask-babel:
425 # datetimeformat, dateformat, timeformat, timedeltaformat 426 # datetimeformat, dateformat, timeformat, timedeltaformat
426 427
427 app.jinja_env.globals.update({ 428 app.jinja_env.globals.update({
428 # please note that flask-babel/jinja2.ext installs: 429 # please note that flask-babel/jinja2.ext installs: