Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

[1.1.X] Fixed #13114 -- Modified escapejs to produce output that is J…

…SON compliant. Thanks to David Danier for the report.

Backport of r12780 from trunk.

git-svn-id: http://code.djangoproject.com/svn/django/branches/releases/1.1.X@12781 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit c64669a6140444cd799d1b9bc826e67ae8b0d55a 1 parent d3ee556
Russell Keith-Magee authored March 15, 2010
20  django/template/defaultfilters.py
@@ -68,22 +68,22 @@ def capfirst(value):
68 68
 capfirst = stringfilter(capfirst)
69 69
 
70 70
 _base_js_escapes = (
71  
-    ('\\', r'\x5C'),
72  
-    ('\'', r'\x27'),
73  
-    ('"', r'\x22'),
74  
-    ('>', r'\x3E'),
75  
-    ('<', r'\x3C'),
76  
-    ('&', r'\x26'),
77  
-    ('=', r'\x3D'),
78  
-    ('-', r'\x2D'),
79  
-    (';', r'\x3B'),
  71
+    ('\\', r'\u005C'),
  72
+    ('\'', r'\u0027'),
  73
+    ('"', r'\u0022'),
  74
+    ('>', r'\u003E'),
  75
+    ('<', r'\u003C'),
  76
+    ('&', r'\u0026'),
  77
+    ('=', r'\u003D'),
  78
+    ('-', r'\u002D'),
  79
+    (';', r'\u003B'),
80 80
     (u'\u2028', r'\u2028'),
81 81
     (u'\u2029', r'\u2029')
82 82
 )
83 83
 
84 84
 # Escape every ASCII character with a value less than 32.
85 85
 _js_escapes = (_base_js_escapes +
86  
-               tuple([('%c' % z, '\\x%02X' % z) for z in range(32)]))
  86
+               tuple([('%c' % z, '\\u%04X' % z) for z in range(32)]))
87 87
 
88 88
 def escapejs(value):
89 89
     """Hex encodes characters for use in JavaScript strings."""
6  docs/ref/templates/builtins.txt
@@ -760,8 +760,8 @@ use keyword syntax::
760 760
 
761 761
     {% url path.to.some_view arg1=v1,arg2=v2 %}
762 762
 
763  
-Do not mix both positional and keyword syntax in a single call. All arguments 
764  
-required by the URLconf should be present. 
  763
+Do not mix both positional and keyword syntax in a single call. All arguments
  764
+required by the URLconf should be present.
765 765
 
766 766
 For example, suppose you have a view, ``app_views.client``, whose URLconf
767 767
 takes a client ID (here, ``client()`` is a method inside the views file
@@ -1077,7 +1077,7 @@ For example::
1077 1077
     {{ value|escapejs }}
1078 1078
 
1079 1079
 If ``value`` is ``"testing\r\njavascript \'string" <b>escaping</b>"``,
1080  
-the output will be ``"testing\\x0D\\x0Ajavascript \\x27string\\x22 \\x3Cb\\x3Eescaping\\x3C/b\\x3E"``.
  1080
+the output will be ``"testing\\u000D\\u000Ajavascript \\u0027string\\u0022 \\u003Cb\\u003Eescaping\\u003C/b\\u003E"``.
1081 1081
 
1082 1082
 .. templatefilter:: filesizeformat
1083 1083
 
8  tests/regressiontests/defaultfilters/tests.py
@@ -72,16 +72,16 @@
72 72
 u'Hello world'
73 73
 
74 74
 >>> escapejs(u'"double quotes" and \'single quotes\'')
75  
-u'\\x22double quotes\\x22 and \\x27single quotes\\x27'
  75
+u'\\u0022double quotes\\u0022 and \\u0027single quotes\\u0027'
76 76
 
77 77
 >>> escapejs(ur'\ : backslashes, too')
78  
-u'\\x5C : backslashes, too'
  78
+u'\\u005C : backslashes, too'
79 79
 
80 80
 >>> escapejs(u'and lots of whitespace: \r\n\t\v\f\b')
81  
-u'and lots of whitespace: \\x0D\\x0A\\x09\\x0B\\x0C\\x08'
  81
+u'and lots of whitespace: \\u000D\\u000A\\u0009\\u000B\\u000C\\u0008'
82 82
 
83 83
 >>> escapejs(ur'<script>and this</script>')
84  
-u'\\x3Cscript\\x3Eand this\\x3C/script\\x3E'
  84
+u'\\u003Cscript\\u003Eand this\\u003C/script\\u003E'
85 85
 
86 86
 >>> escapejs(u'paragraph separator:\u2029and line separator:\u2028')
87 87
 u'paragraph separator:\\u2029and line separator:\\u2028'
4  tests/regressiontests/templates/filters.py
@@ -295,8 +295,8 @@ def get_filter_tests():
295 295
         'autoescape-stringfilter03': (r'{{ safe|capfirst }}', {'safe': SafeClass()}, 'You &gt; me'),
296 296
         'autoescape-stringfilter04': (r'{% autoescape off %}{{ safe|capfirst }}{% endautoescape %}', {'safe': SafeClass()}, 'You &gt; me'),
297 297
 
298  
-        'escapejs01': (r'{{ a|escapejs }}', {'a': 'testing\r\njavascript \'string" <b>escaping</b>'}, 'testing\\x0D\\x0Ajavascript \\x27string\\x22 \\x3Cb\\x3Eescaping\\x3C/b\\x3E'),
299  
-        'escapejs02': (r'{% autoescape off %}{{ a|escapejs }}{% endautoescape %}', {'a': 'testing\r\njavascript \'string" <b>escaping</b>'}, 'testing\\x0D\\x0Ajavascript \\x27string\\x22 \\x3Cb\\x3Eescaping\\x3C/b\\x3E'),
  298
+        'escapejs01': (r'{{ a|escapejs }}', {'a': 'testing\r\njavascript \'string" <b>escaping</b>'}, 'testing\\u000D\\u000Ajavascript \\u0027string\\u0022 \\u003Cb\\u003Eescaping\\u003C/b\\u003E'),
  299
+        'escapejs02': (r'{% autoescape off %}{{ a|escapejs }}{% endautoescape %}', {'a': 'testing\r\njavascript \'string" <b>escaping</b>'}, 'testing\\u000D\\u000Ajavascript \\u0027string\\u0022 \\u003Cb\\u003Eescaping\\u003C/b\\u003E'),
300 300
 
301 301
 
302 302
         # length filter.

0 notes on commit c64669a

Please sign in to comment.
Something went wrong with that request. Please try again.