##// END OF EJS Templates
encoding: backport paranoid escaping from templatefilters.jsonescape()...
Yuya Nishihara -
r28069:b2d24c28 default
parent child Browse files
Show More
@@ -391,6 +391,8 b' class normcasespecs(object):'
391 _jsonmap[0x0c] = '\\f'
391 _jsonmap[0x0c] = '\\f'
392 _jsonmap[0x0d] = '\\r'
392 _jsonmap[0x0d] = '\\r'
393 _paranoidjsonmap = _jsonmap[:]
393 _paranoidjsonmap = _jsonmap[:]
394 _paranoidjsonmap[0x3c] = '\\u003c' # '<' (e.g. escape "</script>")
395 _paranoidjsonmap[0x3e] = '\\u003e' # '>'
394 _jsonmap.extend(chr(x) for x in xrange(128, 256))
396 _jsonmap.extend(chr(x) for x in xrange(128, 256))
395
397
396 def jsonescape(s, paranoid=False):
398 def jsonescape(s, paranoid=False):
@@ -419,8 +421,8 b' def jsonescape(s, paranoid=False):'
419 >>> jsonescape('')
421 >>> jsonescape('')
420 ''
422 ''
421
423
422 If paranoid, non-ascii characters are also escaped. This is suitable for
424 If paranoid, non-ascii and common troublesome characters are also escaped.
423 web output.
425 This is suitable for web output.
424
426
425 >>> jsonescape('escape boundary: \\x7e \\x7f \\xc2\\x80', paranoid=True)
427 >>> jsonescape('escape boundary: \\x7e \\x7f \\xc2\\x80', paranoid=True)
426 'escape boundary: ~ \\\\u007f \\\\u0080'
428 'escape boundary: ~ \\\\u007f \\\\u0080'
@@ -430,6 +432,8 b' def jsonescape(s, paranoid=False):'
430 'utf-8: caf\\\\u00e9'
432 'utf-8: caf\\\\u00e9'
431 >>> jsonescape('non-BMP: \\xf0\\x9d\\x84\\x9e', paranoid=True)
433 >>> jsonescape('non-BMP: \\xf0\\x9d\\x84\\x9e', paranoid=True)
432 'non-BMP: \\\\ud834\\\\udd1e'
434 'non-BMP: \\\\ud834\\\\udd1e'
435 >>> jsonescape('<foo@example.org>', paranoid=True)
436 '\\\\u003cfoo@example.org\\\\u003e'
433 '''
437 '''
434
438
435 if paranoid:
439 if paranoid:
General Comments 0
You need to be logged in to leave comments. Login now