# HG changeset patch # User Yuya Nishihara # Date 1281456381 -32400 # Node ID 9dac951d0185b7733707229fb44a0bf4648551e7 # Parent 3b65c3c3cc8d3d85427f8b3f7ccab4900480d42f templatefilters: use \uxxxx style escape for JSON string It's embeddable in plain javascript, and also conforms to JSON standard. diff -r 3b65c3c3cc8d -r 9dac951d0185 mercurial/templatefilters.py --- a/mercurial/templatefilters.py Fri Aug 13 17:21:06 2010 +0200 +++ b/mercurial/templatefilters.py Wed Aug 11 01:06:21 2010 +0900 @@ -148,7 +148,13 @@ def jsonescape(s): for k, v in _escapes: s = s.replace(k, v) - return s + + def uescape(c): + if ord(c) < 0x80: + return c + else: + return '\\u%04x' % ord(c) + return ''.join(uescape(c) for c in s) def json(obj): if obj is None or obj is False or obj is True: @@ -157,9 +163,9 @@ return str(obj) elif isinstance(obj, str): u = unicode(obj, encoding.encoding, 'replace') - return '"%s"' % jsonescape(u).encode('utf-8') + return '"%s"' % jsonescape(u) elif isinstance(obj, unicode): - return '"%s"' % jsonescape(obj).encode('utf-8') + return '"%s"' % jsonescape(obj) elif hasattr(obj, 'keys'): out = [] for k, v in obj.iteritems(): diff -r 3b65c3c3cc8d -r 9dac951d0185 tests/test-hgweb-commands.out --- a/tests/test-hgweb-commands.out Fri Aug 13 17:21:06 2010 +0200 +++ b/tests/test-hgweb-commands.out Wed Aug 11 01:06:21 2010 +0900 @@ -984,5 +984,5 @@ } % 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"]]]; +var data = [["40b4d6888e92", [0, 1], [[0, 0, 1]], "\u80fd", "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