Django

Code

Changeset 6892

Show
Ignore:
Timestamp:
12/04/07 15:08:29 (9 months ago)
Author:
jacob
Message:

Fixed #4131: added an "escapejs" filter for use in JavaScript? strings, and updated the documentation on addslashes to point to the new ticket. Featuring contributions from Ned Batchelder, Jeremy Dunck, and Andy Durdin.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • django/trunk/django/template/defaultfilters.py

    r6787 r6892  
    4444 
    4545def addslashes(value): 
    46     """Adds slashes - useful for passing strings to JavaScript, for example.""" 
     46    """ 
     47    Adds slashes before quotes. Useful for escaping strings in CSV, for 
     48    example. Less useful for escaping JavaScript; use the ``escapejs`` 
     49    filter instead. 
     50    """ 
    4751    return value.replace('\\', '\\\\').replace('"', '\\"').replace("'", "\\'") 
    4852addslashes.is_safe = True 
     
    5458capfirst.is_safe=True 
    5559capfirst = stringfilter(capfirst) 
     60 
     61_js_escapes = ( 
     62    ('\\', '\\\\'), 
     63    ('"', '\\"'), 
     64    ("'", "\\'"), 
     65    ('\n', '\\n'), 
     66    ('\r', '\\r'), 
     67    ('\b', '\\b'), 
     68    ('\f', '\\f'), 
     69    ('\t', '\\t'), 
     70    ('\v', '\\v'), 
     71    ('</', '<\\/'), 
     72) 
     73def escapejs(value): 
     74    """Backslash-escapes characters for use in JavaScript strings.""" 
     75    for bad, good in _js_escapes: 
     76        value = value.replace(bad, good) 
     77    return value 
     78escapejs = stringfilter(escapejs) 
    5679 
    5780def fix_ampersands(value): 
  • django/trunk/docs/templates.txt

    r6874 r6892  
    12281228~~~~~~~~~~ 
    12291229 
    1230 Adds slashes. Useful for passing strings to JavaScript, for example. 
    1231  
     1230Adds slashes before quotes. Useful for escaping strings in CSV, for example. 
     1231 
     1232**New in Django development version**: for escaping data in JavaScript strings, 
     1233use the `escapejs` filter instead. 
    12321234 
    12331235capfirst 
     
    13021304it is safe to use this function even in auto-escaping environments. If you want 
    13031305multiple escaping passes to be applied, use the ``force_escape`` filter. 
     1306 
     1307escapejs 
     1308~~~~~~~~ 
     1309 
     1310**New in Django development version** 
     1311 
     1312Escapes characters for use in JavaScript strings. This does *not* make the 
     1313string safe for use in HTML, but does protect you from syntax errors when using 
     1314templates to generate JavaScript/JSON. 
    13041315 
    13051316filesizeformat 
  • django/trunk/tests/regressiontests/defaultfilters/tests.py

    r6752 r6892  
    5050u'Hello world' 
    5151 
     52>>> escapejs(u'"double quotes" and \'single quotes\'') 
     53u'\\"double quotes\\" and \\\'single quotes\\\'' 
     54 
     55>>> escapejs(ur'\ : backslashes, too') 
     56u'\\\\ : backslashes, too' 
     57 
     58>>> escapejs(u'and lots of whitespace: \r\n\t\v\f\b') 
     59u'and lots of whitespace: \\r\\n\\t\\v\\f\\b' 
     60 
     61>>> escapejs(ur'<script>and this</script>') 
     62u'<script>and this<\\/script>' 
     63 
    5264>>> fix_ampersands(u'Jack & Jill & Jeroboam') 
    5365u'Jack &amp; Jill &amp; Jeroboam'