7
7
8
8
const darkThemes = [ "dark" , "ayu" ] ;
9
9
window . currentTheme = document . getElementById ( "themeStyle" ) ;
10
- window . mainTheme = document . getElementById ( "mainThemeStyle" ) ;
11
10
12
11
// WARNING: RUSTDOC_MOBILE_BREAKPOINT MEDIA QUERY
13
12
// If you update this line, then you also need to update the media query with the same
@@ -44,8 +43,6 @@ function getSettingValue(settingName) {
44
43
45
44
const localStoredTheme = getSettingValue ( "theme" ) ;
46
45
47
- const savedHref = [ ] ;
48
-
49
46
// eslint-disable-next-line no-unused-vars
50
47
function hasClass ( elem , className ) {
51
48
return elem && elem . classList && elem . classList . contains ( className ) ;
@@ -102,6 +99,7 @@ function onEach(arr, func, reversed) {
102
99
* @param {function(?) } func - The callback
103
100
* @param {boolean } [reversed] - Whether to iterate in reverse
104
101
*/
102
+ // eslint-disable-next-line no-unused-vars
105
103
function onEachLazy ( lazyArray , func , reversed ) {
106
104
return onEach (
107
105
Array . prototype . slice . call ( lazyArray ) ,
@@ -125,30 +123,37 @@ function getCurrentValue(name) {
125
123
}
126
124
}
127
125
128
- function switchTheme ( styleElem , mainStyleElem , newThemeName , saveTheme ) {
126
+ // Get a value from the rustdoc-vars div, which is used to convey data from
127
+ // Rust to the JS. If there is no such element, return null.
128
+ const getVar = ( function getVar ( name ) {
129
+ const el = document . getElementById ( "rustdoc-vars" ) ;
130
+ if ( el ) {
131
+ return el . attributes [ "data-" + name ] . value ;
132
+ } else {
133
+ return null ;
134
+ }
135
+ } ) ;
136
+
137
+ function switchTheme ( newThemeName , saveTheme ) {
129
138
// If this new value comes from a system setting or from the previously
130
139
// saved theme, no need to save it.
131
140
if ( saveTheme ) {
132
141
updateLocalStorage ( "theme" , newThemeName ) ;
133
142
}
134
143
135
- if ( savedHref . length === 0 ) {
136
- onEachLazy ( document . getElementsByTagName ( "link" ) , el => {
137
- savedHref . push ( el . href ) ;
138
- } ) ;
144
+ let newHref ;
145
+
146
+ if ( newThemeName === "light" || newThemeName === "dark" || newThemeName === "ayu" ) {
147
+ newHref = getVar ( "static-root-path" ) + getVar ( "theme-" + newThemeName + "-css" ) ;
148
+ } else {
149
+ newHref = getVar ( "root-path" ) + newThemeName + getVar ( "resource-suffix" ) + ".css" ;
139
150
}
140
- const newHref = savedHref . find ( url => {
141
- const m = url . match ( / s t a t i c \. f i l e s \/ ( .* ) - [ a - f 0 - 9 ] { 16 } \. c s s $ / ) ;
142
- if ( m && m [ 1 ] === newThemeName ) {
143
- return true ;
144
- }
145
- const m2 = url . match ( / \/ ( [ ^ / ] * ) \. c s s $ / ) ;
146
- if ( m2 && m2 [ 1 ] . startsWith ( newThemeName ) ) {
147
- return true ;
148
- }
149
- } ) ;
150
- if ( newHref && newHref !== styleElem . href ) {
151
- styleElem . href = newHref ;
151
+
152
+ if ( ! window . currentTheme ) {
153
+ document . write ( `<link rel="stylesheet" id="themeStyle" href="${ newHref } ">` ) ;
154
+ window . currentTheme = document . getElementById ( "themeStyle" ) ;
155
+ } else if ( newHref !== window . currentTheme . href ) {
156
+ window . currentTheme . href = newHref ;
152
157
}
153
158
}
154
159
@@ -164,7 +169,7 @@ const updateTheme = (function() {
164
169
*/
165
170
function updateTheme ( ) {
166
171
const use = ( theme , saveTheme ) => {
167
- switchTheme ( window . currentTheme , window . mainTheme , theme , saveTheme ) ;
172
+ switchTheme ( theme , saveTheme ) ;
168
173
} ;
169
174
170
175
// maybe the user has disabled the setting in the meantime!
0 commit comments