base_url now works for static assets as well

pull/708/head
Simon Willison 2020-03-24 15:08:45 -07:00
rodzic 0b37a104fd
commit b1f953b5de
5 zmienionych plików z 38 dodań i 28 usunięć

Wyświetl plik

@ -1,7 +1,7 @@
<script src="/-/static/sql-formatter-2.3.3.min.js" defer></script>
<script src="/-/static/codemirror-5.31.0.js"></script>
<link rel="stylesheet" href="/-/static/codemirror-5.31.0-min.css" />
<script src="/-/static/codemirror-5.31.0-sql.min.js"></script>
<script src="{{ base_url }}-/static/sql-formatter-2.3.3.min.js" defer></script>
<script src="{{ base_url }}-/static/codemirror-5.31.0.js"></script>
<link rel="stylesheet" href="{{ base_url }}-/static/codemirror-5.31.0-min.css" />
<script src="{{ base_url }}-/static/codemirror-5.31.0-sql.min.js"></script>
<style>
.CodeMirror { height: auto; min-height: 70px; width: 80%; border: 1px solid #ddd; }
.CodeMirror-scroll { max-height: 200px; }

Wyświetl plik

@ -2,7 +2,7 @@
<html>
<head>
<title>{% block title %}{% endblock %}</title>
<link rel="stylesheet" href="/-/static/app.css?{{ app_css_hash }}">
<link rel="stylesheet" href="{{ base_url }}-/static/app.css?{{ app_css_hash }}">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
{% for url in extra_css_urls %}
<link rel="stylesheet" href="{{ url.url }}"{% if url.sri %} integrity="{{ url.sri }}" crossorigin="anonymous"{% endif %}>

Wyświetl plik

@ -351,7 +351,7 @@ def prepare_connection(conn, database, datasette):
@hookimpl
def extra_css_urls(template, database, table, datasette):
return ['https://example.com/{}/extra-css-urls-demo.css'.format(
return ['https://plugin-example.com/{}/extra-css-urls-demo.css'.format(
base64.b64encode(json.dumps({
"template": template,
"database": database,
@ -363,9 +363,9 @@ def extra_css_urls(template, database, table, datasette):
@hookimpl
def extra_js_urls():
return [{
'url': 'https://example.com/jquery.js',
'url': 'https://plugin-example.com/jquery.js',
'sri': 'SRIHASH',
}, 'https://example.com/plugin1.js']
}, 'https://plugin-example.com/plugin1.js']
@hookimpl
@ -421,9 +421,9 @@ import json
@hookimpl
def extra_js_urls():
return [{
'url': 'https://example.com/jquery.js',
'url': 'https://plugin-example.com/jquery.js',
'sri': 'SRIHASH',
}, 'https://example.com/plugin2.js']
}, 'https://plugin-example.com/plugin2.js']
@hookimpl

Wyświetl plik

@ -1174,19 +1174,29 @@ def test_base_url_config(base_url, path):
for client in make_app_client(config={"base_url": base_url}):
response = client.get(base_url + path.lstrip("/"))
soup = Soup(response.body, "html.parser")
for a in soup.findAll("a"):
href = a["href"]
if not href.startswith("#") and href not in {
"https://github.com/simonw/datasette",
"https://github.com/simonw/datasette/blob/master/LICENSE",
"https://github.com/simonw/datasette/blob/master/tests/fixtures.py",
}:
for el in soup.findAll(["a", "link", "script"]):
if "href" in el.attrs:
href = el["href"]
elif "src" in el.attrs:
href = el["src"]
else:
continue # Could be a <script>...</script>
if (
not href.startswith("#")
and href
not in {
"https://github.com/simonw/datasette",
"https://github.com/simonw/datasette/blob/master/LICENSE",
"https://github.com/simonw/datasette/blob/master/tests/fixtures.py",
}
and not href.startswith("https://plugin-example.com/")
):
# If this has been made absolute it may start http://localhost/
if href.startswith("http://localhost/"):
href = href[len("http://localost/") :]
assert href.startswith(base_url), {
"base_url": base_url,
"path": path,
"href_in_document": href,
"link_parent": str(a.parent),
"href_or_src": href,
"element_parent": str(el.parent),
}

Wyświetl plik

@ -64,7 +64,7 @@ def test_plugin_extra_js_urls(app_client):
== {
"integrity": "SRIHASH",
"crossorigin": "anonymous",
"src": "https://example.com/jquery.js",
"src": "https://plugin-example.com/jquery.js",
}
]
@ -74,7 +74,7 @@ def test_plugins_with_duplicate_js_urls(app_client):
response = app_client.get("/fixtures")
# This test is a little tricky, as if the user has any other plugins in
# their current virtual environment those may affect what comes back too.
# What matters is that https://example.com/jquery.js is only there once
# What matters is that https://plugin-example.com/jquery.js is only there once
# and it comes before plugin1.js and plugin2.js which could be in either
# order
scripts = Soup(response.body, "html.parser").findAll("script")
@ -82,16 +82,16 @@ def test_plugins_with_duplicate_js_urls(app_client):
# No duplicates allowed:
assert len(srcs) == len(set(srcs))
# jquery.js loaded once:
assert 1 == srcs.count("https://example.com/jquery.js")
assert 1 == srcs.count("https://plugin-example.com/jquery.js")
# plugin1.js and plugin2.js are both there:
assert 1 == srcs.count("https://example.com/plugin1.js")
assert 1 == srcs.count("https://example.com/plugin2.js")
assert 1 == srcs.count("https://plugin-example.com/plugin1.js")
assert 1 == srcs.count("https://plugin-example.com/plugin2.js")
# jquery comes before them both
assert srcs.index("https://example.com/jquery.js") < srcs.index(
"https://example.com/plugin1.js"
assert srcs.index("https://plugin-example.com/jquery.js") < srcs.index(
"https://plugin-example.com/plugin1.js"
)
assert srcs.index("https://example.com/jquery.js") < srcs.index(
"https://example.com/plugin2.js"
assert srcs.index("https://plugin-example.com/jquery.js") < srcs.index(
"https://plugin-example.com/plugin2.js"
)