Skip to content

Commit e55b26c

Browse files
committed
Add better handling for file URIs
1 parent 359c393 commit e55b26c

File tree

1 file changed

+17
-4
lines changed

1 file changed

+17
-4
lines changed

templates/switchers.js

+17-4
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,14 @@
11
'use strict';
22

3+
// File URIs must begin with either one or three forward slashes
4+
const _is_file_uri = (uri) => uri.startsWith('file:/');
5+
6+
const _IS_LOCAL = _is_file_uri(window.location.href);
37
const _CURRENT_RELEASE = DOCUMENTATION_OPTIONS.VERSION || '';
48
const _CURRENT_VERSION = _CURRENT_RELEASE.split('.', 2).join('.');
59
const _CURRENT_LANGUAGE = DOCUMENTATION_OPTIONS.LANGUAGE?.toLowerCase() || 'en';
610
const _CURRENT_PREFIX = (() => {
11+
if (_IS_LOCAL) return null;
712
// Sphinx 7.2+ defines the content root data attribute in the HTML element.
813
const _CONTENT_ROOT = document.documentElement.dataset.content_root;
914
if (_CONTENT_ROOT !== undefined) {
@@ -20,6 +25,10 @@ const all_languages = $LANGUAGES;
2025
const _create_version_select = () => {
2126
const select = document.createElement('select');
2227
select.className = 'version-select';
28+
if (_IS_LOCAL) {
29+
select.disabled = true;
30+
select.title = 'Version switching is disabled in local builds';
31+
}
2332

2433
for (const [version, title] of Object.entries(all_versions)) {
2534
const option = document.createElement('option');
@@ -44,6 +53,10 @@ const _create_language_select = () => {
4453

4554
const select = document.createElement('select');
4655
select.className = 'language-select';
56+
if (_IS_LOCAL) {
57+
select.disabled = true;
58+
select.title = 'Language switching is disabled in local builds';
59+
}
4760

4861
for (const [language, title] of Object.entries(all_languages)) {
4962
const option = document.createElement('option');
@@ -59,10 +72,6 @@ const _create_language_select = () => {
5972
const _navigate_to_first_existing = (urls) => {
6073
// Navigate to the first existing URL in urls.
6174
for (const url of urls) {
62-
if (url.startsWith('file:///')) {
63-
window.location.href = url;
64-
return;
65-
}
6675
fetch(url)
6776
.then((response) => {
6877
if (response.ok) {
@@ -82,6 +91,8 @@ const _navigate_to_first_existing = (urls) => {
8291
};
8392

8493
const _on_version_switch = (event) => {
94+
if (_IS_LOCAL) return;
95+
8596
const selected_version = event.target.value;
8697
// English has no language prefix.
8798
const new_prefix_en = `/${selected_version}/`;
@@ -105,6 +116,8 @@ const _on_version_switch = (event) => {
105116
};
106117

107118
const _on_language_switch = (event) => {
119+
if (_IS_LOCAL) return;
120+
108121
const selected_language = event.target.value;
109122
// English has no language prefix.
110123
const new_prefix =

0 commit comments

Comments
 (0)