Ticket #2986: addslashes.patch

File addslashes.patch, 2.1 KB (added by Thomas Steinacher <tom@…>, 17 years ago)

Even better patch. Patches addslashes to escape '\n' and uses it in the admin view. Maybe this should be moved into django.utils.javascript?

  • django/contrib/admin/views/main.py

     
    1212from django.http import Http404, HttpResponse, HttpResponseRedirect
    1313from django.utils.html import escape
    1414from django.utils.text import capfirst, get_text_list
     15from django.template.defaultfilters import addslashes
    1516import operator
    1617
    1718from django.contrib.admin.models import LogEntry, ADDITION, CHANGE, DELETION
     
    263264                    post_url_continue += "?_popup=1"
    264265                return HttpResponseRedirect(post_url_continue % pk_value)
    265266            if request.POST.has_key("_popup"):
     267               print pk_value
    266268                if type(pk_value) is str: # Quote if string, so JavaScript doesn't think it's a variable.
    267                     pk_value = '"%s"' % pk_value.replace('"', '\\"')
     269                    pk_value = '"%s"' % addslashes(pk_value)
    268270                return HttpResponse('<script type="text/javascript">opener.dismissAddAnotherPopup(window, %s, "%s");</script>' % \
    269                     (pk_value, str(new_object).replace('"', '\\"')))
     271                    (pk_value, addslashes(str(new_object))))
    270272            elif request.POST.has_key("_addanother"):
    271273                request.user.message_set.create(message=msg + ' ' + (_("You may add another %s below.") % opts.verbose_name))
    272274                return HttpResponseRedirect(request.path)
  • django/template/defaultfilters.py

     
    1515
    1616def addslashes(value):
    1717    "Adds slashes - useful for passing strings to JavaScript, for example."
    18     return value.replace('\\', '\\\\').replace('"', '\\"').replace("'", "\\'")
     18    return value.replace('\\', '\\\\').replace('"', '\\"').replace("'", "\\'").replace('\n', '\\n')
    1919
    2020def capfirst(value):
    2121    "Capitalizes the first character of the value"
Back to Top