|
17 | 17 | threading._DummyThread._Thread__stop = lambda x: 1
|
18 | 18 |
|
19 | 19 |
|
20 |
| -def replace_insensitive(string, target, replacement): |
21 |
| - """ |
22 |
| - Similar to string.replace() but is case insensitive. |
23 |
| - Code borrowed from: |
24 |
| - http://forums.devshed.com/python-programming-11/case-insensitive-string-replace-490921.html |
25 |
| - """ |
26 |
| - no_case = string.lower() |
27 |
| - index = no_case.rfind(target.lower()) |
28 |
| - if index >= 0: |
29 |
| - return string[:index] + replacement + string[index + len(target):] |
30 |
| - else: # no results so return the original string |
31 |
| - return string |
32 |
| - |
33 |
| - |
34 | 20 | def show_toolbar(request):
|
35 | 21 | """
|
36 | 22 | Default function to determine whether to show the toolbar on a given page.
|
@@ -94,10 +80,14 @@ def process_response(self, request, response):
|
94 | 80 | response.set_cookie('djdt', 'hide', 864000)
|
95 | 81 | if ('gzip' not in response.get('Content-Encoding', '') and
|
96 | 82 | response.get('Content-Type', '').split(';')[0] in _HTML_TYPES):
|
97 |
| - response.content = replace_insensitive( |
98 |
| - force_text(response.content, encoding=settings.DEFAULT_CHARSET), |
99 |
| - self.insert_before, |
100 |
| - force_text(toolbar.render_toolbar() + self.insert_before)) |
| 83 | + content = force_text(response.content, encoding=settings.DEFAULT_CHARSET) |
| 84 | + try: |
| 85 | + insert_at = content.lower().rindex(self.insert_before.lower()) |
| 86 | + except ValueError: |
| 87 | + pass |
| 88 | + else: |
| 89 | + toolbar_content = toolbar.render_toolbar() |
| 90 | + response.content = content[:insert_at] + toolbar_content + content[insert_at:] |
101 | 91 | if response.get('Content-Length', None):
|
102 | 92 | response['Content-Length'] = len(response.content)
|
103 | 93 | return response
|
0 commit comments