templatefilters: make json filter handle multibyte characters correctly

It aims to fix javascript error of hgweb's graph view in Japanese 'cp932'
encoding.

'cp932' contains multibyte characters ending with '\x5c' (backslash),
e.g. '\x94\x5c' for Japanese Kanji 'Noh'.
Due to json filter escapes '\' to '\\', multibyte string ending with
'\x5c' is translated to "xxx\", resulting javascript parse error on
a web browser.

This patch changes json() to pass unicode to jsonescape().

Unicode decoding error handler changed to 'replace' by Patrick Mézard.
This commit is contained in:
Yuya Nishihara 2010-08-07 16:27:16 +09:00
parent 10293e99b9
commit aaffe8d6bf
3 changed files with 20 additions and 2 deletions

View File

@ -156,9 +156,10 @@ def json(obj):
elif isinstance(obj, int) or isinstance(obj, float):
return str(obj)
elif isinstance(obj, str):
return '"%s"' % jsonescape(obj)
u = unicode(obj, encoding.encoding, 'replace')
return '"%s"' % jsonescape(u).encode('utf-8')
elif isinstance(obj, unicode):
return json(obj.encode('utf-8'))
return '"%s"' % jsonescape(obj).encode('utf-8')
elif hasattr(obj, 'keys'):
out = []
for k, v in obj.iteritems():

View File

@ -60,5 +60,19 @@ echo % failing unbundle, requires POST request
echo % Static files
"$TESTDIR/get-with-headers.py" 127.0.0.1:$HGPORT '/static/style.css'
echo % Stop and restart with HGENCODING=cp932
"$TESTDIR/killdaemons.py"
HGENCODING=cp932 hg serve --config server.uncompressed=False -n test \
-p $HGPORT -d --pid-file=hg.pid -E errors.log
cat hg.pid >> $DAEMON_PIDS
# commit message with Japanese Kanji 'Noh', which ends with '\x5c'
echo foo >> foo
HGENCODING=cp932 hg ci -m `python -c 'print("\x94\x5c")'`
echo % Graph json escape of multibyte character
"$TESTDIR/get-with-headers.py" 127.0.0.1:$HGPORT '/graph/' \
| grep '^var data ='
echo % ERRORS ENCOUNTERED
cat errors.log

View File

@ -982,4 +982,7 @@ ul#graphnodes li .info {
position: relative;
top: -1px;
}
% Stop and restart with HGENCODING=cp932
% Graph json escape of multibyte character
var data = [["40b4d6888e92", [0, 1], [[0, 0, 1]], "能", "test", "1970-01-01", ["stable", true], ["tip"]], ["1d22e65f027e", [0, 1], [[0, 0, 1]], "branch", "test", "1970-01-01", ["stable", false], []], ["a4f92ed23982", [0, 1], [[0, 0, 1]], "Added tag 1.0 for changeset 2ef0ac749a14", "test", "1970-01-01", ["default", true], []], ["2ef0ac749a14", [0, 1], [], "base", "test", "1970-01-01", ["default", false], ["1.0"]]];
% ERRORS ENCOUNTERED