Ticket #4131: patch_4131.diff
File patch_4131.diff, 3.0 KB (added by , 17 years ago) |
---|
-
django/template/defaultfilters.py
36 36 37 37 38 38 def addslashes(value): 39 " Adds slashes - useful for passing strings toJavaScript, for example."39 "Backslash-escapes backslashes and quotes - useful for passing strings to CSV or JavaScript, for example." 40 40 return value.replace('\\', '\\\\').replace('"', '\\"').replace("'", "\\'") 41 41 addslashes = stringfilter(addslashes) 42 42 … … 45 45 return value and value[0].upper() + value[1:] 46 46 capfirst = stringfilter(capfirst) 47 47 48 def escapejs(value): 49 "Backslash-escapes characters disallowed from JavaScript strings." 50 maps = ( 51 ('\\', '\\\\'), 52 ('"', '\\"'), 53 ("'", "\\'"), 54 ('\n', '\\n'), 55 ('\r', '\\r'), 56 ('\b', '\\b'), 57 ('\f', '\\f'), 58 ('\t', '\\t'), 59 ('\v', '\\v'), 60 ('</', '<\\/'), 61 ) 62 for bad, good in maps: 63 value = value.replace(bad, good) 64 return value 65 escapejs = stringfilter(escapejs) 66 48 67 def fix_ampersands(value): 49 68 "Replaces ampersands with ``&`` entities" 50 69 from django.utils.html import fix_ampersands -
tests/regressiontests/defaultfilters/tests.py
43 43 >>> capfirst(u'hello world') 44 44 u'Hello world' 45 45 46 >>> escapejs(u'"double quotes" and \'single quotes\'') 47 u'\\"double quotes\\" and \\\'single quotes\\\'' 48 49 >>> escapejs(ur'\ : backslashes, too') 50 u'\\\\ : backslashes, too' 51 52 >>> escapejs(u'and lots of whitespace: \r\n\t\v\f\b') 53 u'and lots of whitespace: \\r\\n\\t\\v\\f\\b' 54 55 >>> escapejs(ur'<script>and this</script>') 56 u'<script>and this<\\/script>' 57 46 58 >>> fix_ampersands(u'Jack & Jill & Jeroboam') 47 59 u'Jack & Jill & Jeroboam' 48 60 -
docs/templates.txt
996 996 addslashes 997 997 ~~~~~~~~~~ 998 998 999 Adds slashes. Useful for passing strings to JavaScript, for example.999 Backslash-escapes backslashes and quotes - useful for passing strings to CSV or JavaScript, for example. `escapejs` is preferable for passing strings to JavaScript. 1000 1000 1001 1002 1001 capfirst 1003 1002 ~~~~~~~~ 1004 1003 … … 1057 1056 * ``'"'`` (double quote) to ``'"'`` 1058 1057 * ``"'"`` (single quote) to ``'''`` 1059 1058 1059 escapejs 1060 ~~~~~~~~ 1061 1062 Backslash-escapes characters disallowed from JavaScript strings. It makes these changes: 1063 1064 * ``\`` to ``\\`` 1065 * ``"`` to ``\"`` 1066 * ``'`` to ``\'`` 1067 * *line feed* to ``\n`` 1068 * *carriage return* to ``\r`` 1069 * *backspace* to ``\b`` 1070 * *form feed* to ``\f`` 1071 * *tab* to ``\t`` 1072 * *vertical tab* to ``\v`` 1073 * ``</`` to ``<\/`` 1074 1060 1075 filesizeformat 1061 1076 ~~~~~~~~~~~~~~ 1062 1077