Skip to content

Commit 8ca719f

Browse files
authored
Merge pull request #48 from aio-libs/fix-rendering
Fix page rendering #46
2 parents 3f71ba7 + 9465143 commit 8ca719f

File tree

3 files changed

+15
-9
lines changed

3 files changed

+15
-9
lines changed

aiohttp_debugtoolbar/toolbar.py

+6-8
Original file line numberDiff line numberDiff line change
@@ -66,25 +66,23 @@ def inject(self, request, response):
6666
css_path = request.app.router[STATIC_ROUTE_NAME].url(
6767
filename='css/toolbar_button.css')
6868

69+
toolbar_css = toolbar_css_template % {'css_path': css_path}
6970
toolbar_html = toolbar_html_template % {
7071
'button_style': button_style,
7172
'css_path': css_path,
7273
'toolbar_url': toolbar_url}
7374

7475
toolbar_html = toolbar_html.encode(response.charset or 'utf-8')
76+
toolbar_css = toolbar_css.encode(response.charset or 'utf-8')
77+
response_html = replace_insensitive(
78+
response_html, b'</head>', toolbar_css + b'</head>')
7579
response.body = replace_insensitive(
7680
response_html, b'</body>',
7781
toolbar_html + b'</body>')
82+
toolbar_css_template = """\
83+
<link rel="stylesheet" type="text/css" href="%(css_path)s">"""
7884

7985
toolbar_html_template = """\
80-
<script type="text/javascript">
81-
var fileref=document.createElement("link")
82-
fileref.setAttribute("rel", "stylesheet")
83-
fileref.setAttribute("type", "text/css")
84-
fileref.setAttribute("href", "%(css_path)s")
85-
document.getElementsByTagName("head")[0].appendChild(fileref)
86-
</script>
87-
8886
<div id="pDebug">
8987
<div style="display: block; %(button_style)s" id="pDebugToolbarHandle">
9088
<a title="Show Toolbar" id="pShowToolBarButton"

tests/conftest.py

+7-1
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,13 @@ def create(*, debug=False, ssl_ctx=None, **kw):
4747
setup(app, **kw)
4848
port = unused_port
4949

50-
tplt = "<html><body><h1>{{ head }}</h1>{{ text }}</body></html>"
50+
tplt = """
51+
<html>
52+
<head></head>
53+
<body>
54+
<h1>{{ head }}</h1>{{ text }}
55+
</body>
56+
</html>"""
5157
loader = jinja2.DictLoader({'tplt.html': tplt})
5258
aiohttp_jinja2.setup(app, loader=loader)
5359

tests/test_middleware.py

+2
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ def handler(request):
1313
return aiohttp_jinja2.render_template(
1414
'tplt.html', request,
1515
{'head': 'HEAD', 'text': 'text'})
16+
1617
app, url = yield from create_server()
1718
app.router.add_route('GET', '/', handler)
1819

@@ -24,6 +25,7 @@ def handler(request):
2425
loop=loop, connector=conn)
2526
assert 200 == resp.status
2627
txt = yield from resp.text()
28+
assert 'toolbar_button.css' in txt
2729
assert 'pDebugToolbarHandle' in txt
2830
resp.close()
2931

0 commit comments

Comments
 (0)