Ticket #4131: addslash_escape.diff

File addslash_escape.diff, 832 bytes (added by Jeremy Dunck <jdunck@…>, 13 years ago)
  • django/template/defaultfilters.py

     
    4747
    4848def addslashes(value):
    4949    "Adds slashes - useful for passing strings to JavaScript, for example."
    50     return value.replace('\\', '\\\\').replace('"', '\\"').replace("'", "\\'")
     50    maps = (
     51        ('\\', '\\\\'),
     52        ('"', '\\"'),
     53        ("'", "\\'"),
     54        ('\n', '\\n'),
     55        ('\r', ''),
     56        ('\b', '\\b'),
     57        ('\f', '\\f'),
     58        ('\t', '\\t'),
     59        ('\v', '\\v'),
     60        ('</', '<\\/'),
     61        )
     62
     63    for bad, good in maps:
     64        value = value.replace(bad, good)
     65    return value
    5166addslashes = stringfilter(addslashes)
    5267
    5368def capfirst(value):
Back to Top