Ticket #7177: 7177-updated.diff

File 7177-updated.diff, 2.7 KB (added by Eric Holscher, 16 years ago)

Combining the code into one function

  • django/template/defaultfilters.py

     
    6363capfirst = stringfilter(capfirst)
    6464
    6565_js_escapes = (
    66     ('\\', '\\\\'),
    67     ('"', '\\"'),
    68     ("'", "\\'"),
    69     ('\n', '\\n'),
    70     ('\r', '\\r'),
    71     ('\b', '\\b'),
    72     ('\f', '\\f'),
    73     ('\t', '\\t'),
    74     ('\v', '\\v'),
    75     ('</', '<\\/'),
    76 )
     66    ('\\', r'\x5C'),
     67    ('\'', r'\x27'),
     68    ('"', r'\x22'),
     69    ('>', r'\x3E'),
     70    ('<', r'\x3C'),
     71    ('&', r'\x26'),
     72    ('=', r'\x3D'),
     73    ('-', r'\x2D'),
     74    (';', r'\x3B')
     75) + tuple([('%c' % z, '\\x%02X' % z) for z in range(32)])
     76
    7777def escapejs(value):
    78     """Backslash-escapes characters for use in JavaScript strings."""
     78    """Hex encodes characters for use in JavaScript strings."""
    7979    for bad, good in _js_escapes:
    8080        value = value.replace(bad, good)
    8181    return value
  • tests/regressiontests/templates/filters.py

     
    262262        'autoescape-stringfilter02': (r'{% autoescape off %}{{ unsafe|capfirst }}{% endautoescape %}', {'unsafe': UnsafeClass()}, 'You & me'),
    263263        'autoescape-stringfilter03': (r'{{ safe|capfirst }}', {'safe': SafeClass()}, 'You &gt; me'),
    264264        'autoescape-stringfilter04': (r'{% autoescape off %}{{ safe|capfirst }}{% endautoescape %}', {'safe': SafeClass()}, 'You &gt; me'),
     265
     266        'escapejs01': (r'{{ a|escapejs }}', {'a': 'testing\r\njavascript \'string" <b>escaping</b>'}, 'testing\\x0D\\x0Ajavascript \\x27string\\x22 \\x3Cb\\x3Eescaping\\x3C/b\\x3E'),
     267        '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'),
    265268    }
    266269
  • AUTHORS

     
    190190    hipertracker@gmail.com
    191191    Deryck Hodge <http://www.devurandom.org/>
    192192    Brett Hoerner <bretthoerner@bretthoerner.com>
     193    Eric Holscher <http://ericholscher.com>
    193194    Ian Holsman <http://feh.holsman.net/>
    194195    Kieran Holland <http://www.kieranholland.com>
    195196    Sung-Jin Hong <serialx.net@gmail.com>
     
    426427    ymasuda@ethercube.com
    427428    Jarek Zgoda <jarek.zgoda@gmail.com>
    428429    Cheng Zhang
     430    Collin Grady <collin@collingrady.com>
     431    Mike Wiacek <mjwiacek@google.com>
    429432
    430433A big THANK YOU goes to:
    431434
Back to Top