From 6b2566c01fdef268fe9461c45c40deae7a7060ef Mon Sep 17 00:00:00 2001 From: Matt Good Date: Fri, 17 Apr 2015 12:56:10 -0700 Subject: [PATCH] Enable toolbar on HTML5 pages without tag Includes the toolbar on HTML5 pages without an explicit tag by checking for the HTML5 ``. Fixes #79 --- flask_debugtoolbar/__init__.py | 41 ++++++++++++++++++++++------------ 1 file changed, 27 insertions(+), 14 deletions(-) diff --git a/flask_debugtoolbar/__init__.py b/flask_debugtoolbar/__init__.py index 65e1092..7fc07de 100644 --- a/flask_debugtoolbar/__init__.py +++ b/flask_debugtoolbar/__init__.py @@ -188,24 +188,37 @@ class DebugToolbarExtension(object): # If the http response code is 200 then we process to add the # toolbar to the returned html response. - if (response.status_code == 200 and + if not (response.status_code == 200 and + response.is_sequence and response.headers['content-type'].startswith('text/html')): - for panel in self.debug_toolbars[real_request].panels: - panel.process_response(real_request, response) + return response - if response.is_sequence: - response_html = response.data.decode(response.charset) + response_html = response.data.decode(response.charset) - toolbar_html = self.debug_toolbars[real_request].render_toolbar() + no_case = response_html.lower() + body_end = no_case.rfind('') - content = replace_insensitive( - response_html, '', toolbar_html + '') - if content is response_html: - warnings.warn('Could not insert debug toolbar. tag not found in response.') - else: - content = content.encode(response.charset) - response.response = [content] - response.content_length = len(content) + if body_end >= 0: + before = response_html[:body_end] + after = response_html[body_end:] + elif no_case.startswith(''): + before = response_html + after = '' + else: + warnings.warn('Could not insert debug toolbar. tag not found in response.') + return response + + toolbar = self.debug_toolbars[real_request] + + for panel in toolbar.panels: + panel.process_response(real_request, response) + + toolbar_html = toolbar.render_toolbar() + + content = ''.join((before, toolbar_html, after)) + content = content.encode(response.charset) + response.response = [content] + response.content_length = len(content) return response