Ticket #4131: patch_4131.diff

File patch_4131.diff, 3.0 KB (added by durdinator, 17 years ago)

escapejs with tests and docs (2nd attempt)

  • django/template/defaultfilters.py

     
    3636
    3737
    3838def addslashes(value):
    39     "Adds slashes - useful for passing strings to JavaScript, for example."
     39    "Backslash-escapes backslashes and quotes - useful for passing strings to CSV or JavaScript, for example."
    4040    return value.replace('\\', '\\\\').replace('"', '\\"').replace("'", "\\'")
    4141addslashes = stringfilter(addslashes)
    4242
     
    4545    return value and value[0].upper() + value[1:]
    4646capfirst = stringfilter(capfirst)
    4747
     48def 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
     65escapejs = stringfilter(escapejs)
     66
    4867def fix_ampersands(value):
    4968    "Replaces ampersands with ``&amp;`` entities"
    5069    from django.utils.html import fix_ampersands
  • tests/regressiontests/defaultfilters/tests.py

     
    4343>>> capfirst(u'hello world')
    4444u'Hello world'
    4545
     46>>> escapejs(u'"double quotes" and \'single quotes\'')
     47u'\\"double quotes\\" and \\\'single quotes\\\''
     48
     49>>> escapejs(ur'\ : backslashes, too')
     50u'\\\\ : backslashes, too'
     51
     52>>> escapejs(u'and lots of whitespace: \r\n\t\v\f\b')
     53u'and lots of whitespace: \\r\\n\\t\\v\\f\\b'
     54
     55>>> escapejs(ur'<script>and this</script>')
     56u'<script>and this<\\/script>'
     57
    4658>>> fix_ampersands(u'Jack & Jill & Jeroboam')
    4759u'Jack &amp; Jill &amp; Jeroboam'
    4860
  • docs/templates.txt

     
    996996addslashes
    997997~~~~~~~~~~
    998998
    999 Adds slashes. Useful for passing strings to JavaScript, for example.
     999Backslash-escapes backslashes and quotes - useful for passing strings to CSV or JavaScript, for example.  `escapejs` is preferable for passing strings to JavaScript.
    10001000
    1001 
    10021001capfirst
    10031002~~~~~~~~
    10041003
     
    10571056    * ``'"'`` (double quote) to ``'&quot;'``
    10581057    * ``"'"`` (single quote) to ``'&#39;'``
    10591058
     1059escapejs
     1060~~~~~~~~
     1061
     1062Backslash-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
    10601075filesizeformat
    10611076~~~~~~~~~~~~~~
    10621077
Back to Top